Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Change api method params to posix-style opts #5079

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9180ca6
Fix two CLI option parsing bugs
ghubstan Jan 8, 2021
cc08554
Add @Singleton annotation
ghubstan Jan 8, 2021
9ae1a29
Integrate new protection tools into api's offer & trade services
ghubstan Jan 8, 2021
0293196
Merge branch 'master' into 03-support-trigger-price
ghubstan Jan 11, 2021
0c6005e
Stub out support for OpenOffer's triggerPrice in api
ghubstan Jan 11, 2021
f1a6783
Display fiat ccy code in upper case
ghubstan Jan 11, 2021
eaf41eb
Merge branch 'master' into 03-support-trigger-price
ghubstan Jan 12, 2021
0d0ba41
Move CLI method enum to its own class
ghubstan Jan 12, 2021
653aa0f
Add gRPC service Help definition
ghubstan Jan 12, 2021
f2f55f9
Add new GrpcHelpService boilerplate
ghubstan Jan 12, 2021
6b5dc97
Add HelpService to gRPC Server
ghubstan Jan 12, 2021
3a280d6
Stub out new api CoreHelpService
ghubstan Jan 12, 2021
1dc8d9c
Add gRPC client stub for help
ghubstan Jan 12, 2021
57b3182
Add CLI method specific posix-style option parsers
ghubstan Jan 12, 2021
9d8bb87
Use posix-style method options in CLI
ghubstan Jan 12, 2021
9223a66
Delete NegativeNumberOptions class
ghubstan Jan 12, 2021
c1d463b
Add opt parser for no-arg methods (to detect method help opt)
ghubstan Jan 12, 2021
be6a0a9
Use no-arg method opt parser in CLI (detect method help opt)
ghubstan Jan 12, 2021
97a1f7b
Add basic help for no-arg methods
ghubstan Jan 12, 2021
06067af
Short circuit method opt validation if user wants help
ghubstan Jan 12, 2021
e3911ff
Add 1st cut of takeoffer method help
ghubstan Jan 12, 2021
86958e3
Remove position number info from method param desc
ghubstan Jan 12, 2021
57840a2
Make CLI method name the only positional cmd line option
ghubstan Jan 13, 2021
791ab35
Fix file conflict
ghubstan Jan 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
511 changes: 269 additions & 242 deletions cli/src/main/java/bisq/cli/CliMain.java

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CurrencyFormat {
static final BigDecimal BSQ_SATOSHI_DIVISOR = new BigDecimal(100);
static final DecimalFormat BSQ_FORMAT = new DecimalFormat("###,###,###,##0.00");

static final BigDecimal SECURITY_DEPOSIT_MULTIPLICAND = new BigDecimal("0.01");

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatSatoshis(long sats) {
return BTC_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down Expand Up @@ -99,6 +101,15 @@ static long toSatoshis(String btc) {
}
}

static double toSecurityDepositAsPct(String securityDepositInput) {
try {
return new BigDecimal(securityDepositInput)
.multiply(SECURITY_DEPOSIT_MULTIPLICAND).doubleValue();
} catch (NumberFormatException e) {
throw new IllegalArgumentException(format("'%s' is not a number", securityDepositInput));
}
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
private static String formatFeeSatoshis(long sats) {
return BTC_TX_FEE_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/java/bisq/cli/GrpcStubs.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.proto.grpc.DisputeAgentsGrpc;
import bisq.proto.grpc.GetVersionGrpc;
import bisq.proto.grpc.HelpGrpc;
import bisq.proto.grpc.OffersGrpc;
import bisq.proto.grpc.PaymentAccountsGrpc;
import bisq.proto.grpc.PriceGrpc;
Expand All @@ -33,6 +34,7 @@
public class GrpcStubs {

public final DisputeAgentsGrpc.DisputeAgentsBlockingStub disputeAgentsService;
public final HelpGrpc.HelpBlockingStub helpService;
public final GetVersionGrpc.GetVersionBlockingStub versionService;
public final OffersGrpc.OffersBlockingStub offersService;
public final PaymentAccountsGrpc.PaymentAccountsBlockingStub paymentAccountsService;
Expand All @@ -53,6 +55,7 @@ public GrpcStubs(String apiHost, int apiPort, String apiPassword) {
}));

this.disputeAgentsService = DisputeAgentsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.helpService = HelpGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.versionService = GetVersionGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.offersService = OffersGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.paymentAccountsService = PaymentAccountsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
Expand Down
56 changes: 56 additions & 0 deletions cli/src/main/java/bisq/cli/Method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.cli;

/**
* Currently supported api methods.
*/
public enum Method {
createoffer,
canceloffer,
getoffer,
getmyoffer,
getoffers,
getmyoffers,
takeoffer,
gettrade,
confirmpaymentstarted,
confirmpaymentreceived,
keepfunds,
withdrawfunds,
getpaymentmethods,
getpaymentacctform,
createpaymentacct,
getpaymentaccts,
getversion,
getbalance,
getaddressbalance,
getfundingaddresses,
getunusedbsqaddress,
sendbsq,
sendbtc,
gettxfeerate,
settxfeerate,
unsettxfeerate,
gettransaction,
lockwallet,
unlockwallet,
removewalletpassword,
setwalletpassword,
registerdisputeagent
}
83 changes: 0 additions & 83 deletions cli/src/main/java/bisq/cli/NegativeNumberOptions.java

This file was deleted.

2 changes: 1 addition & 1 deletion cli/src/main/java/bisq/cli/TableFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static String formatOfferTable(List<OfferInfo> offerInfo, String fiatCurrency) {
+ padEnd(COL_HEADER_PAYMENT_METHOD, paymentMethodColWidth, ' ') + COL_HEADER_DELIMITER
+ COL_HEADER_CREATION_DATE + COL_HEADER_DELIMITER
+ COL_HEADER_UUID.trim() + "%n";
String headerLine = format(headersFormat, fiatCurrency, fiatCurrency);
String headerLine = format(headersFormat, fiatCurrency.toUpperCase(), fiatCurrency.toUpperCase());

String colDataFormat = "%-" + (COL_HEADER_DIRECTION.length() + COL_HEADER_DELIMITER.length()) + "s" // left
+ "%" + (COL_HEADER_PRICE.length() - 1) + "s" // rt justify to end of hdr
Expand Down
57 changes: 57 additions & 0 deletions cli/src/main/java/bisq/cli/opts/AbstractMethodOptionParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.cli.opts;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import java.util.List;

import lombok.Getter;

abstract class AbstractMethodOptionParser implements MethodOpts {

// The full command line args passed to CliMain.main(String[] args).
// CLI and Method level arguments are derived from args by an ArgumentList(args).
protected final String[] args;

protected final OptionParser parser = new OptionParser();

// The help option for a specific api method, e.g., -m=takeoffer -help.
protected final OptionSpec<Void> helpOpt = parser.accepts("help", "Print method help").forHelp();

@Getter
protected OptionSet options;
@Getter
protected List<String> nonOptionArguments;

protected AbstractMethodOptionParser(String[] args) {
this.args = args;
}

public AbstractMethodOptionParser parse() {
options = parser.parse(new ArgumentList(args).getMethodArguments());
nonOptionArguments = (List<String>) options.nonOptionArguments();
return this;
}

public boolean isForHelp() {
return options.has(helpOpt);
}
}
122 changes: 122 additions & 0 deletions cli/src/main/java/bisq/cli/opts/ArgumentList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.cli.opts;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

/**
* Wrapper for an array of command line arguments.
*
* Used to remove CLI connection and authentication arguments or method arguments
* before parsing at the CLI or method level.
*
*/
public class ArgumentList {

private final Predicate<String> isCliOpt = (o) ->
o.startsWith("--password") || o.startsWith("-password")
|| o.startsWith("--port") || o.startsWith("-port")
|| o.startsWith("--host") || o.startsWith("-host");


// The method name is the only positional option in a command, and easy to identify.
// If the positional argument does not match a Method, or there are more than one
// positional arguments, the parser and/or CLI will fail as expected.
private final Predicate<String> isMethodNameOpt = (o) -> !o.startsWith("-");

private final Predicate<String> isHelpOpt = (o) -> o.startsWith("--help") || o.startsWith("-help");

private final String[] arguments;
private int currentIndex;

public ArgumentList(String... arguments) {
this.arguments = arguments.clone();
}

/**
* Returns only the CLI connection & authentication, and method name args
* (--password, --host, --port, --help, method name) contained in the original
* String[] args; excludes the method specific arguments.
*
* If String[] args contains both a method name (the only positional opt) and a help
* argument (--help, -help), it is assumed the user wants method help, not CLI help,
* and the help argument is not included in the returned String[].
*/
public String[] getCLIArguments() {
List<String> prunedArguments = new ArrayList<>();
currentIndex = 0;
Optional<String> methodNameArgument = Optional.empty();
Optional<String> helpArgument = Optional.empty();
while (hasMore()) {
String arg = peek();
if (isMethodNameOpt.test(arg)) {
methodNameArgument = Optional.of(arg);
prunedArguments.add(arg);
}

if (isCliOpt.test(arg))
prunedArguments.add(arg);

if (isHelpOpt.test(arg))
helpArgument = Optional.of(arg);

next();
}

// Include the saved CLI help argument if the positional method name argument
// was not found.
if (!methodNameArgument.isPresent() && helpArgument.isPresent())
prunedArguments.add(helpArgument.get());

return prunedArguments.toArray(new String[0]);
}

/**
* Returns only the method args contained in the original String[] args; excludes the
* CLI connection & authentication opts (--password, --host, --port), plus the
* positional method name arg.
*/
public String[] getMethodArguments() {
List<String> prunedArguments = new ArrayList<>();
currentIndex = 0;
while (hasMore()) {
String arg = peek();
if (!isCliOpt.test(arg) && !isMethodNameOpt.test(arg)) {
prunedArguments.add(arg);
}
next();
}
return prunedArguments.toArray(new String[0]);
}


boolean hasMore() {
return currentIndex < arguments.length;
}

String next() {
return arguments[currentIndex++];
}

String peek() {
return arguments[currentIndex];
}
}
Loading