From 2ae75b34f842648dd11912faad6da9c7ed3d0d7a Mon Sep 17 00:00:00 2001 From: Dimitri John Ledkov Date: Mon, 8 Apr 2024 14:49:20 +0100 Subject: [PATCH] index: add json index writing YOLO - no tests neither before nor after Signed-off-by: Dimitri John Ledkov --- pkg/build/build.go | 5 +---- pkg/cli/index.go | 4 ++++ pkg/index/index.go | 13 +++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) 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 }