Skip to content

Commit

Permalink
index: add json index writing
Browse files Browse the repository at this point in the history
YOLO - no tests neither before nor after

Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@chainguard.dev>
  • Loading branch information
xnox committed Apr 8, 2024
1 parent 5016fe5 commit 0975002
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/md/melange_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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)")
Expand Down
13 changes: 13 additions & 0 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
type Index struct {
PackageFiles []string
IndexFile string
IndexJsonFile string
SourceIndexFile string
MergeIndexFileFlag bool
SigningKey string
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0975002

Please sign in to comment.