diff --git a/internal/cmdrender/cmd.go b/internal/cmdrender/cmd.go index 1ac0c52491..d7badf4085 100644 --- a/internal/cmdrender/cmd.go +++ b/internal/cmdrender/cmd.go @@ -23,7 +23,6 @@ import ( "os" docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/fndocs" - "github.com/GoogleContainerTools/kpt/internal/errors" "github.com/GoogleContainerTools/kpt/internal/util/cmdutil" "github.com/GoogleContainerTools/kpt/internal/util/pkgutil" "github.com/spf13/cobra" @@ -79,11 +78,9 @@ func (r *Runner) preRunE(c *cobra.Command, args []string) error { r.pkgPath = args[0] } if r.resultsDirPath != "" { - if _, err := os.Stat(r.resultsDirPath); err != nil { - if errors.Is(err, os.ErrNotExist) { - return fmt.Errorf("results-dir %q must exist", r.resultsDirPath) - } - return fmt.Errorf("results-dir %q check failed: %w", r.resultsDirPath, err) + err := os.MkdirAll(r.resultsDirPath, 0755) + if err != nil { + return fmt.Errorf("cannot read or create results dir %q: %w", r.resultsDirPath, err) } } return cmdutil.ValidateImagePullPolicyValue(r.imagePullPolicy) diff --git a/internal/docs/generated/fndocs/docs.go b/internal/docs/generated/fndocs/docs.go index 2f627444b2..5ad2d9156f 100644 --- a/internal/docs/generated/fndocs/docs.go +++ b/internal/docs/generated/fndocs/docs.go @@ -110,8 +110,8 @@ Flags: 3. OUT_DIR_PATH: output resources are written to provided directory, the directory is created if it doesn't already exist. --results-dir: - Path to a directory to write structured results. Directory must exist. - Structured results emitted by the functions are aggregated and saved + Path to a directory to write structured results. Directory will be created if + it doesn't exist. Structured results emitted by the functions are aggregated and saved to ` + "`" + `results.yaml` + "`" + ` file in the specified directory. If not specified, no result files are written to the local filesystem. ` @@ -227,8 +227,8 @@ Flags: 3. OUT_DIR_PATH: output resources are written to provided directory, the directory is created if it doesn't already exist. --results-dir: - Path to a directory to write structured results. Directory must exist. - Structured results emitted by the functions are aggregated and saved + Path to a directory to write structured results. Directory will be created if + it doesn't exist. Structured results emitted by the functions are aggregated and saved to ` + "`" + `results.yaml` + "`" + ` file in the specified directory. If not specified, no result files are written to the local filesystem. ` diff --git a/pkg/test/runner/runner.go b/pkg/test/runner/runner.go index 8082290c03..0ee1ccb4b8 100644 --- a/pkg/test/runner/runner.go +++ b/pkg/test/runner/runner.go @@ -141,12 +141,7 @@ func (r *Runner) runFnEval() error { var resultsDir, destDir string if r.IsFnResultExpected() { - // create result dir resultsDir = filepath.Join(tmpDir, "results") - err = os.Mkdir(resultsDir, 0755) - if err != nil { - return fmt.Errorf("failed to create results dir %s: %w", resultsDir, err) - } } if r.IsOutOfPlace() { @@ -281,12 +276,7 @@ func (r *Runner) runFnRender() error { var resultsDir, destDir string if r.IsFnResultExpected() { - // create result dir resultsDir = filepath.Join(tmpDir, "results") - err = os.Mkdir(resultsDir, 0755) - if err != nil { - return fmt.Errorf("failed to create results dir %s: %w", resultsDir, err) - } } if r.IsOutOfPlace() { diff --git a/site/reference/cli/fn/eval/README.md b/site/reference/cli/fn/eval/README.md index fc6ad36e28..c08656dfc2 100644 --- a/site/reference/cli/fn/eval/README.md +++ b/site/reference/cli/fn/eval/README.md @@ -114,8 +114,8 @@ fn-args: 3. OUT_DIR_PATH: output resources are written to provided directory, the directory is created if it doesn't already exist. --results-dir: - Path to a directory to write structured results. Directory must exist. - Structured results emitted by the functions are aggregated and saved + Path to a directory to write structured results. Directory will be created if + it doesn't exist. Structured results emitted by the functions are aggregated and saved to `results.yaml` file in the specified directory. If not specified, no result files are written to the local filesystem. ``` diff --git a/site/reference/cli/fn/render/README.md b/site/reference/cli/fn/render/README.md index 336cb9b3cf..3a67a514d0 100644 --- a/site/reference/cli/fn/render/README.md +++ b/site/reference/cli/fn/render/README.md @@ -62,8 +62,8 @@ PKG_PATH: 3. OUT_DIR_PATH: output resources are written to provided directory, the directory is created if it doesn't already exist. --results-dir: - Path to a directory to write structured results. Directory must exist. - Structured results emitted by the functions are aggregated and saved + Path to a directory to write structured results. Directory will be created if + it doesn't exist. Structured results emitted by the functions are aggregated and saved to `results.yaml` file in the specified directory. If not specified, no result files are written to the local filesystem. ``` diff --git a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go index 3997d58477..838ef227fd 100644 --- a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go +++ b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go @@ -236,6 +236,12 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error { if err := cmdutil.ValidateImagePullPolicyValue(r.ImagePullPolicy); err != nil { return err } + if r.ResultsDir != "" { + err := os.MkdirAll(r.ResultsDir, 0755) + if err != nil { + return fmt.Errorf("cannot read or create results dir %q: %w", r.ResultsDir, err) + } + } var dataItems []string if c.ArgsLenAtDash() >= 0 { dataItems = append(dataItems, args[c.ArgsLenAtDash():]...)