diff --git a/docs/md/melange_index.md b/docs/md/melange_index.md index 5b70f26e0..00a2cd8e3 100644 --- a/docs/md/melange_index.md +++ b/docs/md/melange_index.md @@ -30,6 +30,7 @@ melange index [flags] ``` -a, --arch string Index only packages which match the expected architecture -h, --help help for index + -j, --jsonoutput string Output generated JSON index to FILE -m, --merge Merge pre-existing index entries -o, --output string Output generated index to FILE (default "APKINDEX.tar.gz") --signing-key string Key to use for signing the index (optional) diff --git a/pkg/build/build.go b/pkg/build/build.go index 8cfbdbcb5..c23d7218c 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -929,6 +929,7 @@ func (b *Build) BuildPackage(ctx context.Context) error { index.WithSigningKey(b.SigningKey), index.WithMergeIndexFileFlag(true), index.WithIndexFile(filepath.Join(packageDir, "APKINDEX.tar.gz")), + index.WithIndexJsonFile(filepath.Join(packageDir, "APKINDEX.json")), } idx, err := index.New(opts...) @@ -939,10 +940,6 @@ func (b *Build) BuildPackage(ctx context.Context) error { if err := idx.GenerateIndex(ctx); err != nil { return fmt.Errorf("unable to generate index: %w", err) } - - if err := idx.WriteJSONIndex(filepath.Join(packageDir, "APKINDEX.json")); err != nil { - return fmt.Errorf("unable to generate JSON index: %w", err) - } } return nil diff --git a/pkg/cli/index.go b/pkg/cli/index.go index 678e80066..87ef8609a 100644 --- a/pkg/cli/index.go +++ b/pkg/cli/index.go @@ -24,6 +24,7 @@ import ( // Index is a constructor for a cobra.Command which wraps the IndexCmd function. func Index() *cobra.Command { var apkIndexFilename string + var apkIndexJsonFilename string var sourceIndexFilename string var expectedArch string var signingKey string @@ -38,6 +39,7 @@ func Index() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { options := []index.Option{ index.WithIndexFile(apkIndexFilename), + index.WithIndexJsonFile(apkIndexJsonFilename), index.WithSourceIndexFile(sourceIndexFilename), index.WithExpectedArch(expectedArch), index.WithMergeIndexFileFlag(mergeIndexEntries), @@ -50,6 +52,8 @@ func Index() *cobra.Command { } cmd.Flags().StringVarP(&apkIndexFilename, "output", "o", "APKINDEX.tar.gz", "Output generated index to FILE") + // No default for now, as it's too new and is unexpected + cmd.Flags().StringVarP(&apkIndexJsonFilename, "jsonoutput", "j", "", "Output generated JSON index to FILE") cmd.Flags().StringVarP(&sourceIndexFilename, "source", "s", "APKINDEX.tar.gz", "Source FILE to use for pre-existing index entries") cmd.Flags().StringVarP(&expectedArch, "arch", "a", "", "Index only packages which match the expected architecture") cmd.Flags().StringVar(&signingKey, "signing-key", "", "Key to use for signing the index (optional)") diff --git a/pkg/index/index.go b/pkg/index/index.go index 43769270a..6d2e6222c 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -34,6 +34,7 @@ import ( type Index struct { PackageFiles []string IndexFile string + IndexJsonFile string SourceIndexFile string MergeIndexFileFlag bool SigningKey string @@ -58,6 +59,13 @@ func WithIndexFile(indexFile string) Option { } } +func WithIndexJsonFile(indexJsonFile string) Option { + return func(idx *Index) error { + idx.IndexJsonFile = indexJsonFile + return nil + } +} + func WithSourceIndexFile(indexFile string) Option { return func(idx *Index) error { idx.SourceIndexFile = indexFile @@ -225,6 +233,11 @@ func (idx *Index) GenerateIndex(ctx context.Context) error { if err := idx.WriteArchiveIndex(ctx, idx.IndexFile); err != nil { return fmt.Errorf("writing index: %w", err) } + if idx.IndexJsonFile != "" { + if err := idx.WriteJSONIndex(idx.IndexJsonFile); err != nil { + return fmt.Errorf("unable to generate JSON index: %w", err) + } + } return nil }