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]
```
### 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
```
### 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

@ -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) {

@ -16,7 +16,7 @@ var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s
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:
-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)

@ -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
}

@ -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,

Loading…
Cancel
Save