diff --git a/Makefile b/Makefile index f5fac5a..c008785 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ ifndef GITHUB_TOKEN $(error GITHUB_TOKEN is not defined) endif sed -E -i.bak $(VERSION_REGEX) 'main.go' && rm main.go.bak + git commit -am 'Update version to $(version)' git tag -a $(version) -m '$(version)' git push origin $(version) goreleaser --rm-dist diff --git a/README.md b/README.md index b5ae4e1..007bb76 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ go install smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] ``` + ### Options: ``` @@ -55,6 +56,14 @@ smug [] [-f, --file ] [-w, --windows ]... [-a, ### Examples +To create a new project, or edit an existing one in the `$EDITOR`: + +```console +xyz@localhost:~$ smug new project + +xyz@localhost:~$ smug edit project +``` + To start/stop a project and all windows, run: ```console diff --git a/config.go b/config.go index 0735ff6..cd673a6 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,12 @@ package main -import "gopkg.in/yaml.v2" +import ( + "io/ioutil" + "os" + "os/exec" + + "gopkg.in/yaml.v2" +) type Pane struct { Root string `yaml:"root"` @@ -26,14 +32,36 @@ type Config struct { Windows []Window `yaml:"windows"` } -func ParseConfig(data string) (*Config, error) { +func EditConfig(path string) error { + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vim" + } + + cmd := exec.Command(editor, path) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() +} + +func GetConfig(path string) (Config, error) { + f, err := ioutil.ReadFile(path) + if err != nil { + return Config{}, err + } + + return ParseConfig(string(f)) +} + +func ParseConfig(data string) (Config, error) { c := Config{} err := yaml.Unmarshal([]byte(data), &c) - if err != nil { - return nil, err + return Config{}, err } - return &c, nil + return c, nil } diff --git a/main.go b/main.go index 01bb518..d3cb817 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,12 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" ) -const version = "v0.1.6" +const version = "v0.1.7" var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s @@ -55,18 +54,6 @@ func main() { configPath = filepath.Join(userConfigDir, options.Project+".yml") } - f, err := ioutil.ReadFile(configPath) - if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) - } - - config, err := ParseConfig(string(f)) - if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) - } - var logger *log.Logger if options.Debug { logFile, err := os.Create(filepath.Join(userConfigDir, "smug.log")) @@ -89,10 +76,15 @@ func main() { } else { fmt.Println("Starting new windows...") } - err = smug.Start(*config, options, context) + config, err := GetConfig(configPath) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } + + err = smug.Start(config, options, context) if err != nil { fmt.Println("Oops, an error occurred! Rolling back...") - smug.Stop(*config, options, context) + smug.Stop(config, options, context) } case CommandStop: if len(options.Windows) == 0 { @@ -100,11 +92,21 @@ func main() { } else { fmt.Println("Killing windows...") } - err = smug.Stop(*config, options, context) - } + config, err := GetConfig(configPath) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } - if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) + err = smug.Stop(config, options, context) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } + case CommandNew: + case CommandEdit: + err := EditConfig(configPath) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } } + } diff --git a/options.go b/options.go index 62630ab..05dad71 100644 --- a/options.go +++ b/options.go @@ -10,9 +10,11 @@ import ( const ( CommandStart = "start" CommandStop = "stop" + CommandNew = "new" + CommandEdit = "edit" ) -var validCommands = []string{CommandStart, CommandStop} +var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit} type Options struct { Command string