Skip to content

Commit

Permalink
Merge pull request #9 from LucienZhang/exec_mode
Browse files Browse the repository at this point in the history
exec mode, close #5, close #6, close #7, close #8
  • Loading branch information
LucienZhang authored Dec 22, 2021
2 parents 463cc39 + 16b48ea commit 6f51762
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
45 changes: 33 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"syscall"

"github.com/LucienZhang/goto/configs"
"github.com/google/shlex"
"github.com/manifoldco/promptui"
"github.com/riywo/loginshell"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/viper"
Expand All @@ -24,10 +26,12 @@ const (
)

type commandEntity struct {
Name string
Desc string
Color string
Cmd string
Name string
Desc string
Color string
Cmd string
Shell string
ExecMode bool
}

func (c commandEntity) RGB(v interface{}) string {
Expand All @@ -44,13 +48,12 @@ func (c commandEntity) RGB(v interface{}) string {
}

type config struct {
Shell string
StartInSearchMode bool
Commands []commandEntity
}

var (
conf *config
conf = &config{}
templates = promptui.SelectTemplates{
Active: fmt.Sprintf("%s {{ .Name | underline | .RGB }}", promptui.IconSelect),
Inactive: " {{ .Name | .RGB }}",
Expand Down Expand Up @@ -85,8 +88,24 @@ Complete documentation is available at https://github.com/LucienZhang/goto`,
}
idx, _, err := prompt.Run()
cobra.CheckErr(err)
shellCmd := exec.Command(conf.Shell, "-c", conf.Commands[idx].Cmd)
return syscall.Exec(shellCmd.Path, argv(shellCmd), addCriticalEnv(dedupEnv(envv(shellCmd))))
currCmd := &conf.Commands[idx]
var cmdToExec *exec.Cmd
if currCmd.ExecMode {
currCmdArgs, err := shlex.Split(currCmd.Cmd)
cobra.CheckErr(err)
if len(currCmdArgs) < 1 {
return fmt.Errorf("command %s is empty", currCmd.Name)
}
cmdToExec = exec.Command(currCmdArgs[0], currCmdArgs[1:]...)
} else {
wrapperShell := currCmd.Shell
if wrapperShell == "" {
wrapperShell, err = loginshell.Shell()
cobra.CheckErr(err)
}
cmdToExec = exec.Command(wrapperShell, "-c", currCmd.Cmd)
}
return syscall.Exec(cmdToExec.Path, argv(cmdToExec), addCriticalEnv(dedupEnv(envv(cmdToExec))))
},
DisableAutoGenTag: true,
}
Expand All @@ -107,21 +126,23 @@ func initConfig() {
cobra.CheckErr(err)
fullConfigFilePath := path.Join(home, configFilePath)
viper.SetConfigFile(fullConfigFilePath)
viper.SetDefault("StartInSearchMode", false)
viper.SetDefault("Commands", []commandEntity{{"Help", "Show help information", "255;255;51", `echo 'Please config your commands in file ~/.goto/.goto.yaml.
Complete documentation is available at https://github.com/LucienZhang/goto'`, "", false}})

if err := viper.ReadInConfig(); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(path.Dir(fullConfigFilePath), 0755)
cobra.CheckErr(err)
file, err := os.OpenFile(fullConfigFilePath, os.O_RDONLY|os.O_CREATE, 0644)
// file, err := os.OpenFile(fullConfigFilePath, os.O_RDONLY|os.O_CREATE, 0644)
// file.Close()
err = viper.SafeWriteConfigAs(fullConfigFilePath)
cobra.CheckErr(err)
file.Close()
} else {
cobra.CheckErr(err)
}
}

conf = &config{"bash", false, []commandEntity{{"Help", "Show help information", "255;255;51", `echo 'Please config your commands in file ~/.goto/.goto.yaml.
Complete documentation is available at https://github.com/LucienZhang/goto'`}}}
err = viper.Unmarshal(conf)
cobra.CheckErr(err)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/LucienZhang/goto
go 1.17

require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/manifoldco/promptui v0.8.0
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down Expand Up @@ -216,6 +218,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab h1:ZjX6I48eZSFetPb41dHudEyVr5v953N15TsNZXlkcWY=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab/go.mod h1:/PfPXh0EntGc3QAAyUaviy4S9tzy4Zp0e2ilq4voC6E=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
Expand Down

0 comments on commit 6f51762

Please sign in to comment.