@@ -41,27 +41,66 @@ func runInitMonorepo(cmd *cobra.Command, args []string) {
41
41
initproject .IsMonorepo = true
42
42
initproject .RunInitProject (cmd , args )
43
43
44
- // Get apps to initialize
45
- appsOfMonorepoDir , err := prompt .PromptForMonorepoApps (cmd )
46
- if err != nil {
47
- printer .Error (cmd , err )
48
- return
49
- }
50
-
51
44
m , err := manifest .Restore ()
52
45
if err != nil {
53
46
printer .Error (cmd , err )
54
47
return
55
48
}
56
49
57
- // Initialize each app
58
- for _ , appDir := range appsOfMonorepoDir {
59
- printer .Info (fmt .Sprintf ("Initializing app %s" , appDir ))
60
- err := initializeMonorepoApp (cmd , appDir , orgID , m .Config , appService , envService , m .Secret )
50
+ for {
51
+ appPath , err := prompt .PromptString (cmd , "Provide the path to an application:" )
52
+ if err != nil {
53
+ printer .Error (cmd , err )
54
+ return
55
+ }
56
+
57
+ cleanPath := filepath .Clean (appPath )
58
+
59
+ if _ , err := os .Stat (cleanPath ); os .IsNotExist (err ) {
60
+ printer .Error (cmd , fmt .Errorf ("Directory does not exist: %s" , cleanPath ))
61
+ continue
62
+ }
63
+
64
+ if _ , err := os .Stat (cleanPath ); os .IsNotExist (err ) {
65
+ printer .Error (cmd , fmt .Errorf ("directory does not exist: %s" , cleanPath ))
66
+ continue
67
+ }
68
+
69
+ currentDir , err := os .Getwd ()
61
70
if err != nil {
62
- printer .Error (cmd , fmt .Errorf ("failed to initialize app %s: %w" , appDir , err ))
71
+ printer .Error (cmd , fmt .Errorf ("Failed to get current directory: %w" , err ))
72
+ return
73
+ }
74
+
75
+ absPath , err := filepath .Abs (cleanPath )
76
+ if err != nil {
77
+ printer .Error (cmd , fmt .Errorf ("Invalid path: %w" , err ))
78
+ continue
79
+ }
80
+
81
+ // Check if path is within current directory using filepath.Rel
82
+ relPath , err := filepath .Rel (currentDir , absPath )
83
+ if err != nil || strings .HasPrefix (relPath , ".." ) {
84
+ printer .Error (cmd , fmt .Errorf ("Invalid path: must be within current directory" ))
63
85
continue
64
86
}
87
+
88
+ printer .Info (fmt .Sprintf ("Initializing app %s" , cleanPath ))
89
+ err = initializeMonorepoApp (cmd , cleanPath , orgID , m .Config , appService , envService , m .Secret )
90
+ if err != nil {
91
+ printer .Error (cmd , fmt .Errorf ("Failed to initialize app %s: %w" , cleanPath , err ))
92
+ continue
93
+ }
94
+
95
+ if err := manifest .AddAppToLocalProject (cleanPath ); err != nil {
96
+ printer .Error (cmd , fmt .Errorf ("Failed to add app to local project: %w" , err ))
97
+ continue
98
+ }
99
+
100
+ moreApps := prompt .PromptYesNo (cmd , "Do you have another app?" , false )
101
+ if ! moreApps .Confirmed {
102
+ break
103
+ }
65
104
}
66
105
}
67
106
@@ -73,7 +112,7 @@ func initializeMonorepoApp(cmd *cobra.Command, appDir string, orgID string, mc m
73
112
var appName string
74
113
if ! response .Confirmed {
75
114
if response .IsFlag {
76
- return fmt .Errorf ("operation cancelled due to --no flag for app: %s" , defaultAppName )
115
+ return fmt .Errorf ("Operation cancelled due to --no flag for app: %s" , defaultAppName )
77
116
}
78
117
79
118
// Prompt for a new app name
@@ -97,7 +136,7 @@ func initializeMonorepoApp(cmd *cobra.Command, appDir string, orgID string, mc m
97
136
98
137
if ! response .Confirmed {
99
138
if response .IsFlag {
100
- return fmt .Errorf ("operation cancelled due to --no flag for app ID: %s" , defaultAppAlternateId )
139
+ return fmt .Errorf ("Operation cancelled due to --no flag for app ID: %s" , defaultAppAlternateId )
101
140
}
102
141
103
142
// Prompt for a custom app ID
@@ -133,7 +172,7 @@ func initializeMonorepoApp(cmd *cobra.Command, appDir string, orgID string, mc m
133
172
return handleErr
134
173
}
135
174
if existingApp == nil {
136
- return fmt .Errorf ("operation cancelled for app: %s" , appName )
175
+ return fmt .Errorf ("Operation cancelled for app: %s" , appName )
137
176
}
138
177
139
178
newApp = * existingApp
@@ -201,7 +240,7 @@ func CreateAndPushEmptyEnvFileMonorepo(cmd *cobra.Command, envService *env.EnvSe
201
240
202
241
// Ensure the directory exists
203
242
if err := os .MkdirAll (appDir , 0755 ); err != nil {
204
- return fmt .Errorf ("failed to create directory %s: %w" , appDir , err )
243
+ return fmt .Errorf ("Failed to create directory %s: %w" , appDir , err )
205
244
}
206
245
207
246
err = CreateGitignoredFileMonorepo (cmd , fullEnvPath , envFileName )
@@ -246,7 +285,7 @@ func CreateAndPushEmptyEnvFileMonorepo(cmd *cobra.Command, envService *env.EnvSe
246
285
}
247
286
248
287
if err := db .UpsertSecret (secretKey , newEnvDecrypted , version ); err != nil {
249
- return fmt .Errorf ("failed to save local environment: %w" , err )
288
+ return fmt .Errorf ("Failed to save local environment: %w" , err )
250
289
}
251
290
252
291
return nil
@@ -256,7 +295,7 @@ func CreateGitignoredFileMonorepo(cmd *cobra.Command, fullPath, fileName string)
256
295
// Ensure the directory exists
257
296
dir := filepath .Dir (fullPath )
258
297
if err := os .MkdirAll (dir , 0755 ); err != nil {
259
- return fmt .Errorf ("failed to create directory %s: %w" , dir , err )
298
+ return fmt .Errorf ("Failed to create directory %s: %w" , dir , err )
260
299
}
261
300
262
301
// check if the file already exists.
@@ -268,20 +307,20 @@ func CreateGitignoredFileMonorepo(cmd *cobra.Command, fullPath, fileName string)
268
307
// Create the file
269
308
file , err := os .Create (fullPath )
270
309
if err != nil {
271
- printer .Error (cmd , fmt .Errorf ("error creating %s: %w" , fullPath , err ))
310
+ printer .Error (cmd , fmt .Errorf ("Error creating %s: %w" , fullPath , err ))
272
311
return err
273
312
}
274
313
defer file .Close ()
275
314
276
315
// Write '# KEY=Value' to the file
277
316
_ , err = file .WriteString ("# Example\n # KEY=Value\n " )
278
317
if err != nil {
279
- printer .Error (cmd , fmt .Errorf ("error writing to %s: %w" , fullPath , err ))
318
+ printer .Error (cmd , fmt .Errorf ("Error writing to %s: %w" , fullPath , err ))
280
319
return err
281
320
}
282
321
283
322
if err := gitutil .EnsureGitignore (fileName ); err != nil {
284
- printer .Error (cmd , fmt .Errorf ("error adding %s to .gitignore: %w. Please do this manually if you wish" , fileName , err ))
323
+ printer .Error (cmd , fmt .Errorf ("Error adding %s to .gitignore: %w. Please do this manually if you wish" , fileName , err ))
285
324
// don't error here. Keep going.
286
325
}
287
326
0 commit comments