Allow passing custom settings (#60)

master
Ivan 3 years ago committed by GitHub
parent 6a55cc9603
commit 1bf548bb96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -47,7 +47,6 @@ go install
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug]
``` ```
### Options: ### Options:
``` ```
@ -58,6 +57,14 @@ smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a,
--detach Detach session. The same as `-d` flag in the tmux --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 ### Examples
To create a new project, or edit an existing one in the `$EDITOR`: 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 #### Example 1
```yaml ```yaml
session: blog session: blog
@ -147,6 +153,7 @@ windows:
- docker-compose exec php /bin/sh - docker-compose exec php /bin/sh
- clear - clear
``` ```
#### Example 2 #### Example 2
```yaml ```yaml

@ -49,13 +49,22 @@ func EditConfig(path string) error {
return cmd.Run() return cmd.Run()
} }
func GetConfig(path string) (Config, error) { func GetConfig(path string, settings map[string]string) (Config, error) {
f, err := ioutil.ReadFile(path) f, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return Config{}, err 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) { func ParseConfig(data string) (Config, error) {

@ -16,7 +16,7 @@ var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s
Usage: Usage:
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] [<key>=<value>]...
Options: Options:
-f, --file %s -f, --file %s
@ -90,7 +90,7 @@ func main() {
} else { } else {
fmt.Println("Starting new windows...") fmt.Println("Starting new windows...")
} }
config, err := GetConfig(configPath) config, err := GetConfig(configPath, options.Settings)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
@ -108,7 +108,7 @@ func main() {
} else { } else {
fmt.Println("Killing windows...") fmt.Println("Killing windows...")
} }
config, err := GetConfig(configPath) config, err := GetConfig(configPath, options.Settings)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

@ -23,6 +23,7 @@ type Options struct {
Project string Project string
Config string Config string
Windows []string Windows []string
Settings map[string]string
Attach bool Attach bool
Detach bool Detach bool
Debug bool Debug bool
@ -71,6 +72,7 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
debug := flags.BoolP("debug", "d", false, DebugUsage) debug := flags.BoolP("debug", "d", false, DebugUsage)
err := flags.Parse(argv) err := flags.Parse(argv)
if err == pflag.ErrHelp { if err == pflag.ErrHelp {
return Options{}, ErrHelp return Options{}, ErrHelp
} }
@ -91,10 +93,23 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
windows = &wl 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{ return Options{
Project: project, Project: project,
Config: *config, Config: *config,
Command: cmd, Command: cmd,
Settings: settings,
Windows: *windows, Windows: *windows,
Attach: *attach, Attach: *attach,
Detach: *detach, Detach: *detach,

@ -24,6 +24,7 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -38,6 +39,7 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -52,6 +54,7 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -66,6 +69,7 @@ var usageTestTable = []struct {
Attach: true, Attach: true,
Detach: true, Detach: true,
Debug: true, Debug: true,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -80,6 +84,7 @@ var usageTestTable = []struct {
Attach: true, Attach: true,
Detach: false, Detach: false,
Debug: true, Debug: true,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -94,6 +99,7 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -108,6 +114,61 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
},
nil,
0,
},
{
[]string{"start", "project", "a=b", "x=y"},
Options{
Command: "start",
Project: "project",
Config: "",
Windows: []string{},
Attach: false,
Detach: false,
Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
},
nil,
0,
},
{
[]string{"start", "-f", "test.yml", "a=b", "x=y"},
Options{
Command: "start",
Project: "",
Config: "test.yml",
Windows: []string{},
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", "a=b", "x=y"},
Options{
Command: "start",
Project: "",
Config: "test.yml",
Windows: []string{"win1", "win2"},
Attach: false,
Detach: false,
Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
}, },
nil, nil,
0, 0,

Loading…
Cancel
Save