-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildtopia.go
932 lines (785 loc) · 33.9 KB
/
buildtopia.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
package main
//
//// IMPORTS
//
import (
// Modules in GOROOT
"fmt"
"os"
"strconv"
// External modules
cobra "github.com/spf13/cobra"
cobra_doc "github.com/spf13/cobra/doc"
)
//
////
//
func main() {
var program Program
var userDataDir string
// Initialize program
program = initializeDefaultProgram("")
var rootCmd = &cobra.Command{
Use: fmt.Sprintf("%v [command]", program.name),
Short: program.shortDescription,
Long: program.shortDescription,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if userDataDir != "" {
showAttention(fmt.Sprintf("Running %v using a custom user data directory: %v", program.name, userDataDir), program.indentLevel)
space()
program = initializeDefaultProgram(userDataDir)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
displayProgramInfo(program)
space()
showText(fmt.Sprintf("Run %v to get started.", blue.Sprintf("%v --help/-h", program.name)), program.indentLevel)
finishProgram(0)
},
}
rootCmd.PersistentFlags().StringVarP(&userDataDir, "directory", "D", "", "Custom user data directory")
var showVersionCmd = &cobra.Command{
Use: "version",
Short: "Show the program's version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(program.nameAscii)
space()
showInfoSectionTitle(fmt.Sprintf("Version: %s", green.Sprintf(program.version)), program.indentLevel)
},
}
var userInitCmd = &cobra.Command{
Use: "init",
Short: "Create user data directory",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(program.nameAscii)
space()
if userDataDir == "" {
userDataDir = program.userDataDir
}
showText(fmt.Sprintf("Creating user data directory at %v", green.Sprintf(userDataDir)), program.indentLevel)
if _, err := os.Stat(program.userDataDir); os.IsNotExist(err) {
verifyUserDataDirectory(false, decrementProgramIndentLevel(program, 1))
} else {
showAttention("> User data directory already exists", program.indentLevel+1)
}
},
}
var docsCmd = &cobra.Command{
Use: "docs",
Short: "Program documentation",
}
//
//// DOCUMENTATION
//
var docsOutDir string
var docsGenerateCmd = &cobra.Command{
Use: "generate",
Short: "Generate program documentation (markdown files)",
Run: func(cmd *cobra.Command, args []string) {
var response functionResponse
space()
fmt.Println(program.nameAscii)
space()
showInfoSectionTitle("Generating program documentation", program.indentLevel)
_, err := os.Stat(docsOutDir)
if os.IsNotExist(err) {
err := os.MkdirAll(docsOutDir, 0755)
if err != nil {
response = functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to create output directory -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
response = functionResponse{
exitCode: 0,
message: fmt.Sprintf("Created output directory at '%v'", docsOutDir),
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
}
} else {
if err != nil {
response = functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to verify if output directory exists -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
response = functionResponse{
exitCode: 1,
message: fmt.Sprintf("Output directory '%v' already exists. Remove it before generating the documentation", docsOutDir),
logLevel: "attention",
indentLevel: program.indentLevel + 1,
}
}
}
handleFunctionResponse(response, true)
err = cobra_doc.GenMarkdownTree(rootCmd, "./docs")
if err != nil {
response = functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to build documentation -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
response = functionResponse{
exitCode: 0,
message: "Finished",
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
}
handleFunctionResponse(response, true)
},
}
docsGenerateCmd.Flags().StringVarP(&docsOutDir, "out", "o", "./docs", "Output directory")
//
//// UTILITIES
//
var utilitiesCmd = &cobra.Command{
Use: "utils",
Short: "Utilites for hooks execution",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if userDataDir != "" {
showAttention(fmt.Sprintf("Running %v using a custom user data directory: %v", program.name, userDataDir), program.indentLevel)
space()
program = initializeDefaultProgram(userDataDir)
}
return nil
},
}
var utilityMsgCmd = &cobra.Command{
Use: "msg <color_hex> <message>",
Short: "Print a message in a specific color given a HEX code",
Long: `The 'msg' command allows you to print messages in a color
specified by a HEX code.
Arguments:
1. color_hex: This should be a valid HEX color code. The message will be
printed in this color.
2. message: This is the message that you want to print in the console. The
message will be printed in the color specified by color_hex.`,
Example: "utils msg '#FF5733' 'This is a colored message'",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
utilsMsg(args[0], args[1], program)
},
}
var utilityAttentionCmd = &cobra.Command{
Use: "attention <message>",
Short: "Display attention message",
Long: `The 'attention' command allows you to display a message
to attract user's attention.
Argument:
1. message: This is the attention-grabbing message you want to display.`,
Example: "utils attention 'This is an attention message'",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsShowAttention(args[0], program)
},
}
var utilityErrorCmd = &cobra.Command{
Use: "error <message>",
Short: "Display error message",
Long: `The 'error' command allows you to display an error
message.
Argument:
1. message: This is the error message you want to display.`,
Example: "utils error 'This is an error message'",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsShowError(args[0], program)
},
}
var utilitySuccessCmd = &cobra.Command{
Use: "success <message>",
Short: "Display success message",
Long: `The 'success' command allows you to display a success
message.
Argument:
1. message: This is the success message you want to display.`,
Example: "utils success 'This is a success message'",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsShowSuccess(args[0], program)
},
}
var utilitySectionCmd = &cobra.Command{
Use: "section <message>",
Short: "Display section title",
Long: `The 'section' command allows you to display a section
title.
Argument:
1. message: This is the section title you want to display.`,
Example: "utils section 'This is a section title'",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsShowSection(args[0], program)
},
}
var utilityHrCmd = &cobra.Command{
Use: "hr <char> <factor>",
Short: "Display a horizontal line",
Long: `The 'hr' command allows you to display a horizontal line.
Arguments:
1. char: This character is used to construct the horizontal line.
2. factor: This is the length factor of the line. The factor represents the
percentage of the terminal's width.`,
Example: "utils hr '-' 0.45",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
factor, _ := strconv.ParseFloat(args[1], 64)
utilsHr(args[0], factor, program)
},
}
var utilityConfirmCmd = &cobra.Command{
Use: "confirm <message>",
Short: "Ask for confirmation",
Long: `The 'confirm' command prompts the user for a confirmation
based on a provided message.
Argument:
1. message: This is the message to display when asking for confirmation.
The command will then wait for user input. If the user confirms (by typing
'y' or 'yes'),
the command will terminate the program with an exit status of 0.
If the user rejects (by typing 'n' or 'no'), the command will terminate the
program with an exit status of 1.`,
Example: "utils confirm 'Are you sure you want to continue?'",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsAskConfirmation(args[0], program)
},
}
var utilitySSHAgentStartCmd = &cobra.Command{
Use: "sshagent-start <privateKeyPath> <tempDir>",
Short: "Starts an ssh-agent process and adds a private key",
Long: `The 'sshagent-start' command starts an ssh-agent process
and adds a private key to it.
Arguments:
1. privateKeyPath: The path to the private key that will be added to the
ssh-agent.
2. tempDir: The temporary directory in which the ssh-agent will create its
socket and pid files. For example, it can be set with environment variables
$CRATE_TEMP_DIR or $TARGET_TEMP_DIR.`,
Example: "utils sshagent-start '/path/to/my/key' $TARGET_TEMP_DIR",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
response := utilsSSHAgentStart(args[0], args[1], program)
handleFunctionResponse(response, true)
},
}
var utilitySSHAgentStopCmd = &cobra.Command{
Use: "sshagent-stop <tempDir>",
Short: "Stops the ssh-agent process",
Long: `The 'sshagent-stop' command stops the ssh-agent process.
Arguments:
1. tempDir: The directory where the ssh-agent created its socket and pid
files. For example, it can be set with environment variables $CRATE_TEMP_DIR or
$TARGET_TEMP_DIR.`,
Example: "utils sshagent-stop $TARGET_TEMP_DIR",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
response := utilsSSHAgentStop(args[0], program)
handleFunctionResponse(response, true)
},
}
var utilitySSHAgentGetPIDCmd = &cobra.Command{
Use: "sshagent-getpid <tempDir>",
Short: "Get the process ID of the ssh-agent",
Long: `The 'sshagent-getpid' command retrieves the process ID of
the ssh-agent.
Arguments:
1. tempDir: The directory where the ssh-agent created its pid file. For
example, it can be set with environment variables $CRATE_TEMP_DIR or
$TARGET_TEMP_DIR.`,
Example: "utils sshagent-getpid $TARGET_TEMP_DIR",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsSSHAgentGetPID(args[0])
},
}
var utilitySSHAgentGetSockCmd = &cobra.Command{
Use: "sshagent-getsock <tempDir>",
Short: "Get the socket path of the ssh-agent",
Long: `The 'sshagent-getsock' command retrieves the socket path
of the ssh-agent.
Arguments:
1. tempDir: The directory where the ssh-agent created its socket file. For
example, it can be set with environment variables $CRATE_TEMP_DIR or
$TARGET_TEMP_DIR.`,
Example: "utils sshagent-getsock $TARGET_TEMP_DIR",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utilsSSHAgentGetSock(args[0])
},
}
//
////
//
var interactiveSelection bool
var notCreateTempDir bool
var notRemoveTempDir bool
var notPrintOutput bool
var cratePreHooks []string
var cratePostHooks []string
//
//// CRATES
//
var crateName string
var crateNames []string
var crateHooksNames []string
var allCrates bool
var cratesCmd = &cobra.Command{
Use: "crates",
Short: "Manage crates",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if userDataDir != "" {
showAttention(fmt.Sprintf("Running %v using a custom user data directory: %v", program.name, userDataDir), program.indentLevel)
space()
program = initializeDefaultProgram(userDataDir)
}
// Verify user data directory
response := verifyUserDataDirectory(true, program)
handleFunctionResponse(response, true)
return nil
},
}
var cratesEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit crates",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesEdit(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesEditCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
cratesEditCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesEditCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesEditCmd.Flags().SetInterspersed(false)
var cratesViewCmd = &cobra.Command{
Use: "view",
Short: "View crates",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesView(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesViewCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
cratesViewCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesViewCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesViewCmd.Flags().SetInterspersed(false)
var cratesEnableCmd = &cobra.Command{
Use: "enable",
Short: "Enable crates",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesEnable(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesEnableCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
cratesEnableCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesEnableCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesEnableCmd.Flags().SetInterspersed(false)
var cratesDisableCmd = &cobra.Command{
Use: "disable",
Short: "Disable crates",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesDisable(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesDisableCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
cratesDisableCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesDisableCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesDisableCmd.Flags().SetInterspersed(false)
var cratesCreateCmd = &cobra.Command{
Use: "create",
Short: "Create crates",
Run: func(cmd *cobra.Command, args []string) {
response := cratesCreate(program)
handleFunctionResponse(response, true)
},
}
var cratesRmCmd = &cobra.Command{
Use: "rm",
Short: "Remove crates",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesRm(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesRmCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
cratesRmCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesRmCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesRmCmd.Flags().SetInterspersed(false)
var cratesLsCmd = &cobra.Command{
Use: "ls",
Short: "List all crates",
Run: func(cmd *cobra.Command, args []string) {
response := cratesLs(program)
handleFunctionResponse(response, true)
},
}
var cratesHooksCmd = &cobra.Command{
Use: "hooks",
Short: "Manage crate hooks",
}
var cratesHooksLsCmd = &cobra.Command{
Use: "ls",
Short: "List crate hooks",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesHooksLs(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
cratesHooksLsCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
cratesHooksLsCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesHooksLsCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesHooksLsCmd.Flags().SetInterspersed(false)
var cratesHooksRunCmd = &cobra.Command{
Use: "run",
Short: "Run crate hook(s)",
Run: func(cmd *cobra.Command, args []string) {
if len(crateHooksNames) == 0 {
response := functionResponse{
exitCode: 1,
logLevel: "error",
message: fmt.Sprintf("Flag '--hook/-k' should be specified"),
indentLevel: program.indentLevel,
}
handleFunctionResponse(response, true)
}
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = cratesRunHooks(selectedCrates, crateHooksNames, notCreateTempDir, notRemoveTempDir, notPrintOutput, false, true, program)
handleFunctionResponse(response, true)
},
}
cratesHooksRunCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
cratesHooksRunCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
cratesHooksRunCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
cratesHooksRunCmd.Flags().StringSliceVarP(&crateHooksNames, "hook", "k", nil, "Hook(s) name(s)")
cratesHooksRunCmd.Flags().BoolVarP(¬CreateTempDir, "nocreatetemp", "", false, "Do not create the temporary directory before running the hook(s) (by default, it is created)")
cratesHooksRunCmd.Flags().BoolVarP(¬RemoveTempDir, "noremovetemp", "", false, "Do not remove the temporary directory after the hook(s) has/have finished running (by default, it is removed)")
cratesHooksRunCmd.Flags().BoolVarP(¬PrintOutput, "quiet", "q", false, "Do not print command output (silent)")
cratesHooksRunCmd.Flags().SetInterspersed(false)
//
//// TARGETS
//
var targetHooksNames []string
var targetNames []string
var allTargets bool
var targetsCmd = &cobra.Command{
Use: "targets",
Short: "Manage targets",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if userDataDir != "" {
showAttention(fmt.Sprintf("Running %v using a custom user data directory: %v", program.name, userDataDir), program.indentLevel)
space()
program = initializeDefaultProgram(userDataDir)
}
// Verify user data directory
response := verifyUserDataDirectory(true, program)
handleFunctionResponse(response, true)
return nil
},
}
var targetsLsCmd = &cobra.Command{
Use: "ls",
Short: "List targets",
Run: func(cmd *cobra.Command, args []string) {
selectedCrates, response := getSelectedCratesFromCLI(crateNames, allCrates, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsLs(selectedCrates, program)
handleFunctionResponse(response, true)
},
}
targetsLsCmd.Flags().StringSliceVarP(&crateNames, "crate", "c", nil, "Crate(s) name(s)")
targetsLsCmd.Flags().BoolVarP(&allCrates, "all", "a", false, "Include all crates")
targetsLsCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsLsCmd.Flags().SetInterspersed(false)
var targetsEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsEdit(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsEditCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsEditCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsEditCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsEditCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsEditCmd.Flags().SetInterspersed(false)
var targetsViewCmd = &cobra.Command{
Use: "view",
Short: "View targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsView(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsViewCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsViewCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsViewCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsViewCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsViewCmd.Flags().SetInterspersed(false)
var targetsBuildCmd = &cobra.Command{
Use: "build",
Short: "Build targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsBuild(crate, selectedTargets, cratePreHooks, cratePostHooks, program)
handleFunctionResponse(response, true)
},
}
targetsBuildCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsBuildCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsBuildCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsBuildCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsBuildCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsBuildCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsBuildCmd.Flags().SetInterspersed(false)
var targetsCheckCmd = &cobra.Command{
Use: "check",
Short: "Check targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsCheck(crate, selectedTargets, cratePreHooks, cratePostHooks, program)
handleFunctionResponse(response, true)
},
}
targetsCheckCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsCheckCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsCheckCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsCheckCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsCheckCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsCheckCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsCheckCmd.Flags().SetInterspersed(false)
var targetsInstallCmd = &cobra.Command{
Use: "install",
Short: "Install targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsInstall(crate, selectedTargets, cratePreHooks, cratePostHooks, program)
handleFunctionResponse(response, true)
},
}
targetsInstallCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsInstallCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsInstallCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsInstallCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsInstallCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsInstallCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsInstallCmd.Flags().SetInterspersed(false)
var targetsCleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsClean(crate, selectedTargets, cratePreHooks, cratePostHooks, program)
handleFunctionResponse(response, true)
},
}
targetsCleanCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsCleanCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsCleanCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsCleanCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsCleanCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsCleanCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsCleanCmd.Flags().SetInterspersed(false)
var targetsUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsUpdate(crate, selectedTargets, cratePreHooks, cratePostHooks, program)
handleFunctionResponse(response, true)
},
}
targetsUpdateCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsUpdateCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsUpdateCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsUpdateCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsUpdateCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsUpdateCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsUpdateCmd.Flags().SetInterspersed(false)
var targetsEnableCmd = &cobra.Command{
Use: "enable",
Short: "Enable targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsEnable(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsEnableCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsEnableCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsEnableCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsEnableCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsEnableCmd.Flags().SetInterspersed(false)
var targetsDisableCmd = &cobra.Command{
Use: "disable",
Short: "Disable targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsDisable(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsDisableCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsDisableCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsDisableCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsDisableCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsDisableCmd.Flags().SetInterspersed(false)
var targetsCreateCmd = &cobra.Command{
Use: "create",
Short: "Create targets",
Run: func(cmd *cobra.Command, args []string) {
response := targetsCreate(program)
handleFunctionResponse(response, true)
},
}
var targetsRmCmd = &cobra.Command{
Use: "rm",
Short: "Remove targets",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsRm(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsRmCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsRmCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsRmCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsRmCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsRmCmd.Flags().SetInterspersed(false)
var targetsHooksCmd = &cobra.Command{
Use: "hooks",
Short: "Manage target hooks",
}
var targetsHooksLsCmd = &cobra.Command{
Use: "ls",
Short: "List target hooks",
Run: func(cmd *cobra.Command, args []string) {
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsHooksLs(crate, selectedTargets, program)
handleFunctionResponse(response, true)
},
}
targetsHooksLsCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsHooksLsCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsHooksLsCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsHooksLsCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsHooksLsCmd.Flags().SetInterspersed(false)
var targetsHooksRunCmd = &cobra.Command{
Use: "run",
Short: "Run target hook(s)",
Run: func(cmd *cobra.Command, args []string) {
if len(targetHooksNames) == 0 {
response := functionResponse{
exitCode: 1,
logLevel: "error",
message: fmt.Sprintf("Flag '--hook/-k' should be specified"),
indentLevel: program.indentLevel,
}
handleFunctionResponse(response, true)
}
crate, selectedTargets, response := getSelectedTargetsFromCLI(crateName, targetNames, allTargets, interactiveSelection, true, program)
handleFunctionResponse(response, true)
response = targetsRunHooks(crate, selectedTargets, targetHooksNames, cratePreHooks, cratePostHooks, notCreateTempDir, notRemoveTempDir, notPrintOutput, false, true, program)
handleFunctionResponse(response, true)
},
}
targetsHooksRunCmd.Flags().StringVarP(&crateName, "crate", "c", "", "Crate name")
targetsHooksRunCmd.Flags().StringSliceVarP(&targetNames, "target", "t", nil, "Target(s) name(s)")
targetsHooksRunCmd.Flags().BoolVarP(&allTargets, "all", "a", false, "Include all targets")
targetsHooksRunCmd.Flags().BoolVarP(&interactiveSelection, "interactive", "i", false, "Interactive selection")
targetsHooksRunCmd.Flags().StringSliceVarP(&targetHooksNames, "hook", "k", nil, "Hook(s) name(s)")
targetsHooksRunCmd.Flags().BoolVarP(¬CreateTempDir, "nocreatetemp", "", false, "Do not create the temporary directory before running the hook(s) (by default, it is created)")
targetsHooksRunCmd.Flags().BoolVarP(¬RemoveTempDir, "noremovetemp", "n", false, "Do not remove the temporary directory after the hook(s) has/have finished running (by default, it is removed)")
targetsHooksRunCmd.Flags().StringSliceVarP(&cratePreHooks, "cratepre", "", nil, "Crate pre hook(s)")
targetsHooksRunCmd.Flags().StringSliceVarP(&cratePostHooks, "cratepost", "", nil, "Crate post hook(s)")
targetsHooksRunCmd.Flags().BoolVarP(¬PrintOutput, "quiet", "q", false, "Do not print command output (silent)")
targetsHooksRunCmd.Flags().SetInterspersed(false)
//
////
//
// Add Cobra commands
rootCmd.AddCommand(cratesCmd)
rootCmd.AddCommand(targetsCmd)
rootCmd.AddCommand(utilitiesCmd)
rootCmd.AddCommand(showVersionCmd)
rootCmd.AddCommand(userInitCmd)
rootCmd.AddCommand(docsCmd)
docsCmd.AddCommand(docsGenerateCmd)
utilitiesCmd.AddCommand(utilityMsgCmd)
utilitiesCmd.AddCommand(utilityAttentionCmd)
utilitiesCmd.AddCommand(utilityErrorCmd)
utilitiesCmd.AddCommand(utilitySuccessCmd)
utilitiesCmd.AddCommand(utilitySectionCmd)
utilitiesCmd.AddCommand(utilityHrCmd)
utilitiesCmd.AddCommand(utilityConfirmCmd)
utilitiesCmd.AddCommand(utilitySSHAgentStartCmd)
utilitiesCmd.AddCommand(utilitySSHAgentStopCmd)
utilitiesCmd.AddCommand(utilitySSHAgentGetPIDCmd)
utilitiesCmd.AddCommand(utilitySSHAgentGetSockCmd)
cratesCmd.AddCommand(cratesEditCmd)
cratesCmd.AddCommand(cratesViewCmd)
cratesCmd.AddCommand(cratesEnableCmd)
cratesCmd.AddCommand(cratesDisableCmd)
cratesCmd.AddCommand(cratesCreateCmd)
cratesCmd.AddCommand(cratesRmCmd)
cratesCmd.AddCommand(cratesLsCmd)
cratesCmd.AddCommand(cratesHooksCmd)
cratesHooksCmd.AddCommand(cratesHooksRunCmd)
cratesHooksCmd.AddCommand(cratesHooksLsCmd)
targetsCmd.AddCommand(targetsEditCmd)
targetsCmd.AddCommand(targetsViewCmd)
targetsCmd.AddCommand(targetsBuildCmd)
targetsCmd.AddCommand(targetsInstallCmd)
targetsCmd.AddCommand(targetsCheckCmd)
targetsCmd.AddCommand(targetsCleanCmd)
targetsCmd.AddCommand(targetsUpdateCmd)
targetsCmd.AddCommand(targetsEnableCmd)
targetsCmd.AddCommand(targetsDisableCmd)
targetsCmd.AddCommand(targetsCreateCmd)
targetsCmd.AddCommand(targetsRmCmd)
targetsCmd.AddCommand(targetsLsCmd)
targetsCmd.AddCommand(targetsHooksCmd)
targetsHooksCmd.AddCommand(targetsHooksRunCmd)
targetsHooksCmd.AddCommand(targetsHooksLsCmd)
if err := rootCmd.Execute(); err != nil {
showError("Error: "+err.Error(), program.indentLevel)
finishProgram(1)
}
}