From f150ff277f4e75bfa3417bc02a5fe52da66d95d1 Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Fri, 26 Jul 2019 10:39:38 +0200 Subject: [PATCH] Allow creating secrets matching existing folders with --force (#1154) Fixes #1149 Signed-off-by: Dominik Schulz --- pkg/action/generate.go | 2 ++ pkg/ctxutil/ctxutil.go | 21 +++++++++++++++++++++ pkg/store/sub/write.go | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/action/generate.go b/pkg/action/generate.go index 67feeaf7..3ebb11f3 100644 --- a/pkg/action/generate.go +++ b/pkg/action/generate.go @@ -41,6 +41,8 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error { name := args.Get(0) key, length := keyAndLength(args) + ctx = ctxutil.WithForce(ctx, force) + // ask for name of the secret if it wasn't provided already if name == "" { var err error diff --git a/pkg/ctxutil/ctxutil.go b/pkg/ctxutil/ctxutil.go index 1dbf008a..3d102f09 100644 --- a/pkg/ctxutil/ctxutil.go +++ b/pkg/ctxutil/ctxutil.go @@ -30,6 +30,7 @@ const ( ctxKeyAlias ctxKeyAutoPrint ctxKeyGitInit + ctxKeyForce ) // ProgressCallback is a callback for updateing progress @@ -541,3 +542,23 @@ func IsGitInit(ctx context.Context) bool { } return bv } + +// WithForce returns a context with the force flag set +func WithForce(ctx context.Context, bv bool) context.Context { + return context.WithValue(ctx, ctxKeyForce, bv) +} + +// HasForce returns true if the context has the force flag set +func HasForce(ctx context.Context) bool { + _, ok := ctx.Value(ctxKeyForce).(bool) + return ok +} + +// IsForce returns the force flag value of the default (false) +func IsForce(ctx context.Context) bool { + bv, ok := ctx.Value(ctxKeyForce).(bool) + if !ok { + return false + } + return bv +} diff --git a/pkg/store/sub/write.go b/pkg/store/sub/write.go index 45b1c42b..cca20875 100644 --- a/pkg/store/sub/write.go +++ b/pkg/store/sub/write.go @@ -20,7 +20,7 @@ func (s *Store) Set(ctx context.Context, name string, sec store.Secret) error { p := s.passfile(name) - if s.IsDir(ctx, name) { + if s.IsDir(ctx, name) && !ctxutil.IsForce(ctx) { return errors.Errorf("a folder named %s already exists", name) }