diff --git a/README.md b/README.md index 4e9cd76..5371337 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ go install smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] ``` - ### Options: ``` @@ -58,6 +57,14 @@ smug [] [-f, --file ] [-w, --windows ]... [-a, --detach Detach session. The same as `-d` flag in the tmux ``` +### Custom settings + +You can pass custom settings into your configuration file. Use `${variable_name}` syntax in your config and then pass key-value args: + +``` +smug start project variable_name=value +``` + ### Examples To create a new project, or edit an existing one in the `$EDITOR`: @@ -110,7 +117,6 @@ Configuration files stored in the `~/.config/smug` directory in the `YAML` forma #### Example 1 - ```yaml session: blog @@ -147,6 +153,7 @@ windows: - docker-compose exec php /bin/sh - clear ``` + #### Example 2 ```yaml diff --git a/config.go b/config.go index dd89d5e..daeb8cb 100644 --- a/config.go +++ b/config.go @@ -49,13 +49,22 @@ func EditConfig(path string) error { return cmd.Run() } -func GetConfig(path string) (Config, error) { +func GetConfig(path string, settings map[string]string) (Config, error) { f, err := ioutil.ReadFile(path) if err != nil { return Config{}, err } - return ParseConfig(string(f)) + config := string(f) + config = os.Expand(config, func(v string) string { + if val, ok := settings[v]; ok { + return val + } + + return v + }) + + return ParseConfig(config) } func ParseConfig(data string) (Config, error) { diff --git a/main.go b/main.go index 4209834..1568827 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s Usage: - smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] + smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] [=]... Options: -f, --file %s @@ -90,7 +90,7 @@ func main() { } else { fmt.Println("Starting new windows...") } - config, err := GetConfig(configPath) + config, err := GetConfig(configPath, options.Settings) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) @@ -108,7 +108,7 @@ func main() { } else { fmt.Println("Killing windows...") } - config, err := GetConfig(configPath) + config, err := GetConfig(configPath, options.Settings) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/options.go b/options.go index 06decb1..1e4f949 100644 --- a/options.go +++ b/options.go @@ -19,13 +19,14 @@ const ( var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint} type Options struct { - Command string - Project string - Config string - Windows []string - Attach bool - Detach bool - Debug bool + Command string + Project string + Config string + Windows []string + Settings map[string]string + Attach bool + Detach bool + Debug bool } var ErrHelp = errors.New("help requested") @@ -71,6 +72,7 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { debug := flags.BoolP("debug", "d", false, DebugUsage) err := flags.Parse(argv) + if err == pflag.ErrHelp { return Options{}, ErrHelp } @@ -91,13 +93,26 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { windows = &wl } + 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] + } + } + return Options{ - Project: project, - Config: *config, - Command: cmd, - Windows: *windows, - Attach: *attach, - Detach: *detach, - Debug: *debug, + Project: project, + Config: *config, + Command: cmd, + Settings: settings, + Windows: *windows, + Attach: *attach, + Detach: *detach, + Debug: *debug, }, nil } diff --git a/options_test.go b/options_test.go index f551de3..38a6759 100644 --- a/options_test.go +++ b/options_test.go @@ -17,13 +17,14 @@ var usageTestTable = []struct { { []string{"start", "smug"}, Options{ - Command: "start", - Project: "smug", - Config: "", - Windows: []string{}, - Attach: false, - Detach: false, - Debug: false, + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: false, + Detach: false, + Debug: false, + Settings: map[string]string{}, }, nil, 0, @@ -31,13 +32,14 @@ var usageTestTable = []struct { { []string{"start", "smug", "-w", "foo"}, Options{ - Command: "start", - Project: "smug", - Config: "", - Windows: []string{"foo"}, - Attach: false, - Detach: false, - Debug: false, + Command: "start", + Project: "smug", + Config: "", + Windows: []string{"foo"}, + Attach: false, + Detach: false, + Debug: false, + Settings: map[string]string{}, }, nil, 0, @@ -45,13 +47,14 @@ var usageTestTable = []struct { { []string{"start", "smug:foo,bar"}, Options{ - Command: "start", - Project: "smug", - Config: "", - Windows: []string{"foo", "bar"}, - Attach: false, - Detach: false, - Debug: false, + Command: "start", + Project: "smug", + Config: "", + Windows: []string{"foo", "bar"}, + Attach: false, + Detach: false, + Debug: false, + Settings: map[string]string{}, }, nil, 0, @@ -59,33 +62,83 @@ var usageTestTable = []struct { { []string{"start", "smug", "--attach", "--debug", "--detach"}, Options{ - Command: "start", - Project: "smug", - Config: "", - Windows: []string{}, - Attach: true, - Detach: true, - Debug: true, + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: true, + Detach: true, + Debug: true, + Settings: map[string]string{}, }, nil, 0, }, { []string{"start", "smug", "-ad"}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: true, + Detach: false, + Debug: true, + Settings: map[string]string{}, + }, + nil, + 0, + }, + { + []string{"start", "-f", "test.yml"}, + Options{ + Command: "start", + Project: "", + Config: "test.yml", + Windows: []string{}, + Attach: false, + Detach: false, + Debug: false, + Settings: map[string]string{}, + }, + nil, + 0, + }, + { + []string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2"}, + Options{ + Command: "start", + Project: "", + Config: "test.yml", + Windows: []string{"win1", "win2"}, + Attach: false, + Detach: false, + Debug: false, + Settings: map[string]string{}, + }, + nil, + 0, + }, + { + []string{"start", "project", "a=b", "x=y"}, Options{ Command: "start", - Project: "smug", + Project: "project", Config: "", Windows: []string{}, - Attach: true, + Attach: false, Detach: false, - Debug: true, + Debug: false, + Settings: map[string]string{ + "a": "b", + "x": "y", + }, }, nil, 0, }, { - []string{"start", "-f", "test.yml"}, + []string{"start", "-f", "test.yml", "a=b", "x=y"}, Options{ Command: "start", Project: "", @@ -94,12 +147,16 @@ var usageTestTable = []struct { Attach: false, Detach: false, Debug: false, + Settings: map[string]string{ + "a": "b", + "x": "y", + }, }, nil, 0, }, { - []string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2"}, + []string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2", "a=b", "x=y"}, Options{ Command: "start", Project: "", @@ -108,6 +165,10 @@ var usageTestTable = []struct { Attach: false, Detach: false, Debug: false, + Settings: map[string]string{ + "a": "b", + "x": "y", + }, }, nil, 0,