New project commands (#24)

* Add new/edit commands
master
Ivan 3 years ago committed by GitHub
parent 40106e141d
commit e0a4407559
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,6 +15,7 @@ ifndef GITHUB_TOKEN
$(error GITHUB_TOKEN is not defined) $(error GITHUB_TOKEN is not defined)
endif endif
sed -E -i.bak $(VERSION_REGEX) 'main.go' && rm main.go.bak 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 tag -a $(version) -m '$(version)'
git push origin $(version) git push origin $(version)
goreleaser --rm-dist goreleaser --rm-dist

@ -44,6 +44,7 @@ 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:
``` ```
@ -55,6 +56,14 @@ smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a,
### Examples ### 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: To start/stop a project and all windows, run:
```console ```console

@ -1,6 +1,12 @@
package main package main
import "gopkg.in/yaml.v2" import (
"io/ioutil"
"os"
"os/exec"
"gopkg.in/yaml.v2"
)
type Pane struct { type Pane struct {
Root string `yaml:"root"` Root string `yaml:"root"`
@ -26,14 +32,36 @@ type Config struct {
Windows []Window `yaml:"windows"` 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{} c := Config{}
err := yaml.Unmarshal([]byte(data), &c) err := yaml.Unmarshal([]byte(data), &c)
if err != nil { if err != nil {
return nil, err return Config{}, err
} }
return &c, nil return c, nil
} }

@ -2,13 +2,12 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
) )
const version = "v0.1.6" const version = "v0.1.7"
var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s
@ -55,18 +54,6 @@ func main() {
configPath = filepath.Join(userConfigDir, options.Project+".yml") 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 var logger *log.Logger
if options.Debug { if options.Debug {
logFile, err := os.Create(filepath.Join(userConfigDir, "smug.log")) logFile, err := os.Create(filepath.Join(userConfigDir, "smug.log"))
@ -89,10 +76,15 @@ func main() {
} else { } else {
fmt.Println("Starting new windows...") 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 { if err != nil {
fmt.Println("Oops, an error occurred! Rolling back...") fmt.Println("Oops, an error occurred! Rolling back...")
smug.Stop(*config, options, context) smug.Stop(config, options, context)
} }
case CommandStop: case CommandStop:
if len(options.Windows) == 0 { if len(options.Windows) == 0 {
@ -100,11 +92,21 @@ func main() {
} else { } else {
fmt.Println("Killing windows...") 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 { err = smug.Stop(config, options, context)
fmt.Fprintln(os.Stderr, err.Error()) if err != nil {
os.Exit(1) fmt.Fprintf(os.Stderr, err.Error())
}
case CommandNew:
case CommandEdit:
err := EditConfig(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
}
} }
} }

@ -10,9 +10,11 @@ import (
const ( const (
CommandStart = "start" CommandStart = "start"
CommandStop = "stop" CommandStop = "stop"
CommandNew = "new"
CommandEdit = "edit"
) )
var validCommands = []string{CommandStart, CommandStop} var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit}
type Options struct { type Options struct {
Command string Command string

Loading…
Cancel
Save