-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters