Skip to content

Commit 19862ad

Browse files
🌱 fix: alpha generate command. If the output-dir path is not informed 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
1 parent 48db38c commit 19862ad

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

‎pkg/cli/alpha/command.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ Then we will re-scaffold the project by Kubebuilder in the directory specified b
5555
},
5656
}
5757
scaffoldCmd.Flags().StringVar(&opts.InputDir, "input-dir", "",
58-
"path to a Kubebuilder project file if not in the current working directory")
58+
"Specifies the full path to a Kubebuilder project file. If not provided, "+
59+
"the current working directory is used.")
5960
scaffoldCmd.Flags().StringVar(&opts.OutputDir, "output-dir", "",
60-
"path to output the scaffolding. defaults a directory in the current working directory")
61+
"Specifies the full path where the scaffolded files will be output. "+
62+
"Defaults to a directory within the current working directory.")
6163

6264
return scaffoldCmd
6365
}

‎pkg/cli/alpha/internal/generate.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,36 @@ type Generate struct {
4444
OutputDir string
4545
}
4646

47-
const defaultOutputDir = "output-dir"
48-
4947
// Generate handles the migration and scaffolding process.
5048
func (opts *Generate) Generate() error {
5149
config, err := loadProjectConfig(opts.InputDir)
5250
if err != nil {
5351
return err
5452
}
5553

54+
if opts.OutputDir == "" {
55+
cwd, err := os.Getwd()
56+
if err != nil {
57+
return fmt.Errorf("failed to get working directory: %w", err)
58+
}
59+
opts.OutputDir = cwd
60+
if _, err := os.Stat(opts.OutputDir); err == nil {
61+
log.Warn("Using current working directory to re-scaffold the project")
62+
log.Warn("This directory will be cleaned up and all files removed before the re-generation")
63+
64+
// Ensure we clean the correct directory
65+
log.Info("Cleaning directory:", opts.OutputDir)
66+
67+
// Use an absolute path to target files directly
68+
cleanupCmd := fmt.Sprintf("rm -rf %s/*", opts.OutputDir)
69+
err = util.RunCmd("Running cleanup", "sh", "-c", cleanupCmd)
70+
if err != nil {
71+
log.Error("Cleanup failed:", err)
72+
return err
73+
}
74+
}
75+
}
76+
5677
if err := createDirectory(opts.OutputDir); err != nil {
5778
return err
5879
}
@@ -87,17 +108,8 @@ func (opts *Generate) Generate() error {
87108

88109
// Validate ensures the options are valid and kubebuilder is installed.
89110
func (opts *Generate) Validate() error {
90-
cwd, err := os.Getwd()
91-
if err != nil {
92-
return fmt.Errorf("failed to get working directory: %w", err)
93-
}
94-
95-
opts.InputDir, err = getInputPath(cwd, opts.InputDir)
96-
if err != nil {
97-
return err
98-
}
99-
100-
opts.OutputDir, err = getOutputPath(cwd, opts.OutputDir)
111+
var err error
112+
opts.InputDir, err = getInputPath(opts.InputDir)
101113
if err != nil {
102114
return err
103115
}
@@ -225,9 +237,13 @@ func createAPIWithDeployImage(resource v1alpha1.ResourceData) error {
225237
}
226238

227239
// Helper function to get input path.
228-
func getInputPath(currentWorkingDirectory, inputPath string) (string, error) {
240+
func getInputPath(inputPath string) (string, error) {
229241
if inputPath == "" {
230-
inputPath = currentWorkingDirectory
242+
cwd, err := os.Getwd()
243+
if err != nil {
244+
return "", fmt.Errorf("failed to get working directory: %w", err)
245+
}
246+
inputPath = cwd
231247
}
232248
projectPath := fmt.Sprintf("%s/%s", inputPath, yaml.DefaultPath)
233249
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
@@ -236,17 +252,6 @@ func getInputPath(currentWorkingDirectory, inputPath string) (string, error) {
236252
return inputPath, nil
237253
}
238254

239-
// Helper function to get output path.
240-
func getOutputPath(currentWorkingDirectory, outputPath string) (string, error) {
241-
if outputPath == "" {
242-
outputPath = fmt.Sprintf("%s/%s", currentWorkingDirectory, defaultOutputDir)
243-
}
244-
if _, err := os.Stat(outputPath); err == nil {
245-
return "", fmt.Errorf("output path %s already exists", outputPath)
246-
}
247-
return outputPath, nil
248-
}
249-
250255
// Helper function to get Init arguments for Kubebuilder.
251256
func getInitArgs(store store.Store) []string {
252257
var args []string

‎test/e2e/alphagenerate/generate_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,28 @@ var _ = Describe("kubebuilder", func() {
6868
kbc.Destroy()
6969
})
7070

71-
It("should regenerate the project with success", func() {
71+
It("should regenerate the project in current directory with success", func() {
7272
generateProject(kbc)
73-
regenerateProject(kbc, projectOutputDir)
74-
validateProjectFile(kbc, projectFilePath)
73+
regenerateProject(kbc)
74+
By("checking that the project file was generated in the current directory")
75+
validateProjectFile(kbc, filepath.Join(kbc.Dir, "PROJECT"))
7576
})
7677

7778
It("should regenerate project with grafana plugin with success", func() {
7879
generateProjectWithGrafanaPlugin(kbc)
79-
regenerateProject(kbc, projectOutputDir)
80+
regenerateProjectWith(kbc, projectOutputDir)
8081
validateGrafanaPlugin(projectFilePath)
8182
})
8283

8384
It("should regenerate project with DeployImage plugin with success", func() {
8485
generateProjectWithDeployImagePlugin(kbc)
85-
regenerateProject(kbc, projectOutputDir)
86+
regenerateProjectWith(kbc, projectOutputDir)
8687
validateDeployImagePlugin(projectFilePath)
8788
})
8889

8990
It("should regenerate project with helm plugin with success", func() {
9091
generateProjectWithHelmPlugin(kbc)
91-
regenerateProject(kbc, projectOutputDir)
92+
regenerateProjectWith(kbc, projectOutputDir)
9293
validateHelmPlugin(projectFilePath)
9394
})
9495
})
@@ -185,7 +186,13 @@ func generateProject(kbc *utils.TestContext) {
185186
Expect(err).NotTo(HaveOccurred(), "Failed to scaffold API with external API")
186187
}
187188

188-
func regenerateProject(kbc *utils.TestContext, projectOutputDir string) {
189+
func regenerateProject(kbc *utils.TestContext) {
190+
By("regenerating the project")
191+
err := kbc.Regenerate()
192+
Expect(err).NotTo(HaveOccurred(), "Failed to regenerate project")
193+
}
194+
195+
func regenerateProjectWith(kbc *utils.TestContext, projectOutputDir string) {
189196
By("regenerating the project")
190197
err := kbc.Regenerate(
191198
fmt.Sprintf("--input-dir=%s", kbc.Dir),

0 commit comments

Comments
 (0)