Skip to content

Commit

Permalink
Merge pull request #19 from perfectsense/feature/picocli
Browse files Browse the repository at this point in the history
Implement picocli replacing airlift
  • Loading branch information
JC authored Aug 25, 2020
2 parents 51eb26c + 538194f commit 90f85cf
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 56 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
CHANGELOG
=========

## 0.99.3 (Unreleased)
## 0.99.3 (25th August, 2020)

ENHANCEMENTS:

* [18](https://github.com/perfectsense/gyro-ssh-plugin/issues/18): Convert to `Picocli` for command line argument parsing.

## 0.99.2 (April 23rd, 2020)

Expand Down
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ repositories {
maven {
url 'https://artifactory.psdops.com/gyro-snapshots'
}

maven {
url 'https://artifactory.psdops.com/gyro-releases'
}
}

dependencies {
api 'gyro:gyro-core:0.99.2-SNAPSHOT'
api 'gyro:gyro-core:0.99.5'

implementation 'com.psddev:dari-util:3.3.607-xe0f27a'
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/gyro/plugin/ssh/AbstractInstanceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

package gyro.plugin.ssh;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import gyro.core.GyroCore;
import gyro.core.GyroException;
import gyro.core.GyroInstance;
Expand All @@ -27,27 +37,17 @@
import gyro.core.resource.Resource;
import gyro.core.scope.FileScope;
import gyro.core.scope.RootScope;
import io.airlift.airline.Arguments;
import io.airlift.airline.Option;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

public abstract class AbstractInstanceCommand extends AbstractCommand {

private List<GyroInstance> instances = new ArrayList<>();

@Option(name = { "-r", "--refresh" }, description = "Refresh instance data from the cloud provider.")
@Option(names = { "-r", "--refresh" }, description = "Refresh instance data from the cloud provider.")
public boolean refresh;

@Arguments
@Parameters(description = "gyro configuration files to look for instances in.")
private List<String> files;

protected RootScope current;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/gyro/plugin/ssh/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@
import com.psddev.dari.util.ObjectUtils;
import gyro.core.GyroCore;
import gyro.core.GyroInstance;
import io.airlift.airline.Command;
import gyro.core.command.VersionCommand;
import picocli.CommandLine.Command;

@Command(name = "list", description = "List instances found in provided config file.")
@Command(name = "list",
header = "List instances found in provided config file.",
synopsisHeading = "%n",
parameterListHeading = "%nParameters:%n",
optionListHeading = "%nOptions:%n",
usageHelpWidth = 100,
mixinStandardHelpOptions = true,
versionProvider = VersionCommand.class
)
public class ListCommand extends AbstractInstanceCommand {

private static final Table LIST_TABLE = new Table()
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/gyro/plugin/ssh/SshCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,41 @@

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;

import com.psddev.dari.util.ObjectUtils;
import gyro.core.GyroCore;
import gyro.core.GyroException;
import gyro.core.GyroInstance;
import gyro.core.resource.Diffable;
import gyro.core.resource.DiffableInternals;
import gyro.core.scope.DiffableScope;
import io.airlift.airline.Command;
import io.airlift.airline.Option;

@Command(name = "ssh", description = "SSH to a running instance.")
import gyro.core.command.VersionCommand;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "ssh",
header = "SSH to a running instance.",
synopsisHeading = "%n",
descriptionHeading = "%nDescription:%n%n",
description = "Connect to instances defined in gyro configuration using ssh. If multiple instances are found "
+ "a list will be presented to chose from will. If only one instance is found it will be connected to "
+ "automatically.",
parameterListHeading = "%nParameters:%n",
optionListHeading = "%nOptions:%n",
usageHelpWidth = 100,
mixinStandardHelpOptions = true,
versionProvider = VersionCommand.class
)
public class SshCommand extends AbstractInstanceCommand {

@Option(name = { "-e", "--execute" }, description = "Command to execute on host(s).")
@Option(names = { "-e", "--execute" }, description = "Command to execute on host(s).")
public String command;

@Option(name = { "-c", "--continue" }, description = "Ignore exit code and continue running -e command to run on all hosts.")
@Option(names = { "-c", "--continue" }, description = "Ignore exit code and continue running -e command to run on all hosts.")
public boolean force;

@Option(name = { "--tmux" }, description = "Open a tmux session with each host.")
@Option(names = { "--tmux" }, description = "Open a tmux session with each host.")
public boolean useTmux;

@Inject
@ArgGroup(exclusive = false)
public SshOptions sshOptions;

public String command() {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/gyro/plugin/ssh/SshOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.psddev.dari.util.ObjectUtils;
import gyro.core.GyroCore;
import gyro.core.GyroException;
import gyro.core.GyroInstance;
import gyro.core.resource.Diffable;
import gyro.core.resource.DiffableInternals;
import io.airlift.airline.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.Option;

public class SshOptions {

Expand All @@ -47,19 +43,19 @@ public class SshOptions {
.addColumn("Name", 54)
.addColumn("Hostname", 55);

@Option(name = {"-u", "--user"}, description = "User to log in as.")
@Option(names = {"-u", "--user"}, description = "User to log in as.")
public String user;

@Option(name = {"-k", "--keyfile"}, description = "Private key to use (i.e. ssh -i ~/.ssh/id_rsa).")
@Option(names = {"-k", "--keyfile"}, description = "Private key to use (i.e. ssh -i ~/.ssh/id_rsa).")
public String keyfile;

@Option(name = {"-q", "--quiet"}, description = "Quiet mode.")
@Option(names = {"-q", "--quiet"}, description = "Quiet mode.")
public boolean quiet;

@Option(name = {"-o", "--options"}, description = "Options pass to ssh -o option.")
@Option(names = {"-o", "--options"}, description = "Options pass to ssh -o option.")
public String options;

@Option(name = { "-j", "--jumphost" }, description = "Jump through jump host.")
@Option(names = { "-j", "--jumphost" }, description = "Jump through jump host.")
public boolean useJumpHost;

private List<GyroInstance> jumpHosts = new ArrayList<>();
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/gyro/plugin/ssh/TunnelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@

package gyro.plugin.ssh;

import javax.inject.Inject;

import java.awt.*;
import java.awt.Desktop;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import gyro.core.GyroCore;
import gyro.core.GyroException;
import gyro.core.GyroInstance;
import io.airlift.airline.Command;
import io.airlift.airline.Option;

@Command(name = "tunnel", description = "Tunnel to a running instance.")
import gyro.core.command.VersionCommand;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "tunnel",
header = "Tunnel to a running instance.",
synopsisHeading = "%n",
descriptionHeading = "%nDescription:%n%n",
description = "Tunnel to instance defined in gyro configuration using ssh tunnel. If multiple instances are found "
+ "a list will be presented to chose from will. If only one instance is found it will be connected to "
+ "automatically.",
parameterListHeading = "%nParameters:%n",
optionListHeading = "%nOptions:%n",
usageHelpWidth = 100,
mixinStandardHelpOptions = true,
versionProvider = VersionCommand.class
)
public class TunnelCommand extends AbstractInstanceCommand {

private static final Table TUNNEL_TABLE = new Table().
Expand All @@ -41,16 +53,16 @@ public class TunnelCommand extends AbstractInstanceCommand {
addColumn("State", 12).
addColumn("Hostname", 65);

@Option(name = {"--localPort"}, description = "Local port to listen on.")
@Option(names = "--localPort", description = "Local port to listen on.")
public Integer localPort;

@Option(name = {"--remotePort"}, description = "Remote port to connect to.")
@Option(names = "--remotePort", description = "Remote port to connect to.")
public Integer remotePort;

@Option(name = {"--nobrowser"}, description = "Don't open browser automatically.")
@Option(names = "--nobrowser", description = "Don't open browser automatically.")
public boolean noBrowser;

@Inject
@ArgGroup(exclusive = false)
public SshOptions sshOptions;

@Override
Expand Down

0 comments on commit 90f85cf

Please sign in to comment.