@@ -20,6 +20,7 @@ import (
20
20
"os"
21
21
"regexp"
22
22
"strings"
23
+ "sync"
23
24
24
25
"github.com/spf13/cobra"
25
26
"github.com/spf13/cobra/doc"
@@ -80,10 +81,16 @@ type CommandFunc func(Flags) (int, error)
80
81
// Commands returns a list of commands initialised by
81
82
// RegisterCommand
82
83
func Commands () map [string ]Command {
84
+ commandsMu .RLock ()
85
+ defer commandsMu .RUnlock ()
86
+
83
87
return commands
84
88
}
85
89
86
- var commands = make (map [string ]Command )
90
+ var (
91
+ commandsMu sync.RWMutex
92
+ commands = make (map [string ]Command )
93
+ )
87
94
88
95
func init () {
89
96
RegisterCommand (Command {
@@ -441,7 +448,7 @@ EXPERIMENTAL: May be changed or removed.
441
448
})
442
449
443
450
defaultFactory .Use (func (rootCmd * cobra.Command ) {
444
- rootCmd . AddCommand ( caddyCmdToCobra ( Command {
451
+ manpageCommand := Command {
445
452
Name : "manpage" ,
446
453
Usage : "--directory <path>" ,
447
454
Short : "Generates the manual pages for Caddy commands" ,
@@ -471,11 +478,12 @@ argument of --directory. If the directory does not exist, it will be created.
471
478
return caddy .ExitCodeSuccess , nil
472
479
})
473
480
},
474
- }))
481
+ }
475
482
476
483
// source: https://github.com/spf13/cobra/blob/main/shell_completions.md
477
- rootCmd .AddCommand (& cobra.Command {
478
- Use : "completion [bash|zsh|fish|powershell]" ,
484
+ completionCommand := Command {
485
+ Name : "completion" ,
486
+ Usage : "[bash|zsh|fish|powershell]" ,
479
487
Short : "Generate completion script" ,
480
488
Long : fmt .Sprintf (`To load completions:
481
489
@@ -516,24 +524,37 @@ argument of --directory. If the directory does not exist, it will be created.
516
524
PS> %[1]s completion powershell > %[1]s.ps1
517
525
# and source this file from your PowerShell profile.
518
526
` , rootCmd .Root ().Name ()),
519
- DisableFlagsInUseLine : true ,
520
- ValidArgs : []string {"bash" , "zsh" , "fish" , "powershell" },
521
- Args : cobra .MatchAll (cobra .ExactArgs (1 ), cobra .OnlyValidArgs ),
522
- RunE : func (cmd * cobra.Command , args []string ) error {
523
- switch args [0 ] {
524
- case "bash" :
525
- return cmd .Root ().GenBashCompletion (os .Stdout )
526
- case "zsh" :
527
- return cmd .Root ().GenZshCompletion (os .Stdout )
528
- case "fish" :
529
- return cmd .Root ().GenFishCompletion (os .Stdout , true )
530
- case "powershell" :
531
- return cmd .Root ().GenPowerShellCompletionWithDesc (os .Stdout )
532
- default :
533
- return fmt .Errorf ("unrecognized shell: %s" , args [0 ])
527
+ CobraFunc : func (cmd * cobra.Command ) {
528
+ cmd .DisableFlagsInUseLine = true
529
+ cmd .ValidArgs = []string {"bash" , "zsh" , "fish" , "powershell" }
530
+ cmd .Args = cobra .MatchAll (cobra .ExactArgs (1 ), cobra .OnlyValidArgs )
531
+ cmd .RunE = func (cmd * cobra.Command , args []string ) error {
532
+ switch args [0 ] {
533
+ case "bash" :
534
+ return cmd .Root ().GenBashCompletion (os .Stdout )
535
+ case "zsh" :
536
+ return cmd .Root ().GenZshCompletion (os .Stdout )
537
+ case "fish" :
538
+ return cmd .Root ().GenFishCompletion (os .Stdout , true )
539
+ case "powershell" :
540
+ return cmd .Root ().GenPowerShellCompletionWithDesc (os .Stdout )
541
+ default :
542
+ return fmt .Errorf ("unrecognized shell: %s" , args [0 ])
543
+ }
534
544
}
535
545
},
536
- })
546
+ }
547
+
548
+ rootCmd .AddCommand (caddyCmdToCobra (manpageCommand ))
549
+ rootCmd .AddCommand (caddyCmdToCobra (completionCommand ))
550
+
551
+ // add manpage and completion commands to the map of
552
+ // available commands, because they're not registered
553
+ // through RegisterCommand.
554
+ commandsMu .Lock ()
555
+ commands [manpageCommand .Name ] = manpageCommand
556
+ commands [completionCommand .Name ] = completionCommand
557
+ commandsMu .Unlock ()
537
558
})
538
559
}
539
560
@@ -552,6 +573,9 @@ argument of --directory. If the directory does not exist, it will be created.
552
573
//
553
574
// This function should be used in init().
554
575
func RegisterCommand (cmd Command ) {
576
+ commandsMu .Lock ()
577
+ defer commandsMu .Unlock ()
578
+
555
579
if cmd .Name == "" {
556
580
panic ("command name is required" )
557
581
}
@@ -570,6 +594,7 @@ func RegisterCommand(cmd Command) {
570
594
defaultFactory .Use (func (rootCmd * cobra.Command ) {
571
595
rootCmd .AddCommand (caddyCmdToCobra (cmd ))
572
596
})
597
+ commands [cmd .Name ] = cmd
573
598
}
574
599
575
600
var commandNameRegex = regexp .MustCompile (`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$` )
0 commit comments