diff --git a/Makefile b/Makefile index fa16cbd..d6548bc 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ VERSION ?= 0.0.7 LDFLAGS ?= -ldflags "-s -w -X 'tmuxist/command.Version=$(VERSION)'" +# HACK: make [target] [ARGS...] ARGS = $(filter-out $@,$(MAKECMDGOALS)) %: @: diff --git a/command/attach.go b/command/attach.go index 172d752..1e1c9ae 100644 --- a/command/attach.go +++ b/command/attach.go @@ -3,6 +3,7 @@ package command import ( "context" "flag" + "os" "github.com/google/subcommands" @@ -39,13 +40,17 @@ func (cmd *AttachCommand) SetFlags(f *flag.FlagSet) { // Execute executes attach tmux session and returns an ExitStatus. func (cmd *AttachCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - fpath, err := config.ConfigurationPath(cmd.profile) + path, err := config.ConfigurationPath(cmd.profile) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure } + if _, err := os.Stat(path); err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } - c, err := config.LoadFile(fpath) + c, err := config.LoadFile(path) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure diff --git a/command/edit.go b/command/edit.go index be2f9e4..a199811 100644 --- a/command/edit.go +++ b/command/edit.go @@ -50,12 +50,17 @@ func (cmd *EditCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interfa return subcommands.ExitFailure } - cfgPath, err := config.ConfigurationPath(cmd.profile) + path, err := config.ConfigurationPath(cmd.profile) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure } - shell := exec.Command(editor, cfgPath) + if _, err := os.Stat(path); err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } + + shell := exec.Command(editor, path) shell.Stdin = os.Stdin shell.Stdout = os.Stdout shell.Stderr = os.Stderr diff --git a/command/init.go b/command/init.go index bdef623..abe52dd 100644 --- a/command/init.go +++ b/command/init.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "os" "os/user" - "path/filepath" "text/template" "github.com/google/subcommands" @@ -44,7 +43,13 @@ func (cmd *InitCommand) SetFlags(f *flag.FlagSet) { // Execute executes create configuration and returns an ExitStatus. func (cmd *InitCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - cfgDirPath, err := path_helper.Fullpath(config.ConfigDirPath) + path, err := config.ConfigurationDirectoryPath() + if err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } + + cfgDirPath, err := path_helper.Fullpath(path) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure @@ -53,7 +58,7 @@ func (cmd *InitCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interfa logger.Warn(err.Error()) } - cfgPath := filepath.Join(cfgDirPath, cmd.profile+".toml") + cfgPath, err := config.ConfigurationPath(cmd.profile) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure diff --git a/command/kill.go b/command/kill.go index 8b11efd..116e4d9 100644 --- a/command/kill.go +++ b/command/kill.go @@ -3,6 +3,7 @@ package command import ( "context" "flag" + "os" "github.com/google/subcommands" @@ -39,13 +40,17 @@ func (cmd *KillCommand) SetFlags(f *flag.FlagSet) { // Execute executes kill tmux session and returns an ExitStatus. func (cmd *KillCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - fpath, err := config.ConfigurationPath(cmd.profile) + path, err := config.ConfigurationPath(cmd.profile) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure } + if _, err := os.Stat(path); err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } - c, err := config.LoadFile(fpath) + c, err := config.LoadFile(path) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure diff --git a/command/list.go b/command/list.go new file mode 100644 index 0000000..fb5f7c3 --- /dev/null +++ b/command/list.go @@ -0,0 +1,71 @@ +package command + +import ( + "context" + "flag" + "fmt" + "os" + "path/filepath" + "tmuxist/config" + "tmuxist/logger" + + "github.com/google/subcommands" +) + +// LIstCommand represents a version command. +type LIstCommand struct{} + +// Name returns the name of LIstCommand. +func (*LIstCommand) Name() string { + return "list" +} + +// Synopsis returns a short string describing LIstCommand. +func (*LIstCommand) Synopsis() string { + return "List tmuxist profiles" +} + +// Usage returns a long string explaining LIstCommand and givinig usage. +func (*LIstCommand) Usage() string { + return "list: show tmuxist profiles\n" +} + +// SetFlags adds the flags for LIstCommand to the specified set. +func (*LIstCommand) SetFlags(f *flag.FlagSet) { +} + +// Execute executes print version and returns an ExitStatus. +func (*LIstCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + path, err := config.ConfigurationDirectoryPath() + if err != nil { + logger.Err(err.Error()) + logger.Err("Please execute: `tmuxist init`") + return subcommands.ExitFailure + } + + err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + + _, err = filepath.Match("*.toml", path) + if err != nil { + return err + } + + c, err := config.LoadFile(path) + if err != nil { + return err + } + fmt.Println(c.Name) + return nil + }) + if err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } + return subcommands.ExitSuccess +} diff --git a/command/start.go b/command/start.go index 4ac8cef..b2d5065 100644 --- a/command/start.go +++ b/command/start.go @@ -3,6 +3,7 @@ package command import ( "context" "flag" + "os" "github.com/google/subcommands" @@ -39,13 +40,17 @@ func (cmd *StartCommand) SetFlags(f *flag.FlagSet) { // Execute executes startup tmux session and returns an ExitStatus. func (cmd *StartCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - fpath, err := config.ConfigurationPath(cmd.profile) + path, err := config.ConfigurationPath(cmd.profile) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure } + if _, err := os.Stat(path); err != nil { + logger.Err(err.Error()) + return subcommands.ExitFailure + } - c, err := config.LoadFile(fpath) + c, err := config.LoadFile(path) if err != nil { logger.Err(err.Error()) return subcommands.ExitFailure diff --git a/config/loader.go b/config/loader.go index eec5ac6..b3974bb 100644 --- a/config/loader.go +++ b/config/loader.go @@ -13,7 +13,7 @@ import ( const ( // ConfigDirPath is tmuxist configuration parent directory path. - ConfigDirPath = "~/.config/tmuxist" + configDirPath = "~/.config/tmuxist" ) // DefaultProfileName returns "default" or TMUXIST_PROFILE. @@ -54,13 +54,25 @@ func LoadFileByProfile(profile string) (*Config, error) { return c, nil } +// ConfigurationDirectoryPath returns ConfigDirPath. +func ConfigurationDirectoryPath() (string, error) { + p, err := path_helper.Fullpath(configDirPath) + if err != nil { + return "", err + } + + return p, nil +} + // ConfigurationPath returns configuration path by profile. func ConfigurationPath(profile string) (string, error) { - p, err := path_helper.Fullpath(filepath.Join(ConfigDirPath, profile+".toml")) + path, err := ConfigurationDirectoryPath() if err != nil { return "", err } - if _, err := os.Stat(p); err != nil { + + p, err := path_helper.Fullpath(filepath.Join(path, profile+".toml")) + if err != nil { return "", err } diff --git a/main.go b/main.go index 78c5d5c..94d7619 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( func main() { logger.Setup(os.Stderr) + subcommands.Register(&command.LIstCommand{}, "") subcommands.Register(&command.InitCommand{}, "") subcommands.Register(&command.EditCommand{}, "") subcommands.Register(&command.PrintCommand{}, "")