/
root.go
141 lines (121 loc) · 3.81 KB
/
root.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
package cmd
import (
"fmt"
"log"
"os"
"path"
"github.com/lunarway/shuttle/pkg/ui"
"github.com/lunarway/shuttle/pkg/config"
"github.com/spf13/cobra"
)
var (
projectPath string
uii ui.UI = ui.Create()
verboseFlag bool
clean bool
skipGitPlanPulling bool
plan string
version = "<dev-version>"
commit = "<unspecified-commit>"
)
const rootCmdCompletion = `
__shuttle_run_script_args() {
local cur prev args_output args
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
template=$'{{ range $i, $arg := .Args }}{{ $arg.Name }}\n{{ end }}'
if args_output=$(shuttle --skip-pull run "$1" --help --template "$template" 2>/dev/null); then
args=($(echo "${args_output}"))
COMPREPLY=( $( compgen -W "${args[*]}" -- "$cur" ) )
compopt -o nospace
fi
}
# find available scripts to run
__shuttle_run_scripts() {
local cur prev scripts currentScript
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
currentScript="${COMP_WORDS[2]}"
if [[ ! "${prev}" = "run" ]]; then
__shuttle_run_script_args $currentScript
return 0
fi
template=$'{{ range $name, $script := .Scripts }}{{ $name }}\n{{ end }}'
if scripts_output=$(shuttle --skip-pull ls --template "$template" 2>/dev/null); then
scripts=($(echo "${scripts_output}"))
COMPREPLY=( $( compgen -W "${scripts[*]}" -- "$cur" ) )
fi
return 0
}
# called when the build in completion fails to match
__shuttle_custom_func() {
case ${last_command} in
shuttle_run)
__shuttle_run_scripts
return
;;
*)
;;
esac
}
`
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "shuttle",
Short: "A CLI for handling shared build and deploy tools between many projects no matter what technologies the project is using.",
Long: fmt.Sprintf(`shuttle %s
A CLI for handling shared build and deploy tools between many
projects no matter what technologies the project is using.
Read more about shuttle at https://github.com/lunarway/shuttle`, version),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verboseFlag {
uii = uii.SetUserLevel(ui.LevelVerbose)
}
uii.Verboseln("Running shuttle")
uii.Verboseln("- version: %s", version)
uii.Verboseln("- commit: %s", commit)
uii.Verboseln("- project-path: %s", projectPath)
},
BashCompletionFunction: rootCmdCompletion,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
var uii = ui.Create()
uii.ExitWithErrorCode(1, "%s", err)
}
}
func init() {
rootCmd.PersistentFlags().StringVarP(&projectPath, "project", "p", ".", "Project path")
rootCmd.PersistentFlags().BoolVarP(&clean, "clean", "c", false, "Start from clean setup")
rootCmd.PersistentFlags().BoolVar(&skipGitPlanPulling, "skip-pull", false, "Skip git plan pulling step")
rootCmd.PersistentFlags().StringVar(&plan, "plan", "", `Overload the plan used.
Specifying a local path with either an absolute path (/some/plan) or a relative path (../some/plan) to another location
for the selected plan.
Select a version of a git plan by using #branch, #sha or #tag
If none of above is used, then the argument will expect a full plan spec.`)
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Print verbose output")
}
func getProjectContext() config.ShuttleProjectContext {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
var fullProjectPath string
if path.IsAbs(projectPath) {
fullProjectPath = projectPath
} else {
fullProjectPath = path.Join(dir, projectPath)
}
if plan == "" {
env := os.Getenv("SHUTTLE_PLAN_OVERLOAD")
if env != "" {
plan = env
}
}
var c config.ShuttleProjectContext
c.Setup(fullProjectPath, uii, clean, skipGitPlanPulling, plan)
return c
}