smug/options.go

123 lines
3.0 KiB
Go
Raw Normal View History

2020-12-20 13:48:13 +00:00
package main
import (
"errors"
2020-12-20 13:48:13 +00:00
"strings"
"github.com/spf13/pflag"
2020-12-20 13:48:13 +00:00
)
const (
CommandStart = "start"
CommandStop = "stop"
CommandNew = "new"
CommandEdit = "edit"
CommandList = "list"
CommandPrint = "print"
)
2020-12-20 13:48:13 +00:00
var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint}
2020-12-20 13:48:13 +00:00
type Options struct {
Command string
Project string
Config string
Windows []string
Settings map[string]string
Attach bool
Detach bool
Debug bool
InsideCurrentSession bool
2020-12-20 13:48:13 +00:00
}
var ErrHelp = errors.New("help requested")
2020-12-20 13:48:13 +00:00
const (
WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session"
AttachUsage = "Force switch client for a session"
DetachUsage = "Detach tmux session. The same as -d flag in the tmux"
DebugUsage = "Print all commands to ~/.config/smug/smug.log"
FileUsage = "A custom path to a config file"
InsideCurrentSessionUsage = "Create all windows inside current session"
)
2020-12-20 13:48:13 +00:00
// Creates a new FlagSet.
// Moved it to a variable to be able to override it in the tests.
var NewFlagSet = func(cmd string) *pflag.FlagSet {
f := pflag.NewFlagSet(cmd, pflag.ContinueOnError)
return f
}
func ParseOptions(argv []string, helpRequested func()) (Options, error) {
if len(argv) == 0 {
helpRequested()
return Options{}, ErrHelp
}
if argv[0] == "--help" || argv[0] == "-h" {
helpRequested()
return Options{}, ErrHelp
}
2020-12-20 13:48:13 +00:00
cmd := argv[0]
if !Contains(validCommands, cmd) {
helpRequested()
return Options{}, ErrHelp
}
flags := NewFlagSet(cmd)
config := flags.StringP("file", "f", "", FileUsage)
windows := flags.StringArrayP("windows", "w", []string{}, WindowsUsage)
attach := flags.BoolP("attach", "a", false, AttachUsage)
2021-04-13 06:43:35 +00:00
detach := flags.Bool("detach", false, DetachUsage)
debug := flags.BoolP("debug", "d", false, DebugUsage)
insideCurrentSession := flags.BoolP("inside-current-session", "i", false, InsideCurrentSessionUsage)
err := flags.Parse(argv)
2021-04-29 20:33:03 +00:00
if err == pflag.ErrHelp {
return Options{}, ErrHelp
2020-12-20 13:48:13 +00:00
}
if err != nil {
return Options{}, err
}
var project string
if *config == "" && len(argv) > 1 {
project = argv[1]
}
2020-12-20 13:48:13 +00:00
if strings.Contains(project, ":") {
parts := strings.Split(project, ":")
project = parts[0]
wl := strings.Split(parts[1], ",")
windows = &wl
2020-12-20 13:48:13 +00:00
}
2021-04-29 20:33:03 +00:00
settings := make(map[string]string)
userSettings := flags.Args()[1:]
if len(userSettings) > 0 {
for _, kv := range userSettings {
s := strings.Split(kv, "=")
if len(s) < 2 {
continue
}
settings[s[0]] = s[1]
}
}
2021-04-13 06:43:35 +00:00
return Options{
Project: project,
Config: *config,
Command: cmd,
Settings: settings,
Windows: *windows,
Attach: *attach,
Detach: *detach,
Debug: *debug,
InsideCurrentSession: *insideCurrentSession,
2021-04-13 06:43:35 +00:00
}, nil
2020-12-20 13:48:13 +00:00
}