Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: restructure organization commands #332

Merged
merged 14 commits into from
Jun 4, 2024
11 changes: 2 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ var addNotificationCmd = &cobra.Command{
},
}

var addOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
addCmd.AddCommand(addDeployTargetCmd)
addCmd.AddCommand(addGroupCmd)
Expand All @@ -43,4 +34,6 @@ func init() {
addCmd.AddCommand(addUserSSHKeyCmd)
addCmd.AddCommand(addVariableCmd)
addCmd.AddCommand(addDeployTargetConfigCmd)
addCmd.AddCommand(addDeployTargetToOrganizationCmd)
addCmd.AddCommand(addAdministratorToOrganizationCmd)
}
12 changes: 3 additions & 9 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ var deleteNotificationCmd = &cobra.Command{
},
}

var deleteOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
deleteCmd.AddCommand(deleteEnvCmd)
deleteCmd.AddCommand(deleteGroupCmd)
Expand All @@ -44,4 +35,7 @@ func init() {
deleteCmd.AddCommand(deleteVariableCmd)
deleteCmd.AddCommand(deleteDeployTargetConfigCmd)
deleteCmd.AddCommand(deleteOrganizationCmd)
deleteCmd.AddCommand(removeDeployTargetFromOrganizationCmd)
deleteCmd.AddCommand(removeAdministratorFromOrganizationCmd)
deleteCmd.AddCommand(removeProjectFromOrganizationCmd)
}
64 changes: 36 additions & 28 deletions cmd/deploytarget.go
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,27 @@ var deleteDeployTargetCmd = &cobra.Command{
}

var addDeployTargetToOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
Use: "organization-deploytarget",
Aliases: []string{"org-dt"},
Short: "Add a deploy target to an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)

organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
deploytarget, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {

if err := requiredInputCheck("Organization name", organizationName, "Deploy Target", strconv.Itoa(int(deploytarget))); err != nil {
return err
}

Expand All @@ -359,38 +358,43 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)
if err := handleErr(err); err != nil {
return nil
}

deployTargetInput := s.AddDeployTargetToOrganizationInput{
DeployTarget: deployTarget,
DeployTarget: deploytarget,
Organization: organization.ID,
}

deployTargetResponse, err := l.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)

if err := handleErr(err); err != nil {
return nil
}
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTargetResponse.Name,
"Organization Name": organizationName,
"Deploy Target": deploytarget,
"Organization Name": deployTargetResponse.Name,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
var removeDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "organization-deploytarget",
Aliases: []string{"org-dt"},
Short: "Remove a deploy target from an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)
if err != nil {
return err
}

organizationName, err := cmd.Flags().GetString("name")
if err != nil {
Expand All @@ -399,11 +403,11 @@ var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
deploytarget, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deploytarget))); err != nil {
return err
}

Expand All @@ -417,20 +421,24 @@ var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)
if err := handleErr(err); err != nil {
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
DeployTarget: deployTarget,
DeployTarget: deploytarget,
Organization: organization.ID,
}

if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deployTarget, organization.Name)) {
if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deploytarget, organization.Name)) {
_, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)
if err := handleErr(err); err != nil {
return nil
}
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTarget,
"Deploy Target": deploytarget,
"Organization Name": organizationName,
},
}
Expand All @@ -454,13 +462,13 @@ func init() {
addDeployTargetCmd.Flags().StringP("build-image", "", "", "DeployTarget build image to use (if different to the default)")

addDeployTargetToOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
addDeployTargetToOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")
addDeployTargetToOrganizationCmd.Flags().UintP("deploytarget", "D", 0, "ID of DeployTarget")

deleteDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
deleteDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget")

RemoveDeployTargetFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
RemoveDeployTargetFromOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")
removeDeployTargetFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to organization to match other flags in other commands. There may be other organization commands that use name, but if they can be updated to be organization that'd be wicked. That way there is no confusion across the flagset for organizations.

It was initially confusing to me when i went to use --name in one place (lagoon list organization-users --organization ${orgname}) only to find it was --organization and then vice versa when using a different command (lagoon delete organization-deploytarget --name ${orgname}).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only one that should be --name is maybe add organization.

Alternatively, if the flag is changed to organization-name then we cover all possibly cases on all commands

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've changed the flag to organization-name.

removeDeployTargetFromOrganizationCmd.Flags().UintP("deploytarget", "D", 0, "ID of DeployTarget")

updateDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
updateDeployTargetCmd.Flags().StringP("console-url", "", "", "DeployTarget console URL")
Expand Down
4 changes: 2 additions & 2 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var updateEnvironmentCmd = &cobra.Command{
if err != nil {
return err
}
openShift, err := cmd.Flags().GetUint("deploy-target")
openShift, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func init() {
updateEnvironmentCmd.Flags().String("route", "", "Update the route for the selected environment")
updateEnvironmentCmd.Flags().String("routes", "", "Update the routes for the selected environment")
updateEnvironmentCmd.Flags().UintVarP(&environmentAutoIdle, "auto-idle", "a", 1, "Auto idle setting of the environment")
updateEnvironmentCmd.Flags().UintP("deploy-target", "d", 0, "Reference to OpenShift Object this Environment should be deployed to")
updateEnvironmentCmd.Flags().UintP("deploytarget", "d", 0, "Reference to OpenShift Object this Environment should be deployed to")
updateEnvironmentCmd.Flags().String("environment-type", "", "Update the environment type - production | development")
updateEnvironmentCmd.Flags().String("deploy-type", "", "Update the deploy type - branch | pullrequest | promote")
}
4 changes: 3 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ var getOrganizationCmd = &cobra.Command{
&token,
debug)
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)
if err != nil {
return err
}

if organization.Name == "" {
output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions)
Expand Down
Loading
Loading