-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroot.go
More file actions
85 lines (73 loc) · 2.2 KB
/
root.go
File metadata and controls
85 lines (73 loc) · 2.2 KB
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
package main
import (
"path/filepath"
"strings"
"github.com/FurqanSoftware/bullet/cfg"
"github.com/FurqanSoftware/bullet/scope"
"github.com/FurqanSoftware/bullet/spec"
"github.com/spf13/cobra"
)
var (
flagConfig string
flagSelect bool
currentScope scope.Scope
currentConfiguration cfg.Configuration
)
// RootCmd is the top-level Cobra command for the bullet CLI.
var RootCmd = &cobra.Command{
Use: "bullet",
Short: "Bullet is a fast application deployment tool",
Long: `Bullet is a fast and flexible application deployment tool built by Furqan Software and friends. Complete documentation is available at https://bullettool.com/.`,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
for c := cmd; c != nil; c = c.Parent() {
if c.Name() == "completion" || c.Name() == "help" {
return nil
}
}
printBanner()
g, err := cfg.NewLoader().
ParseFileIfExists("Bulletcfg." + flagConfig).
ApplyEnvironment().
ApplyFlags(cmd.Flags()).
Configuration()
if err != nil {
return err
}
s := scope.Scope{}
s.Spec, err = spec.ParseFile("Bulletspec")
if err != nil {
return err
}
s.Spec.ExpandVars(g.Vars)
s.Nodes, err = scope.ParseNodeSet(g.Hosts, g.Port, g.Identity)
if err != nil {
return err
}
currentScope = s
currentConfiguration = g
if flagSelect {
currentScope, err = NewSelector().Nodes(currentScope)
if err != nil {
return err
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func init() {
RootCmd.PersistentFlags().StringVarP(&flagConfig, "config", "c", "", "Name of the configuration to apply")
RootCmd.PersistentFlags().BoolVarP(&flagSelect, "select", "s", false, "Prompt to select a subset of nodes before running")
RootCmd.RegisterFlagCompletionFunc("config", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
matches, _ := filepath.Glob("Bulletcfg.*")
names := make([]string, 0, len(matches))
for _, m := range matches {
names = append(names, strings.TrimPrefix(m, "Bulletcfg."))
}
return names, cobra.ShellCompDirectiveNoFileComp
})
cfg.AddFlags(RootCmd.PersistentFlags())
}