-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||
namespace D2L.Bmx; | ||||||
|
||||||
internal interface IConsoleWriter { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||
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})" ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alternatively:
Suggested change
🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My vote is |
||||||
Console.ResetColor(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ internal class OktaAuthenticator( | |
IOktaApi oktaApi, | ||
IOktaSessionStorage sessionStorage, | ||
IConsolePrompter consolePrompter, | ||
IConsoleWriter consoleWriter, | ||
BmxConfig config | ||
) { | ||
public async Task<AuthenticatedOktaApi> AuthenticateAsync( | ||
|
@@ -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 ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
@@ -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 ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace D2L.Bmx; | ||
|
||
internal record ParameterSource { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use a record type rather than an enum so it's:
|
||
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; | ||
} |
There was a problem hiding this comment.
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
?