forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (214 loc) · 7.75 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// gs (git-spice) is a command line tool for stacking Git branches.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"github.com/alecthomas/kong"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/mattn/go-isatty"
"go.abhg.dev/gs/internal/browser"
"go.abhg.dev/gs/internal/cli/shorthand"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/forge/github"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/secret"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/ui"
"go.abhg.dev/komplete"
)
// Version of git-spice.
// Set by goreleaser at build time.
var _version = "dev"
var (
// _secretStash is the secret stash used by the application.
//
// This is overridden in tests to use a memory stash.
_secretStash secret.Stash = new(secret.Keyring)
// _browserLauncher opens URLs in the user's configured browser.
_browserLauncher browser.Launcher = new(browser.Browser)
)
var errNoPrompt = fmt.Errorf("not allowed to prompt for input")
var _highlightStyle = ui.NewStyle().Foreground(ui.Cyan).Bold(true)
func main() {
logger := log.NewWithOptions(os.Stderr, log.Options{
Level: log.InfoLevel,
})
// Register supported forges.
forge.Register(&github.Forge{Log: logger})
styles := log.DefaultStyles()
styles.Levels[log.DebugLevel] = ui.NewStyle().SetString("DBG").Bold(true)
styles.Levels[log.InfoLevel] = ui.NewStyle().SetString("INF").Foreground(lipgloss.Color("10")).Bold(true) // green
styles.Levels[log.WarnLevel] = ui.NewStyle().SetString("WRN").Foreground(lipgloss.Color("11")).Bold(true) // yellow
styles.Levels[log.ErrorLevel] = ui.NewStyle().SetString("ERR").Foreground(lipgloss.Color("9")).Bold(true) // red
styles.Levels[log.FatalLevel] = ui.NewStyle().SetString("FTL").Foreground(lipgloss.Color("9")).Bold(true) // red
logger.SetStyles(styles)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
go func() {
select {
case <-sigc:
logger.Info("Cleaning up. Press Ctrl-C again to exit immediately.")
cancel()
case <-ctx.Done():
}
}()
// On macOS, UserConfigDir always returns ~/Library/Application Support.
// XDG_CONFIG_HOME should take precedence if set.
userConfigDir := os.Getenv("XDG_CONFIG_HOME")
if userConfigDir == "" {
var err error
userConfigDir, err = os.UserConfigDir()
if err != nil {
logger.Fatalf("Error getting user config directory: %v", err)
}
}
spiceConfig, err := spice.LoadConfig(
ctx,
git.NewConfig(git.ConfigOptions{Log: logger}),
spice.ConfigOptions{
Log: logger,
},
)
if err != nil {
logger.Error("Error loading spice configuration; continuing without it.", "error", err)
}
secretStash := &secret.FallbackStash{
Primary: _secretStash,
Secondary: &secret.InsecureStash{
Path: filepath.Join(filepath.Join(userConfigDir, "git-spice"), "secrets.json"),
Log: logger,
},
}
var cmd mainCmd
// Forges may register additional command line flags
// by implementing CLIPlugin.
for f := range forge.All {
if plugin := f.CLIPlugin(); plugin != nil {
cmd.Plugins = append(cmd.Plugins, plugin)
}
}
cmdName := filepath.Base(os.Args[0])
parser, err := kong.New(&cmd,
kong.Name(cmdName),
kong.Description("gs (git-spice) is a command line tool for stacking Git branches."),
kong.Resolvers(spiceConfig),
kong.Bind(logger, &cmd.globalOptions),
kong.BindTo(ctx, (*context.Context)(nil)),
kong.BindTo(secretStash, (*secret.Stash)(nil)),
kong.Vars{
// Default to prompting only when the terminal is interactive.
"defaultPrompt": strconv.FormatBool(isatty.IsTerminal(os.Stdin.Fd())),
},
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Help(func(options kong.HelpOptions, ctx *kong.Context) error {
if err := kong.DefaultHelpPrinter(options, ctx); err != nil {
return err
}
// For the help of the top-level command,
// print a note about shorthand aliases.
if len(ctx.Command()) == 0 {
_, _ = fmt.Fprintf(ctx.Stdout,
"\nAliases can be combined to form shorthands for commands. For example:\n"+
" %[1]v bc => %[1]v branch create\n"+
" %[1]v cc => %[1]v commit create\n",
cmdName,
)
}
return nil
}),
)
if err != nil {
panic(err)
}
// The default help flag text has a period at the end,
// which doesn't match the rest of our help text.
// Remove the period and place it in the same group
// as the other global flags.
if help := parser.Model.HelpFlag; help != nil {
help.Help = "Show help for the command"
help.Group = &kong.Group{
Key: "globals",
Title: "Global Flags:",
}
}
builtinShorthands, err := shorthand.NewBuiltin(parser.Model)
if err != nil {
panic(err)
}
// user-configured shorthands take precedence over builtins.
shorthands := shorthand.Sources{spiceConfig, builtinShorthands}
komplete.Run(parser,
komplete.WithTransformCompleted(func(args []string) []string {
return shorthand.Expand(shorthands, args)
}),
komplete.WithPredictor("branches", komplete.PredictFunc(predictBranches)),
komplete.WithPredictor("trackedBranches", komplete.PredictFunc(predictTrackedBranches)),
komplete.WithPredictor("remotes", komplete.PredictFunc(predictRemotes)),
komplete.WithPredictor("dirs", komplete.PredictFunc(predictDirs)),
komplete.WithPredictor("forges", komplete.PredictFunc(predictForges)),
)
args := os.Args[1:]
if len(args) == 0 {
// If invoked with no arguments, show help,
// but then exit with a non-zero status code.
args = []string{"--help"}
parser.Exit = func(int) {
logger.Print("")
logger.Fatalf("%v: please provide a command", cmdName)
}
} else {
// Otherwise, expand the shorthands before parsing.
args = shorthand.Expand(shorthands, args)
}
kctx, err := parser.Parse(args)
if err != nil {
logger.Fatalf("%v: %v", cmdName, err)
}
if err := kctx.Run(builtinShorthands); err != nil {
logger.Fatalf("%v: %v", cmdName, err)
}
}
type globalOptions struct {
// Flags that are not accessed directly by command implementations:
Version versionFlag `help:"Print version information and quit"`
Verbose bool `short:"v" help:"Enable verbose output" env:"GIT_SPICE_VERBOSE"`
Dir kong.ChangeDirFlag `short:"C" placeholder:"DIR" help:"Change to DIR before doing anything" predictor:"dirs"`
// Flags that are accessed directly:
Prompt bool `name:"prompt" negatable:"" default:"${defaultPrompt}" help:"Whether to prompt for missing information"`
}
type mainCmd struct {
kong.Plugins
globalOptions `group:"globals"`
Shell shellCmd `cmd:"" group:"Shell"`
Auth authCmd `cmd:"" group:"Authentication"`
Repo repoCmd `cmd:"" aliases:"r" group:"Repository"`
Log logCmd `cmd:"" aliases:"l" group:"Log"`
Stack stackCmd `cmd:"" aliases:"s" group:"Stack"`
Upstack upstackCmd `cmd:"" aliases:"us" group:"Stack"`
Downstack downstackCmd `cmd:"" aliases:"ds" group:"Stack"`
Branch branchCmd `cmd:"" aliases:"b" group:"Branch"`
Commit commitCmd `cmd:"" aliases:"c" group:"Commit"`
Rebase rebaseCmd `cmd:"" aliases:"rb" group:"Rebase"`
// Navigation
Up upCmd `cmd:"" aliases:"u" group:"Navigation" help:"Move up one branch"`
Down downCmd `cmd:"" aliases:"d" group:"Navigation" help:"Move down one branch"`
Top topCmd `cmd:"" aliases:"U" group:"Navigation" help:"Move to the top of the stack"`
Bottom bottomCmd `cmd:"" aliases:"D" group:"Navigation" help:"Move to the bottom of the stack"`
Trunk trunkCmd `cmd:"" group:"Navigation" help:"Move to the trunk branch"`
// Hidden commands:
DumpMD dumpMarkdownCmd `name:"dumpmd" hidden:"" cmd:"" help:"Dump a Markdown reference to stdout and quit"`
}
func (cmd *mainCmd) AfterApply(kctx *kong.Context, logger *log.Logger) error {
if cmd.Verbose {
logger.SetLevel(log.DebugLevel)
}
return nil
}