Newer
Older
package it.inaf.ia2.gms.cli;
import it.inaf.ia2.gms.client.GmsClient;
import it.inaf.ia2.gms.client.GmsClientBuilder;
Sonia Zorba
committed
import it.inaf.ia2.gms.client.model.Permission;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.List;
Sonia Zorba
committed
public class CLI {
Sonia Zorba
committed
public static void main(String[] args) throws Exception {
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
new CLI(args).run();
}
private final String args[];
private int argIndex;
private String gmsBaseUrl;
private String rapBaseUrl;
private String clientId;
private String clientSecret;
private String token;
private GmsClient client;
private CLI(String... args) {
this.args = args;
}
private void run() throws Exception {
if (args.length < 2) {
displayUsage();
}
boolean commandParsed = false;
while (argIndex < args.length && !commandParsed) {
switch (args[argIndex]) {
case "--config-file":
loadConfigFromFile(new File(getNextArg()));
break;
case "--token-file":
loadTokenFromFile(new File(getNextArg()));
break;
case "--gms-url":
gmsBaseUrl = getNextArg();
break;
case "--rap-url":
rapBaseUrl = getNextArg();
break;
case "--client-id":
clientId = getNextArg();
break;
case "--client-secret":
clientSecret = getNextArg();
break;
default:
verifyConfigLoaded();
createClient();
parseCommand();
commandParsed = true;
break;
}
argIndex++;
}
}
private String getNextArg() {
if (argIndex + 1 == args.length) {
System.err.println("Missing value for option " + args[argIndex]);
System.exit(1);
}
return args[++argIndex];
Sonia Zorba
committed
}
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
private void verifyConfigLoaded() {
if (gmsBaseUrl == null) {
// Attempt reading gms.properties in current directory
loadConfigFromFile(new File("gms.properties"));
}
if (clientId == null && token == null) {
// Attempt loading token.txt in current directory
loadTokenFromFile(new File("token.txt"));
}
if (token != null && (clientSecret == null || rapBaseUrl == null)) {
System.err.println("Client secret and RAP base URL not configured");
System.exit(1);
}
}
private void createClient() {
GmsClientBuilder clientBuilder = new GmsClientBuilder()
.setGmsBaseUrl(gmsBaseUrl);
if (token != null) {
client = clientBuilder.build();
client.setAccessToken(token);
} else {
client = clientBuilder.setClientId(clientId)
.setClientSecret(clientSecret)
.setRapBaseUrl(rapBaseUrl)
.build();
}
}
private void loadConfigFromFile(File config) {
System.err.println("Config file " + config.getAbsolutePath() + " doesn't exist");
System.exit(1);
}
Properties properties = new Properties();
try (InputStream in = new FileInputStream(config)) {
properties.load(in);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
gmsBaseUrl = properties.getProperty("gms_url");
if (gmsBaseUrl == null) {
System.err.println("Missing gms_url in gms.properties");
rapBaseUrl = properties.getProperty("rap_url");
clientId = properties.getProperty("client_id");
clientSecret = properties.getProperty("client_secret");
}
private void loadTokenFromFile(File tokenFile) {
if (!tokenFile.exists()) {
System.err.println("Token file " + tokenFile.getAbsolutePath() + " doesn't exist");
try (InputStream in = new FileInputStream(tokenFile)) {
java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");
token = s.next().trim();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
private void parseCommand() {
switch (args[argIndex]) {
Sonia Zorba
committed
boolean leaf = false;
if (argIndex + 2 < args.length) {
leaf = Boolean.parseBoolean(args[argIndex + 2]);
Sonia Zorba
committed
}
client.createGroup(args[argIndex + 1], leaf);
System.out.println("Group created");
break;
case "delete-group":
client.deleteGroup(args[argIndex + 1]);
System.out.println("Group deleted");
break;
case "add-member":
if (argIndex + 2 >= args.length) {
client.addMember(args[argIndex + 1], args[argIndex + 2]);
System.out.println("Member added");
break;
case "remove-member":
if (argIndex + 2 >= args.length) {
client.removeMember(args[argIndex + 1], args[argIndex + 2]);
System.out.println("Member removed");
break;
case "add-permission":
if (argIndex + 3 >= args.length) {
client.addPermission(args[argIndex + 1], args[argIndex + 2], Permission.valueOf(args[argIndex + 3]));
System.out.println("Permission added");
break;
case "delete-permission":
if (argIndex + 2 >= args.length) {
client.removePermission(args[argIndex + 1], args[argIndex + 2]);
System.out.println("Permission removed");
case "get-member-email-addresses":
Permission permission = null;
if (argIndex + 2 < args.length) {
permission = Permission.valueOf(args[argIndex + 2]);
}
List<String> addresses = client.getMemberEmailAddresses(args[argIndex + 1], permission);
for (String address : addresses) {
System.out.println(address);
}
break;
default:
displayUsage();
break;
}
}
private void displayUsage() {
System.out.println("gms-client\n"
+ " [--config-file <file>]\n"
+ " [--token-file <file>]\n"
+ " [--gms-url <url>]\n"
+ " [--rap-url <url>]\n"
+ " [--client-id <id>]\n"
+ " [--client-secret <secret>]\n"
+ " create-group <name1.name2.name3> [<leaf>]\n"
Sonia Zorba
committed
+ " delete-group <name1.name2.name3>\n"
+ " add-member <name1.name2.name3> <user_id>\n"
+ " remove-member <name1.name2.name3> <user_id>\n"
+ " add-permission <name1.name2.name3> <user_id> <permission>\n"
+ " delete-permission <name1.name2.name3> <user_id>\n"
+ " get-member-email-addresses <name1.name2.name3> [<permission>]");