From b57057083d04c1a1f30a24141b431a22fa61048e Mon Sep 17 00:00:00 2001 From: Camila Macedo <7708031+camilamacedo86@users.noreply.github.com> Date: Fri, 17 Jan 2025 18:48:35 +0000 Subject: [PATCH] fix: alpha generate command. If the output-dir path is not informed than it should be the current directory --- pkg/cli/alpha/command.go | 6 ++- pkg/cli/alpha/internal/generate.go | 57 ++++++++++++++----------- test/e2e/alphagenerate/generate_test.go | 21 ++++++--- 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/pkg/cli/alpha/command.go b/pkg/cli/alpha/command.go index 485bc097512..4c0302efdea 100644 --- a/pkg/cli/alpha/command.go +++ b/pkg/cli/alpha/command.go @@ -55,9 +55,11 @@ Then we will re-scaffold the project by Kubebuilder in the directory specified b }, } scaffoldCmd.Flags().StringVar(&opts.InputDir, "input-dir", "", - "path to a Kubebuilder project file if not in the current working directory") + "Specifies the full path to a Kubebuilder project file. If not provided, "+ + "the current working directory is used.") scaffoldCmd.Flags().StringVar(&opts.OutputDir, "output-dir", "", - "path to output the scaffolding. defaults a directory in the current working directory") + "Specifies the full path where the scaffolded files will be output. "+ + "Defaults to a directory within the current working directory.") return scaffoldCmd } diff --git a/pkg/cli/alpha/internal/generate.go b/pkg/cli/alpha/internal/generate.go index 192670e43c5..2a9050ee55c 100644 --- a/pkg/cli/alpha/internal/generate.go +++ b/pkg/cli/alpha/internal/generate.go @@ -44,8 +44,6 @@ type Generate struct { OutputDir string } -const defaultOutputDir = "output-dir" - // Generate handles the migration and scaffolding process. func (opts *Generate) Generate() error { config, err := loadProjectConfig(opts.InputDir) @@ -53,6 +51,29 @@ func (opts *Generate) Generate() error { return err } + if opts.OutputDir == "" { + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("failed to get working directory: %w", err) + } + opts.OutputDir = cwd + if _, err := os.Stat(opts.OutputDir); err == nil { + log.Warn("Using current working directory to re-scaffold the project") + log.Warn("This directory will be cleaned up and all files removed before the re-generation") + + // Ensure we clean the correct directory + log.Info("Cleaning directory:", opts.OutputDir) + + // Use an absolute path to target files directly + cleanupCmd := fmt.Sprintf("rm -rf %s/*", opts.OutputDir) + err = util.RunCmd("Running cleanup", "sh", "-c", cleanupCmd) + if err != nil { + log.Error("Cleanup failed:", err) + return err + } + } + } + if err := createDirectory(opts.OutputDir); err != nil { return err } @@ -87,17 +108,8 @@ func (opts *Generate) Generate() error { // Validate ensures the options are valid and kubebuilder is installed. func (opts *Generate) Validate() error { - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("failed to get working directory: %w", err) - } - - opts.InputDir, err = getInputPath(cwd, opts.InputDir) - if err != nil { - return err - } - - opts.OutputDir, err = getOutputPath(cwd, opts.OutputDir) + var err error + opts.InputDir, err = getInputPath(opts.InputDir) if err != nil { return err } @@ -225,9 +237,13 @@ func createAPIWithDeployImage(resource v1alpha1.ResourceData) error { } // Helper function to get input path. -func getInputPath(currentWorkingDirectory, inputPath string) (string, error) { +func getInputPath(inputPath string) (string, error) { if inputPath == "" { - inputPath = currentWorkingDirectory + cwd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to get working directory: %w", err) + } + inputPath = cwd } projectPath := fmt.Sprintf("%s/%s", inputPath, yaml.DefaultPath) if _, err := os.Stat(projectPath); os.IsNotExist(err) { @@ -236,17 +252,6 @@ func getInputPath(currentWorkingDirectory, inputPath string) (string, error) { return inputPath, nil } -// Helper function to get output path. -func getOutputPath(currentWorkingDirectory, outputPath string) (string, error) { - if outputPath == "" { - outputPath = fmt.Sprintf("%s/%s", currentWorkingDirectory, defaultOutputDir) - } - if _, err := os.Stat(outputPath); err == nil { - return "", fmt.Errorf("output path %s already exists", outputPath) - } - return outputPath, nil -} - // Helper function to get Init arguments for Kubebuilder. func getInitArgs(store store.Store) []string { var args []string diff --git a/test/e2e/alphagenerate/generate_test.go b/test/e2e/alphagenerate/generate_test.go index 0e631f0ea25..1dfb6859946 100644 --- a/test/e2e/alphagenerate/generate_test.go +++ b/test/e2e/alphagenerate/generate_test.go @@ -68,27 +68,28 @@ var _ = Describe("kubebuilder", func() { kbc.Destroy() }) - It("should regenerate the project with success", func() { + It("should regenerate the project in current directory with success", func() { generateProject(kbc) - regenerateProject(kbc, projectOutputDir) - validateProjectFile(kbc, projectFilePath) + regenerateProject(kbc) + By("checking that the project file was generated in the current directory") + validateProjectFile(kbc, filepath.Join(kbc.Dir, "PROJECT")) }) It("should regenerate project with grafana plugin with success", func() { generateProjectWithGrafanaPlugin(kbc) - regenerateProject(kbc, projectOutputDir) + regenerateProjectWith(kbc, projectOutputDir) validateGrafanaPlugin(projectFilePath) }) It("should regenerate project with DeployImage plugin with success", func() { generateProjectWithDeployImagePlugin(kbc) - regenerateProject(kbc, projectOutputDir) + regenerateProjectWith(kbc, projectOutputDir) validateDeployImagePlugin(projectFilePath) }) It("should regenerate project with helm plugin with success", func() { generateProjectWithHelmPlugin(kbc) - regenerateProject(kbc, projectOutputDir) + regenerateProjectWith(kbc, projectOutputDir) validateHelmPlugin(projectFilePath) }) }) @@ -185,7 +186,13 @@ func generateProject(kbc *utils.TestContext) { Expect(err).NotTo(HaveOccurred(), "Failed to scaffold API with external API") } -func regenerateProject(kbc *utils.TestContext, projectOutputDir string) { +func regenerateProject(kbc *utils.TestContext) { + By("regenerating the project") + err := kbc.Regenerate() + Expect(err).NotTo(HaveOccurred(), "Failed to regenerate project") +} + +func regenerateProjectWith(kbc *utils.TestContext, projectOutputDir string) { By("regenerating the project") err := kbc.Regenerate( fmt.Sprintf("--input-dir=%s", kbc.Dir),