2020-12-20 13:48:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-30 14:40:12 +00:00
|
|
|
"errors"
|
2020-12-20 13:48:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
"github.com/spf13/pflag"
|
2020-12-20 13:48:13 +00:00
|
|
|
)
|
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
const (
|
|
|
|
CommandStart = "start"
|
|
|
|
CommandStop = "stop"
|
2021-01-08 09:59:45 +00:00
|
|
|
CommandNew = "new"
|
|
|
|
CommandEdit = "edit"
|
2021-02-26 19:27:11 +00:00
|
|
|
CommandList = "list"
|
2021-03-21 19:59:19 +00:00
|
|
|
CommandPrint = "print"
|
2020-12-30 14:40:12 +00:00
|
|
|
)
|
2020-12-20 13:48:13 +00:00
|
|
|
|
2021-03-21 19:59:19 +00:00
|
|
|
var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint}
|
2020-12-30 17:20:02 +00:00
|
|
|
|
2020-12-20 13:48:13 +00:00
|
|
|
type Options struct {
|
2022-02-01 11:49:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
var ErrHelp = errors.New("help requested")
|
2020-12-20 13:48:13 +00:00
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
const (
|
2022-02-01 11:49:36 +00:00
|
|
|
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-30 14:40:12 +00:00
|
|
|
)
|
2020-12-20 13:48:13 +00:00
|
|
|
|
2020-12-30 14:40:12 +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 {
|
2020-12-30 17:20:02 +00:00
|
|
|
f := pflag.NewFlagSet(cmd, pflag.ContinueOnError)
|
|
|
|
return f
|
2020-12-30 14:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseOptions(argv []string, helpRequested func()) (Options, error) {
|
2020-12-30 17:20:02 +00:00
|
|
|
if len(argv) == 0 {
|
|
|
|
helpRequested()
|
|
|
|
return Options{}, ErrHelp
|
|
|
|
}
|
|
|
|
|
|
|
|
if argv[0] == "--help" || argv[0] == "-h" {
|
2020-12-30 14:40:12 +00:00
|
|
|
helpRequested()
|
|
|
|
return Options{}, ErrHelp
|
2020-12-23 23:38:39 +00:00
|
|
|
}
|
2020-12-20 13:48:13 +00:00
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
cmd := argv[0]
|
2020-12-30 17:20:02 +00:00
|
|
|
if !Contains(validCommands, cmd) {
|
|
|
|
helpRequested()
|
|
|
|
return Options{}, ErrHelp
|
|
|
|
}
|
2020-12-30 14:40:12 +00:00
|
|
|
|
|
|
|
flags := NewFlagSet(cmd)
|
|
|
|
|
2020-12-30 17:20:02 +00:00
|
|
|
config := flags.StringP("file", "f", "", FileUsage)
|
2020-12-30 14:40:12 +00:00
|
|
|
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)
|
2020-12-30 14:40:12 +00:00
|
|
|
debug := flags.BoolP("debug", "d", false, DebugUsage)
|
2022-02-01 11:49:36 +00:00
|
|
|
insideCurrentSession := flags.BoolP("inside-current-session", "i", false, InsideCurrentSessionUsage)
|
2020-12-30 14:40:12 +00:00
|
|
|
|
|
|
|
err := flags.Parse(argv)
|
2021-04-29 20:33:03 +00:00
|
|
|
|
2020-12-30 14:40:12 +00:00
|
|
|
if err == pflag.ErrHelp {
|
|
|
|
return Options{}, ErrHelp
|
2020-12-20 13:48:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 13:42:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return Options{}, err
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:20:02 +00:00
|
|
|
var project string
|
2021-02-26 19:27:11 +00:00
|
|
|
if *config == "" && len(argv) > 1 {
|
2020-12-30 17:20:02 +00:00
|
|
|
project = argv[1]
|
|
|
|
}
|
|
|
|
|
2020-12-20 13:48:13 +00:00
|
|
|
if strings.Contains(project, ":") {
|
|
|
|
parts := strings.Split(project, ":")
|
|
|
|
project = parts[0]
|
2020-12-30 14:40:12 +00:00
|
|
|
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{
|
2022-02-01 11:49:36 +00:00
|
|
|
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
|
|
|
}
|