Skip to content

Commit

Permalink
print parameter value when provided via CLI arg or config
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbao committed Jul 22, 2024
1 parent 55ae7ba commit 7233f66
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/D2L.Bmx/AwsCredsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ bool cache
);
}

var accountSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( account ) && !string.IsNullOrEmpty( config.Account ) ) {
account = config.Account;
accountSource = ParameterSource.Config;
}
if( !string.IsNullOrEmpty( account ) && !nonInteractive ) {
Console.Error.WriteLine( $"{ParameterDescriptions.Account}: {account} (from {accountSource})" );
}

var roleSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( role ) && !string.IsNullOrEmpty( config.Role ) ) {
role = config.Role;
roleSource = ParameterSource.Config;
}
if( !string.IsNullOrEmpty( role ) && !nonInteractive ) {
Console.Error.WriteLine( $"{ParameterDescriptions.Role}: {role} (from {roleSource})" );
}

if( duration is null or 0 ) {
Expand Down
28 changes: 18 additions & 10 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ public async Task<AuthenticatedOktaApi> AuthenticateAsync(
bool nonInteractive,
bool ignoreCache
) {
var orgSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( org ) && !string.IsNullOrEmpty( config.Org ) ) {
org = config.Org;
orgSource = ParameterSource.Config;
}
if( string.IsNullOrEmpty( org ) ) {
if( !string.IsNullOrEmpty( config.Org ) ) {
org = config.Org;
} else if( !nonInteractive ) {
org = consolePrompter.PromptOrg( allowEmptyInput: false );
} else {
if( nonInteractive ) {
throw new BmxException( "Org value was not provided" );
}
org = consolePrompter.PromptOrg( allowEmptyInput: false );
} else if( !nonInteractive ) {
Console.Error.WriteLine( $"{ParameterDescriptions.Org}: {org} (from {orgSource})" );
}

var userSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( user ) && !string.IsNullOrEmpty( config.User ) ) {
user = config.User;
userSource = ParameterSource.Config;
}
if( string.IsNullOrEmpty( user ) ) {
if( !string.IsNullOrEmpty( config.User ) ) {
user = config.User;
} else if( !nonInteractive ) {
user = consolePrompter.PromptUser( allowEmptyInput: false );
} else {
if( nonInteractive ) {
throw new BmxException( "User value was not provided" );
}
user = consolePrompter.PromptUser( allowEmptyInput: false );
} else if( !nonInteractive ) {
Console.Error.WriteLine( $"{ParameterDescriptions.User}: {user} (from {userSource})" );
}

oktaApi.SetOrganization( org );
Expand Down
2 changes: 1 addition & 1 deletion src/D2L.Bmx/ParameterDescriptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace D2L.Bmx;

internal static class ParameterDescriptions {
public const string Org = "Okta org short name or domain name";
public const string Org = "Okta org or domain name";
public const string User = "Okta username";
public const string Password = "Okta password";
public const string Account = "AWS account name";
Expand Down
14 changes: 14 additions & 0 deletions src/D2L.Bmx/ParameterSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace D2L.Bmx;

internal record ParameterSource {
public string Description { get; init; }

private ParameterSource( string description ) {
Description = description;
}

public static ParameterSource CliArg => new( "command line argument" );
public static ParameterSource Config => new( "config file" );

public override string ToString() => Description;
}

0 comments on commit 7233f66

Please sign in to comment.