Skip to content

Commit

Permalink
Allow passing custom settings (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Apr 29, 2021
1 parent 6a55cc9 commit 1bf548b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 54 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ go install
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug]
```


### Options:

```
Expand All @@ -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`:
Expand Down Expand Up @@ -110,7 +117,6 @@ Configuration files stored in the `~/.config/smug` directory in the `YAML` forma

#### Example 1


```yaml
session: blog

Expand Down Expand Up @@ -147,6 +153,7 @@ windows:
- docker-compose exec php /bin/sh
- clear
```
#### Example 2
```yaml
Expand Down
13 changes: 11 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
43 changes: 29 additions & 14 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
127 changes: 94 additions & 33 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,128 @@ 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,
},
{
[]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,
},
{
[]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,
},
{
[]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: "",
Expand All @@ -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: "",
Expand All @@ -108,6 +165,10 @@ var usageTestTable = []struct {
Attach: false,
Detach: false,
Debug: false,
Settings: map[string]string{
"a": "b",
"x": "y",
},
},
nil,
0,
Expand Down

0 comments on commit 1bf548b

Please sign in to comment.