Skip to content

Commit 0f2b7ae

Browse files
committed
Add terminal support for Windows
1 parent 8552eae commit 0f2b7ae

File tree

6 files changed

+120
-16
lines changed

6 files changed

+120
-16
lines changed

api/assets/bindata.go

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

api/handlers/rest_ApiTerminal_realtime.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ func ApiTerminal_realtime(w http.ResponseWriter, r *http.Request, session *sessi
3131
cm2 = "cmd"
3232
}
3333

34-
ctx := exec.InteractiveExec(cm1, cm2)
34+
var ctx exec.ProcessContext
35+
36+
if globals.Windows {
37+
ctx = exec.InteractiveExec("cmd", "/k", "echo 'HELLO globals.Windows user.'")
38+
} else {
39+
ctx = exec.InteractiveExec(cm1, cm2)
40+
}
41+
3542
reader := methods.Reader{Conn: c}
3643
go ctx.Receive(&reader, 5*time.Hour)
3744

@@ -55,7 +62,11 @@ func ApiTerminal_realtime(w http.ResponseWriter, r *http.Request, session *sessi
5562
fmt.Println("Restarting")
5663
ctx.Cancel()
5764
ctx.Stop()
58-
ctx = exec.InteractiveExec(cm1, cm2)
65+
if globals.Windows {
66+
ctx = exec.InteractiveExec("cmd", "/k", "echo 'HELLO globals.Windows user.'")
67+
} else {
68+
ctx = exec.InteractiveExec(cm1, cm2)
69+
}
5970
reader = methods.Reader{Conn: c}
6071
go ctx.Receive(&reader, 5*time.Hour)
6172
} else {

api/handlers/rest_POSTApiAct.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ func POSTApiAct(w http.ResponseWriter, r *http.Request, session *sessions.Sessio
3434
if _, err := os.Stat(dir); os.IsNotExist(err) {
3535
if strings.Contains(useGos, "Scratch") {
3636
app.Type = "app"
37-
err = os.MkdirAll(filepath.Join(os.ExpandEnv("$GOPATH"), "src", app.Name), 0700)
37+
38+
fullDir := filepath.Join(os.ExpandEnv("$GOPATH"), "src", app.Name)
39+
err = os.MkdirAll(fullDir, 0700)
3840

3941
if err != nil {
4042
response = templates.Alert(types.Alertbs{Type: "danger", Text: "Error creating package " + r.FormValue("name") + ":" + err.Error(), Redirect: "javascript:console.log('error!')"})
43+
} else {
44+
45+
os.Chdir(fullDir)
46+
core.RunCmdSmart("go mod init")
4147
}
4248

4349
} else if strings.Contains(useGos, "faas") {

api/handlers/rest_POSTApiNew.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func POSTApiNew(w http.ResponseWriter, r *http.Request, session *sessions.Sessio
2020
inputs := []types.Inputs{}
2121
inputs = append(inputs, types.Inputs{Type: "text", Name: "name", Misc: "required", Text: "Package Name"})
2222
inputs = append(inputs, types.Inputs{Type: "hidden", Name: "type", Value: "0"})
23-
inputs = append(inputs, types.Inputs{Type: "select", Misc: "Project type", Name: "usegos", Value: "Scratch", Options: []string{"Scratch", "Existing package", "faas"}})
23+
inputs = append(inputs, types.Inputs{Type: "select", Misc: "Project type", Name: "usegos", Value: "Scratch", Options: []string{"Scratch", "Existing package"}})
2424

2525
response = templates.Modal(types.SModal{Body: "", Title: "Add Package", Color: "#ededed", Form: types.Forms{Link: "/api/act", CTA: "Add Package", Class: "warning btn-block", Buttons: []types.SButton{}, Inputs: inputs}})
2626

gos.gxml

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
}
9595

9696
if _, err := os.Stat(os.ExpandEnv("$GOPATH") + "/bin/gocode" + trailerEx); os.IsNotExist(err) {
97-
fmt.Println("Go code completion not present, installing from github.com/nsf/gocode")
97+
fmt.Println("Go code completion not present, installing from github.com/mdempsky/gocode")
9898
if Windows {
9999
//
100100
core.RunCmdSmart("go get -u -ldflags -H=windowsgui github.com/mdempsky/gocode")
@@ -1574,7 +1574,7 @@ for _, v := range apps {
15741574
inputs := []Inputs{}
15751575
inputs = append(inputs, Inputs{Type:"text",Name:"name",Misc: "required",Text:"Package Name"})
15761576
inputs = append(inputs, Inputs{Type:"hidden", Name: "type" ,Value: "0"})
1577-
inputs = append(inputs, Inputs{Type:"select", Misc : "Project type" , Name: "usegos" ,Value: "Scratch", Options : []string{"Scratch", "Existing package", "faas"} })
1577+
inputs = append(inputs, Inputs{Type:"select", Misc : "Project type" , Name: "usegos" ,Value: "Scratch", Options : []string{"Scratch", "Existing package"} })
15781578

15791579
response = net_bModal(sModal{Body: "",Title: "Add Package",Color: "#ededed",Form:Forms{Link:"/api/act",CTA:"Add Package",Class:"warning btn-block",Buttons:[]SButton{}, Inputs:inputs }})
15801580

@@ -1604,10 +1604,17 @@ for _, v := range apps {
16041604
if _, err := os.Stat(dir); os.IsNotExist(err) {
16051605
if strings.Contains(useGos, "Scratch") {
16061606
app.Type = "app"
1607-
err = os.MkdirAll(filepath.Join(os.ExpandEnv("$GOPATH"), "src", app.Name ), 0700 )
1607+
1608+
fullDir := filepath.Join(os.ExpandEnv("$GOPATH"), "src", app.Name )
1609+
err = os.MkdirAll(fullDir, 0700 )
1610+
16081611

16091612
if err != nil {
16101613
response = net_bAlert(Alertbs{Type:"danger",Text:"Error creating package " + r.FormValue("name") + ":" + err.Error(),Redirect: "javascript:console.log('error!')"})
1614+
} else {
1615+
1616+
os.Chdir(fullDir)
1617+
core.RunCmdSmart("go mod init")
16111618
}
16121619

16131620
} else if strings.Contains(useGos, "faas") {
@@ -2457,7 +2464,21 @@ for _, v := range apps {
24572464
}
24582465
defer c.Close()
24592466

2460-
ctx := exec.InteractiveExec("bash", "-i")
2467+
cm1,cm2 := "bash", "-i"
2468+
2469+
if Windows {
2470+
cm1 = "start"
2471+
cm2 = "cmd"
2472+
}
2473+
2474+
var ctx exec.ProcessContext
2475+
2476+
if Windows {
2477+
ctx = exec.InteractiveExec("cmd", "/k", "echo 'HELLO Windows user.'")
2478+
} else {
2479+
ctx = exec.InteractiveExec(cm1, cm2)
2480+
}
2481+
24612482
reader := methods.Reader{Conn : c}
24622483
go ctx.Receive(&reader, 5*time.Hour)
24632484

@@ -2482,7 +2503,11 @@ for _, v := range apps {
24822503
fmt.Println("Restarting")
24832504
ctx.Cancel()
24842505
ctx.Stop()
2485-
ctx = exec.InteractiveExec("bash", "-i")
2506+
if Windows {
2507+
ctx = exec.InteractiveExec("cmd", "/k", "echo 'HELLO Windows user.'")
2508+
} else {
2509+
ctx = exec.InteractiveExec(cm1, cm2)
2510+
}
24862511
reader = methods.Reader{Conn : c}
24872512
go ctx.Receive(&reader, 5*time.Hour)
24882513
} else {

tmpl/js.tmpl

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,62 @@
410410

411411
var working = false;
412412
var termx = null;
413+
let winDir;
414+
let comRan = 0
413415
let prevBuff = "";
414416
window.socketTerminal = new WebSocket("ws://" + window.location.host + "/api/terminal_realtime");
415417

418+
function reset(){
419+
420+
if(socketTerminal.readyState === 2 || socketTerminal.readyState === 3){
421+
422+
window.socketTerminal = new WebSocket("ws://" + window.location.host + "/api/terminal_realtime");
423+
working = false;
424+
prevBuff = "";
425+
comRan = 0
426+
winDir = null
427+
$('#terminaldefault').terminal().set_prompt('user@Strutkure $')
428+
socketTerminal.addEventListener('message', handleMsg);
429+
430+
}
431+
}
416432

417433
// Listen for messages
434+
socketTerminal.addEventListener('error', function (event) {
435+
436+
reset()
437+
438+
})
418439

419-
socketTerminal.addEventListener('message', function (event) {
440+
function handleMsg(event) {
420441
let term = $('#terminaldefault').terminal();
421442

443+
444+
445+
if(winDir == "await"){
446+
if(prevBuff == event.data) return
447+
let path = event.data.split("\n")
448+
winDir = path[path.length - 1]
449+
}
450+
451+
if(!comRan && event.data.includes("globals.Windows user.")){
452+
winDir = "await"
453+
let path = event.data.split("\n")
454+
455+
if(path.length > 2){
456+
winDir = path[path.length - 1]
457+
}
458+
}
459+
460+
if (winDir && event.data.includes(winDir.replace(">", ""))){
461+
working = false;
462+
prevBuff = "";
463+
term.set_prompt(winDir)
464+
return
465+
}
466+
467+
comRan++
468+
422469
if(event.data.includes("bash") && event.data.includes("$") && event.data.length < 50){
423470
working = false;
424471
prevBuff = "";
@@ -444,31 +491,46 @@
444491
var br = window.$breakpoints[ window.$arrayIndex ];
445492
if(!br){
446493
delete window.$awaitDelve
447-
socketTerminal.send("continue \n");
494+
window.socketTerminal.send("continue \n");
448495
}
449496
else {
450497

451-
socketTerminal.send(br + "\n");
498+
window.socketTerminal.send(br + "\n");
452499
window.$arrayIndex++;
453500
}
454501

455502

456503
}
457-
});
504+
}
505+
socketTerminal.addEventListener('message', handleMsg);
458506

459507
jQuery(function($, undefined) {
460508
$('#terminaldefault').terminal(function(command, term) {
461509
termx = term.echo;
462510

511+
if(winDir && command == "killnow") return reset()
512+
463513
if (command !== '') {
464514
working = true;
515+
let cmC = command.trim()
516+
517+
if(winDir){
518+
if(cmC[0] == "c" && cmC[1] == "d"){
519+
winDir = "await"
520+
prevBuff = command
521+
}
522+
}
465523

466524
setTimeout(()=> {
467-
socketTerminal.send(command + "\n");
525+
window.socketTerminal.send(command + "\n");
468526
}, 400)
469527

470528
}
471529

530+
if(winDir){
531+
term.set_prompt(( working ? "": winDir ) )
532+
return
533+
}
472534
term.set_prompt(( working ? "":'user@Strutkure $' ) )
473535

474536
}, {

0 commit comments

Comments
 (0)