[mike@mwxm4]
This commit is contained in:
@@ -45,6 +45,13 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Answered before setup(), which exits when nothing is configured yet: an
|
||||
// update has to work on a machine that has never run mgsh, and `--version`
|
||||
// is what the freshly downloaded binary is probed with, right there.
|
||||
if updateFlags() {
|
||||
return
|
||||
}
|
||||
|
||||
setup()
|
||||
useColor = readline.IsTerminal(int(os.Stdout.Fd()))
|
||||
|
||||
@@ -68,12 +75,52 @@ func main() {
|
||||
}
|
||||
updateDirState()
|
||||
runCommand(cmdline)
|
||||
updateNote() // after the output: a footer, not a headline
|
||||
return
|
||||
}
|
||||
|
||||
updateNote() // before the prompt: a session starts here, not when it ends
|
||||
runInteractive()
|
||||
}
|
||||
|
||||
// updateFlags answers the self-update options and reports whether it did. They
|
||||
// are deliberately spelled with dashes and kept out of parseArgs: `mgsh update`
|
||||
// is the command for everyday use, and these are what works when there is no
|
||||
// configuration to read yet.
|
||||
func updateFlags() bool {
|
||||
if len(os.Args) < 2 {
|
||||
return false
|
||||
}
|
||||
switch os.Args[1] {
|
||||
case "--version":
|
||||
fmt.Printf("mgsh %s\n", VERSION)
|
||||
case "--update":
|
||||
if err := selfUpdate.install(os.Stdout); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "mgsh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
case "--check-update":
|
||||
if err := selfUpdate.check(os.Stdout); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "mgsh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
case updateRefreshFlag: // the background run, not in the help
|
||||
selfUpdate.refresh()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// updateNote prints the once-a-day hint, when there is one. It costs nothing:
|
||||
// the line comes from the note in the cache directory, and the asking behind it
|
||||
// happens in the background, at most once a day.
|
||||
func updateNote() {
|
||||
if hint := selfUpdate.daily(); hint != "" {
|
||||
fmt.Fprintln(os.Stderr, col(cDark, hint))
|
||||
}
|
||||
}
|
||||
|
||||
// runInteractive drives the colored, history- and completion-enabled REPL.
|
||||
func runInteractive() {
|
||||
home, _ := os.UserHomeDir()
|
||||
@@ -148,6 +195,7 @@ func parseArgs() (int, string, bool) {
|
||||
"push": 1, "pushremote": 1, "deleteremote": 1, "list": 1, "tag": 1, "archive": 1, "show": 1,
|
||||
"pull": 1, "fetch": 1, "status": 1, "diff": 1, "overview": 1,
|
||||
"config": 1, "count": 1, "login": 1, "cloneall": 1, "release": 1,
|
||||
"update": 1,
|
||||
}
|
||||
if c, ok := cls[a0]; ok {
|
||||
return c, strings.Join(os.Args[1:], " "), false
|
||||
|
||||
Reference in New Issue
Block a user