Skip to content

Commit

Permalink
Add default config for gmux.
Browse files Browse the repository at this point in the history
Signed-off-by: corvofeng <corvofeng@gmail.com>
  • Loading branch information
corvofeng committed Apr 8, 2024
1 parent afed37f commit 72e8c73
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
60 changes: 47 additions & 13 deletions cmd/gmux/internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ func parseKeyValue(arg string) []string {
return nil
}

// inject the variables
func (c *rootCmd) ParseConfig(varMap map[string]string, configPath string) (lib.Config, error) {
content, err := os.ReadFile(configPath)
if err != nil {
c.Logger.Errorf("Error reading config file: %s %s", configPath, err)
return lib.Config{}, err
}
projContent := string(content)
projContent = lib.RenderERB(projContent, varMap)
config, err := lib.ParseConfig(projContent)
if err != nil {
c.Logger.Errorf("Error parsing config file: %s %s", configPath, err)
return lib.Config{}, err
}

return config, nil
}
func CreateDefaultConfig() lib.Config {
config := lib.Config{
Name: "gmux-default",
Tmux: "tmux -L gmux-default",
Root: "~/",
Windows: []lib.Window{
{
Name: "default",
Root: "~/",
Panes: []lib.Pane{
{
Commands: []string{"ls"},
},
},
},
},
}
return config
}

func (c *rootCmd) Run(cmd *cobra.Command, args []string) error {
varMap := make(map[string]string)
for _, set := range flagSets {
Expand All @@ -62,21 +99,18 @@ func (c *rootCmd) Run(cmd *cobra.Command, args []string) error {
}

configPath := lib.ParseConfigPath(flagDirectory, flagProject)
content, err := os.ReadFile(configPath)
if err != nil {
c.Logger.Errorf("Error reading config file: %s %s", configPath, err)
return err
}
projContent := string(content)
var config lib.Config

if len(varMap) > 0 {
projContent = lib.RenderERB(projContent, varMap)
}
config, err := lib.ParseConfig(projContent)
if err != nil {
c.Logger.Errorf("Error parsing config file: %s %s", configPath, err)
return err
if _, err := os.Stat(configPath); os.IsNotExist(err) {
c.Logger.Warn("Although the gmux works without a config file, it is recommended to create one.")
config = CreateDefaultConfig()
} else {
if config, err = c.ParseConfig(varMap, configPath); err != nil {
c.Logger.Errorf("Parse config error: %s", err)
return err
}
}

config.TmuxArgs = args
config.Debug = flagDebug

Expand Down
24 changes: 24 additions & 0 deletions lib/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"bytes"
"fmt"
"gmux/lib/asset"
"os"
"os/exec"
Expand Down Expand Up @@ -65,6 +66,25 @@ func shellescape(str string) string {
return str
}

/*
*
- ConfigCheck for tmux config
We have such script template:
{{$.Tmux}} split-window -c {{$window.Root}} -t {{$.Name}}:{{$winId}}
We can't predict the program behavior if $window.Root is empty.
*/
func ConfigCheck(config *Config) error {
if config.Root == "" {
return fmt.Errorf("tmux Root is required")
}
for _, window := range config.Windows {
if window.Root == "" {
return fmt.Errorf("window root for %s is required", window.Name)
}
}
return nil
}

func RunTmux(log log.FieldLogger, config *Config) {
funcMap := template.FuncMap{
"TmuxHasSession": TmuxHasSession,
Expand All @@ -74,6 +94,10 @@ func RunTmux(log log.FieldLogger, config *Config) {
return i + 1
},
}
if err := ConfigCheck(config); err != nil {
log.Error(err)
return
}

// 创建一个新模板并解析模板字符串
tmpl, err := template.New("bashScript").
Expand Down

0 comments on commit 72e8c73

Please sign in to comment.