|
@@ -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,63 @@ 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")) {
|
|
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
|
+
|
|
120
|
+ if(list.contains("send") && list.get(list.size() - 2).equals("to")) {
|
|
121
|
+ StringBuilder message = new StringBuilder();
|
|
122
|
+ String fromid = list.get(1);
|
|
123
|
+ String toid = list.get(list.size() - 1);
|
|
124
|
+ for (int i = 2; i < list.size() - 2; i++) {
|
|
125
|
+ if (i == 2) {
|
|
126
|
+ message.append(list.get(i).substring(1) + " ");
|
|
127
|
+ } else if (i == list.size() - 3) {
|
|
128
|
+ message.append(list.get(i).substring(0, list.get(i).length() - 1));
|
|
129
|
+ } else {
|
|
130
|
+ message.append(list.get(i) + " ");
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+ Message messageToSend = new Message(toid, message.toString(), fromid);
|
|
134
|
+ String messageToString = objectMapper.writeValueAsString(messageToSend);
|
|
135
|
+ webber.MakeURLCall("/ids/" + toid + "/messages", "POST", messageToString);
|
|
136
|
+ continue;
|
|
137
|
+ }
|
75
|
138
|
|
76
|
139
|
//!! command returns the last command in history
|
77
|
140
|
if (list.get(list.size() - 1).equals("!!")) {
|