Skip to content

Commit

Permalink
Implemented a simple cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignas committed Dec 23, 2023
1 parent 3058557 commit 5a879e0
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
31 changes: 31 additions & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id 'application'
id 'java'
id 'com.github.spotbugs'
id 'idea'
}

group = 'lt.pow.nukagit'
version = '0.1.0-SNAPSHOT'
mainClassName = 'lt.pow.nukagit.cli.Main'

repositories {
mavenCentral()
}

dependencies {
implementation project(":proto")
implementation 'com.google.protobuf:protobuf-java:_'
implementation 'io.grpc:grpc-stub:_'
implementation 'io.grpc:grpc-protobuf:_'
implementation 'javax.annotation:javax.annotation-api:_'
implementation 'io.grpc:grpc-netty:_'

// Command line parsing
implementation 'info.picocli:picocli:_'
}

java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
56 changes: 56 additions & 0 deletions cli/src/main/java/lt/pow/nukagit/cli/AddUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lt.pow.nukagit.cli;

import lt.pow.nukagit.proto.Users;
import picocli.CommandLine;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

@CommandLine.Command(name = "add_user", description = "Add user")
public class AddUser implements Runnable {

@CommandLine.ParentCommand
private Main parent;

@CommandLine.Parameters(index = "0", description = "Username to add.")
private String username;

@CommandLine.Option(names = {"-k", "--key"}, description = "Specify a key (exclusive with --key-file)")
private String keyString;

@CommandLine.Option(names = {"-F", "--key-file"}, description = "Specify a key file (exclusive with --key)")
private File keyFile;

@Override
public void run() {
if (keyString != null && keyFile != null) {
System.err.println("Cannot specify both --key and --key-file");
System.exit(1);
} else if (keyString == null && keyFile == null) {
System.err.println("Must specify either --key or --key-file");
System.exit(1);
}

if (keyFile != null) {
// read key from file
byte[] keyBytes;
try {
keyBytes = Files.readAllBytes(keyFile.toPath());
} catch (IOException e) {
System.err.println("Failed to read key file: " + e.getMessage());
System.exit(1);
return;
}
keyString = new String(keyBytes, StandardCharsets.UTF_8);
}

System.out.println("Adding User " + username + " with key " + keyString);
parent.usersGrpcClient().createUser(Users.CreateUserRequest.newBuilder()
.setUsername(username)
.setPublicKey(keyString)
.build());
System.out.println("User added");
}
}
65 changes: 65 additions & 0 deletions cli/src/main/java/lt/pow/nukagit/cli/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package lt.pow.nukagit.cli;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lt.pow.nukagit.proto.RepositoriesServiceGrpc;
import lt.pow.nukagit.proto.UsersServiceGrpc;
import picocli.CommandLine;

@CommandLine.Command(name = "nuka_cli", description = "NukaGit CLI client",
subcommands = {
AddUser.class
})
public class Main implements Runnable {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this help message")
private boolean helpRequested;

@CommandLine.Option(names = {"-H", "--host"}, description = "host to connect to", defaultValue = "localhost")
private String host;

@CommandLine.Option(names = {"-p", "--port"}, description = "port to connect to", defaultValue = "50051")
private Integer port;

private ManagedChannel channel;
RepositoriesServiceGrpc.RepositoriesServiceBlockingStub repositoriesGrpcClient;
UsersServiceGrpc.UsersServiceBlockingStub usersGrpcClient;

ManagedChannel getChannel() {
if (channel == null) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext()
.build();
}
return channel;
}

public RepositoriesServiceGrpc.RepositoriesServiceBlockingStub repositoriesGrpcClient() {
if (repositoriesGrpcClient == null) {
repositoriesGrpcClient = RepositoriesServiceGrpc.newBlockingStub(getChannel());
}
return repositoriesGrpcClient;
}

public UsersServiceGrpc.UsersServiceBlockingStub usersGrpcClient() {
if (usersGrpcClient == null) {
usersGrpcClient = UsersServiceGrpc.newBlockingStub(getChannel());
}
return usersGrpcClient;
}

@Override
public void run() {
CommandLine cmd = new CommandLine(this);
// This method is called if no subcommands are specified
if (helpRequested || cmd.getUnmatchedArguments().isEmpty()) {
cmd.usage(System.out);
} else {
System.out.println("No valid subcommands provided. Use -h or --help for usage information.");
}
}

public static void main(String[] args) {
CommandLine commandLine = new CommandLine(new Main());
commandLine.execute(args);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

rootProject.name = 'nukagit'
include 'proto'
include 'proto', 'cli'

refreshVersions {
rejectVersionIf {
Expand Down

0 comments on commit 5a879e0

Please sign in to comment.