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

print parameter value if and only if provided via CLI arg or config #464

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions src/D2L.Bmx/AwsCredsCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AwsCredentials Credentials
internal class AwsCredsCreator(
IAwsClient awsClient,
IConsolePrompter consolePrompter,
IConsoleWriter consoleWriter,
IAwsCredentialCache awsCredentialCache,
BmxConfig config
) {
Expand All @@ -30,12 +31,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 ) {
consoleWriter.WriteParameter( ParameterDescriptions.Account, account, accountSource );
Comment on lines +39 to +40
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable to not print these out when user has explicitly specified --non-interactive?

}

var roleSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( role ) && !string.IsNullOrEmpty( config.Role ) ) {
role = config.Role;
roleSource = ParameterSource.Config;
}
if( !string.IsNullOrEmpty( role ) && !nonInteractive ) {
consoleWriter.WriteParameter( ParameterDescriptions.Role, role, roleSource );
}

if( duration is null or 0 ) {
Expand Down
6 changes: 2 additions & 4 deletions src/D2L.Bmx/ConsolePrompter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal interface IConsolePrompter {
string PromptOrg( bool allowEmptyInput );
string PromptProfile();
string PromptUser( bool allowEmptyInput );
string PromptPassword( string user, string org );
string PromptPassword();
int? PromptDuration();
string PromptAccount( string[] accounts );
string PromptRole( string[] roles );
Expand Down Expand Up @@ -63,9 +63,7 @@ string IConsolePrompter.PromptUser( bool allowEmptyInput ) {
return user;
}

string IConsolePrompter.PromptPassword( string user, string org ) {
Console.Error.WriteLine( $"{ParameterDescriptions.Org}: {org}" );
Console.Error.WriteLine( $"{ParameterDescriptions.User}: {user}" );
string IConsolePrompter.PromptPassword() {
return GetMaskedInput( $"{ParameterDescriptions.Password}: " );
}

Expand Down
17 changes: 17 additions & 0 deletions src/D2L.Bmx/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace D2L.Bmx;

internal interface IConsoleWriter {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're reasonably close to a point where some unit tests around our prompting and printing logic would be worthwhile.
I could really use a few when I was writing up #463
Interfaces are handy then.

void WriteParameter( string description, string value, ParameterSource source );
}

internal class ConsoleWriter : IConsoleWriter {
void IConsoleWriter.WriteParameter( string description, string value, ParameterSource source ) {
Console.ResetColor();
Console.Error.Write( $"{description}: " );
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Error.Write( value );
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Error.WriteLine( $" (from {source})" );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively:

Suggested change
Console.Error.WriteLine( $" (from {source})" );
Console.Error.WriteLine( $" (source: {source})" );

🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My vote is from

Console.ResetColor();
}
}
31 changes: 20 additions & 11 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class OktaAuthenticator(
IOktaApi oktaApi,
IOktaSessionStorage sessionStorage,
IConsolePrompter consolePrompter,
IConsoleWriter consoleWriter,
BmxConfig config
) {
public async Task<AuthenticatedOktaApi> AuthenticateAsync(
Expand All @@ -21,24 +22,32 @@ public async Task<AuthenticatedOktaApi> AuthenticateAsync(
bool nonInteractive,
bool ignoreCache
) {
var orgSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( org ) && !string.IsNullOrEmpty( config.Org ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aside: writing all this overwrite/print/prompt logic really has a fizz buzz feel - it feels like you should be able to consolidate some actions and/or logical branches, but actually you can't...

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 ) {
consoleWriter.WriteParameter( ParameterDescriptions.Org, org, 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 ) {
consoleWriter.WriteParameter( ParameterDescriptions.User, user, userSource );
}

oktaApi.SetOrganization( org );
Expand All @@ -50,7 +59,7 @@ bool ignoreCache
throw new BmxException( "Okta authentication failed. Please run `bmx login` first." );
}

string password = consolePrompter.PromptPassword( user, org );
string password = consolePrompter.PromptPassword();

var authnResponse = await oktaApi.AuthenticateAsync( user, password );

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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a record type rather than an enum so it's:

  • easy to customize ToString()
  • easy to extend in the future to supply config file path

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;
}
8 changes: 8 additions & 0 deletions src/D2L.Bmx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
new OktaApi(),
new OktaSessionStorage(),
new ConsolePrompter(),
new ConsoleWriter(),
config
) );
return handler.HandleAsync(
Expand Down Expand Up @@ -121,16 +122,19 @@

printCommand.SetHandler( ( InvocationContext context ) => {
var consolePrompter = new ConsolePrompter();
var consoleWriter = new ConsoleWriter();
var config = new BmxConfigProvider( new FileIniDataParser() ).GetConfiguration();
var handler = new PrintHandler(
new OktaAuthenticator(
new OktaApi(),
new OktaSessionStorage(),
consolePrompter,
consoleWriter,
config ),
new AwsCredsCreator(
new AwsClient( new AmazonSecurityTokenServiceClient( new AnonymousAWSCredentials() ) ),
consolePrompter,
consoleWriter,
new AwsCredsCache(),
config )
);
Expand Down Expand Up @@ -172,19 +176,23 @@

writeCommand.SetHandler( ( InvocationContext context ) => {
var consolePrompter = new ConsolePrompter();
var consoleWriter = new ConsoleWriter();
var config = new BmxConfigProvider( new FileIniDataParser() ).GetConfiguration();
var handler = new WriteHandler(
new OktaAuthenticator(
new OktaApi(),
new OktaSessionStorage(),
consolePrompter,
consoleWriter,
config ),
new AwsCredsCreator(
new AwsClient( new AmazonSecurityTokenServiceClient( new AnonymousAWSCredentials() ) ),
consolePrompter,
consoleWriter,
new AwsCredsCache(),
config ),
consolePrompter,
consoleWriter,
config,
new FileIniDataParser()
);
Expand Down
15 changes: 11 additions & 4 deletions src/D2L.Bmx/WriteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class WriteHandler(
OktaAuthenticator oktaAuth,
AwsCredsCreator awsCredsCreator,
IConsolePrompter consolePrompter,
IConsoleWriter consoleWriter,
BmxConfig config,
FileIniDataParser parser
) {
Expand Down Expand Up @@ -45,12 +46,18 @@ bool useCredentialProcess
cache: cacheAwsCredentials
);

var profileSource = ParameterSource.CliArg;
if( string.IsNullOrEmpty( profile ) && !string.IsNullOrEmpty( config.Profile ) ) {
profile = config.Profile;
profileSource = ParameterSource.Config;
}
if( string.IsNullOrEmpty( profile ) ) {
if( !string.IsNullOrEmpty( config.Profile ) ) {
profile = config.Profile;
} else {
profile = consolePrompter.PromptProfile();
if( nonInteractive ) {
throw new BmxException( "Profile value was not provided" );
}
profile = consolePrompter.PromptProfile();
} else if( !nonInteractive ) {
consoleWriter.WriteParameter( ParameterDescriptions.Profile, profile, profileSource );
}

if( !string.IsNullOrEmpty( output ) && !Path.IsPathRooted( output ) ) {
Expand Down
Loading