Skip to content

Commit

Permalink
Implement list profiles command (#9)
Browse files Browse the repository at this point in the history
* Add comment on Makefile

The make command can specify multiple targets, such as `make t1 t2 ...`.
The place where I added the comment is a hack to make the target only the first one and put the rest in the ARGS variable.

* Tidyup configuration path func

* Implement list profiles command
  • Loading branch information
corrupt952 authored Nov 7, 2021
1 parent 11b716a commit f6bcac9
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 14 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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))
%:
@:
Expand Down
9 changes: 7 additions & 2 deletions command/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"flag"
"os"

"github.com/google/subcommands"

Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions command/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"os"
"os/user"
"path/filepath"
"text/template"

"github.com/google/subcommands"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions command/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"flag"
"os"

"github.com/google/subcommands"

Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions command/list.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 7 additions & 2 deletions command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"flag"
"os"

"github.com/google/subcommands"

Expand Down Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}, "")
Expand Down

0 comments on commit f6bcac9

Please sign in to comment.