Skip to content

Commit

Permalink
Merge pull request #68 from dwango/feature/fix-issue-64
Browse files Browse the repository at this point in the history
Implement `ignore-not-found` option.
  • Loading branch information
s-dwinter authored May 7, 2024
2 parents 67eeea3 + 5d3d8aa commit 698375a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
16 changes: 12 additions & 4 deletions internal/client/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"fmt"

kms "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
kmsTypes "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
"github.com/aws/aws-sdk-go-v2/service/ssm"
ssmTypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/dwango/yashiro/pkg/config"
)

Expand All @@ -46,7 +48,7 @@ func newAwsClient(cfg *config.AwsConfig) (Client, error) {
}, nil
}

func (c awsClient) GetValues(ctx context.Context, ignoreEmpty bool) (Values, error) {
func (c awsClient) GetValues(ctx context.Context, ignoreNotFound bool) (Values, error) {
values := make(Values, len(c.parameterStoreValue)+len(c.secretsManagerValue))

for _, v := range c.parameterStoreValue {
Expand All @@ -56,13 +58,14 @@ func (c awsClient) GetValues(ctx context.Context, ignoreEmpty bool) (Values, err
})

if err != nil {
var notFoundErr *ssmTypes.ParameterNotFound
if ignoreNotFound && errors.As(err, &notFoundErr) {
continue
}
return nil, err
}

if err := values.SetValue(v, output.Parameter.Value); err != nil {
if ignoreEmpty && errors.Is(err, ErrValueIsEmpty) {
continue
}
return nil, err
}
}
Expand All @@ -71,7 +74,12 @@ func (c awsClient) GetValues(ctx context.Context, ignoreEmpty bool) (Values, err
output, err := c.kmsClient.GetSecretValue(ctx, &kms.GetSecretValueInput{
SecretId: &v.Name,
})

if err != nil {
var notFoundErr *kmsTypes.ResourceNotFoundException
if ignoreNotFound && errors.As(err, &notFoundErr) {
continue
}
return nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (
var (
ErrNotfoundValueConfig = errors.New("not found value config")
ErrValueIsEmpty = errors.New("value is empty")
ErrInvalidJSON = errors.New("invalid json string")
)

// Client is the external stores client.
type Client interface {
GetValues(ctx context.Context, ignoreEmpty bool) (Values, error)
GetValues(ctx context.Context, ignoreNotFound bool) (Values, error)
}

// New returns a new Client.
Expand Down Expand Up @@ -63,7 +64,7 @@ func (v Values) SetValue(cfg config.Value, value *string) error {
if cfg.GetIsJSON() {
val = make(map[string]any)
if err := json.Unmarshal([]byte(*value), &val); err != nil {
return fmt.Errorf("%w: invalid json string", err)
return fmt.Errorf("%w: %w", ErrInvalidJSON, err)
}
} else {
val = *value
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const example = ` # specify single file.

func newTemplateCommand() *cobra.Command {
var configFile string
var ignoreNotFound bool

cmd := cobra.Command{
Use: "template <file>",
Expand All @@ -54,7 +55,7 @@ func newTemplateCommand() *cobra.Command {
return err
}

eng, err := engine.New(cfg)
eng, err := engine.New(cfg, engine.IgnoreNotFound(ignoreNotFound))
if err != nil {
return err
}
Expand All @@ -70,6 +71,7 @@ func newTemplateCommand() *cobra.Command {

f := cmd.Flags()
f.StringVarP(&configFile, "config", "c", config.DefaultConfigFilename, "specify config file.")
f.BoolVar(&ignoreNotFound, "ignore-not-found", false, "ignore values are not found in the external store.")

return &cmd
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func New(cfg *config.Config, option ...Option) (Engine, error) {
}

func (e engine) Render(ctx context.Context, text string, dest io.Writer) error {
values, err := e.client.GetValues(ctx, e.option.IgnoreEmpty)
values, err := e.client.GetValues(ctx, e.option.IgnoreNotFound)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/engine/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ package engine
// Option is configurable Engine behavior.
type Option func(*opts)

// IgnoreEmpty ignores empty value from external store.
func IgnoreEmpty() Option {
// IgnoreNotFound ignores values are not found in the external store.
func IgnoreNotFound(b bool) Option {
return func(o *opts) {
o.IgnoreEmpty = true
o.IgnoreNotFound = b
}
}

type opts struct {
IgnoreEmpty bool
IgnoreNotFound bool
}

var defaultOpts = &opts{
IgnoreEmpty: false,
IgnoreNotFound: false,
}

0 comments on commit 698375a

Please sign in to comment.