Skip to content

Commit

Permalink
Feat: separate main
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Sep 19, 2022
1 parent ede63ef commit 490fcb8
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 83 deletions.
79 changes: 79 additions & 0 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
@@ -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())
}
File renamed without changes.
4 changes: 2 additions & 2 deletions core/common.go → console/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"strconv"
"strings"

"github.com/MCSManager/pty/core/interfaces"
"github.com/MCSManager/pty/console/iface"
)

var (
ErrProcessNotStarted = errors.New("process has not been started")
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)
Expand Down
5 changes: 2 additions & 3 deletions core/console.go → console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/console_windows.go → console/console_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion core/interfaces/iface.go → console/iface/iface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interfaces
package iface

import (
"io"
Expand Down
File renamed without changes.
76 changes: 2 additions & 74 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 490fcb8

Please sign in to comment.