From edff8721cfd3f791009653ea176a0b85cce116a9 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 18 Nov 2021 10:49:15 +0800 Subject: [PATCH] feat: add dir option --- cmd/gterm/main.go | 3 +++ gterm.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/cmd/gterm/main.go b/cmd/gterm/main.go index dfcdc8a..c7717df 100644 --- a/cmd/gterm/main.go +++ b/cmd/gterm/main.go @@ -31,6 +31,7 @@ var ( flagAppName string flagAddr string flagShell string + flagDir string flagLogLevel int flagDev bool flagVer bool @@ -42,6 +43,7 @@ func init() { flag.StringVar(&flagAppName, "name", "gterm", "the application title") flag.StringVar(&flagAddr, "addr", ":8000", "address to bind") flag.StringVar(&flagShell, "shell", "bash", "the shell behind") + flag.StringVar(&flagDir, "dir", ".", "the working dir") flag.Var(&flagArgs, "arg", "additional args to pass to cmd") flag.BoolVar(&flagDev, "dev", false, "is development mode") flag.IntVar(&flagLogLevel, "log-level", 9, "log level") @@ -73,6 +75,7 @@ func main() { g.AppName = flagAppName g.Cmd = flagShell g.Args = flagArgs + g.Dir = flagDir server := &http.Server{ Addr: flagAddr, diff --git a/gterm.go b/gterm.go index b8523d0..4aa80b1 100644 --- a/gterm.go +++ b/gterm.go @@ -42,6 +42,8 @@ type GTerm struct { Args []string // Command is the path to the binary we should create a TTY for Cmd string + // Dir working dir + Dir string // Envs env pairs to pass to the command Envs []string // ErrorLimit defines the number of consecutive errors that can happen @@ -145,6 +147,9 @@ func (g *GTerm) WS(w http.ResponseWriter, r *http.Request) { cmd := exec.Command(g.Cmd, g.Args...) cmd.Env = g.Envs + if g.Dir != "" && g.Dir != "." { + cmd.Dir = g.Dir + } tty, err := pty.Start(cmd) if err != nil { l.Error(err)