pull/36/head
Evan Chen 2021-10-22 12:37:41 +08:00
parent 863b489412
commit a956221c4e
7 changed files with 132 additions and 20 deletions

View File

@ -23,7 +23,7 @@ run: build
.PHONY: web
web:
npm run build
# npm run js-dev
npm run js-dev
.PHONY: build
build: web

12
file.go
View File

@ -50,34 +50,32 @@ func (f *File) Write(data []byte) error {
}
func (f *File) Do() (string, error) {
log.Println("do ", f.Action)
if f.Action == "" {
return "", nil
}
timeout := time.After(2 * time.Second)
cmd := &exec.Cmd{}
if runtime.GOOS == "windows" {
cmd = exec.Command(WIN_SHELL, "/c", f.Action)
} else {
exec.Command(UNIX_SHELL, "-c", f.Action)
cmd = exec.Command(UNIX_SHELL, "-c", f.Action)
}
log.Println("DO: ", f.Action)
done := make(chan string, 1)
log.Println("start ", f.Action)
go func() {
out, _ := cmd.CombinedOutput()
// real cmd err is only pass down
// real cmd err is unhandled, but passed to client
// if err != nil {
// return string(out), err
// }
done <- string(out)
}()
select {
case <-timeout:
case <-time.After(5 * time.Second):
cmd.Process.Kill()
log.Println("timeout")
return "", errors.New("command timeout")
case out := <-done:
log.Println(out)
log.Println("\n", out)
return out, nil
}
}

104
public/js/back.js Normal file
View File

@ -0,0 +1,104 @@
window.ToolIsFollow = false;
async function FileGet(follower=false){
let f = ''
if (Active == 'ConfigUI') {
if (follower) f = '?f=true'
const res = await fetch('/api/conf'+f)
.catch(err=>{console.log(err);return;});
const body = await res.text();
if(!res.ok){
Catch(res)
return
}
editor.session.setValue(body);
}
else {
if (follower) f = '&f=true'
const res = await fetch('/api/file?name=' + Active + f)
.catch(err=>{console.log(err);return;});
const body = await res.json();
if(!res.ok){
Catch(res)
return
}
editor.session.setValue(body.data);
}
}
async function FileSave(){
if (Active == 'ConfigUI') {
const res = await fetch('/api/conf', {
method: 'POST',
body: editor.getValue(),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(err=>{console.log(err);return;});
if (res.ok) window.location.reload();
else Catch(res)
}
else {
const res = await fetch('/api/file', {
method: 'POST',
body: JSON.stringify({"name":Active,"data":editor.getValue()}),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(err=>{console.log(err);return;});
if(!res.ok) Catch(res)
}
}
async function FileApply(){
if (Active == 'ConfigUI') {
return;
}
else {
const res = await fetch('/api/apply?name='+ Active, {
method: 'POST',
}).catch(err=>{console.log(err);return;});
if(!res.ok){
const result = await Catch(res)
result_editor.session.setValue(result)
return
}
const result = await res.text()
result_editor.session.setValue(result)
}
}
async function Catch(res){
console.trace()
console.log(res)
const msg = await res.text()
ShowError(msg)
return msg
}
// starting point
(function(){
// block ctl-s
window.addEventListener("keypress", function(event) {
if (!(event.code == 115 && event.ctrlKey) && !(event.code == 19)) return true
alert("Ctrl-S pressed")
event.preventDefault()
return false
})
// setup ace editor
setEditor()
// setup result code block
setResult()
// for follow mode
setInterval((async ()=>{
if (ToolIsFollow){
await FileGet(true)
editor.gotoLine(editor.session.getLength());
}
}), 1000)
}())

View File

@ -2,11 +2,12 @@
window.ToolIsFollow = false;
async function FileGet(follower=false){
FileGet= async function(follower=false){
let f = ''
if (Active == 'ConfigUI') {
if (follower) f = '?f=true'
const res = await fetch('/api/conf'+f);
const res = await fetch('/api/conf'+f)
.catch(err=>{console.log(err);return;});
const body = await res.text();
if(!res.ok){
Catch(res)
@ -16,7 +17,8 @@ async function FileGet(follower=false){
}
else {
if (follower) f = '&f=true'
const res = await fetch('/api/file?name=' + Active + f);
const res = await fetch('/api/file?name=' + Active + f)
.catch(err=>{console.log(err);return;});
const body = await res.json();
if(!res.ok){
Catch(res)
@ -26,7 +28,7 @@ async function FileGet(follower=false){
}
}
async function FileSave(){
FileSave = async function(){
if (Active == 'ConfigUI') {
const res = await fetch('/api/conf', {
method: 'POST',
@ -34,7 +36,7 @@ async function FileSave(){
headers: new Headers({
'Content-Type': 'application/json'
})
})
}).catch(err=>{console.log(err);return;});
if (res.ok) window.location.reload();
else Catch(res)
}
@ -45,19 +47,19 @@ async function FileSave(){
headers: new Headers({
'Content-Type': 'application/json'
})
})
}).catch(err=>{console.log(err);return;});
if(!res.ok) Catch(res)
}
}
async function FileApply(){
FileApply = async function(){
if (Active == 'ConfigUI') {
return;
}
else {
const res = await fetch('/api/apply?name='+ Active, {
method: 'POST',
})
}).catch(err=>{console.log(err);return;});
if(!res.ok){
const result = await Catch(res)
result_editor.session.setValue(result)
@ -68,7 +70,7 @@ async function FileApply(){
}
}
async function Catch(res){
Catch=async function(res){
console.trace()
console.log(res)
const msg = await res.text()
@ -91,4 +93,12 @@ async function Catch(res){
editor.gotoLine(editor.session.getLength());
}
}), 1000)
// block ctl-s
window.addEventListener("keypress", function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true
alert("Ctrl-S pressed")
event.preventDefault()
return false
})
}())

View File

@ -22,7 +22,7 @@ $modal-content-width: 90vw;
}
.icn-spinner {
animation: spin-animation 0.5s infinite;
animation: spin-animation 1s infinite;
display: inline-block;
}

View File

@ -1,7 +1,8 @@
{{define "base/footer"}}
<script src="/public/ace/js/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/public/ace/js/ext-beautify.js" type="text/javascript" charset="utf-8"></script>
<script src="/public/js/main.js"></script>
<!-- <script src="/public/js/main.js"></script> -->
<script src="/public/js/back.js"></script>
</body>
</html>
{{end}}

View File

@ -29,6 +29,5 @@ var Active = "{{.File.Name}}";
</div>
</div>
{{template "components/result" .}}
{{template "base/footer" .}}
{{end}}