@@ -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.
5048func (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.
89110func (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.
251256func getInitArgs (store store.Store ) []string {
252257 var args []string
0 commit comments