Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/skpr/rsync/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import (
)

var (
cmdLong = "Sync files from remote and local directories"
cmdLong = `Sync files from remote and local directories.

Private files (/mnt/private) may contain personal information and are not downloaded by default.
Syncing from a parent directory, such as /mnt/, excludes them automatically. Syncing from the
private filesystem itself fails unless --include-private is set. Only download private files to
investigate a specific issue, with approval.`

cmdExample = `
# Sync from local to a remote environment
skpr rsync ./foo/ dev:/mnt/temporary/foo/`
skpr rsync ./foo/ dev:/mnt/temporary/foo/

# Download public files from a remote environment
skpr rsync dev:/mnt/public/ ./files/

# Download private files from a remote environment (asks for confirmation)
skpr rsync --include-private dev:/mnt/private/ ./private/`
)

// NewCommand creates a new cobra.Command for 'rsync' sub command
Expand All @@ -37,6 +48,8 @@ func NewCommand() *cobra.Command {
cmd.Flags().StringSliceVar(&command.Excludes, "exclude", []string{}, "Exclude files matching PATTERN.")
cmd.Flags().StringVar(&command.ExcludeFrom, "exclude-from", "", "Exclude files matching patterns in FILE.")
cmd.Flags().BoolVar(&command.DryRun, "dry-run", false, "Skpr will not request a confirmation for configuration changes.")
cmd.Flags().BoolVar(&command.IncludePrivate, "include-private", false, "Download private files ("+v1rsync.PrivateFilesPath+"). Asks for confirmation.")
cmd.Flags().BoolVarP(&command.Force, "force", "f", false, "Skip the confirmation prompt when downloading private files.")

return cmd
}
87 changes: 80 additions & 7 deletions internal/command/rsync/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,111 @@ import (
"os/exec"

"github.com/pkg/errors"

"github.com/skpr/cli/internal/confirmation"
)

// ErrPrivateFilesNotIncluded is returned when a download includes private files without --include-private.
var ErrPrivateFilesNotIncluded = errors.New("this sync would download private files, which may contain personal information. " +
"Only download private files to investigate a specific issue, with approval. " +
"Use --include-private to download them")

// Command to rsync files.
type Command struct {
Source string
Destination string
Excludes []string
ExcludeFrom string
DryRun bool
Source string
Destination string
Excludes []string
ExcludeFrom string
DryRun bool
IncludePrivate bool
Force bool

// confirm defaults to confirmation.Confirm.
confirm func(force bool, message string) bool
}

// Run the command.
func (c *Command) Run(_ context.Context) error {
plan, err := c.planPrivateFiles()
if err != nil {
return err
}

if plan.confirm {
confirm := c.confirm
if confirm == nil {
confirm = confirmation.Confirm
}

msg := fmt.Sprintf("You are about to download private files from %s. Private files may contain personal information. "+
"Only download them to investigate a specific issue, with approval. Continue? [yes/no]", plan.environment)
if ok := confirm(c.Force, msg); !ok {
fmt.Fprintln(os.Stderr, "Download cancelled.")
return nil
}
}

executable, err := os.Executable()
if err != nil {
return errors.Wrap(err, "failed to lookup binary path")
}

args := c.generateArgs(executable)
if plan.exclude != "" {
fmt.Fprintf(os.Stderr, "Excluding private files (%s). Use --include-private to download them.\n", PrivateFilesPath)
}

args := c.generateArgs(executable, plan.exclude)
cmd := exec.Command("rsync", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

// privateFilesPlan describes how a sync handles private files.
type privateFilesPlan struct {
environment string
confirm bool
// exclude is an rsync pattern that skips private files.
exclude string
}

// planPrivateFiles decides how the sync handles private files. Dry runs skip confirmation.
func (c *Command) planPrivateFiles() (privateFilesPlan, error) {
env, remotePath, ok := splitRemote(c.Source)
if !ok {
return privateFilesPlan{}, nil
}

plan := privateFilesPlan{environment: env}

switch classifyRemotePath(remotePath) {
case privateScopeRequiresFlag:
if !c.IncludePrivate {
return plan, ErrPrivateFilesNotIncluded
}

plan.confirm = !c.DryRun
case privateScopeParent:
if c.IncludePrivate {
plan.confirm = !c.DryRun
} else {
plan.exclude = privateExcludePattern(remotePath)
}
}

return plan, nil
}

// generateArgs generates the args for the rsync command.
func (c *Command) generateArgs(executable string) []string {
func (c *Command) generateArgs(executable, privateExclude string) []string {
args := []string{
"-avz",
"--progress",
"-e", fmt.Sprintf("%s-rsh", executable),
}
if privateExclude != "" {
args = append(args, "--exclude", privateExclude)
}
for _, ex := range c.Excludes {
args = append(args, "--exclude", ex)
}
Expand Down
174 changes: 113 additions & 61 deletions internal/command/rsync/command_test.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,135 @@
package rsync

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateArgs(t *testing.T) {
c := Command{
Source: "source",
Destination: "destination",
tests := []struct {
name string
command Command
privateExclude string
want []string
}{
{
name: "defaults",
command: Command{Source: "source", Destination: "destination"},
want: []string{"-avz", "--progress", "-e", "skpr-rsh", "source", "destination"},
},
{
name: "all options",
command: Command{
Source: "dev:/mnt/",
Destination: "destination",
Excludes: []string{"js/*", "css/*"},
ExcludeFrom: "foo.txt",
DryRun: true,
},
privateExclude: "/private/",
// The private exclude comes first so user patterns can't override it.
want: []string{
"-avz", "--progress", "-e", "skpr-rsh",
"--exclude", "/private/",
"--exclude", "js/*",
"--exclude", "css/*",
"--exclude-from", "foo.txt",
"--dry-run",
"dev:/mnt/", "destination",
},
},
}

args := c.generateArgs("skpr")

assert.Equal(t, []string{
"-avz",
"--progress",
"-e", "skpr-rsh",
"source",
"destination",
}, args)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.command.generateArgs("skpr", tt.privateExclude))
})
}
}

func TestGenerateArgsExcludes(t *testing.T) {
c := Command{
Source: "source",
Destination: "destination",
Excludes: []string{
"js/*",
"css/*",
"styles/*",
},
func TestPlanPrivateFiles(t *testing.T) {
plan := func(c Command) (privateFilesPlan, error) {
c.Destination = "destination"
return c.planPrivateFiles()
}

args := c.generateArgs("skpr")
t.Run("not private", func(t *testing.T) {
for source, env := range map[string]string{
"./private/": "",
"./a:b/": "",
"dev:/mnt/public/": "dev",
"dev:files/": "dev",
"dev:/mnt/private-old/": "dev",
"dev:/mnt/pub*/": "dev",
"dev:/var/*": "dev",
} {
got, err := plan(Command{Source: source})
assert.NoError(t, err, source)
assert.Equal(t, privateFilesPlan{environment: env}, got, source)
}
})

assert.Equal(t, []string{
"-avz",
"--progress",
"-e", "skpr-rsh",
"--exclude", "js/*",
"--exclude", "css/*",
"--exclude", "styles/*",
"source",
"destination",
}, args)
}
t.Run("requires flag", func(t *testing.T) {
for _, source := range []string{
"dev:/mnt/private/",
"dev:/mnt/private/webform",
"dev:/mnt/public/../private",
"dev:/mnt/.",
"dev:/mnt/*",
"dev:/mnt/priv*/",
"dev:/mnt/private/*.pdf",
"dev:/m*",
"dev:/var/*/../mnt/private",
} {
_, err := plan(Command{Source: source})
assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source)

func TestGenerateArgsExcludeFrom(t *testing.T) {
c := Command{
Source: "source",
Destination: "destination",
ExcludeFrom: "foo.txt",
}
args := c.generateArgs("skpr")
assert.Equal(t, []string{
"-avz",
"--progress",
"-e", "skpr-rsh",
"--exclude-from", "foo.txt",
"source",
"destination",
}, args)
_, err = plan(Command{Source: source, DryRun: true})
assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source)

got, err := plan(Command{Source: source, IncludePrivate: true})
assert.NoError(t, err, source)
assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source)

got, err = plan(Command{Source: source, IncludePrivate: true, DryRun: true})
assert.NoError(t, err, source)
assert.Equal(t, privateFilesPlan{environment: "dev"}, got, source)
}
})

t.Run("parent", func(t *testing.T) {
for source, exclude := range map[string]string{
"dev:/mnt/": "/private/",
"dev:/mnt": "/mnt/private/",
"dev:/": "/mnt/private/",
} {
got, err := plan(Command{Source: source})
assert.NoError(t, err, source)
assert.Equal(t, privateFilesPlan{environment: "dev", exclude: exclude}, got, source)

got, err = plan(Command{Source: source, IncludePrivate: true})
assert.NoError(t, err, source)
assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source)
}
})
}

func TestGenerateDryRun(t *testing.T) {
func TestRunPrivateDeclined(t *testing.T) {
var prompted bool
c := Command{
Source: "source",
Destination: "destination",
DryRun: true,
Source: "dev:/mnt/private/",
Destination: "destination",
IncludePrivate: true,
confirm: func(force bool, message string) bool {
prompted = true
assert.Contains(t, message, "dev")
return false
},
}
args := c.generateArgs("skpr")
assert.Equal(t, []string{
"-avz",
"--progress",
"-e", "skpr-rsh",
"--dry-run",
"source",
"destination",
}, args)

// Declining returns before rsync runs.
assert.NoError(t, c.Run(context.Background()))
assert.True(t, prompted)
}
Loading
Loading