Skip to content

Commit

Permalink
Merge pull request #320 from hypnoglow/270/verbose
Browse files Browse the repository at this point in the history
Add verbose output; print chart name on reindex error
  • Loading branch information
hypnoglow authored Oct 26, 2023
2 parents 4d3243c + 385c3c4 commit 7221bdb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cmd/helm-s3/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
type options struct {
timeout time.Duration
acl string
verbose bool
}

// newDefaultOptions returns default options.
func newDefaultOptions() *options {
return &options{
timeout: 5 * time.Minute,
acl: os.Getenv("S3_ACL"),
verbose: false,
}
}
12 changes: 10 additions & 2 deletions cmd/helm-s3/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -23,6 +24,7 @@ func newReindexCommand(opts *options) *cobra.Command {
act := &reindexAction{
printer: nil,
acl: "",
verbose: false,
repoName: "",
relative: false,
}
Expand All @@ -40,6 +42,7 @@ func newReindexCommand(opts *options) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
act.printer = cmd
act.acl = opts.acl
act.verbose = opts.verbose
act.repoName = args[0]
return act.run(cmd.Context())
},
Expand All @@ -56,7 +59,8 @@ type reindexAction struct {

// global flags

acl string
acl string
verbose bool

// args

Expand Down Expand Up @@ -90,6 +94,10 @@ func (act *reindexAction) run(ctx context.Context) error {
baseURL = ""
}

if act.verbose {
act.printer.Printf("[DEBUG] Adding %s to index.\n", item.Filename)
}

filename := escapeIfRelative(item.Filename, act.relative)

if err := idx.Add(item.Meta.Value(), filename, baseURL, item.Hash); err != nil {
Expand All @@ -102,7 +110,7 @@ func (act *reindexAction) run(ctx context.Context) error {
}()

for err = range errs {
return errors.Wrap(err, "traverse the chart repository")
return fmt.Errorf("traverse the chart repository: %v", err)
}

idx := <-builtIndex
Expand Down
5 changes: 5 additions & 0 deletions cmd/helm-s3/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ The default timeout for all commands is 5 minutes. If you don't use MFA, it may
be reasonable to lower the timeout for the most commands, e.g. to 10 seconds.
In contrast, in cases where you want to reindex big repository with thousands of
charts, you definitely want to increase the timeout.
[Verbose output]
You can enable verbose output with '--verbose' flag.
`

func newRootCmd() *cobra.Command {
Expand Down Expand Up @@ -72,6 +76,7 @@ func newRootCmd() *cobra.Command {
flags := cmd.PersistentFlags()
flags.StringVar(&opts.acl, "acl", opts.acl, "S3 Object ACL to use for charts and indexes. Can be sourced from S3_ACL environment variable.")
flags.DurationVar(&opts.timeout, "timeout", opts.timeout, "Timeout for the whole operation to complete.")
flags.BoolVar(&opts.verbose, "verbose", opts.verbose, "Enable verbose output.")

cmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
return newBadUsageError(err)
Expand Down
10 changes: 5 additions & 5 deletions internal/awss3/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *Storage) traverse(ctx context.Context, repoURI string, items chan<- Cha
Key: obj.Key,
})
if err != nil {
errs <- errors.Wrap(err, "head s3 object")
errs <- fmt.Errorf("head s3 object %q: %s", key, err)
return
}

Expand All @@ -139,7 +139,7 @@ func (s *Storage) traverse(ctx context.Context, repoURI string, items chan<- Cha
Key: obj.Key,
})
if err != nil {
errs <- errors.Wrap(err, "get s3 object")
errs <- fmt.Errorf("get s3 object %q: %s", key, err)
return
}

Expand All @@ -149,13 +149,13 @@ func (s *Storage) traverse(ctx context.Context, repoURI string, items chan<- Cha
ch, err := helmutil.LoadArchive(tr)
objectOut.Body.Close()
if err != nil {
errs <- errors.Wrap(err, "load archive from s3 object")
errs <- fmt.Errorf("load archive from s3 object %q: %s", key, err)
return
}

digest, err := helmutil.Digest(buf)
if err != nil {
errs <- errors.WithMessage(err, "get chart hash")
errs <- fmt.Errorf("get chart hash for %q: %s", key, err)
return
}

Expand All @@ -164,7 +164,7 @@ func (s *Storage) traverse(ctx context.Context, repoURI string, items chan<- Cha
} else {
meta := helmutil.NewChartMetadata()
if err := meta.UnmarshalJSON([]byte(*serializedChartMeta)); err != nil {
errs <- errors.Wrap(err, "unserialize chart meta")
errs <- fmt.Errorf("unserialize chart meta for %q: %s", key, err)
return
}

Expand Down

0 comments on commit 7221bdb

Please sign in to comment.