-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
181 lines (148 loc) · 5.5 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2020 Dmitry Ermolov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
)
// Contains path that was used to launch cod.
// If relative or full path were used to launch app then CodBinaryPath is absolute and cleaned.
// If cod was launched from PATH then this variable is just `cod` (or whatever is the name of binary).
var CodBinaryPath = "cod"
var GitSha = "<unknown-git-sha>"
var Version = fmt.Sprintf("0.0.1rc (compiled from: %v)", GitSha)
func fatal(err error) {
_, err = fmt.Fprintf(os.Stderr, "%v: error: %v\n", path.Base(CodBinaryPath), err)
if err != nil {
panic(err)
}
os.Exit(1)
}
func verifyFatal(err error) {
if err != nil {
fatal(err)
}
}
func getCodBinaryPath() string {
exe, err := os.Executable()
verifyFatal(err)
if !strings.ContainsRune(exe, os.PathSeparator) {
return exe
}
if filepath.IsAbs(exe) {
return filepath.Clean(exe)
}
result, err := filepath.Abs(exe)
verifyFatal(err)
return result
}
func main() {
CodBinaryPath = getCodBinaryPath()
//
// Arguments
//
var shell string
var pid uint
var foreground bool
var selectors []string
var createConfig bool
addShellArg := func(c *kingpin.CmdClause) *kingpin.CmdClause {
c.Arg("shell", "Shell name (bash, zsh or fish).").Required().StringVar(&shell)
return c
}
addPidArg := func(c *kingpin.CmdClause) *kingpin.CmdClause {
c.Arg("pid", "PID of the shell").Required().UintVar(&pid)
return c
}
app := kingpin.New("cod", "Shell autocomplete generator based on `--help' texts.")
app.UsageTemplate(kingpin.CompactUsageTemplate)
app.Version(Version)
learn := app.Command("learn", "Learn new completions from help command.")
learnArgs := learn.Arg("subject", "Subject to learn.").Required().Strings()
list := app.Command("list", "List known commands.").Alias("ls")
list.Arg("selector", "Items to list.").StringsVar(&selectors)
remove := app.Command("remove", "Forget known command").Alias("rm")
remove.Arg("selector", "Items to remove.").Required().StringsVar(&selectors)
update := app.Command("update", "Update known command")
update.Arg("selector", "Items to update.").Required().StringsVar(&selectors)
init := app.Command("init", "Output shell initialization script.")
addPidArg(init)
addShellArg(init)
exampleConfig := app.Command("example-config", "print example configuration to stdout")
exampleConfig.Flag(
"create",
"write configuration to config file instead of printing it to stdout (doesn't work if config file already exists)",
).BoolVar(&createConfig)
daemon := app.Command("daemon", "Start cod daemon.")
daemon.Flag("foreground", "Run daemon in foreground.").BoolVar(&foreground)
api := app.Command("api", "shell <-> cod interaction.").Hidden()
apiAttach := api.Command("attach", "Attach daemon.").Hidden()
addPidArg(apiAttach)
addShellArg(apiAttach)
apiBashCleanCompletions := api.Command("bash-clean-completions", "Clean completions").Hidden()
appBase := apiBashCleanCompletions.Arg("executable", "executable to clean").Required().String()
apiPollUpdates := api.Command("poll-updates", "poll updates from server").Hidden()
addPidArg(apiPollUpdates)
apiPostexec := api.Command("postexec", "check if command is help and suggest to learn it").Hidden()
addPidArg(apiPostexec)
apiPostexecCommand := apiPostexec.Arg("command", "command to analyze").Required().String()
apiListClients := api.Command("list-clients", "help list all attached shells").Hidden()
apiCompleteWords := api.Command("complete-words", "Get completions for given command line.").Hidden()
addPidArg(apiCompleteWords)
apiCompleteWordsCWord := apiCompleteWords.Arg("c-word", "Index of a word being completed.").Required().Int()
apiCompleteWordsWords := apiCompleteWords.Arg("words", "Command line being completed.").Required().Strings()
apiForkedDaemon := api.Command("forked-daemon", "Helper method to run a daemon").Hidden()
notifyPid := apiForkedDaemon.Arg("pid", "Pid to notify after command start").Required().Int()
//
// Work
//
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
// commands
case learn.FullCommand():
learnMain(*learnArgs)
case list.FullCommand():
listMain(selectors)
case init.FullCommand():
initMain(pid, shell)
case daemon.FullCommand():
daemonMain(foreground)
case remove.FullCommand():
removeMain(selectors)
case update.FullCommand():
updateMain(selectors)
case exampleConfig.FullCommand():
exampleConfigMain(createConfig)
// api
case apiAttach.FullCommand():
shellApiAttachMain(pid, shell)
case apiPollUpdates.FullCommand():
apiPollUpdatesMain(pid)
case apiPostexec.FullCommand():
apiPostexecMain(pid, *apiPostexecCommand)
case apiCompleteWords.FullCommand():
apiCompleteWordsMain(pid, *apiCompleteWordsCWord, *apiCompleteWordsWords)
case apiListClients.FullCommand():
apiListClientsMain()
case apiForkedDaemon.FullCommand():
forkedDaemonMain(*notifyPid)
case apiBashCleanCompletions.FullCommand():
apiBashCleanCompletionsMain(*appBase)
default:
panic("unexpected command")
}
}