Merge pull request #131 from sgotti/cmd_projectgroup_update

cmd: add project group update command
This commit is contained in:
Simone Gotti 2019-10-02 09:22:19 +02:00 committed by GitHub
commit 70a49fced7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 5 deletions

View File

@ -75,11 +75,11 @@ func projectGroupCreate(cmd *cobra.Command, args []string) error {
log.Infof("creating project group")
project, _, err := gwclient.CreateProjectGroup(context.TODO(), req)
projectGroup, _, err := gwclient.CreateProjectGroup(context.TODO(), req)
if err != nil {
return errors.Errorf("failed to create project group: %w", err)
}
log.Infof("project group %s created, ID: %s", project.Name, project.ID)
log.Infof("project group %s created, ID: %s", projectGroup.Name, projectGroup.ID)
return nil
}

View File

@ -0,0 +1,89 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"
"github.com/spf13/cobra"
errors "golang.org/x/xerrors"
)
var cmdProjectGroupUpdate = &cobra.Command{
Use: "update",
Short: "update a project group",
Run: func(cmd *cobra.Command, args []string) {
if err := projectGroupUpdate(cmd, args); err != nil {
log.Fatalf("err: %v", err)
}
},
}
type projectGroupUpdateOptions struct {
ref string
name string
parentPath string
visibility string
}
var projectGroupUpdateOpts projectGroupUpdateOptions
func init() {
flags := cmdProjectGroupUpdate.Flags()
flags.StringVarP(&projectGroupUpdateOpts.ref, "ref", "", "", "current project group path or id")
flags.StringVarP(&projectGroupUpdateOpts.name, "name", "n", "", "project group name")
flags.StringVar(&projectGroupUpdateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "user/user01/group01/subgroub01") or project group id where the project group should be moved`)
flags.StringVar(&projectGroupUpdateOpts.visibility, "visibility", "public", `project group visibility (public or private)`)
if err := cmdProjectGroupUpdate.MarkFlagRequired("ref"); err != nil {
log.Fatal(err)
}
cmdProjectGroup.AddCommand(cmdProjectGroupUpdate)
}
func projectGroupUpdate(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)
req := &gwapitypes.UpdateProjectGroupRequest{}
flags := cmd.Flags()
if flags.Changed("name") {
req.Name = &projectGroupUpdateOpts.name
}
if flags.Changed("parent") {
req.ParentRef = &projectGroupUpdateOpts.parentPath
}
if flags.Changed("visibility") {
if !IsValidVisibility(projectGroupUpdateOpts.visibility) {
return errors.Errorf("invalid visibility %q", projectGroupUpdateOpts.visibility)
}
req.Name = &projectGroupUpdateOpts.visibility
}
log.Infof("updating project group")
projectGroup, _, err := gwclient.UpdateProjectGroup(context.TODO(), projectGroupUpdateOpts.ref, req)
if err != nil {
return errors.Errorf("failed to update project group: %w", err)
}
log.Infof("project group %s update, ID: %s", projectGroup.Name, projectGroup.ID)
return nil
}

View File

@ -140,9 +140,20 @@ func (c *Client) CreateProjectGroup(ctx context.Context, req *gwapitypes.CreateP
return nil, nil, err
}
project := new(gwapitypes.ProjectResponse)
resp, err := c.getParsedResponse(ctx, "POST", "/projectgroups", nil, jsonContent, bytes.NewReader(reqj), project)
return project, resp, err
projectGroup := new(gwapitypes.ProjectResponse)
resp, err := c.getParsedResponse(ctx, "POST", "/projectgroups", nil, jsonContent, bytes.NewReader(reqj), projectGroup)
return projectGroup, resp, err
}
func (c *Client) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *gwapitypes.UpdateProjectGroupRequest) (*gwapitypes.ProjectResponse, *http.Response, error) {
reqj, err := json.Marshal(req)
if err != nil {
return nil, nil, err
}
projectGroup := new(gwapitypes.ProjectResponse)
resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projectgroups", url.PathEscape(projectGroupRef)), nil, jsonContent, bytes.NewReader(reqj), projectGroup)
return projectGroup, resp, err
}
func (c *Client) DeleteProjectGroup(ctx context.Context, projectGroupRef string) (*http.Response, error) {