Skip to content

Commit

Permalink
allow !cmd style external command invocation in shell
Browse files Browse the repository at this point in the history
  • Loading branch information
sagan committed Jan 11, 2024
1 parent e89bab9 commit ce5a0e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shell

import (
"fmt"
"strings"

"github.com/c-bata/go-prompt"
"github.com/google/shlex"
Expand Down Expand Up @@ -68,6 +69,10 @@ var ptoolPrompt = &cobraprompt.CobraPrompt{
if err != nil {
return nil
}
// allow "!cmd" style (no space between) external cmd invocation
if len(words) > 0 && strings.HasPrefix(words[0], "!") && len(words[0]) > 1 {
words = append([]string{"!", words[0][1:]}, words[1:]...)
}
return words
},
}
Expand All @@ -79,12 +84,9 @@ func shell(command *cobra.Command, args []string) error {
if config.Fork || config.LockFile != "" {
return fmt.Errorf("--fork or --lock flag can NOT be used with shell")
}
// once in shell, one can only quit shell by exit the program.
config.InShell = true
cmd.RootCmd.SilenceErrors = false
defer func() {
config.InShell = false
cmd.RootCmd.SilenceErrors = true
}()
ptoolPrompt.GoPromptOptions = append(ptoolPrompt.GoPromptOptions,
prompt.OptionMaxSuggestion(uint16(config.Get().ShellMaxSuggestions)))
history, err := cmd.ShellHistory.Load()
Expand Down
6 changes: 6 additions & 0 deletions ptool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"os"
"runtime"
_ "time/tzdata"

_ "github.com/sagan/ptool/client/all"
Expand All @@ -10,5 +12,9 @@ import (
)

func main() {
if runtime.GOOS == "windows" {
// https://github.com/golang/go/issues/43947
os.Setenv("NoDefaultCurrentDirectoryInExePath", "1")
}
cmd.Execute()
}

0 comments on commit ce5a0e9

Please sign in to comment.