This repository was archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
104 lines (83 loc) · 2.42 KB
/
main.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
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"github.com/posener/complete"
"go.coder.com/cli"
)
// Dedicated to nhooyr_software.
var _ interface {
cli.Command
cli.FlaggedCommand
cli.ParentCommand
} = new(rootCmd)
type rootCmd struct {
globalFlags
installAutocomplete bool
uninstallAutocomplete bool
}
func (r *rootCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "sail",
Usage: "[GLOBAL FLAGS] COMMAND [COMMAND FLAGS] [ARGS....]",
Desc: `A utility for managing Docker-based code-server environments.
More info: https://github.com/cdr/sail
[project] can be of form <org>/<repo> for GitHub repos, or the full git clone address.`,
}
}
func (r *rootCmd) Run(fl *flag.FlagSet) {
if r.handleAutocomplete() {
return
}
// The root command doesn't do anything.
fl.Usage()
}
func (r *rootCmd) RegisterFlags(fl *flag.FlagSet) {
fl.BoolVar(&r.verbose, "v", false, "Enable debug logging.")
fl.StringVar(&r.configPath, "config",
filepath.Join(metaRoot(), "sail.toml"),
"Path to config.",
)
// We don't use these directly, just added for visability on fl.Usage().
fl.BoolVar(&r.installAutocomplete, "install-autocomplete", false, "Install autocomplete")
fl.BoolVar(&r.uninstallAutocomplete, "uninstall-autocomplete", false, "Uninstall autocomplete")
}
func (r rootCmd) Subcommands() []cli.Command {
extHostCmd := &installExtHostCmd{}
return []cli.Command{
&runcmd{gf: &r.globalFlags},
&shellcmd{gf: &r.globalFlags},
&editcmd{gf: &r.globalFlags},
&lscmd{},
&rmcmd{gf: &r.globalFlags},
&proxycmd{},
extHostCmd,
&chromeExtInstallCmd{cmd: extHostCmd},
&versioncmd{},
}
}
func main() {
root := &rootCmd{}
if (len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "chrome-extension://")) ||
(len(os.Args) >= 3 && strings.HasPrefix(os.Args[2], "chrome-extension://")) ||
(len(os.Args) >= 2 && strings.HasSuffix(os.Args[1], "com.coder.sail.json")) {
runNativeMsgHost()
return
}
cli.RunRoot(root)
}
func (r *rootCmd) handleAutocomplete() bool {
cmds := []cli.Command{r}
cmds = append(cmds, cli.ParentCommand(r).Subcommands()...)
cmp := complete.New("sail", genAutocomplete(cmds))
cmp.InstallName = "install-autocomplete"
cmp.UninstallName = "uninstall-autocomplete"
// only call run if we know we want to install/uninstall autocomplete
if r.installAutocomplete || r.uninstallAutocomplete {
return cmp.Run()
}
// otherwise just process autocomplete
return cmp.Complete()
}