Skip to content

Commit

Permalink
🌱 fix: alpha generate command. If the output-dir path is not informed…
Browse files Browse the repository at this point in the history
… than it should be the current directory (#4500)

fix: alpha generate command. If the output-dir path is not informed than it should be the current directory
  • Loading branch information
camilamacedo86 authored Jan 20, 2025
1 parent 48db38c commit 19862ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
6 changes: 4 additions & 2 deletions pkg/cli/alpha/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
57 changes: 31 additions & 26 deletions pkg/cli/alpha/internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,36 @@ 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)
if err != nil {
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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
21 changes: 14 additions & 7 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 19862ad

Please sign in to comment.