-
Notifications
You must be signed in to change notification settings - Fork 38
/
root.go
153 lines (125 loc) · 4.41 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
142
143
144
145
146
147
148
149
150
151
152
153
package cmd
import (
"os"
"os/user"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stateful/runme/v3/internal/cmd/beta"
"github.com/stateful/runme/v3/internal/extension"
)
var (
fAllowUnknown bool
fAllowUnnamed bool
fChdir string
fFileName string
fFileMode bool
fProject string
fProjectIgnorePatterns []string
fRespectGitignore bool
fSkipRunnerFallback bool
fInsecure bool
fLogEnabled bool
fLogFilePath string
fExtensionHandle string
fStateful bool
)
func Root() *cobra.Command {
cmd := cobra.Command{
Use: "runme",
Short: "Execute commands directly from a README",
Long: "Runme executes commands inside your runbooks, docs, and READMEs. Parses commands\ndirectly from markdown files to make them executable.",
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if strings.HasPrefix(fChdir, "~") {
cmd.PrintErrf("WARNING: --chdir starts with ~ which should be resolved by shell. Try re-running with --chdir %s (note lack of =) if it fails.\n\n", fChdir)
usr, _ := user.Current()
if fChdir == "~" {
fChdir = usr.HomeDir
} else if strings.HasPrefix(fChdir, "~/") {
fChdir = filepath.Join(usr.HomeDir, fChdir[2:])
}
}
// backwards compat
if envProject, ok := os.LookupEnv("RUNME_PROJECT"); ok {
fProject = envProject
}
// if insecure is explicitly set irrespective of its value, skip runner fallback
fSkipRunnerFallback = !cmd.Flags().Changed("insecure")
fFileMode = cmd.Flags().Changed("chdir") || cmd.Flags().Changed("filename")
if fFileMode && !cmd.Flags().Changed("allow-unnamed") {
fAllowUnnamed = true
}
if fExtensionHandle == "" && !fStateful {
fExtensionHandle = extension.DefaultExtensionName
} else {
fExtensionHandle = extension.PlatformExtensionName
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
},
}
setDefaultFlags(&cmd)
pflags := cmd.PersistentFlags()
pflags.BoolVar(&fAllowUnknown, "allow-unknown", true, "Display snippets without known executor")
pflags.BoolVar(&fAllowUnnamed, "allow-unnamed", false, "Allow scripts without explicit names")
pflags.StringVar(&fChdir, "chdir", getCwd(), "Switch to a different working directory before executing the command")
pflags.StringVar(&fFileName, "filename", "README.md", "Name of the README file")
pflags.BoolVar(&fInsecure, "insecure", false, "Run command in insecure-mode")
pflags.StringVar(&fProject, "project", "", "Root project to find runnable tasks")
pflags.BoolVar(&fRespectGitignore, "git-ignore", true, "Whether to respect .gitignore file(s) in project")
pflags.StringArrayVar(&fProjectIgnorePatterns, "ignore-pattern", []string{"node_modules", ".venv"}, "Patterns to ignore in project mode")
pflags.BoolVar(&fLogEnabled, "log", false, "Enable logging")
pflags.StringVar(&fLogFilePath, "log-file", filepath.Join(getTempDir(), "runme.log"), "Log file path")
pflags.BoolVar(&fStateful, "stateful", false, "Set Stateful instead of the Runme default")
setAPIFlags(pflags)
tuiCmd := tuiCmd()
// Make tuiCmd a default command.
cmd.RunE = tuiCmd.RunE
tuiCmd.Flags().VisitAll(func(f *pflag.Flag) {
if flag := cmd.Flags().Lookup(f.Name); flag == nil {
cmd.Flags().AddFlag(f)
}
})
cmd.AddCommand(codeServerCmd())
cmd.AddCommand(environmentCmd())
cmd.AddCommand(fmtCmd())
cmd.AddCommand(listCmd())
cmd.AddCommand(loginCmd())
cmd.AddCommand(logoutCmd())
cmd.AddCommand(printCmd())
cmd.AddCommand(extensionCmd())
cmd.AddCommand(runCmd())
cmd.AddCommand(beta.BetaCmd())
cmd.AddCommand(serverCmd())
cmd.AddCommand(shellCmd())
cmd.AddCommand(suggestCmd())
cmd.AddCommand(tasksCmd())
cmd.AddCommand(tokenCmd())
cmd.AddCommand(tuiCmd)
cmd.SetUsageTemplate(getUsageTemplate(cmd))
return &cmd
}
func getUsageTemplate(cmd cobra.Command) string {
return cmd.UsageTemplate() + `
Feedback:
For issues and questions join the Runme community at https://discord.gg/runme
`
}
func getCwd() string {
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
return cwd
}
func getTempDir() string {
tmp := "/var/tmp"
if _, err := os.Stat(tmp); err != nil {
tmp = os.TempDir()
}
return tmp
}