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)

@ -19,13 +19,14 @@ const (
var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint} var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint}
type Options struct { type Options struct {
Command string Command string
Project string Project string
Config string Config string
Windows []string Windows []string
Attach bool Settings map[string]string
Detach bool Attach bool
Debug bool Detach bool
Debug bool
} }
var ErrHelp = errors.New("help requested") 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) 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,13 +93,26 @@ 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,
Windows: *windows, Settings: settings,
Attach: *attach, Windows: *windows,
Detach: *detach, Attach: *attach,
Debug: *debug, Detach: *detach,
Debug: *debug,
}, nil }, nil
} }

@ -17,13 +17,14 @@ var usageTestTable = []struct {
{ {
[]string{"start", "smug"}, []string{"start", "smug"},
Options{ Options{
Command: "start", Command: "start",
Project: "smug", Project: "smug",
Config: "", Config: "",
Windows: []string{}, Windows: []string{},
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -31,13 +32,14 @@ var usageTestTable = []struct {
{ {
[]string{"start", "smug", "-w", "foo"}, []string{"start", "smug", "-w", "foo"},
Options{ Options{
Command: "start", Command: "start",
Project: "smug", Project: "smug",
Config: "", Config: "",
Windows: []string{"foo"}, Windows: []string{"foo"},
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -45,13 +47,14 @@ var usageTestTable = []struct {
{ {
[]string{"start", "smug:foo,bar"}, []string{"start", "smug:foo,bar"},
Options{ Options{
Command: "start", Command: "start",
Project: "smug", Project: "smug",
Config: "", Config: "",
Windows: []string{"foo", "bar"}, Windows: []string{"foo", "bar"},
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
@ -59,33 +62,83 @@ var usageTestTable = []struct {
{ {
[]string{"start", "smug", "--attach", "--debug", "--detach"}, []string{"start", "smug", "--attach", "--debug", "--detach"},
Options{ Options{
Command: "start", Command: "start",
Project: "smug", Project: "smug",
Config: "", Config: "",
Windows: []string{}, Windows: []string{},
Attach: true, Attach: true,
Detach: true, Detach: true,
Debug: true, Debug: true,
Settings: map[string]string{},
}, },
nil, nil,
0, 0,
}, },
{ {
[]string{"start", "smug", "-ad"}, []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{ Options{
Command: "start", Command: "start",
Project: "smug", Project: "project",
Config: "", Config: "",
Windows: []string{}, Windows: []string{},
Attach: true, Attach: false,
Detach: false, Detach: false,
Debug: true, Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
}, },
nil, nil,
0, 0,
}, },
{ {
[]string{"start", "-f", "test.yml"}, []string{"start", "-f", "test.yml", "a=b", "x=y"},
Options{ Options{
Command: "start", Command: "start",
Project: "", Project: "",
@ -94,12 +147,16 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
}, },
nil, nil,
0, 0,
}, },
{ {
[]string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2"}, []string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2", "a=b", "x=y"},
Options{ Options{
Command: "start", Command: "start",
Project: "", Project: "",
@ -108,6 +165,10 @@ var usageTestTable = []struct {
Attach: false, Attach: false,
Detach: false, Detach: false,
Debug: false, Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
}, },
nil, nil,
0, 0,

Loading…
Cancel
Save