Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 5a0820f

Browse files
authored
Merge pull request #356 from galasa-dev/ash-streams-cli
CLI now supports streams command
2 parents fc876ed + 2d8c561 commit 5a0820f

19 files changed

+1488
-4
lines changed

docs/generated/errors-list.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ The `galasactl` tool can generate the following errors:
229229
- GAL1230E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
230230
- GAL1231E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are: '{}'
231231
- GAL1232E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
232+
- GAL1233E: The stream name provided by the --name field cannot be an empty string.
233+
- GAL1234E: Could not get list of test streams from API server. Reason: '{}'.
234+
- GAL1235E: The name provided with the --name flag cannot be empty and must only contain characters in the following ranges: 'a'-'z', 'A'-'Z', '0'-'9', '-' (dash), '_' (underscore).
235+
- GAL1236E: Failed to get streams. Unexpected http status code {} received from the server.
236+
- GAL1237E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
237+
- GAL1238E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
238+
- GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}'
239+
- GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
232240
- GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '{}', and 'pre-release' repository is '{}'
233241
- GAL2501I: Downloaded {} artifacts to folder '{}'
234242

docs/generated/galasactl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ A tool for controlling Galasa resources using the command-line.
2525
* [galasactl roles](galasactl_roles.md) - Manage roles stored in the Galasa service
2626
* [galasactl runs](galasactl_runs.md) - Manage test runs in the ecosystem
2727
* [galasactl secrets](galasactl_secrets.md) - Manage secrets stored in the Galasa service's credentials store
28+
* [galasactl streams](galasactl_streams.md) - Manages test streams in a Galasa service
2829
* [galasactl users](galasactl_users.md) - Manages users in an ecosystem
2930

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## galasactl streams
2+
3+
Manages test streams in a Galasa service
4+
5+
### Synopsis
6+
7+
Parent command for managing test streams in a Galasa service
8+
9+
### Options
10+
11+
```
12+
-b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
13+
-h, --help Displays the options for the 'streams' command.
14+
--rate-limit-retries int The maximum number of retries that should be made when requests to the Galasa Service fail due to rate limits being exceeded. Must be a whole number. Defaults to 3 retries (default 3)
15+
--rate-limit-retry-backoff-secs float The amount of time in seconds to wait before retrying a command if it failed due to rate limits being exceeded. Defaults to 1 second. (default 1)
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```
21+
--galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
22+
-l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.
23+
```
24+
25+
### SEE ALSO
26+
27+
* [galasactl](galasactl.md) - CLI for Galasa
28+
* [galasactl streams get](galasactl_streams_get.md) - Gets a list of test streams
29+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## galasactl streams get
2+
3+
Gets a list of test streams
4+
5+
### Synopsis
6+
7+
Get a list of test streams from the Galasa service
8+
9+
```
10+
galasactl streams get [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
--format string the output format of the returned streams. Supported formats are: 'summary', 'yaml'. (default "summary")
17+
-h, --help Displays the options for the 'streams get' command.
18+
--name string An optional field indicating the name of a test stream
19+
```
20+
21+
### Options inherited from parent commands
22+
23+
```
24+
-b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
25+
--galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
26+
-l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.
27+
--rate-limit-retries int The maximum number of retries that should be made when requests to the Galasa Service fail due to rate limits being exceeded. Must be a whole number. Defaults to 3 retries (default 3)
28+
--rate-limit-retry-backoff-secs float The amount of time in seconds to wait before retrying a command if it failed due to rate limits being exceeded. Defaults to 1 second. (default 1)
29+
```
30+
31+
### SEE ALSO
32+
33+
* [galasactl streams](galasactl_streams.md) - Manages test streams in a Galasa service
34+

pkg/cmd/commandCollection.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const (
7272
COMMAND_NAME_USERS_DELETE = "users delete"
7373
COMMAND_NAME_ROLES = "roles"
7474
COMMAND_NAME_ROLES_GET = "roles get"
75+
COMMAND_NAME_STREAMS = "streams"
76+
COMMAND_NAME_STREAMS_GET = "streams get"
7577
)
7678

7779
// -----------------------------------------------------------------
@@ -172,6 +174,10 @@ func (commands *commandCollectionImpl) init(factory spi.Factory) error {
172174
err = commands.addRolesCommands(factory, rootCommand, commsFlagSet)
173175
}
174176

177+
if err == nil {
178+
err = commands.addStreamsCommands(factory, rootCommand, commsFlagSet)
179+
}
180+
175181
if err == nil {
176182
commands.setHelpFlags()
177183
}
@@ -508,6 +514,26 @@ func (commands *commandCollectionImpl) addRolesCommands(factory spi.Factory, roo
508514
return err
509515
}
510516

517+
func (commands *commandCollectionImpl) addStreamsCommands(factory spi.Factory, rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
518+
519+
var err error
520+
var streamsCommand spi.GalasaCommand
521+
var streamsGetCommand spi.GalasaCommand
522+
523+
streamsCommand, err = NewStreamsCommand(rootCommand, commsFlagSet)
524+
525+
if err == nil {
526+
streamsGetCommand, err = NewStreamsGetCommand(factory, streamsCommand, commsFlagSet)
527+
if err == nil {
528+
commands.commandMap[streamsCommand.Name()] = streamsCommand
529+
commands.commandMap[streamsGetCommand.Name()] = streamsGetCommand
530+
}
531+
}
532+
533+
return err
534+
535+
}
536+
511537
func (commands *commandCollectionImpl) setHelpFlags() {
512538
for _, command := range commands.commandMap {
513539
command.CobraCommand().Flags().BoolP("help", "h", false, "Displays the options for the '"+command.Name()+"' command.")

pkg/cmd/streams.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
"github.com/galasa-dev/cli/pkg/spi"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
type StreamsCmdValues struct {
15+
name string
16+
}
17+
18+
type StreamsCommand struct {
19+
values *StreamsCmdValues
20+
cobraCommand *cobra.Command
21+
}
22+
23+
// ------------------------------------------------------------------------------------------------
24+
// Constructors methods
25+
// ------------------------------------------------------------------------------------------------
26+
func NewStreamsCommand(rootCmd spi.GalasaCommand, commsFlagSet GalasaFlagSet) (spi.GalasaCommand, error) {
27+
28+
cmd := new(StreamsCommand)
29+
err := cmd.init(rootCmd, commsFlagSet)
30+
return cmd, err
31+
32+
}
33+
34+
// ------------------------------------------------------------------------------------------------
35+
// Public methods
36+
// ------------------------------------------------------------------------------------------------
37+
func (cmd *StreamsCommand) Name() string {
38+
return COMMAND_NAME_STREAMS
39+
}
40+
41+
func (cmd *StreamsCommand) CobraCommand() *cobra.Command {
42+
return cmd.cobraCommand
43+
}
44+
45+
func (cmd *StreamsCommand) Values() interface{} {
46+
return cmd.values
47+
}
48+
49+
// ------------------------------------------------------------------------------------------------
50+
// Private methods
51+
// ------------------------------------------------------------------------------------------------
52+
53+
func (cmd *StreamsCommand) init(rootCmd spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
54+
55+
var err error
56+
cmd.values = &StreamsCmdValues{}
57+
cmd.cobraCommand = cmd.createCobraCommand(rootCmd, commsFlagSet)
58+
59+
return err
60+
61+
}
62+
63+
func (cmd *StreamsCommand) createCobraCommand(
64+
rootCommand spi.GalasaCommand,
65+
commsFlagSet GalasaFlagSet,
66+
) *cobra.Command {
67+
68+
streamsCobraCmd := &cobra.Command{
69+
Use: "streams",
70+
Short: "Manages test streams in a Galasa service",
71+
Long: "Parent command for managing test streams in a Galasa service",
72+
}
73+
74+
streamsCobraCmd.PersistentFlags().AddFlagSet(commsFlagSet.Flags())
75+
rootCommand.CobraCommand().AddCommand(streamsCobraCmd)
76+
77+
return streamsCobraCmd
78+
79+
}
80+
81+
func addStreamNameFlag(cmd *cobra.Command, isMandatory bool, streamCmdValues *StreamsCmdValues) {
82+
83+
flagName := "name"
84+
var description string
85+
86+
if isMandatory {
87+
description = "A mandatory field indicating the name of a test stream."
88+
} else {
89+
description = "An optional field indicating the name of a test stream"
90+
}
91+
92+
cmd.Flags().StringVar(&streamCmdValues.name, flagName, "", description)
93+
94+
if isMandatory {
95+
cmd.MarkFlagRequired(flagName)
96+
}
97+
98+
}

pkg/cmd/streamsGet.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)