|
@@ -1,3 +1,5 @@
|
|
1
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
2
|
+
|
1
|
3
|
import java.io.BufferedReader;
|
2
|
4
|
import java.io.IOException;
|
3
|
5
|
import java.io.InputStream;
|
|
@@ -10,19 +12,28 @@ public class SimpleShell {
|
10
|
12
|
|
11
|
13
|
public static void prettyPrint(String output) {
|
12
|
14
|
// yep, make an effort to format things nicely, eh?
|
13
|
|
- System.out.println(output);
|
|
15
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
16
|
+ try {
|
|
17
|
+ Object json = mapper.readValue(output, Object.class);
|
|
18
|
+ String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
|
|
19
|
+ System.out.println(indented);
|
|
20
|
+ } catch (IOException e) {
|
|
21
|
+ e.printStackTrace();
|
|
22
|
+ }
|
14
|
23
|
}
|
|
24
|
+
|
15
|
25
|
public static void main(String[] args) throws java.io.IOException {
|
16
|
26
|
|
17
|
27
|
YouAreEll webber = new YouAreEll();
|
18
|
28
|
String commandLine;
|
19
|
|
- BufferedReader console = new BufferedReader
|
20
|
|
- (new InputStreamReader(System.in));
|
21
|
|
-
|
|
29
|
+ BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
22
|
30
|
ProcessBuilder pb = new ProcessBuilder();
|
23
|
31
|
List<String> history = new ArrayList<String>();
|
|
32
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
33
|
+
|
24
|
34
|
int index = 0;
|
25
|
35
|
//we break out with <ctrl c>
|
|
36
|
+
|
26
|
37
|
while (true) {
|
27
|
38
|
//read what the user enters
|
28
|
39
|
System.out.println("cmd? ");
|
|
@@ -33,8 +44,10 @@ public class SimpleShell {
|
33
|
44
|
List<String> list = new ArrayList<String>();
|
34
|
45
|
|
35
|
46
|
//if the user entered a return, just loop again
|
36
|
|
- if (commandLine.equals(""))
|
|
47
|
+ if (commandLine.equals("")) {
|
37
|
48
|
continue;
|
|
49
|
+ }
|
|
50
|
+
|
38
|
51
|
if (commandLine.equals("exit")) {
|
39
|
52
|
System.out.println("bye!");
|
40
|
53
|
break;
|
|
@@ -57,7 +70,6 @@ public class SimpleShell {
|
57
|
70
|
}
|
58
|
71
|
|
59
|
72
|
// Specific Commands.
|
60
|
|
-
|
61
|
73
|
// ids
|
62
|
74
|
if (list.contains("ids")) {
|
63
|
75
|
String results = webber.get_ids();
|
|
@@ -66,12 +78,62 @@ public class SimpleShell {
|
66
|
78
|
}
|
67
|
79
|
|
68
|
80
|
// messages
|
69
|
|
- if (list.contains("messages")) {
|
|
81
|
+ if (list.contains("messages") && list.size() == 1) {
|
70
|
82
|
String results = webber.get_messages();
|
71
|
83
|
SimpleShell.prettyPrint(results);
|
72
|
84
|
continue;
|
73
|
85
|
}
|
74
|
86
|
// you need to add a bunch more.
|
|
87
|
+ if(list.contains("ids") && list.size() == 3) {
|
|
88
|
+ String name = list.get(1);
|
|
89
|
+ String github = list.get(2);
|
|
90
|
+ Id id = new Id(name, github);
|
|
91
|
+ String idInfo = objectMapper.writeValueAsString(id);
|
|
92
|
+ webber.MakeURLCall("/ids", "POST", idInfo);
|
|
93
|
+ continue;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ if(list.contains("messages") && list.size() == 2) {
|
|
97
|
+ String results = webber.MakeURLCall("/ids/" + list.get(1) + "/messages", "GET", "");
|
|
98
|
+ SimpleShell.prettyPrint(results);
|
|
99
|
+ continue;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ if(list.contains("send") && list.size() > 2 && !list.get(list.size() - 2).equals("to") && !list.get(list.size() - 3).contains("\'")) {
|
|
103
|
+ StringBuilder message = new StringBuilder();
|
|
104
|
+ String github = list.get(1);
|
|
105
|
+ for(int i = 2; i < list.size(); i++) {
|
|
106
|
+ if(i == 2) {
|
|
107
|
+ message.append(list.get(i).substring(1) + " ");
|
|
108
|
+ } else if(i == list.size() - 1) {
|
|
109
|
+ message.append(list.get(i).substring(0, list.get(i).length() - 1));
|
|
110
|
+ } else {
|
|
111
|
+ message.append(list.get(i) + " ");
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+ Message messageToSend = new Message(list.get(1), message.toString(), "");
|
|
115
|
+ String messageToString = objectMapper.writeValueAsString(messageToSend);
|
|
116
|
+ webber.MakeURLCall("/ids/" + github + "/messages", "POST", messageToString);
|
|
117
|
+ continue;
|
|
118
|
+ }
|
|
119
|
+ if(list.contains("send") && list.get(list.size() - 2).equals("to") && list.get(list.size() - 3).contains("\'")) {
|
|
120
|
+ StringBuilder message = new StringBuilder();
|
|
121
|
+ String fromid = list.get(1);
|
|
122
|
+ String toid = list.get(list.size() - 1);
|
|
123
|
+ for (int i = 2; i < list.size() - 2; i++) {
|
|
124
|
+ if (i == 2) {
|
|
125
|
+ message.append(list.get(i).substring(1) + " ");
|
|
126
|
+ } else if (i == list.size() - 3) {
|
|
127
|
+ message.append(list.get(i).substring(0, list.get(i).length() - 1));
|
|
128
|
+ } else {
|
|
129
|
+ message.append(list.get(i) + " ");
|
|
130
|
+ }
|
|
131
|
+ }
|
|
132
|
+ Message messageToSend = new Message(toid, message.toString(), fromid);
|
|
133
|
+ String messageToString = objectMapper.writeValueAsString(messageToSend);
|
|
134
|
+ webber.MakeURLCall("/ids/" + toid + "/messages", "POST", messageToString);
|
|
135
|
+ continue;
|
|
136
|
+ }
|
75
|
137
|
|
76
|
138
|
//!! command returns the last command in history
|
77
|
139
|
if (list.get(list.size() - 1).equals("!!")) {
|
|
@@ -87,7 +149,12 @@ public class SimpleShell {
|
87
|
149
|
}
|
88
|
150
|
|
89
|
151
|
// wait, wait, what curiousness is this?
|
90
|
|
- Process process = pb.start();
|
|
152
|
+ Process process = null;
|
|
153
|
+ try {
|
|
154
|
+ process = pb.start();
|
|
155
|
+ } catch(IOException e) {
|
|
156
|
+ e.printStackTrace();
|
|
157
|
+ }
|
91
|
158
|
|
92
|
159
|
//obtain the input stream
|
93
|
160
|
InputStream is = process.getInputStream();
|