cmd: implement variable update
This commit is contained in:
parent
49c1c263fd
commit
8c2f37d47c
|
@ -0,0 +1,50 @@
|
|||
// 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 (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cmdProjectGroupVariableUpdate = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "update a project group variable",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := variableUpdate(cmd, "projectgroup", args); err != nil {
|
||||
log.Fatalf("err: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
flags := cmdProjectGroupVariableUpdate.Flags()
|
||||
|
||||
flags.StringVar(&variableUpdateOpts.parentRef, "projectgroup", "", "project group id or full path")
|
||||
flags.StringVarP(&variableUpdateOpts.name, "name", "n", "", "variable name")
|
||||
flags.StringVarP(&variableUpdateOpts.newName, "new-name", "", "", "variable new name")
|
||||
flags.StringVarP(&variableUpdateOpts.file, "file", "f", "", `yaml file containing the variable definition (use "-" to read from stdin`)
|
||||
|
||||
if err := cmdProjectGroupVariableUpdate.MarkFlagRequired("projectgroup"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmdProjectGroupVariableUpdate.MarkFlagRequired("name"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmdProjectGroupVariableUpdate.MarkFlagRequired("file"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cmdProjectGroupVariable.AddCommand(cmdProjectGroupVariableUpdate)
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
// 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"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"agola.io/agola/internal/services/gateway/api"
|
||||
"agola.io/agola/internal/services/types"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/spf13/cobra"
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
var cmdProjectVariableUpdate = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "update a project variable",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := variableUpdate(cmd, "project", args); err != nil {
|
||||
log.Fatalf("err: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
type variableUpdateOptions struct {
|
||||
parentRef string
|
||||
name string
|
||||
newName string
|
||||
file string
|
||||
}
|
||||
|
||||
var variableUpdateOpts variableUpdateOptions
|
||||
|
||||
func init() {
|
||||
flags := cmdProjectVariableUpdate.Flags()
|
||||
|
||||
flags.StringVar(&variableUpdateOpts.parentRef, "project", "", "project id or full path")
|
||||
flags.StringVarP(&variableUpdateOpts.name, "name", "n", "", "variable name")
|
||||
flags.StringVarP(&variableUpdateOpts.newName, "new-name", "", "", "variable new name")
|
||||
flags.StringVarP(&variableUpdateOpts.file, "file", "f", "", `yaml file containing the variable definition (use "-" to read from stdin`)
|
||||
|
||||
if err := cmdProjectVariableUpdate.MarkFlagRequired("project"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmdProjectVariableUpdate.MarkFlagRequired("name"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmdProjectVariableUpdate.MarkFlagRequired("file"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cmdProjectVariable.AddCommand(cmdProjectVariableUpdate)
|
||||
}
|
||||
|
||||
func variableUpdate(cmd *cobra.Command, ownertype string, args []string) error {
|
||||
gwclient := api.NewClient(gatewayURL, token)
|
||||
|
||||
// "github.com/ghodss/yaml" doesn't provide a streaming decoder
|
||||
var data []byte
|
||||
var err error
|
||||
if variableUpdateOpts.file == "-" {
|
||||
data, err = ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
data, err = ioutil.ReadFile(variableUpdateOpts.file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var values []VariableValue
|
||||
if err := yaml.Unmarshal(data, &values); err != nil {
|
||||
log.Fatalf("failed to unmarshall values: %v", err)
|
||||
}
|
||||
rvalues := []types.VariableValue{}
|
||||
for _, value := range values {
|
||||
rvalues = append(rvalues, types.VariableValue{
|
||||
SecretName: value.SecretName,
|
||||
SecretVar: value.SecretVar,
|
||||
When: (*types.When)(value.When),
|
||||
})
|
||||
}
|
||||
req := &api.UpdateVariableRequest{
|
||||
Name: variableUpdateOpts.name,
|
||||
Values: rvalues,
|
||||
}
|
||||
|
||||
flags := cmd.Flags()
|
||||
if flags.Changed("new-name") {
|
||||
req.Name = variableUpdateOpts.newName
|
||||
}
|
||||
|
||||
switch ownertype {
|
||||
case "project":
|
||||
log.Infof("updating project variable")
|
||||
variable, _, err := gwclient.UpdateProjectVariable(context.TODO(), variableUpdateOpts.parentRef, variableUpdateOpts.name, req)
|
||||
if err != nil {
|
||||
return errors.Errorf("failed to update project variable: %w", err)
|
||||
}
|
||||
log.Infof("project variable %q updated, ID: %q", variable.Name, variable.ID)
|
||||
case "projectgroup":
|
||||
log.Infof("updating project group variable")
|
||||
variable, _, err := gwclient.UpdateProjectGroupVariable(context.TODO(), variableUpdateOpts.parentRef, variableUpdateOpts.name, req)
|
||||
if err != nil {
|
||||
return errors.Errorf("failed to update project group variable: %w", err)
|
||||
}
|
||||
log.Infof("project group variable %q updated, ID: %q", variable.Name, variable.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue