diff --git a/docs/md/melange.md b/docs/md/melange.md index 80bec630c..6d9c0fa69 100644 --- a/docs/md/melange.md +++ b/docs/md/melange.md @@ -24,6 +24,7 @@ toc: true * [melange bump](/docs/md/melange_bump.md) - Update a Melange YAML file to reflect a new package version * [melange compile](/docs/md/melange_compile.md) - Compile a YAML configuration file * [melange completion](/docs/md/melange_completion.md) - Generate completion script +* [melange fetch-source](/docs/md/melange_fetch-source.md) - Download package source code * [melange index](/docs/md/melange_index.md) - Creates a repository index from a list of package files * [melange keygen](/docs/md/melange_keygen.md) - Generate a key for package signing * [melange license-check](/docs/md/melange_license-check.md) - Gather and check licensing data diff --git a/docs/md/melange_fetch-source.md b/docs/md/melange_fetch-source.md new file mode 100644 index 000000000..2df58a2c8 --- /dev/null +++ b/docs/md/melange_fetch-source.md @@ -0,0 +1,43 @@ +--- +title: "melange fetch-source" +slug: melange_fetch-source +url: /docs/md/melange_fetch-source.md +draft: false +images: [] +type: "article" +toc: true +--- +## melange fetch-source + +Download package source code + +### Synopsis + +Download the selected package's source code via the melange metadata. + +``` +melange fetch-source file target-directory [flags] +``` + +### Examples + +``` + melange fetch-source vim.apk sources/ +``` + +### Options + +``` + -h, --help help for fetch-source +``` + +### Options inherited from parent commands + +``` + --log-level string log level (e.g. debug, info, warn, error) (default "INFO") +``` + +### SEE ALSO + +* [melange](/docs/md/melange.md) - + diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index ed50807a4..30a5b78ed 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -55,6 +55,7 @@ func New() *cobra.Command { cmd.AddCommand(bumpCmd()) cmd.AddCommand(completion()) cmd.AddCommand(compile()) + cmd.AddCommand(fetchSource()) cmd.AddCommand(indexCmd()) cmd.AddCommand(keygen()) cmd.AddCommand(licenseCheck()) diff --git a/pkg/cli/fetch_source.go b/pkg/cli/fetch_source.go new file mode 100644 index 000000000..38556c4fa --- /dev/null +++ b/pkg/cli/fetch_source.go @@ -0,0 +1,58 @@ +// Copyright 2025 Chainguard, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cli + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/cobra" + + "chainguard.dev/melange/pkg/source" +) + +func fetchSource() *cobra.Command { + cmd := &cobra.Command{ + Use: "fetch-source file target-directory", + Short: "Download package source code", + Long: `Download the selected package's source code via the melange metadata.`, + Example: ` melange fetch-source vim.apk sources/`, + Args: cobra.MinimumNArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + destDir := args[1] + // Create destDir if it doesn't exist + if _, err := os.Stat(destDir); os.IsNotExist(err) { + if err := os.MkdirAll(destDir, 0755); err != nil { + return fmt.Errorf("failed to create destination directory: %w", err) + } + } + + var err error + e := filepath.Ext(args[0]) + if e == ".apk" || e == ".yaml" { + _, err = source.FetchSourceFromMelange(ctx, args[0], args[1]) + } else { + err = fmt.Errorf("unsupported file type: %s", e) + } + + return err + }, + } + + return cmd +}