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)
}
20 changes: 8 additions & 12 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 @@ -323,28 +323,25 @@ 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 {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -366,12 +363,11 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{

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

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)
Expand All @@ -380,8 +376,8 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
}

var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
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)
Expand Down
139 changes: 59 additions & 80 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,65 @@ var addGroupCmd = &cobra.Command{
Use: "group",
Aliases: []string{"g"},
Short: "Add a group to lagoon",
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
Run: func(cmd *cobra.Command, args []string) {
groupFlags := parseGroup(*cmd.Flags())
if groupFlags.Name == "" {
fmt.Println("Missing arguments: Group name is not defined")
cmd.Help()
os.Exit(1)
}
var customReqResult []byte
var err error
customReqResult, err = uClient.AddGroup(groupFlags)
handleError(err)
returnResultData := map[string]interface{}{}
err = json.Unmarshal([]byte(customReqResult), &returnResultData)
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)
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Group name", groupName); err != nil {
return err
}
orgOwner, err := cmd.Flags().GetBool("org-owner")
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("organization")
if err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)

if organizationName != "" {
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
groupInput := s.AddGroupToOrganizationInput{
Name: groupName,
Organization: organization.ID,
AddOrgOwner: orgOwner,
}
_, err = l.AddGroupToOrganization(context.TODO(), &groupInput, lc)
handleError(err)
} else {
groupInput := s.AddGroupInput{
Name: groupName,
}
_, err = l.AddGroup(context.TODO(), &groupInput, lc)
handleError(err)
}

resultData := output.Result{
Result: "success",
ResultData: returnResultData,
Result: "success",
ResultData: map[string]interface{}{
"Group Name": groupName,
},
}
if organizationName != "" {
resultData.ResultData["Organization"] = organizationName
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

Expand Down Expand Up @@ -232,69 +272,10 @@ var deleteGroupCmd = &cobra.Command{
},
}

var addGroupToOrganizationCmd = &cobra.Command{
Use: "group",
Aliases: []string{"g"},
Short: "Add a group 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)
orgOwner, err := cmd.Flags().GetBool("org-owner")
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
groupName, err := cmd.Flags().GetString("group")
if err != nil {
return err
}
if err := requiredInputCheck("Group name", groupName); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

groupInput := s.AddGroupToOrganizationInput{
Name: groupName,
Organization: organization.ID,
AddOrgOwner: orgOwner,
}
group := s.OrgGroup{}
err = lc.AddGroupToOrganization(context.TODO(), &groupInput, &group)
handleError(err)

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Group Name": group.Name,
"Organization Name": organizationName,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

func init() {
addGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addGroupCmd.Flags().StringP("name", "N", "", "Name of the group")
addGroupCmd.Flags().StringP("organization", "O", "", "Name of the organization")
addGroupCmd.Flags().Bool("org-owner", false, "Flag to add the user to the group as an owner")
addUserToGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addUserToGroupCmd.Flags().StringVarP(&groupRole, "role", "R", "", "Role in the group [owner, maintainer, developer, reporter, guest]")
addUserToGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
Expand All @@ -303,7 +284,5 @@ func init() {
deleteUserFromGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addGroupToOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization")
addGroupToOrganizationCmd.Flags().StringP("group", "G", "", "Name of the group")
addGroupToOrganizationCmd.Flags().Bool("org-owner", false, "Flag to add the user to the group as an owner")

}
Loading
Loading