-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from galasa-dev/Iss1949
Added new users command to cli
- Loading branch information
Showing
14 changed files
with
685 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## galasactl users | ||
|
||
Manages users in an ecosystem | ||
|
||
### Synopsis | ||
|
||
Allows interaction with the user servlet to return information about users. | ||
|
||
### Options | ||
|
||
``` | ||
-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 | ||
-h, --help Displays the options for the 'users' command. | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--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. | ||
-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. | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [galasactl](galasactl.md) - CLI for Galasa | ||
* [galasactl users get](galasactl_users_get.md) - Get a list of users | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## galasactl users get | ||
|
||
Get a list of users | ||
|
||
### Synopsis | ||
|
||
Get a list of users stored in the Galasa API server | ||
|
||
``` | ||
galasactl users get [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help Displays the options for the 'users get' command. | ||
-i, --id string A mandatory flag that is required to return the currently logged in user.The input must be a string | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-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 | ||
--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. | ||
-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. | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [galasactl users](galasactl_users.md) - Manages users in an ecosystem | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/galasa-dev/cli/pkg/spi" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type UsersCmdValues struct { | ||
ecosystemBootstrap string | ||
name string | ||
} | ||
|
||
type UsersCommand struct { | ||
values *UsersCmdValues | ||
cobraCommand *cobra.Command | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Constructors methods | ||
// ------------------------------------------------------------------------------------------------ | ||
func NewUsersCommand(rootCmd spi.GalasaCommand) (spi.GalasaCommand, error) { | ||
|
||
cmd := new(UsersCommand) | ||
err := cmd.init(rootCmd) | ||
return cmd, err | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Public methods | ||
// ------------------------------------------------------------------------------------------------ | ||
func (cmd *UsersCommand) Name() string { | ||
return COMMAND_NAME_USERS | ||
} | ||
|
||
func (cmd *UsersCommand) CobraCommand() *cobra.Command { | ||
return cmd.cobraCommand | ||
} | ||
|
||
func (cmd *UsersCommand) Values() interface{} { | ||
return cmd.values | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Private methods | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
func (cmd *UsersCommand) init(rootCmd spi.GalasaCommand) error { | ||
|
||
var err error | ||
|
||
cmd.values = &UsersCmdValues{} | ||
cmd.cobraCommand = cmd.createCobraCommand(rootCmd) | ||
|
||
return err | ||
} | ||
|
||
func (cmd *UsersCommand) createCobraCommand( | ||
rootCommand spi.GalasaCommand, | ||
) *cobra.Command { | ||
|
||
usersCobraCmd := &cobra.Command{ | ||
Use: "users", | ||
Short: "Manages users in an ecosystem", | ||
Long: "Allows interaction with the user servlet to return information about users.", | ||
} | ||
|
||
addBootstrapFlag(usersCobraCmd, &cmd.values.ecosystemBootstrap) | ||
|
||
rootCommand.CobraCommand().AddCommand(usersCobraCmd) | ||
|
||
return usersCobraCmd | ||
} | ||
|
||
func addLoginIdFlag(cmd *cobra.Command, isMandatory bool, userCmdValues *UsersCmdValues) { | ||
|
||
flagName := "id" | ||
var description string | ||
if isMandatory { | ||
description = "A mandatory flag that is required to return the currently logged in user." | ||
} else { | ||
description = "An optional flag that is required to return the currently logged in user." | ||
} | ||
description += "The input must be a string" | ||
|
||
cmd.PersistentFlags().StringVarP(&userCmdValues.name, flagName, "i", "", description) | ||
|
||
if isMandatory { | ||
cmd.MarkPersistentFlagRequired(flagName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/galasa-dev/cli/pkg/api" | ||
"github.com/galasa-dev/cli/pkg/galasaapi" | ||
"github.com/galasa-dev/cli/pkg/spi" | ||
"github.com/galasa-dev/cli/pkg/users" | ||
"github.com/galasa-dev/cli/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Objective: Allow user to do this: | ||
// | ||
// users get | ||
type UsersGetCommand struct { | ||
cobraCommand *cobra.Command | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Constructors methods | ||
// ------------------------------------------------------------------------------------------------ | ||
func NewUsersGetCommand( | ||
factory spi.Factory, | ||
usersGetCommand spi.GalasaCommand, | ||
rootCmd spi.GalasaCommand, | ||
) (spi.GalasaCommand, error) { | ||
|
||
cmd := new(UsersGetCommand) | ||
|
||
err := cmd.init(factory, usersGetCommand, rootCmd) | ||
return cmd, err | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Public methods | ||
// ------------------------------------------------------------------------------------------------ | ||
func (cmd *UsersGetCommand) Name() string { | ||
return COMMAND_NAME_USERS_GET | ||
} | ||
|
||
func (cmd *UsersGetCommand) CobraCommand() *cobra.Command { | ||
return cmd.cobraCommand | ||
} | ||
|
||
func (cmd *UsersGetCommand) Values() interface{} { | ||
// There are no values. | ||
return nil | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Private methods | ||
// ------------------------------------------------------------------------------------------------ | ||
func (cmd *UsersGetCommand) init(factory spi.Factory, usersCommand spi.GalasaCommand, rootCmd spi.GalasaCommand) error { | ||
var err error | ||
|
||
cmd.cobraCommand, err = cmd.createCobraCmd(factory, usersCommand, rootCmd) | ||
|
||
return err | ||
} | ||
|
||
func (cmd *UsersGetCommand) createCobraCmd( | ||
factory spi.Factory, | ||
usersCommand, | ||
rootCmd spi.GalasaCommand, | ||
) (*cobra.Command, error) { | ||
|
||
var err error | ||
|
||
userCommandValues := usersCommand.Values().(*UsersCmdValues) | ||
usersGetCobraCmd := &cobra.Command{ | ||
Use: "get", | ||
Short: "Get a list of users", | ||
Long: "Get a list of users stored in the Galasa API server", | ||
Aliases: []string{COMMAND_NAME_USERS_GET}, | ||
RunE: func(cobraCommand *cobra.Command, args []string) error { | ||
return cmd.executeUsersGet(factory, usersCommand.Values().(*UsersCmdValues), rootCmd.Values().(*RootCmdValues)) | ||
}, | ||
} | ||
|
||
addLoginIdFlag(usersGetCobraCmd, true, userCommandValues) | ||
|
||
usersCommand.CobraCommand().AddCommand(usersGetCobraCmd) | ||
|
||
return usersGetCobraCmd, err | ||
} | ||
|
||
func (cmd *UsersGetCommand) executeUsersGet( | ||
factory spi.Factory, | ||
userCmdValues *UsersCmdValues, | ||
rootCmdValues *RootCmdValues, | ||
) error { | ||
|
||
var err error | ||
// Operations on the file system will all be relative to the current folder. | ||
fileSystem := factory.GetFileSystem() | ||
|
||
err = utils.CaptureLog(fileSystem, rootCmdValues.logFileName) | ||
|
||
if err == nil { | ||
rootCmdValues.isCapturingLogs = true | ||
|
||
log.Println("Galasa CLI - Get users from the ecosystem") | ||
|
||
// Get the ability to query environment variables. | ||
env := factory.GetEnvironment() | ||
|
||
var galasaHome spi.GalasaHome | ||
galasaHome, err = utils.NewGalasaHome(fileSystem, env, rootCmdValues.CmdParamGalasaHomePath) | ||
if err == nil { | ||
|
||
// Read the bootstrap users. | ||
var urlService *api.RealUrlResolutionService = new(api.RealUrlResolutionService) | ||
var bootstrapData *api.BootstrapData | ||
bootstrapData, err = api.LoadBootstrap(galasaHome, fileSystem, env, userCmdValues.ecosystemBootstrap, urlService) | ||
if err == nil { | ||
|
||
var console = factory.GetStdOutConsole() | ||
|
||
apiServerUrl := bootstrapData.ApiServerURL | ||
log.Printf("The API server is at '%s'\n", apiServerUrl) | ||
|
||
authenticator := factory.GetAuthenticator( | ||
apiServerUrl, | ||
galasaHome, | ||
) | ||
|
||
var apiClient *galasaapi.APIClient | ||
apiClient, err = authenticator.GetAuthenticatedAPIClient() | ||
|
||
if err == nil { | ||
// Call to process the command in a unit-testable way. | ||
err = users.GetUsers(userCmdValues.name, apiClient, console) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.