Commit d314eb43 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Created GMS CLI client application

parent 7009ee4d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -57,3 +57,5 @@ nbactions.xml
/gms-ui/target/
/gms/nbactions-release-profile.xml

/gms-client/gms-client-lib/target/
/gms-client/gms-cli/target/
+31 −0
Original line number Diff line number Diff line
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
+3 −0
Original line number Diff line number Diff line
base_url=http://localhost:8081
client_id=test
client_secret=test
+48 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>it.inaf.ia2</groupId>
    <artifactId>gms-cli</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gms-cli</name>
    <description>GMS Command Line Client</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>gms-client-lib</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
+132 −0
Original line number Diff line number Diff line
package it.inaf.ia2.gms.cli;

import it.inaf.ia2.gms.client.GmsClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CLI implements CommandLineRunner {

    private final GmsClient client;

    public CLI() throws IOException {

        File config = new File("gms.properties");
        if (!config.exists()) {
            System.err.println("Unable to find the file gms.properties");
            System.exit(1);
        }

        Properties properties = new Properties();
        try (InputStream in = new FileInputStream(config)) {
            properties.load(in);
        }

        String baseUrl = (String) properties.get("base_url");
        if (baseUrl == null) {
            System.err.println("Missing base_url in gms.properties");
            System.exit(1);
        }

        String clientId = (String) properties.get("client_id");
        if (clientId == null) {
            System.err.println("Missing client_id in gms.properties");
            System.exit(1);
        }

        String clientSecret = (String) properties.get("client_secret");
        if (clientSecret == null) {
            System.err.println("Missing client_secret in gms.properties");
            System.exit(1);
        }

        client = new GmsClient(baseUrl, clientId, clientSecret);
    }

    @Override
    public void run(String... args) throws Exception {
        if (args.length < 2) {
            displayUsage();
        }

        switch (args[0]) {
            case "create-group":
                client.createGroup(getNames(args, 1));
                System.out.println("Group created");
                break;
            case "delete-group":
                client.deleteGroup(getNames(args, 1));
                System.out.println("Group deleted");
                break;
            case "add-member":
                if (args.length < 3) {
                    displayUsage();
                }
                client.addMember(getNames(args, args.length - 2), args[args.length - 1]);
                System.out.println("Member added");
                break;
            case "remove-member":
                if (args.length < 3) {
                    displayUsage();
                }
                client.removeMember(getNames(args, args.length - 2), args[args.length - 1]);
                System.out.println("Member removed");
                break;
            case "add-permission":
                if (args.length < 4) {
                    displayUsage();
                }
                client.addPermission(getNames(args, args.length - 3), args[args.length - 2], args[args.length - 1]);
                System.out.println("Permission added");
                break;
            case "delete-permission":
                if (args.length < 4) {
                    displayUsage();
                }
                client.deletePermission(getNames(args, args.length - 3), args[args.length - 2], args[args.length - 1]);
                System.out.println("Permission added");
                break;
            case "prepare-join":
                if (args.length != 3) {
                    displayUsage();
                }
                client.prepareToJoin(args[1], args[2]);
                System.out.println("Join prepared");
                break;
            default:
                displayUsage();
                break;
        }
    }

    private void displayUsage() {
        System.out.println("java -jar gms-client.jar\n"
                + "    create-group <name1 name2 name3>\n"
                + "    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> <permission>\n"
                + "    prepare-join <from_user_id> <to_user_id>");
        System.exit(0);
    }

    private List<String> getNames(String[] args, int startIndex) {
        return getNames(args, startIndex, args.length - 1);
    }

    private List<String> getNames(String[] args, int startIndex, int endIndex) {
        List<String> names = new ArrayList<>();
        for (int i = startIndex; i <= endIndex; i++) {
            names.add(args[i]);
        }
        return names;
    }
}
Loading