@@ -44,15 +44,36 @@ type Generate struct {
44
44
OutputDir string
45
45
}
46
46
47
- const defaultOutputDir = "output-dir"
48
-
49
47
// Generate handles the migration and scaffolding process.
50
48
func (opts * Generate ) Generate () error {
51
49
config , err := loadProjectConfig (opts .InputDir )
52
50
if err != nil {
53
51
return err
54
52
}
55
53
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
+
56
77
if err := createDirectory (opts .OutputDir ); err != nil {
57
78
return err
58
79
}
@@ -87,17 +108,8 @@ func (opts *Generate) Generate() error {
87
108
88
109
// Validate ensures the options are valid and kubebuilder is installed.
89
110
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 )
101
113
if err != nil {
102
114
return err
103
115
}
@@ -225,9 +237,13 @@ func createAPIWithDeployImage(resource v1alpha1.ResourceData) error {
225
237
}
226
238
227
239
// Helper function to get input path.
228
- func getInputPath (currentWorkingDirectory , inputPath string ) (string , error ) {
240
+ func getInputPath (inputPath string ) (string , error ) {
229
241
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
231
247
}
232
248
projectPath := fmt .Sprintf ("%s/%s" , inputPath , yaml .DefaultPath )
233
249
if _ , err := os .Stat (projectPath ); os .IsNotExist (err ) {
@@ -236,17 +252,6 @@ func getInputPath(currentWorkingDirectory, inputPath string) (string, error) {
236
252
return inputPath , nil
237
253
}
238
254
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
-
250
255
// Helper function to get Init arguments for Kubebuilder.
251
256
func getInitArgs (store store.Store ) []string {
252
257
var args []string
0 commit comments