|
| 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 | +type StreamsGetCmdValues struct { |
| 21 | + outputFormat string |
| 22 | +} |
| 23 | + |
| 24 | +// Objective: Allow user to do this: |
| 25 | +// |
| 26 | +// streams get |
| 27 | +type StreamsGetCommand struct { |
| 28 | + cobraCommand *cobra.Command |
| 29 | + values *StreamsGetCmdValues |
| 30 | +} |
| 31 | + |
| 32 | +// ------------------------------------------------------------------------------------------------ |
| 33 | +// Constructors methods |
| 34 | +// ------------------------------------------------------------------------------------------------ |
| 35 | +func NewStreamsGetCommand( |
| 36 | + factory spi.Factory, |
| 37 | + streamsGetCommand spi.GalasaCommand, |
| 38 | + commsFlagSet GalasaFlagSet, |
| 39 | +) (spi.GalasaCommand, error) { |
| 40 | + |
| 41 | + cmd := new(StreamsGetCommand) |
| 42 | + err := cmd.init(factory, streamsGetCommand, commsFlagSet) |
| 43 | + return cmd, err |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +// ------------------------------------------------------------------------------------------------ |
| 48 | +// Public methods |
| 49 | +// ------------------------------------------------------------------------------------------------ |
| 50 | +func (cmd *StreamsGetCommand) Name() string { |
| 51 | + return COMMAND_NAME_STREAMS_GET |
| 52 | +} |
| 53 | + |
| 54 | +func (cmd *StreamsGetCommand) CobraCommand() *cobra.Command { |
| 55 | + return cmd.cobraCommand |
| 56 | +} |
| 57 | + |
| 58 | +func (cmd *StreamsGetCommand) Values() interface{} { |
| 59 | + return cmd.values |
| 60 | +} |
| 61 | + |
| 62 | +// ------------------------------------------------------------------------------------------------ |
| 63 | +// Private methods |
| 64 | +// ------------------------------------------------------------------------------------------------ |
| 65 | +func (cmd *StreamsGetCommand) init(factory spi.Factory, streamsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error { |
| 66 | + |
| 67 | + var err error |
| 68 | + cmd.values = &StreamsGetCmdValues{} |
| 69 | + cmd.cobraCommand, err = cmd.createCobraCmd(factory, streamsCommand, commsFlagSet) |
| 70 | + return err |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +func (cmd *StreamsGetCommand) createCobraCmd( |
| 75 | + factory spi.Factory, |
| 76 | + streamsCommand spi.GalasaCommand, |
| 77 | + commsFlagSet GalasaFlagSet, |
| 78 | +) (*cobra.Command, error) { |
| 79 | + |
| 80 | + var err error |
| 81 | + |
| 82 | + commsFlagSetValues := commsFlagSet.Values().(*CommsFlagSetValues) |
| 83 | + streamCommandValues := streamsCommand.Values().(*StreamsCmdValues) |
| 84 | + |
| 85 | + streamsGetCobraCmd := &cobra.Command{ |
| 86 | + Use: "get", |
| 87 | + Short: "Gets a list of test streams", |
| 88 | + Long: "Get a list of test streams from the Galasa service", |
| 89 | + Aliases: []string{COMMAND_NAME_STREAMS_GET}, |
| 90 | + RunE: func(cobraCommand *cobra.Command, args []string) error { |
| 91 | + return cmd.executeStreamsGet( |
| 92 | + factory, streamsCommand.Values().(*StreamsCmdValues), commsFlagSetValues, |
| 93 | + ) |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + addStreamNameFlag(streamsGetCobraCmd, false, streamCommandValues) |
| 98 | + |
| 99 | + formatters := streams.GetFormatterNamesAsString() |
| 100 | + streamsGetCobraCmd.Flags().StringVar(&cmd.values.outputFormat, "format", "summary", "the output format of the returned streams. Supported formats are: "+formatters+".") |
| 101 | + |
| 102 | + streamsCommand.CobraCommand().AddCommand(streamsGetCobraCmd) |
| 103 | + |
| 104 | + return streamsGetCobraCmd, err |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +func (cmd *StreamsGetCommand) executeStreamsGet( |
| 109 | + factory spi.Factory, |
| 110 | + streamsCmdValues *StreamsCmdValues, |
| 111 | + commsFlagSetValues *CommsFlagSetValues, |
| 112 | +) error { |
| 113 | + |
| 114 | + var err error |
| 115 | + |
| 116 | + // Operations on the file system will all be relative to the current folder. |
| 117 | + fileSystem := factory.GetFileSystem() |
| 118 | + |
| 119 | + err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName) |
| 120 | + if err == nil { |
| 121 | + |
| 122 | + commsFlagSetValues.isCapturingLogs = true |
| 123 | + log.Println("Galasa CLI - Get streams from the Galasa service") |
| 124 | + |
| 125 | + env := factory.GetEnvironment() |
| 126 | + |
| 127 | + var galasaHome spi.GalasaHome |
| 128 | + galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath) |
| 129 | + |
| 130 | + if err == nil { |
| 131 | + |
| 132 | + var commsClient api.APICommsClient |
| 133 | + commsClient, err = api.NewAPICommsClient( |
| 134 | + commsFlagSetValues.bootstrap, |
| 135 | + commsFlagSetValues.maxRetries, |
| 136 | + commsFlagSetValues.retryBackoffSeconds, |
| 137 | + factory, |
| 138 | + galasaHome, |
| 139 | + ) |
| 140 | + |
| 141 | + if err == nil { |
| 142 | + |
| 143 | + var console = factory.GetStdOutConsole() |
| 144 | + var byteReader = factory.GetByteReader() |
| 145 | + |
| 146 | + getStreamsFunc := func(apiClient *galasaapi.APIClient) error { |
| 147 | + return streams.GetStreams(streamsCmdValues.name, cmd.values.outputFormat, apiClient, console,byteReader) |
| 148 | + } |
| 149 | + |
| 150 | + err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(getStreamsFunc) |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + return err |
| 159 | +} |
0 commit comments