/// <summary> /// Adds a new channel with the specified name. /// </summary> /// <returns>Process exit code.</returns> public override async Task <int> ExecuteAsync() { try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); // If the user tried to mark as internal, indicate that this is currently // unsupported. if (_options.Internal) { Logger.LogError("Cannot currently mark channels as internal."); return(Constants.ErrorCode); } Channel newChannelInfo = await remote.CreateChannelAsync(_options.Name, _options.Classification); Console.WriteLine($"Successfully created new channel with name '{_options.Name}'."); return(Constants.SuccessCode); } catch (RestApiException e) when(e.Response.StatusCode == HttpStatusCode.Conflict) { Logger.LogError($"An existing channel with name '{_options.Name}' already exists"); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, "Error: Failed to create new channel."); return(Constants.ErrorCode); } }
/// <summary> /// Adds a new channel with the specified name. /// </summary> /// <returns>Process exit code.</returns> public override async Task <int> ExecuteAsync() { try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); // If the user tried to mark as internal, indicate that this is currently // unsupported. if (_options.Internal) { Logger.LogError("Cannot currently mark channels as internal."); return(Constants.ErrorCode); } Channel newChannelInfo = await remote.CreateChannelAsync(_options.Name, _options.Classification); switch (_options.OutputFormat) { case DarcOutputType.json: Console.WriteLine(JsonConvert.SerializeObject( new { id = newChannelInfo.Id, name = newChannelInfo.Name, classification = newChannelInfo.Classification }, Formatting.Indented)); break; case DarcOutputType.text: Console.WriteLine($"Successfully created new channel with name '{_options.Name}' and id {newChannelInfo.Id}."); break; default: throw new NotImplementedException($"Output type {_options.OutputFormat} not supported by add-channel"); } return(Constants.SuccessCode); } catch (AuthenticationException e) { Console.WriteLine(e.Message); return(Constants.ErrorCode); } catch (RestApiException e) when(e.Response.Status == (int)HttpStatusCode.Conflict) { Logger.LogError($"An existing channel with name '{_options.Name}' already exists"); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, "Error: Failed to create new channel."); return(Constants.ErrorCode); } }