diff --git a/cli/pkg/get/cmdget.go b/cli/pkg/get/cmdget.go index 4c180dc8b3..d1245f9e74 100644 --- a/cli/pkg/get/cmdget.go +++ b/cli/pkg/get/cmdget.go @@ -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 }) @@ -75,6 +77,7 @@ type Runner struct { strategy string isDeploymentInstance bool force bool + createParents bool } func (r *Runner) preRunE(_ *cobra.Command, args []string) error { @@ -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) diff --git a/cli/pkg/get/cmdget_test.go b/cli/pkg/get/cmdget_test.go index 1088c52751..ace7ab31d7 100644 --- a/cli/pkg/get/cmdget_test.go +++ b/cli/pkg/get/cmdget_test.go @@ -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) { @@ -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 { diff --git a/documentation/content/en/reference/cli/pkg/get/_index.md b/documentation/content/en/reference/cli/pkg/get/_index.md index 2f75108267..a2b2dd6e4c 100644 --- a/documentation/content/en/reference/cli/pkg/get/_index.md +++ b/documentation/content/en/reference/cli/pkg/get/_index.md @@ -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 diff --git a/internal/docs/generated/pkgdocs/docs.go b/internal/docs/generated/pkgdocs/docs.go index 52988af094..4a18879654 100644 --- a/internal/docs/generated/pkgdocs/docs.go +++ b/internal/docs/generated/pkgdocs/docs.go @@ -162,6 +162,9 @@ Args: Flags: + --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