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()
}
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)
if err != nil {
return Config{}, err
return nil, err
}
config := string(f)
c, err := ParseConfig(config, settings)
if err != nil {
return Config{}, err
return nil, err
}
addDefaultEnvs(&c, path)
return c, err
return &c, err
}

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

@ -77,7 +77,7 @@ func (smug Smug) switchOrAttach(target string, attach bool, insideTmuxSession bo
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
if len(windows) == 0 {
sessionRoot := ExpandPath(config.Root)
@ -100,7 +100,7 @@ func (smug Smug) Stop(config Config, options *Options, context Context) error {
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 err error
@ -210,11 +210,17 @@ func (smug Smug) Start(config Config, options *Options, context Context) error {
}
if !options.InsideCurrentSession {
smug.tmux.KillWindow(sessionName + defaultWindowName)
smug.tmux.RenumberWindows(sessionName)
err := smug.tmux.KillWindow(sessionName + defaultWindowName)
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)
}
@ -236,7 +242,7 @@ func (smug Smug) GetConfigFromSession(options *Options, context Context) (Config
}
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 {
return Config{}, err
}

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

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

Loading…
Cancel
Save