split main.go into modules, add --version flag
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type packageFlags struct {
|
||||
dryRun bool
|
||||
}
|
||||
|
||||
func parsePackageFlags(args []string) (packageFlags, string, error) {
|
||||
flags := packageFlags{}
|
||||
var pkg string
|
||||
for _, arg := range args {
|
||||
if arg == "--dry-run" {
|
||||
flags.dryRun = true
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(arg, "-") {
|
||||
return flags, "", fmt.Errorf("unknown flag %q", arg)
|
||||
}
|
||||
if pkg != "" {
|
||||
return flags, "", errors.New("too many arguments")
|
||||
}
|
||||
pkg = arg
|
||||
}
|
||||
if pkg == "" {
|
||||
return flags, "", errors.New("missing package")
|
||||
}
|
||||
return flags, pkg, nil
|
||||
}
|
||||
|
||||
var stdinReader *bufio.Reader
|
||||
|
||||
func newReader() *bufio.Reader {
|
||||
if stdinReader == nil {
|
||||
stdinReader = bufio.NewReader(os.Stdin)
|
||||
}
|
||||
return stdinReader
|
||||
}
|
||||
|
||||
func promptWithDefault(label, def string) string {
|
||||
reader := newReader()
|
||||
if def != "" {
|
||||
fmt.Printf("%s [%s]: ", label, def)
|
||||
} else {
|
||||
fmt.Printf("%s: ", label)
|
||||
}
|
||||
|
||||
text, _ := reader.ReadString('\n')
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
return def
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func promptYesNo(message string, def bool) (bool, error) {
|
||||
reader := newReader()
|
||||
defLabel := "y/N"
|
||||
if def {
|
||||
defLabel = "Y/n"
|
||||
}
|
||||
fmt.Printf("%s [%s]: ", message, defLabel)
|
||||
text, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
text = strings.TrimSpace(strings.ToLower(text))
|
||||
if text == "" {
|
||||
return def, nil
|
||||
}
|
||||
return text == "y" || text == "yes", nil
|
||||
}
|
||||
Reference in New Issue
Block a user