refactor: Code cleanup & improvements

* fix: Fprintf -> Fprint

* fix: Golinter fixes

* refactor: Use pointer semantics for config
master
Ivan 2 years ago committed by GitHub
parent 856940b719
commit abc059242d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -55,22 +55,22 @@ func EditConfig(path string) error {
return cmd.Run() return cmd.Run()
} }
func GetConfig(path string, settings map[string]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 nil, err
} }
config := string(f) config := string(f)
c, err := ParseConfig(config, settings) c, err := ParseConfig(config, settings)
if err != nil { if err != nil {
return Config{}, err return nil, err
} }
addDefaultEnvs(&c, path) addDefaultEnvs(&c, path)
return c, err return &c, err
} }

@ -59,7 +59,7 @@ func newLogger(path string) *log.Logger {
func main() { func main() {
options, err := ParseOptions(os.Args[1:]) options, err := ParseOptions(os.Args[1:])
if err == ErrHelp { if err == ErrHelp {
fmt.Fprintf(os.Stdout, usage) fmt.Fprint(os.Stdout, usage)
os.Exit(0) os.Exit(0)
} }
@ -100,7 +100,7 @@ func main() {
} }
config, err := GetConfig(configPath, options.Settings) config, err := GetConfig(configPath, options.Settings)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
@ -118,25 +118,25 @@ func main() {
} }
config, err := GetConfig(configPath, options.Settings) config, err := GetConfig(configPath, options.Settings)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
err = smug.Stop(config, options, context) err = smug.Stop(config, options, context)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
case CommandNew, CommandEdit: case CommandNew, CommandEdit:
err := EditConfig(configPath) err := EditConfig(configPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
case CommandList: case CommandList:
configs, err := ListConfigs(userConfigDir) configs, err := ListConfigs(userConfigDir)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
@ -144,13 +144,13 @@ func main() {
case CommandPrint: case CommandPrint:
config, err := smug.GetConfigFromSession(options, context) config, err := smug.GetConfigFromSession(options, context)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
d, err := yaml.Marshal(&config) d, err := yaml.Marshal(&config)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }

@ -77,7 +77,7 @@ func (smug Smug) switchOrAttach(target string, attach bool, insideTmuxSession bo
return nil return nil
} }
func (smug Smug) Stop(config Config, options *Options, context Context) error { func (smug Smug) Stop(config *Config, options *Options, context Context) error {
windows := options.Windows windows := options.Windows
if len(windows) == 0 { if len(windows) == 0 {
sessionRoot := ExpandPath(config.Root) sessionRoot := ExpandPath(config.Root)
@ -100,7 +100,7 @@ func (smug Smug) Stop(config Config, options *Options, context Context) error {
return nil return nil
} }
func (smug Smug) Start(config Config, options *Options, context Context) error { func (smug Smug) Start(config *Config, options *Options, context Context) error {
var sessionName string var sessionName string
var err error var err error
@ -210,11 +210,17 @@ func (smug Smug) Start(config Config, options *Options, context Context) error {
} }
if !options.InsideCurrentSession { if !options.InsideCurrentSession {
smug.tmux.KillWindow(sessionName + defaultWindowName) err := smug.tmux.KillWindow(sessionName + defaultWindowName)
smug.tmux.RenumberWindows(sessionName) if err != nil {
return err
}
err = smug.tmux.RenumberWindows(sessionName)
if err != nil {
return err
}
} }
if len(windows) == 0 && len(config.Windows) > 0 && options.Detach == false { if len(windows) == 0 && len(config.Windows) > 0 && !options.Detach {
return smug.switchOrAttach(sessionName+config.Windows[0].Name, attach, context.InsideTmuxSession) return smug.switchOrAttach(sessionName+config.Windows[0].Name, attach, context.InsideTmuxSession)
} }
@ -236,7 +242,7 @@ func (smug Smug) GetConfigFromSession(options *Options, context Context) (Config
} }
for _, w := range tmuxWindows { for _, w := range tmuxWindows {
tmuxPanes, err := smug.tmux.ListPanes(options.Project + ":" + w.Id) tmuxPanes, err := smug.tmux.ListPanes(options.Project + ":" + w.ID)
if err != nil { if err != nil {
return Config{}, err return Config{}, err
} }

@ -9,7 +9,7 @@ import (
) )
var testTable = map[string]struct { var testTable = map[string]struct {
config Config config *Config
options *Options options *Options
context Context context Context
startCommands []string startCommands []string
@ -17,7 +17,7 @@ var testTable = map[string]struct {
commanderOutputs []string commanderOutputs []string
}{ }{
"test with 1 window": { "test with 1 window": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "~/root", Root: "~/root",
BeforeStart: []string{"command1", "command2"}, BeforeStart: []string{"command1", "command2"},
@ -48,7 +48,7 @@ var testTable = map[string]struct {
[]string{"ses", "win1"}, []string{"ses", "win1"},
}, },
"test with 1 window and Detach: true": { "test with 1 window and Detach: true": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
BeforeStart: []string{"command1", "command2"}, BeforeStart: []string{"command1", "command2"},
@ -78,7 +78,7 @@ var testTable = map[string]struct {
[]string{"xyz"}, []string{"xyz"},
}, },
"test with multiple windows and panes": { "test with multiple windows and panes": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{
@ -125,7 +125,7 @@ var testTable = map[string]struct {
[]string{"ses", "ses", "win1", "1"}, []string{"ses", "ses", "win1", "1"},
}, },
"test start windows from option's Windows parameter": { "test start windows from option's Windows parameter": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{
@ -157,7 +157,7 @@ var testTable = map[string]struct {
[]string{"xyz"}, []string{"xyz"},
}, },
"test attach to the existing session": { "test attach to the existing session": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{
@ -176,7 +176,7 @@ var testTable = map[string]struct {
[]string{""}, []string{""},
}, },
"test start a new session from another tmux session": { "test start a new session from another tmux session": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
}, },
@ -194,7 +194,7 @@ var testTable = map[string]struct {
[]string{"xyz"}, []string{"xyz"},
}, },
"test switch a client from another tmux session": { "test switch a client from another tmux session": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{
@ -213,7 +213,7 @@ var testTable = map[string]struct {
[]string{""}, []string{""},
}, },
"test create new windows in current session with same name": { "test create new windows in current session with same name": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{
@ -236,7 +236,7 @@ var testTable = map[string]struct {
[]string{"ses", ""}, []string{"ses", ""},
}, },
"test create new windows in current session with different name": { "test create new windows in current session with different name": {
Config{ &Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{ Windows: []Window{

@ -13,9 +13,6 @@ const (
const ( const (
EvenHorizontal = "even-horizontal" EvenHorizontal = "even-horizontal"
EvenVertical = "even-vertical"
MainHorizontal = "main-horizontal"
MainVertical = "main-vertical"
Tiled = "tiled" Tiled = "tiled"
) )
@ -24,7 +21,7 @@ type Tmux struct {
} }
type TmuxWindow struct { type TmuxWindow struct {
Id string ID string
Name string Name string
Layout string Layout string
Root string Root string
@ -146,7 +143,7 @@ func (tmux Tmux) ListWindows(target string) ([]TmuxWindow, error) {
for _, w := range windowsList { for _, w := range windowsList {
windowInfo := strings.Split(w, ";") windowInfo := strings.Split(w, ";")
window := TmuxWindow{ window := TmuxWindow{
Id: windowInfo[0], ID: windowInfo[0],
Name: windowInfo[1], Name: windowInfo[1],
Layout: windowInfo[2], Layout: windowInfo[2],
Root: windowInfo[3], Root: windowInfo[3],

Loading…
Cancel
Save