From 155dbdcc57872334bd37ff6a366be18bd9a07b5e Mon Sep 17 00:00:00 2001 From: Chenfeng Bao Date: Wed, 24 Jul 2024 12:38:49 -0400 Subject: [PATCH] fix up & touch up user messages --- src/D2L.Bmx/Aws/AwsCredsCache.cs | 2 +- src/D2L.Bmx/BmxConfigProvider.cs | 10 +++++++--- src/D2L.Bmx/Okta/OktaApi.cs | 7 ++++--- src/D2L.Bmx/OktaAuthenticator.cs | 8 ++++---- src/D2L.Bmx/Program.cs | 11 ++++++----- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/D2L.Bmx/Aws/AwsCredsCache.cs b/src/D2L.Bmx/Aws/AwsCredsCache.cs index 14a177d0..3f2f53e1 100644 --- a/src/D2L.Bmx/Aws/AwsCredsCache.cs +++ b/src/D2L.Bmx/Aws/AwsCredsCache.cs @@ -61,7 +61,7 @@ AwsCredentials credentials && o.Org == org ) // Prune older (closer to expiry) credentials for the same role - .GroupBy( o => $"{o.AccountName}.${o.RoleName}", ( _, entries ) => + .GroupBy( o => (o.AccountName, o.RoleName), ( _, entries ) => entries.OrderBy( o => o.Credentials.Expiration ) ) .Select( o => o.Last() ); diff --git a/src/D2L.Bmx/BmxConfigProvider.cs b/src/D2L.Bmx/BmxConfigProvider.cs index 96539e74..5c3a5603 100644 --- a/src/D2L.Bmx/BmxConfigProvider.cs +++ b/src/D2L.Bmx/BmxConfigProvider.cs @@ -8,7 +8,11 @@ internal interface IBmxConfigProvider { void SaveConfiguration( BmxConfig config ); } -internal class BmxConfigProvider( FileIniDataParser parser ) : IBmxConfigProvider { +internal class BmxConfigProvider( + FileIniDataParser parser, + IConsoleWriter consoleWriter +) : IBmxConfigProvider { + public BmxConfig GetConfiguration() { // Main config is at ~/.bmx/config string configFileName = BmxPaths.CONFIG_FILE_NAME; @@ -18,7 +22,7 @@ public BmxConfig GetConfiguration() { var tempdata = parser.ReadFile( configFileName ); data.Merge( tempdata ); } catch( Exception ) { - Console.Error.Write( $"WARNING: Failed to load the global config file {configFileName}." ); + consoleWriter.WriteWarning( $"WARNING: Failed to load the global config file {configFileName}." ); } } // If set, we recursively look up from CWD (all the way to root) for additional bmx config files (labelled as .bmx) @@ -29,7 +33,7 @@ public BmxConfig GetConfiguration() { var tempdata = parser.ReadFile( projectConfigInfo.FullName ); data.Merge( tempdata ); } catch( Exception ) { - Console.Error.Write( $"WARNING: Failed to load the local config file {projectConfigInfo.FullName}." ); + consoleWriter.WriteWarning( $"WARNING: Failed to load the local config file {projectConfigInfo.FullName}." ); } } diff --git a/src/D2L.Bmx/Okta/OktaApi.cs b/src/D2L.Bmx/Okta/OktaApi.cs index 7a4f09f7..0728cdb6 100644 --- a/src/D2L.Bmx/Okta/OktaApi.cs +++ b/src/D2L.Bmx/Okta/OktaApi.cs @@ -88,9 +88,10 @@ await resp.Content.ReadAsStreamAsync(), } string org = _organization ?? "unknown"; - throw new BmxException( - $"Okta authentication for user '{username}' in org '{org}'" - + "failed. Check if org, user, and password is correct" ); + throw new BmxException( $""" + Okta authentication for user '{username}' in org '{org}' failed. + Check if org, user, and password is correct. + """ ); } async Task IOktaApi.IssueMfaChallengeAsync( string stateToken, string factorId ) { diff --git a/src/D2L.Bmx/OktaAuthenticator.cs b/src/D2L.Bmx/OktaAuthenticator.cs index 8d480164..fe092053 100644 --- a/src/D2L.Bmx/OktaAuthenticator.cs +++ b/src/D2L.Bmx/OktaAuthenticator.cs @@ -91,10 +91,10 @@ mfaFactor is OktaMfaQuestionFactor // Security question factor is a static value if( File.Exists( BmxPaths.CONFIG_FILE_NAME ) ) { CacheOktaSession( user, org, sessionResp.Id, sessionResp.ExpiresAt ); } else { - consoleWriter.WriteWarning( - "No config file found. Your Okta session will not be cached. " + - "Consider running `bmx configure` if you own this machine." - ); + consoleWriter.WriteWarning( """ + No config file found. Your Okta session will not be cached. + Consider running `bmx configure` if you own this machine. + """ ); } return new AuthenticatedOktaApi( Org: org, User: user, Api: oktaApi ); } diff --git a/src/D2L.Bmx/Program.cs b/src/D2L.Bmx/Program.cs index 3ae8fa3f..f40b1676 100644 --- a/src/D2L.Bmx/Program.cs +++ b/src/D2L.Bmx/Program.cs @@ -24,12 +24,13 @@ userOption, }; loginCommand.SetHandler( ( InvocationContext context ) => { - var config = new BmxConfigProvider( new FileIniDataParser() ).GetConfiguration(); + var consoleWriter = new ConsoleWriter(); + var config = new BmxConfigProvider( new FileIniDataParser(), consoleWriter ).GetConfiguration(); var handler = new LoginHandler( new OktaAuthenticator( new OktaApi(), new OktaSessionStorage(), new ConsolePrompter(), - new ConsoleWriter(), + consoleWriter, config ) ); return handler.HandleAsync( @@ -67,7 +68,7 @@ configureCommand.SetHandler( ( InvocationContext context ) => { var handler = new ConfigureHandler( - new BmxConfigProvider( new FileIniDataParser() ), + new BmxConfigProvider( new FileIniDataParser(), new ConsoleWriter() ), new ConsolePrompter() ); handler.Handle( org: context.ParseResult.GetValueForOption( orgOption ), @@ -123,7 +124,7 @@ printCommand.SetHandler( ( InvocationContext context ) => { var consolePrompter = new ConsolePrompter(); var consoleWriter = new ConsoleWriter(); - var config = new BmxConfigProvider( new FileIniDataParser() ).GetConfiguration(); + var config = new BmxConfigProvider( new FileIniDataParser(), consoleWriter ).GetConfiguration(); var handler = new PrintHandler( new OktaAuthenticator( new OktaApi(), @@ -177,7 +178,7 @@ writeCommand.SetHandler( ( InvocationContext context ) => { var consolePrompter = new ConsolePrompter(); var consoleWriter = new ConsoleWriter(); - var config = new BmxConfigProvider( new FileIniDataParser() ).GetConfiguration(); + var config = new BmxConfigProvider( new FileIniDataParser(), consoleWriter ).GetConfiguration(); var handler = new WriteHandler( new OktaAuthenticator( new OktaApi(),