Skip to content

Commit

Permalink
[bugfix] Copy with trailing slash at destination.
Browse files Browse the repository at this point in the history
Signed-off-by: Vijay Kesanakurthi <[email protected]>
  • Loading branch information
vijay-kesanakurthi committed Oct 9, 2024
1 parent 3c87c1b commit 970a349
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/action/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package action
import (
"context"
"fmt"
"path/filepath"
"strings"

"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/pkg/ctxutil"
Expand Down Expand Up @@ -30,6 +32,36 @@ func (s *Action) copy(ctx context.Context, from, to string, force bool) error {
return exit.Error(exit.NotFound, nil, "%s does not exist", from)
}

isSourceDir := s.Store.IsDir(ctx, from)
hasTrailingSlash := strings.HasSuffix(to, "/")

if isSourceDir && hasTrailingSlash {
return s.copyFlattenDir(ctx, from, to, force)
}

return s.copyRegular(ctx, from, to, force)
}

func (s *Action) copyFlattenDir(ctx context.Context, from, to string, force bool) error {
entries, err := s.Store.List(ctx, 0)
if err != nil {
return exit.Error(exit.List, err, "failed to list entries in %q", from)
}

for _, entry := range entries {
fromPath := filepath.Join(from, entry)
toPath := filepath.Join(to, filepath.Base(entry))

if err := s.copyRegular(ctx, fromPath, toPath, force); err != nil {
return err
}
}

return nil
}

func (s *Action) copyRegular(ctx context.Context, from, to string, force bool) error {

Check failure on line 63 in internal/action/copy.go

View workflow job for this annotation

GitHub Actions / linux

unnecessary leading newline (whitespace)

Check failure on line 63 in internal/action/copy.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

if !force {
if s.Store.Exists(ctx, to) && !termio.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {
return exit.Error(exit.Aborted, nil, "not overwriting your current secret")
Expand Down

0 comments on commit 970a349

Please sign in to comment.