Skip to content

Commit df7d3cd

Browse files
committed
More work on resolving #255
1 parent e0271b9 commit df7d3cd

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/main/java/com/dtsx/astra/cli/AstraCli.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import lombok.SneakyThrows;
4343
import lombok.experimental.Accessors;
4444
import lombok.val;
45+
import org.apache.commons.lang3.ArrayUtils;
4546
import org.apache.commons.lang3.tuple.Pair;
4647
import org.jetbrains.annotations.Nullable;
4748
import org.jetbrains.annotations.VisibleForTesting;
@@ -53,6 +54,7 @@
5354

5455
import java.nio.file.FileSystems;
5556
import java.nio.file.Files;
57+
import java.util.Arrays;
5658
import java.util.Optional;
5759
import java.util.StringJoiner;
5860
import java.util.function.Supplier;
@@ -197,7 +199,17 @@ public static int run(Ref<CliContext> ctxRef, String... args) {
197199
return DefaultsRenderer.helpWithOverriddenDefaultsRendering(spec, cs);
198200
});
199201

200-
return cmd.execute(args);
202+
val allArgs = ArrayUtils.addAll(defaultArgs(), args);
203+
return cmd.execute(allArgs);
204+
}
205+
206+
private static String[] defaultArgs() {
207+
val defaultArgsStr = Optional.ofNullable(System.getenv(ConstEnvVars.DEFAULT_ARGS))
208+
.orElse("");
209+
210+
return Arrays.stream(defaultArgsStr.split("\\s+"))
211+
.filter(s -> !s.isBlank())
212+
.toArray(String[]::new);
201213
}
202214

203215
private static IFactory mkFactory(Ref<CliContext> ctxRef) {

src/main/java/com/dtsx/astra/cli/commands/AbstractConnectedCmd.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.dtsx.astra.cli.core.exceptions.AstraCliException;
1717
import com.dtsx.astra.cli.core.models.AstraToken;
1818
import com.dtsx.astra.cli.core.output.Hint;
19+
import com.dtsx.astra.cli.core.properties.CliProperties.ConstEnvVars;
1920
import com.dtsx.astra.sdk.utils.AstraEnvironment;
2021
import lombok.val;
2122
import org.apache.commons.lang3.tuple.Pair;
@@ -28,6 +29,7 @@
2829
import java.util.List;
2930
import java.util.Optional;
3031

32+
import static com.dtsx.astra.cli.core.output.ExitCode.PARSE_ISSUE;
3133
import static com.dtsx.astra.cli.core.output.ExitCode.PROFILE_NOT_FOUND;
3234

3335
public abstract class AbstractConnectedCmd<OpRes> extends AbstractCmd<OpRes> {
@@ -71,7 +73,7 @@ public static class ConfigSpec {
7173
@Option(
7274
names = { $Profile.LONG, $Profile.SHORT },
7375
completionCandidates = AvailableProfilesCompletion.class,
74-
description = "The @|code .astrarc|@ profile to use for this command",
76+
description = "The @|code .astrarc|@ profile to use for this command. Can be set via @|code " + ConstEnvVars.PROFILE + "|@.",
7577
paramLabel = $Profile.LABEL
7678
)
7779
public Optional<ProfileName> $profileName;
@@ -121,7 +123,14 @@ private ProfileSource profileSource() {
121123

122124
if ($credsProvider != null && $credsProvider.$config != null) {
123125
val configFile = $credsProvider.$config.$configFile;
124-
val profileName = $credsProvider.$config.$profileName.orElse(ProfileName.DEFAULT);
126+
127+
val defaultProfileName = Optional.ofNullable(System.getenv(ConstEnvVars.PROFILE))
128+
.map(ProfileName::parse)
129+
.map((p) -> p.getRight((e) -> new AstraCliException(PARSE_ISSUE, "Invalid profile name in " + ConstEnvVars.PROFILE + ": '" + e + "'")))
130+
.orElse(ProfileName.DEFAULT);
131+
132+
val profileName = $credsProvider.$config.$profileName
133+
.orElse(defaultProfileName);
125134

126135
// the check for if it's the default file is later used to decide whether to update the completions cache or not
127136
if (configFile.isPresent()) {

src/main/java/com/dtsx/astra/cli/commands/CommonOptions.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public enum ColorMode { auto, never, always }
2727
description = { "One of: ${COMPLETION-CANDIDATES}", SHOW_CUSTOM_DEFAULT + "auto" },
2828
arity = "0..1",
2929
fallbackValue = "always",
30-
defaultValue = "${ASTRA_DEFAULT_COLOR:-auto}",
3130
paramLabel = "WHEN"
3231
)
3332
private void setAnsi(ColorMode mode) {
@@ -40,7 +39,6 @@ private void setAnsi(ColorMode mode) {
4039

4140
@Option(
4241
names = "--no-color",
43-
defaultValue = "${ASTRA_NO_COLOR:-false}", // no DEFAULT in env var on purpose to better adhere to per-program NO_COLOR semantics
4442
hidden = true
4543
)
4644
private void setAnsi(boolean noColor) {
@@ -50,7 +48,7 @@ private void setAnsi(boolean noColor) {
5048
@Option(
5149
names = { "--output", "-o" },
5250
completionCandidates = OutputTypeCompletion.class,
53-
defaultValue = "${ASTRA_DEFAULT_OUTPUT_TYPE:-human}",
51+
defaultValue = "human",
5452
description = "One of: ${COMPLETION-CANDIDATES}",
5553
paramLabel = "FORMAT"
5654
)
@@ -61,7 +59,6 @@ private void setAnsi(boolean noColor) {
6159
@Option(
6260
names = { "-V", "--verbose" },
6361
description = "Enable verbose logging output",
64-
defaultValue = "${ASTRA_DEFAULT_VERBOSE:-false}",
6562
showDefaultValue = Visibility.NEVER
6663
)
6764
private boolean verbose;
@@ -70,7 +67,6 @@ private void setAnsi(boolean noColor) {
7067
@Option(
7168
names = { "-q", "--quiet" },
7269
description = "Only output essential information",
73-
defaultValue = "${ASTRA_DEFAULT_QUIET:-false}",
7470
showDefaultValue = Visibility.NEVER
7571
)
7672
private boolean quiet;
@@ -79,7 +75,6 @@ private void setAnsi(boolean noColor) {
7975
@Option(
8076
names = { "--spinner" },
8177
description = { "Enable/disable loading spinners", SHOW_CUSTOM_DEFAULT + "enabled if tty and not quiet" },
82-
defaultValue = "${ASTRA_DEFAULT_SPINNER:-" + Option.NULL_VALUE + "}",
8378
negatable = true,
8479
fallbackValue = "true"
8580
)
@@ -95,7 +90,6 @@ private void setAnsi(boolean noColor) {
9590
names = "--dump-logs",
9691
description = { "Write all logs to an optionally specified file", SHOW_CUSTOM_DEFAULT + "${cli.home-folder.path}/logs/<file>.log" },
9792
fallbackValue = "__fallback__",
98-
defaultValue = "${ASTRA_DEFAULT_DUMP_LOGS:-" + Option.NULL_VALUE + "}",
9993
paramLabel = "FILE",
10094
arity = "0..1"
10195
)
@@ -130,7 +124,6 @@ private void setDumpLogs(boolean noDumpLogs) {
130124
@Option(
131125
names = "--no-input",
132126
description = "Don't ask for user input (e.g. confirmation prompts)",
133-
defaultValue = "${ASTRA_DEFAULT_NO_INPUT:-false}",
134127
showDefaultValue = Visibility.NEVER
135128
)
136129
private boolean noInput;

src/main/java/com/dtsx/astra/cli/core/properties/CliProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ConstEnvVars {
1919
public static final String IGNORE_BETA_WARNINGS = "ASTRA_IGNORE_BETA_WARNINGS";
2020
public static final String NO_UPDATE_NOTIFIER = "ASTRA_NO_UPDATE_NOTIFIER";
2121
public static final String COMPLETIONS_SETUP = "ASTRA_COMPLETIONS_SETUP";
22+
public static final String DEFAULT_ARGS = "ASTRA_DEFAULT_ARGS";
23+
public static final String PROFILE = "ASTRA_PROFILE";
2224
}
2325

2426
record ExternalSoftware(

0 commit comments

Comments
 (0)