diff --git a/cmd/start/start.go b/cmd/start/start.go new file mode 100644 index 0000000..1d72179 --- /dev/null +++ b/cmd/start/start.go @@ -0,0 +1,79 @@ +package start + +import ( + "encoding/json" + "flag" + "fmt" + "io" + "os" + "runtime" + + pty "github.com/MCSManager/pty/console" + "github.com/mattn/go-colorable" +) + +var ( + dir, cmd, coder, ptySize string + cmds []string + colorAble bool +) + +type PtyInfo struct { + Pid int `json:"pid"` +} + +func init() { + if runtime.GOOS == "windows" { + flag.StringVar(&cmd, "cmd", "[\"cmd\"]", "command") + } else { + flag.StringVar(&cmd, "cmd", "[\"sh\"]", "command") + } + + flag.BoolVar(&colorAble, "color", false, "colorable (default false)") + flag.StringVar(&coder, "coder", "UTF-8", "Coder") + flag.StringVar(&dir, "dir", ".", "command work path") + flag.StringVar(&ptySize, "size", "80,50", "Initialize pty size, stdin will be forwarded directly") +} + +func Main() { + flag.Parse() + json.Unmarshal([]byte(cmd), &cmds) + + con := pty.New(coder, colorAble) + if err := con.ResizeWithString(ptySize); err != nil { + fmt.Printf("[MCSMANAGER-PTY] PTY ReSize Error: %v\n", err) + return + } + + err := con.Start(dir, cmds) + info, _ := json.Marshal(&PtyInfo{ + Pid: con.Pid(), + }) + fmt.Println(string(info)) + if err != nil { + fmt.Printf("[MCSMANAGER-PTY] Process Start Error: %v\n", err) + return + } + defer con.Close() + + HandleStdIO(con) + con.Wait() +} + +func HandleStdIO(c pty.Console) { + go io.Copy(c.StdIn(), os.Stdin) + if runtime.GOOS == "windows" && c.StdErr() != nil { + go io.Copy(os.Stderr, c.StdErr()) + } + handleStdOut(c) +} + +func handleStdOut(c pty.Console) { + var stdout io.Writer + if colorAble { + stdout = colorable.NewColorable(os.Stdout) + } else { + stdout = colorable.NewNonColorable(os.Stdout) + } + io.Copy(stdout, c.StdOut()) +} diff --git a/core/coder.go b/console/coder.go similarity index 100% rename from core/coder.go rename to console/coder.go diff --git a/core/common.go b/console/common.go similarity index 96% rename from core/common.go rename to console/common.go index 9f932f2..a9870b5 100644 --- a/core/common.go +++ b/console/common.go @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "github.com/MCSManager/pty/core/interfaces" + "github.com/MCSManager/pty/console/iface" ) var ( @@ -16,7 +16,7 @@ var ( ErrInvalidCmd = errors.New("invalid command") ) -type Console interfaces.Console +type Console iface.Console func New(coder string, colorAble bool) Console { return newNative(coder, colorAble, 50, 50) diff --git a/core/console.go b/console/console.go similarity index 95% rename from core/console.go rename to console/console.go index 5e553fa..dede9b0 100644 --- a/core/console.go +++ b/console/console.go @@ -10,12 +10,11 @@ import ( "path/filepath" "syscall" + "github.com/MCSManager/pty/console/iface" "github.com/creack/pty" - - "github.com/MCSManager/pty/core/interfaces" ) -var _ interfaces.Console = (*console)(nil) +var _ iface.Console = (*console)(nil) type console struct { file *os.File diff --git a/core/console_windows.go b/console/console_windows.go similarity index 96% rename from core/console_windows.go rename to console/console_windows.go index a5e10da..a7a3ca2 100644 --- a/core/console_windows.go +++ b/console/console_windows.go @@ -13,15 +13,15 @@ import ( "strings" "time" - "github.com/MCSManager/pty/core/go-winpty" - "github.com/MCSManager/pty/core/interfaces" + "github.com/MCSManager/pty/console/go-winpty" + "github.com/MCSManager/pty/console/iface" "github.com/juju/fslock" ) //go:embed winpty var winpty_zip []byte -var _ interfaces.Console = (*console)(nil) +var _ iface.Console = (*console)(nil) type console struct { file *winpty.WinPTY diff --git a/core/go-winpty/defines.go b/console/go-winpty/defines.go similarity index 100% rename from core/go-winpty/defines.go rename to console/go-winpty/defines.go diff --git a/core/go-winpty/util.go b/console/go-winpty/util.go similarity index 100% rename from core/go-winpty/util.go rename to console/go-winpty/util.go diff --git a/core/go-winpty/winpty.go b/console/go-winpty/winpty.go similarity index 100% rename from core/go-winpty/winpty.go rename to console/go-winpty/winpty.go diff --git a/core/go-winpty/winpty_amd64.go b/console/go-winpty/winpty_amd64.go similarity index 100% rename from core/go-winpty/winpty_amd64.go rename to console/go-winpty/winpty_amd64.go diff --git a/core/interfaces/iface.go b/console/iface/iface.go similarity index 96% rename from core/interfaces/iface.go rename to console/iface/iface.go index d4c57ab..4f4b308 100644 --- a/core/interfaces/iface.go +++ b/console/iface/iface.go @@ -1,4 +1,4 @@ -package interfaces +package iface import ( "io" diff --git a/core/winpty b/console/winpty similarity index 100% rename from core/winpty rename to console/winpty diff --git a/main.go b/main.go index f9c646e..c664f29 100644 --- a/main.go +++ b/main.go @@ -1,79 +1,7 @@ package main -import ( - "encoding/json" - "flag" - "fmt" - "io" - "os" - "runtime" - - pty "github.com/MCSManager/pty/core" - "github.com/mattn/go-colorable" -) - -var ( - dir, cmd, coder, ptySize string - cmds []string - colorAble bool -) - -type PtyInfo struct { - Pid int `json:"pid"` -} - -func init() { - if runtime.GOOS == "windows" { - flag.StringVar(&cmd, "cmd", "[\"cmd\"]", "command") - } else { - flag.StringVar(&cmd, "cmd", "[\"sh\"]", "command") - } - - flag.BoolVar(&colorAble, "color", false, "colorable (default false)") - flag.StringVar(&coder, "coder", "UTF-8", "Coder") - flag.StringVar(&dir, "dir", ".", "command work path") - flag.StringVar(&ptySize, "size", "80,50", "Initialize pty size, stdin will be forwarded directly") -} +import "github.com/MCSManager/pty/cmd/start" func main() { - flag.Parse() - json.Unmarshal([]byte(cmd), &cmds) - - con := pty.New(coder, colorAble) - if err := con.ResizeWithString(ptySize); err != nil { - fmt.Printf("[MCSMANAGER-PTY] PTY ReSize Error: %v\n", err) - return - } - - err := con.Start(dir, cmds) - info, _ := json.Marshal(&PtyInfo{ - Pid: con.Pid(), - }) - fmt.Println(string(info)) - if err != nil { - fmt.Printf("[MCSMANAGER-PTY] Process Start Error: %v\n", err) - return - } - defer con.Close() - - HandleStdIO(con) - con.Wait() -} - -func HandleStdIO(c pty.Console) { - go io.Copy(c.StdIn(), os.Stdin) - if runtime.GOOS == "windows" && c.StdErr() != nil { - go io.Copy(os.Stderr, c.StdErr()) - } - handleStdOut(c) -} - -func handleStdOut(c pty.Console) { - var stdout io.Writer - if colorAble { - stdout = colorable.NewColorable(os.Stdout) - } else { - stdout = colorable.NewNonColorable(os.Stdout) - } - io.Copy(stdout, c.StdOut()) + start.Main() }