Skip to content

Commit 490fcb8

Browse files
committed
Feat: separate main
1 parent ede63ef commit 490fcb8

File tree

12 files changed

+89
-83
lines changed

12 files changed

+89
-83
lines changed

cmd/start/start.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package start
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"runtime"
10+
11+
pty "github.com/MCSManager/pty/console"
12+
"github.com/mattn/go-colorable"
13+
)
14+
15+
var (
16+
dir, cmd, coder, ptySize string
17+
cmds []string
18+
colorAble bool
19+
)
20+
21+
type PtyInfo struct {
22+
Pid int `json:"pid"`
23+
}
24+
25+
func init() {
26+
if runtime.GOOS == "windows" {
27+
flag.StringVar(&cmd, "cmd", "[\"cmd\"]", "command")
28+
} else {
29+
flag.StringVar(&cmd, "cmd", "[\"sh\"]", "command")
30+
}
31+
32+
flag.BoolVar(&colorAble, "color", false, "colorable (default false)")
33+
flag.StringVar(&coder, "coder", "UTF-8", "Coder")
34+
flag.StringVar(&dir, "dir", ".", "command work path")
35+
flag.StringVar(&ptySize, "size", "80,50", "Initialize pty size, stdin will be forwarded directly")
36+
}
37+
38+
func Main() {
39+
flag.Parse()
40+
json.Unmarshal([]byte(cmd), &cmds)
41+
42+
con := pty.New(coder, colorAble)
43+
if err := con.ResizeWithString(ptySize); err != nil {
44+
fmt.Printf("[MCSMANAGER-PTY] PTY ReSize Error: %v\n", err)
45+
return
46+
}
47+
48+
err := con.Start(dir, cmds)
49+
info, _ := json.Marshal(&PtyInfo{
50+
Pid: con.Pid(),
51+
})
52+
fmt.Println(string(info))
53+
if err != nil {
54+
fmt.Printf("[MCSMANAGER-PTY] Process Start Error: %v\n", err)
55+
return
56+
}
57+
defer con.Close()
58+
59+
HandleStdIO(con)
60+
con.Wait()
61+
}
62+
63+
func HandleStdIO(c pty.Console) {
64+
go io.Copy(c.StdIn(), os.Stdin)
65+
if runtime.GOOS == "windows" && c.StdErr() != nil {
66+
go io.Copy(os.Stderr, c.StdErr())
67+
}
68+
handleStdOut(c)
69+
}
70+
71+
func handleStdOut(c pty.Console) {
72+
var stdout io.Writer
73+
if colorAble {
74+
stdout = colorable.NewColorable(os.Stdout)
75+
} else {
76+
stdout = colorable.NewNonColorable(os.Stdout)
77+
}
78+
io.Copy(stdout, c.StdOut())
79+
}
File renamed without changes.

core/common.go renamed to console/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"strconv"
99
"strings"
1010

11-
"github.com/MCSManager/pty/core/interfaces"
11+
"github.com/MCSManager/pty/console/iface"
1212
)
1313

1414
var (
1515
ErrProcessNotStarted = errors.New("process has not been started")
1616
ErrInvalidCmd = errors.New("invalid command")
1717
)
1818

19-
type Console interfaces.Console
19+
type Console iface.Console
2020

2121
func New(coder string, colorAble bool) Console {
2222
return newNative(coder, colorAble, 50, 50)

core/console.go renamed to console/console.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"path/filepath"
1111
"syscall"
1212

13+
"github.com/MCSManager/pty/console/iface"
1314
"github.com/creack/pty"
14-
15-
"github.com/MCSManager/pty/core/interfaces"
1615
)
1716

18-
var _ interfaces.Console = (*console)(nil)
17+
var _ iface.Console = (*console)(nil)
1918

2019
type console struct {
2120
file *os.File
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/MCSManager/pty/core/go-winpty"
17-
"github.com/MCSManager/pty/core/interfaces"
16+
"github.com/MCSManager/pty/console/go-winpty"
17+
"github.com/MCSManager/pty/console/iface"
1818
"github.com/juju/fslock"
1919
)
2020

2121
//go:embed winpty
2222
var winpty_zip []byte
2323

24-
var _ interfaces.Console = (*console)(nil)
24+
var _ iface.Console = (*console)(nil)
2525

2626
type console struct {
2727
file *winpty.WinPTY
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package interfaces
1+
package iface
22

33
import (
44
"io"

0 commit comments

Comments
 (0)