/
protocol.go
154 lines (141 loc) · 4.01 KB
/
protocol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 Auburn University. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package protocol provides an implementation of the OpenRefactory protocol
// (server-side), which provides a standard mechanism for text editors to
// communicate with refactoring engines.
package protocol
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/godoctor/godoctor/filesystem"
)
type Reply struct {
Params map[string]interface{}
}
func (r Reply) String() string {
replyJson, _ := json.Marshal(r.Params)
return string(replyJson)
}
type State struct {
State int
About string
Mode string
Dir string
Filesystem filesystem.FileSystem
}
func Run(writer io.Writer, aboutText string, args []string) {
// single command console
if len(args) == 0 {
runSingle(writer, aboutText)
return
}
// list of commands
var argJson []map[string]interface{}
if len(args) == 1 && args[0] == "-" {
// read command list from stdin
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": err.Error()}})
return
}
err = json.Unmarshal(bytes, &argJson)
if err != nil {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": err.Error()}})
return
}
} else {
// assemble command list from args
err := json.Unmarshal([]byte(args[0]), &argJson)
if err != nil {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": err.Error()}})
return
}
}
runList(writer, aboutText, argJson)
}
func runSingle(writer io.Writer, aboutText string) {
cmdList := setup()
var state = State{State: 0, About: aboutText, Mode: "", Dir: "", Filesystem: nil}
var inputJson map[string]interface{}
ioreader := bufio.NewReader(os.Stdin)
for {
input, err := ioreader.ReadBytes('\n')
if err == io.EOF {
// exit
break
} else if err != nil {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": err.Error()}})
continue
}
err = json.Unmarshal(input, &inputJson)
if err != nil {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": err.Error()}})
continue
}
// check command key exists
cmd, found := inputJson["command"]
if !found {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": "Invalid JSON command"}})
continue
}
// if close command, just exit
if cmd == "close" {
break
}
// check command is one we support
if _, found := cmdList[cmd.(string)]; !found {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": "Invalid JSON command"}})
continue
}
// everything good to run command
result, _ := cmdList[cmd.(string)](&state, inputJson) // run the command
printReply(writer, result)
}
}
func runList(writer io.Writer, aboutText string, argJson []map[string]interface{}) {
cmdList := setup()
var state = State{State: 1, About: aboutText, Mode: "", Dir: "", Filesystem: nil}
for i, cmdObj := range argJson {
// has command?
cmd, found := cmdObj["command"]
if !found { // no command
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": "Invalid JSON command"}})
return
}
// valid command?
if _, found := cmdList[cmd.(string)]; found {
resultReply, err := cmdList[cmd.(string)](&state, cmdObj)
if err != nil {
printReply(writer, resultReply)
return
}
// last command?
if i == len(argJson)-1 {
printReply(writer, resultReply)
}
} else {
printReply(writer, Reply{map[string]interface{}{"reply": "Error", "message": "Invalid JSON command"}})
return
}
}
}
// little helpers
func setup() map[string]Command {
cmds := make(map[string]Command)
cmds["about"] = about
cmds["open"] = open
cmds["list"] = list
cmds["setdir"] = setdir
cmds["params"] = params
cmds["put"] = put
cmds["xrun"] = xRun
return cmds
}
func printReply(writer io.Writer, reply Reply) {
fmt.Fprintf(writer, "%s\n", reply)
}