@@ -247,122 +247,138 @@ func complete(d *MigrationData) {
247
247
}
248
248
249
249
func afterRan (d * MigrationData ) {
250
- // Determine the current user's shell
251
- shell := os .Getenv ("SHELL" )
252
- if shell == "" {
253
- fmt .Println ("Unable to determine the user's shell." )
254
- return
255
- }
256
-
257
- var rcFile string
258
- if strings .Contains (shell , "bash" ) {
259
- rcFile = ".bashrc"
260
- } else if strings .Contains (shell , "zsh" ) {
261
- rcFile = ".zshrc"
262
- } else {
263
- d .say (notice , "Not adding DB variables to RC file as shell %s is not BASH or ZSH." , shell )
264
- os .Exit (1 )
265
- }
266
-
267
- // Get the current user's home directory
268
- usr , err := user .Current ()
250
+ i , _ , err := (& promptui.Select {
251
+ Label : d .T ("Do you wish to add Harmony DB credentials to your ~/.bashrc or ~/.zshrc file?" ),
252
+ Items : []string {
253
+ d .T ("Yes" ),
254
+ d .T ("No" )},
255
+ Templates : d .selectTemplates ,
256
+ }).Run ()
269
257
if err != nil {
270
- d .say (notice , "Error getting user home directory: " , err )
258
+ d .say (notice , "Aborting remaining steps. " , err . Error () )
271
259
os .Exit (1 )
272
260
}
273
- rcFilePath := filepath .Join (usr .HomeDir , rcFile )
261
+ if i == 0 {
262
+ // Determine the current user's shell
263
+ shell := os .Getenv ("SHELL" )
264
+ if shell == "" {
265
+ fmt .Println ("Unable to determine the user's shell." )
266
+ return
267
+ }
274
268
275
- // Read the existing rc file content
276
- file , err := os .OpenFile (rcFilePath , os .O_RDWR | os .O_CREATE , 0644 )
277
- if err != nil {
278
- d .say (notice , "Error opening %s file: %s" , rcFile , err )
279
- os .Exit (1 )
280
- }
281
- defer func (file * os.File ) {
282
- _ = file .Close ()
283
- }(file )
284
-
285
- // Variables to be added or updated
286
- exportLines := map [string ]string {
287
- "CURIO_DB_HOST" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , strings .Join (d .HarmonyCfg .Hosts , "," )),
288
- "CURIO_DB_PORT" : fmt .Sprintf ("export CURIO_DB_PORT=%s" , d .HarmonyCfg .Port ),
289
- "CURIO_DB_NAME" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Database ),
290
- "CURIO_DB_USER" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Username ),
291
- "CURIO_DB_PASSWORD" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Password ),
292
- }
293
-
294
- // Flags to track whether we need to append these lines
295
- existingVars := map [string ]bool {
296
- "CURIO_DB_HOST" : false ,
297
- "CURIO_DB_PORT" : false ,
298
- "CURIO_DB_NAME" : false ,
299
- "CURIO_DB_USER" : false ,
300
- "CURIO_DB_PASSWORD" : false ,
301
- }
302
-
303
- var lines []string
304
- scanner := bufio .NewScanner (file )
305
-
306
- for scanner .Scan () {
307
- line := scanner .Text ()
308
- modified := false
309
-
310
- // Check each export line to see if it exists and is not commented out
311
- for key , newValue := range exportLines {
312
- if strings .HasPrefix (line , "export " + key + "=" ) && ! strings .HasPrefix (strings .TrimSpace (line ), "#" ) {
313
- lines = append (lines , newValue )
314
- existingVars [key ] = true
315
- modified = true
316
- break
317
- }
269
+ var rcFile string
270
+ if strings .Contains (shell , "bash" ) {
271
+ rcFile = ".bashrc"
272
+ } else if strings .Contains (shell , "zsh" ) {
273
+ rcFile = ".zshrc"
274
+ } else {
275
+ d .say (notice , "Not adding DB variables to RC file as shell %s is not BASH or ZSH." , shell )
276
+ os .Exit (1 )
318
277
}
319
278
320
- // If no modifications were made, retain the original line
321
- if ! modified {
322
- lines = append (lines , line )
279
+ // Get the current user's home directory
280
+ usr , err := user .Current ()
281
+ if err != nil {
282
+ d .say (notice , "Error getting user home directory:" , err )
283
+ os .Exit (1 )
323
284
}
324
- }
285
+ rcFilePath := filepath . Join ( usr . HomeDir , rcFile )
325
286
326
- if err := scanner .Err (); err != nil {
327
- d .say (notice , "Error reading %s file: %s" , rcFile , err )
328
- os .Exit (1 )
329
- }
287
+ // Read the existing rc file content
288
+ file , err := os .OpenFile (rcFilePath , os .O_RDWR | os .O_CREATE , 0644 )
289
+ if err != nil {
290
+ d .say (notice , "Error opening %s file: %s" , rcFile , err )
291
+ os .Exit (1 )
292
+ }
330
293
331
- // Append missing export lines
332
- for key , added := range existingVars {
333
- if ! added {
334
- lines = append (lines , exportLines [key ])
294
+ // Variables to be added or updated
295
+ exportLines := map [string ]string {
296
+ "CURIO_DB_HOST" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , strings .Join (d .HarmonyCfg .Hosts , "," )),
297
+ "CURIO_DB_PORT" : fmt .Sprintf ("export CURIO_DB_PORT=%s" , d .HarmonyCfg .Port ),
298
+ "CURIO_DB_NAME" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Database ),
299
+ "CURIO_DB_USER" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Username ),
300
+ "CURIO_DB_PASSWORD" : fmt .Sprintf ("export CURIO_DB_HOST=%s" , d .HarmonyCfg .Password ),
335
301
}
336
- }
337
302
338
- // Reopen the file in write mode to overwrite with updated content
339
- file , err = os .OpenFile (rcFilePath , os .O_WRONLY | os .O_TRUNC , 0644 )
340
- if err != nil {
341
- d .say (notice , "Error opening %s file in write mode:" , rcFile , err )
342
- return
343
- }
344
- defer func (file * os.File ) {
345
- _ = file .Close ()
346
- }(file )
303
+ // Flags to track whether we need to append these lines
304
+ existingVars := map [string ]bool {
305
+ "CURIO_DB_HOST" : false ,
306
+ "CURIO_DB_PORT" : false ,
307
+ "CURIO_DB_NAME" : false ,
308
+ "CURIO_DB_USER" : false ,
309
+ "CURIO_DB_PASSWORD" : false ,
310
+ }
347
311
348
- // Write all lines back to the rc file
349
- writer := bufio .NewWriter (file )
350
- for _ , line := range lines {
351
- if _ , err := writer .WriteString (line + "\n " ); err != nil {
352
- d .say (notice , "Error writing to %s file: %s" , rcFile , err )
353
- return
312
+ var lines []string
313
+ scanner := bufio .NewScanner (file )
314
+
315
+ for scanner .Scan () {
316
+ line := scanner .Text ()
317
+ modified := false
318
+
319
+ // Check each export line to see if it exists and is not commented out
320
+ for key , newValue := range exportLines {
321
+ if strings .HasPrefix (line , "export " + key + "=" ) && ! strings .HasPrefix (strings .TrimSpace (line ), "#" ) {
322
+ lines = append (lines , newValue )
323
+ existingVars [key ] = true
324
+ modified = true
325
+ break
326
+ }
327
+ }
328
+
329
+ // If no modifications were made, retain the original line
330
+ if ! modified {
331
+ lines = append (lines , line )
332
+ }
333
+ }
334
+
335
+ if err := scanner .Err (); err != nil {
336
+ d .say (notice , "Error reading %s file: %s" , rcFile , err )
337
+ os .Exit (1 )
338
+ }
339
+
340
+ // Append missing export lines
341
+ for key , added := range existingVars {
342
+ if ! added {
343
+ lines = append (lines , exportLines [key ])
344
+ }
354
345
}
355
- }
356
346
357
- for i := 1 ; i < 6 ; i ++ {
358
- err := writer .Flush ()
347
+ err = file .Close ()
359
348
if err != nil {
349
+ d .say (notice , "Error closing %s file: %s" , rcFile , err )
350
+ os .Exit (1 )
351
+ }
352
+
353
+ // Reopen the file in write mode to overwrite with updated content
354
+ file , err = os .OpenFile (rcFilePath , os .O_WRONLY | os .O_APPEND , 0644 )
355
+ if err != nil {
356
+ d .say (notice , "Error opening %s file in write mode:" , rcFile , err )
357
+ return
358
+ }
359
+ defer func (file * os.File ) {
360
+ _ = file .Close ()
361
+ }(file )
362
+
363
+ // Write all lines back to the rc file
364
+ writer := bufio .NewWriter (file )
365
+ for _ , line := range lines {
366
+ if _ , err := writer .WriteString (line + "\n " ); err != nil {
367
+ d .say (notice , "Error writing to %s file: %s" , rcFile , err )
368
+ return
369
+ }
370
+ }
371
+
372
+ for i := 1 ; i < 6 ; i ++ {
373
+ err := writer .Flush ()
374
+ if err != nil {
375
+ d .say (notice , "Failed to flush thw writes to file %s: %s" , rcFile , err )
376
+ d .say (notice , "Retrying.......(%d/5)" , i )
377
+ continue
378
+ }
360
379
d .say (notice , "Failed to flush thw writes to file %s: %s" , rcFile , err )
361
- d .say (notice , "Retrying.......(%d/5)" , i )
362
- continue
380
+ os .Exit (1 )
363
381
}
364
- d .say (notice , "Failed to flush thw writes to file %s: %s" , rcFile , err )
365
- os .Exit (1 )
366
382
}
367
383
368
384
d .say (plain , "Try the web interface with %s " , code .Render ("curio run --layers=gui" ))
0 commit comments