add an option for command line

This commit is contained in:
Zhi Wang 2022-01-06 20:21:11 -05:00
parent 906991c297
commit 97791896e1

15
main.go
View File

@ -155,6 +155,8 @@ func fromPtyStdout(ws *websocket.Conn, ptmx *os.File, done chan struct{}) {
time.Sleep(closeGracePeriod) time.Sleep(closeGracePeriod)
} }
var cmdToExec string = "bash"
// handle websockets // handle websockets
func wsHandler(w http.ResponseWriter, r *http.Request) { func wsHandler(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
@ -168,7 +170,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
log.Println("\n\nCreated the websocket") log.Println("\n\nCreated the websocket")
ptmx, cmd, err := createPty("bash") ptmx, cmd, err := createPty(cmdToExec)
if err != nil { if err != nil {
log.Println("Failed to create PTY: ", err) log.Println("Failed to create PTY: ", err)
@ -235,6 +237,17 @@ func main() {
gin.DefaultWriter = fp gin.DefaultWriter = fp
} }
// parse the arguments. User can pass the command to execute
// by default, we use bash, but macos users might want to use zsh
// you can also run single program, such as pstree, htop...
// but program might misbehave (htop seems to be fine)
args := os.Args
if len(args) > 1 {
cmdToExec = strings.Join(args[1:], " ")
log.Println(cmdToExec)
}
rt := gin.Default() rt := gin.Default()
rt.SetTrustedProxies(nil) rt.SetTrustedProxies(nil)