/// <summary> /// Implements the <see cref="ProcessRecord"/> method for <see cref="SetGithubCredentialsCmdlet"/>. /// </summary> protected override void ProcessRecord() { try { var config = new GithubCredentials { AccountType = AccountType, AccountName = AccountName, ApiKey = ApiKey }; GithubContext.Credentials = config; if (StorePermanent.IsPresent) { Task.Run(async() => await AuthService.SetConfig(config)); } } catch (Exception e) { ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to set credentials with error: {e.Message}", e), null, ErrorCategory.OperationStopped, null)); } }
/// <summary> /// Create a local config file in the module directory with encrypted auth credentials. /// </summary> /// <param name="config"></param> /// <returns></returns> public static async Task SetConfig(GithubCredentials config) { var encrypted = await EncryptionService.Encrypt(JsonSerializer.Serialize(config)); await File.WriteAllTextAsync(ConfigFile, encrypted); }
/// <summary> /// Set credentials to null /// </summary> public static void Dispose() { Credentials = null; }