|
| 1 | +/* |
| 2 | + * Copyright contributors to the Galasa project |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: EPL-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package cmd |
| 8 | + |
| 9 | +import ( |
| 10 | + "log" |
| 11 | + |
| 12 | + "github.com/galasa-dev/cli/pkg/api" |
| 13 | + "github.com/galasa-dev/cli/pkg/galasaapi" |
| 14 | + "github.com/galasa-dev/cli/pkg/spi" |
| 15 | + "github.com/galasa-dev/cli/pkg/streams" |
| 16 | + "github.com/galasa-dev/cli/pkg/utils" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +// Objective: Allow user to do this: |
| 21 | +// |
| 22 | +// streams delete |
| 23 | +type StreamsDeleteCommand struct { |
| 24 | + cobraCommand *cobra.Command |
| 25 | +} |
| 26 | + |
| 27 | +// ------------------------------------------------------------------------------------------------ |
| 28 | +// Constructors methods |
| 29 | +// ------------------------------------------------------------------------------------------------ |
| 30 | +func NewStreamsDeleteCommand( |
| 31 | + factory spi.Factory, |
| 32 | + streamsDeleteCommand spi.GalasaCommand, |
| 33 | + commsFlagSet GalasaFlagSet, |
| 34 | +) (spi.GalasaCommand, error) { |
| 35 | + |
| 36 | + cmd := new(StreamsDeleteCommand) |
| 37 | + err := cmd.init(factory, streamsDeleteCommand, commsFlagSet) |
| 38 | + return cmd, err |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +// ------------------------------------------------------------------------------------------------ |
| 43 | +// Public methods |
| 44 | +// ------------------------------------------------------------------------------------------------ |
| 45 | +func (cmd *StreamsDeleteCommand) Name() string { |
| 46 | + return COMMAND_NAME_STREAMS_DELETE |
| 47 | +} |
| 48 | + |
| 49 | +func (cmd *StreamsDeleteCommand) CobraCommand() *cobra.Command { |
| 50 | + return cmd.cobraCommand |
| 51 | +} |
| 52 | + |
| 53 | +func (cmd *StreamsDeleteCommand) Values() interface{} { |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// ------------------------------------------------------------------------------------------------ |
| 58 | +// Private methods |
| 59 | +// ------------------------------------------------------------------------------------------------ |
| 60 | + |
| 61 | +func (cmd *StreamsDeleteCommand) init(factory spi.Factory, streamsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error { |
| 62 | + |
| 63 | + var err error |
| 64 | + |
| 65 | + cmd.cobraCommand, err = cmd.createCobraCmd(factory, streamsCommand, commsFlagSet) |
| 66 | + |
| 67 | + return err |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +func (cmd *StreamsDeleteCommand) createCobraCmd( |
| 72 | + factory spi.Factory, |
| 73 | + streamsCommand spi.GalasaCommand, |
| 74 | + commsFlagSet GalasaFlagSet, |
| 75 | +) (*cobra.Command, error) { |
| 76 | + |
| 77 | + var err error |
| 78 | + |
| 79 | + commsFlagSetValues := commsFlagSet.Values().(*CommsFlagSetValues) |
| 80 | + streamsCommandValues := streamsCommand.Values().(*StreamsCmdValues) |
| 81 | + |
| 82 | + streamsDeleteCobraCmd := &cobra.Command{ |
| 83 | + Use: "delete", |
| 84 | + Short: "Deletes a test stream by name", |
| 85 | + Long: "Deletes a single test stream with the given name from the Galasa service", |
| 86 | + Aliases: []string{COMMAND_NAME_STREAMS_DELETE}, |
| 87 | + RunE: func(cobraCommand *cobra.Command, args []string) error { |
| 88 | + return cmd.executeStreamsDelete( |
| 89 | + factory, streamsCommand.Values().(*StreamsCmdValues), commsFlagSetValues, |
| 90 | + ) |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + addStreamNameFlag(streamsDeleteCobraCmd, true, streamsCommandValues) |
| 95 | + streamsCommand.CobraCommand().AddCommand(streamsDeleteCobraCmd) |
| 96 | + |
| 97 | + return streamsDeleteCobraCmd, err |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +func (cmd *StreamsDeleteCommand) executeStreamsDelete( |
| 102 | + factory spi.Factory, |
| 103 | + streamsCmdValues *StreamsCmdValues, |
| 104 | + commsFlagSetValues *CommsFlagSetValues, |
| 105 | +) error { |
| 106 | + |
| 107 | + var err error |
| 108 | + |
| 109 | + // Operations on the file system will all be relative to the current folder. |
| 110 | + fileSystem := factory.GetFileSystem() |
| 111 | + byteReader := factory.GetByteReader() |
| 112 | + |
| 113 | + err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName) |
| 114 | + |
| 115 | + if err == nil { |
| 116 | + |
| 117 | + commsFlagSetValues.isCapturingLogs = true |
| 118 | + |
| 119 | + log.Println("Galasa CLI - Delete test stream from the Galasa service") |
| 120 | + |
| 121 | + // Get the ability to query environment variables. |
| 122 | + env := factory.GetEnvironment() |
| 123 | + |
| 124 | + var galasaHome spi.GalasaHome |
| 125 | + galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath) |
| 126 | + if err == nil { |
| 127 | + |
| 128 | + var commsClient api.APICommsClient |
| 129 | + commsClient, err = api.NewAPICommsClient( |
| 130 | + commsFlagSetValues.bootstrap, |
| 131 | + commsFlagSetValues.maxRetries, |
| 132 | + commsFlagSetValues.retryBackoffSeconds, |
| 133 | + factory, |
| 134 | + galasaHome, |
| 135 | + ) |
| 136 | + |
| 137 | + if err == nil { |
| 138 | + deleteStreamFunc := func(apiClient *galasaapi.APIClient) error { |
| 139 | + // Call to process the command in a unit-testable way. |
| 140 | + return streams.DeleteStream(streamsCmdValues.name, apiClient, byteReader) |
| 141 | + } |
| 142 | + err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(deleteStreamFunc) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return err |
| 148 | + |
| 149 | +} |
0 commit comments