Skip to content
Merged
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
11 changes: 11 additions & 0 deletions cli/pkg/get/cmdget.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func NewRunner(ctx context.Context, parent string) *Runner {
"(Experimental) indicates if this package will be deployed to a cluster.")
c.Flags().BoolVarP(&r.force, "force", "f", false,
"overwrite the local destination directory if it already exists and is not the same package.")
c.Flags().BoolVarP(&r.createParents, "create-parents", "p", false,
"create parent directories if they do not exist.")
_ = c.RegisterFlagCompletionFunc("strategy", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return kptfilev1.UpdateStrategiesAsStrings(), cobra.ShellCompDirectiveDefault
})
Expand All @@ -75,6 +77,7 @@ type Runner struct {
strategy string
isDeploymentInstance bool
force bool
createParents bool
}

func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
Expand All @@ -93,6 +96,14 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
args[1] = resolvedPath
}
}
if r.createParents && len(args) > 1 {
parent := filepath.Dir(args[1])
if _, err := os.Stat(parent); os.IsNotExist(err) {
if err := os.MkdirAll(parent, 0755); err != nil {
return errors.E(op, err)
}
}
}
t, err := parse.GitParseArgs(r.ctx, args, explicitDest)
if err != nil {
return errors.E(op, err)
Expand Down
76 changes: 76 additions & 0 deletions cli/pkg/get/cmdget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,47 @@ func TestCmd_execute(t *testing.T) {
})
}

// TestCmd_execute_createParents tests that get creates multi-directory nested parents when --create-parents is provided.
func TestCmd_execute_createParents(t *testing.T) {
testCases := map[string]struct {
flag string
subDirs []string
}{
"long flag with multi-directory parent": {
flag: "--create-parents",
subDirs: []string{"dir1", "dir2", "dir3"},
},
"shorthand flag with multi-directory parent": {
flag: "-p",
subDirs: []string{"nested", "parent", "dirs"},
},
}

for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
g, w, clean := testutil.SetupRepoAndWorkspace(t, testutil.Content{
Data: testutil.Dataset1,
Branch: "master",
})
defer clean()

defer testutil.Chdir(t, w.WorkspaceDirectory)()

destParts := append([]string{w.WorkspaceDirectory}, tc.subDirs...)
destParts = append(destParts, "pkg")
dest := filepath.Join(destParts...)

r := get.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
r.Command.SetArgs([]string{tc.flag, "file://" + g.RepoDirectory + ".git/", dest})
err := r.Command.Execute()

assert.NoError(t, err)
assert.DirExists(t, dest)
g.AssertEqual(t, filepath.Join(g.DatasetDirectory, testutil.Dataset1), dest, true)
})
}
}

// TestCmdMainBranch_execute tests that get is correctly invoked if default branch
// is main and master branch doesn't exist
func TestCmdMainBranch_execute(t *testing.T) {
Expand Down Expand Up @@ -372,6 +413,41 @@ func TestCmd_Execute_flagAndArgParsing(t *testing.T) {
assert.Contains(t, err.Error(), "unknown update strategy \"does-not-exist\"")
},
},
"destination parent does not exist without create-parents flag": {
argsFunc: func(repo, dir string) []string {
return []string{fmt.Sprintf("file://%s.git", repo), filepath.Join(dir, "missing-parent", "my-app")}
},
runE: failRun,
validations: func(repo, dir string, r *get.Runner, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "parent directory")
assert.Contains(t, err.Error(), "does not exist")
},
},
"destination parent does not exist with create-parents flag": {
argsFunc: func(repo, dir string) []string {
return []string{"--create-parents", fmt.Sprintf("file://%s.git", repo), filepath.Join(dir, "missing-parent", "my-app")}
},
runE: NoOpRunE,
validations: func(repo, dir string, r *get.Runner, err error) {
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("file://%s", repo), r.Get.Git.Repo)
assert.Equal(t, "master", r.Get.Git.Ref)
assert.Equal(t, filepath.Join(dir, "missing-parent", "my-app"), r.Get.Destination)
},
},
"destination parent does not exist with -p shorthand flag": {
argsFunc: func(repo, dir string) []string {
return []string{"-p", fmt.Sprintf("file://%s.git", repo), filepath.Join(dir, "missing-parent", "my-app")}
},
runE: NoOpRunE,
validations: func(repo, dir string, r *get.Runner, err error) {
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("file://%s", repo), r.Get.Git.Repo)
assert.Equal(t, "master", r.Get.Git.Ref)
assert.Equal(t, filepath.Join(dir, "missing-parent", "my-app"), r.Get.Destination)
},
},
}

for tn, tc := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions documentation/content/en/reference/cli/pkg/get/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ LOCAL_DEST_DIRECTORY:
#### Flags

```shell
--create-parents, -p:
Create parent directories if they do not exist. It is `false` by default.

--force:
Overwrite the local destination directory if it already exists and is not the
same package. Re-fetching the same upstream package is always idempotent and
Expand Down
3 changes: 3 additions & 0 deletions internal/docs/generated/pkgdocs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading