Skip to content

Commit 337fd7d

Browse files
committed
fetch-source: export the simple fetch source mechanism via cli
Signed-off-by: Łukasz 'sil2100' Zemczak <lukasz.zemczak@chainguard.dev>
1 parent 5b4c1e4 commit 337fd7d

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

docs/md/melange.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ toc: true
2525
* [melange compile](/docs/md/melange_compile.md) - Compile a YAML configuration file
2626
* [melange completion](/docs/md/melange_completion.md) - Generate completion script
2727
* [melange convert](/docs/md/melange_convert.md) - EXPERIMENTAL COMMAND - Attempts to convert packages/gems/apkbuild files into melange configuration files
28+
* [melange fetch-source](/docs/md/melange_fetch-source.md) - Download package source code
2829
* [melange index](/docs/md/melange_index.md) - Creates a repository index from a list of package files
2930
* [melange keygen](/docs/md/melange_keygen.md) - Generate a key for package signing
3031
* [melange license-check](/docs/md/melange_license-check.md) - Gather and check licensing data

docs/md/melange_fetch-source.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: "melange fetch-source"
3+
slug: melange_fetch-source
4+
url: /docs/md/melange_fetch-source.md
5+
draft: false
6+
images: []
7+
type: "article"
8+
toc: true
9+
---
10+
## melange fetch-source
11+
12+
Download package source code
13+
14+
### Synopsis
15+
16+
Download the selected package's source code via the melange metadata.
17+
18+
```
19+
melange fetch-source file target-directory/
20+
```
21+
22+
### Examples
23+
24+
```
25+
melange fetch-source vim.apk sources/
26+
```
27+
28+
### Options
29+
30+
```
31+
-h, --help help for fetch-source
32+
```
33+
34+
### Options inherited from parent commands
35+
36+
```
37+
--log-level string log level (e.g. debug, info, warn, error) (default "INFO")
38+
```
39+
40+
### SEE ALSO
41+
42+
* [melange](/docs/md/melange.md) -
43+

pkg/cli/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func New() *cobra.Command {
5656
cmd.AddCommand(completion())
5757
cmd.AddCommand(compile())
5858
cmd.AddCommand(convert())
59+
cmd.AddCommand(fetchSource())
5960
cmd.AddCommand(indexCmd())
6061
cmd.AddCommand(keygen())
6162
cmd.AddCommand(licenseCheck())

pkg/cli/fetch_source.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 Chainguard, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"path/filepath"
21+
22+
"github.com/spf13/cobra"
23+
24+
"chainguard.dev/melange/pkg/source"
25+
)
26+
27+
func fetchSource() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "fetch-source file",
30+
Short: "Download package source code",
31+
Long: `Download the selected package's source code via the melange metadata.`,
32+
Example: ` melange fetch-source vim.apk sources/`,
33+
Args: cobra.MinimumNArgs(2),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
ctx := cmd.Context()
36+
37+
destDir := args[1]
38+
// Create destDir if it doesn't exist
39+
if _, err := os.Stat(destDir); os.IsNotExist(err) {
40+
if err := os.MkdirAll(destDir, 0755); err != nil {
41+
return fmt.Errorf("failed to create destination directory: %w", err)
42+
}
43+
}
44+
45+
var err error
46+
e := filepath.Ext(args[0])
47+
if e == ".apk" || e == ".yaml" {
48+
_, err = source.FetchSourceFromMelange(ctx, args[0], args[1])
49+
} else {
50+
err = fmt.Errorf("unsupported file type: %s", e)
51+
}
52+
53+
return err
54+
},
55+
}
56+
57+
return cmd
58+
}

0 commit comments

Comments
 (0)