From 42908abfd638b425f3725d79c3b748581905128a Mon Sep 17 00:00:00 2001 From: berkgokden Date: Fri, 3 May 2019 16:08:01 +0200 Subject: [PATCH] Added Edit command LAM-227 --- client/client.go | 48 ++++++++++++++++++++++ cmd/edit.go | 105 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 +- models/models.go | 4 ++ 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 cmd/edit.go diff --git a/client/client.go b/client/client.go index 5ff093b..0ab36de 100644 --- a/client/client.go +++ b/client/client.go @@ -350,6 +350,54 @@ func (s *restClient) Delete(resourceName string, name string, values map[string] } +func (s *restClient) GetSpec(resourceName string, name string, outputFormat string, values map[string]string) (string, error) { + url, _ := getUrlForResource((*s).url, (*s).version, resourceName, "", name, values) + + resp, getResourceError := resty.R(). + SetHeader("Content-Type", "application/json"). + SetHeader("Accept", "application/json"). + SetAuthToken((*s).token). + SetError(&errorResponse{}). + Get(url) + + if getResourceError != nil { + return "", getResourceError + } + + if resp.IsError() { + return "", getError(resp) + } + + var withSpec models.WithSpecification + unmarshalError := json.Unmarshal(resp.Body(), &withSpec) + if unmarshalError != nil { + return "", unmarshalError + } + + specification, marshallSpecError := json.Marshal(withSpec.Specification) + if marshallSpecError != nil { + return "", marshallSpecError + } + + source := "" + if outputFormat == "yaml" { + yaml, err_2 := yaml.JSONToYAML(specification) + if err_2 != nil { + return "", err_2 + } + source = string(yaml) + } else { + var prettyJSON bytes.Buffer + error := json.Indent(&prettyJSON, specification, "", " ") + if error != nil { + return "", error + } + source = string(prettyJSON.Bytes()) + } + return source, nil + +} + func (s *restClient) Get(resourceName string, name string, outputFormat string, values map[string]string) (string, error) { url, _ := getUrlForResource((*s).url, (*s).version, resourceName, "", name, values) diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..41c1a5f --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,105 @@ +// Copyright © 2018 Developer developer@vamp.io +// +// 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 ( + "errors" + "fmt" + "io/ioutil" + "os" + "os/exec" + + "github.com/magneticio/vampkubistcli/client" + "github.com/magneticio/vampkubistcli/logging" + "github.com/magneticio/vampkubistcli/util" + "github.com/spf13/cobra" +) + +// editCmd represents the edit command +var editCmd = &cobra.Command{ + Use: "edit", + Short: "Edits a resource", + Long: AddAppName(`To edit a resource +Run as $AppName edit resourceType ResourceName + +Example: + $AppName edit project myproject + $AppName edit -p myproject cluster mycluster`), + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) < 2 { + return errors.New("Not Enough Arguments") + } + Type = args[0] + Name = args[1] + restClient := client.NewRestClient(Config.Url, Config.Token, Config.APIVersion, logging.Verbose, Config.Cert) + values := make(map[string]string) + values["project"] = Config.Project + values["cluster"] = Config.Cluster + values["virtual_cluster"] = Config.VirtualCluster + values["application"] = Application + spec, getSpecError := restClient.GetSpec(Type, Name, SourceFileType, values) + if getSpecError != nil { + return getSpecError + } + + vi := "vim" + tmpDir := os.TempDir() + tmpFile, tmpFileErr := ioutil.TempFile(tmpDir, "vampkubist") + if tmpFileErr != nil { + return tmpFileErr + } + wrriteTempFileError := ioutil.WriteFile(tmpFile.Name(), []byte(spec), 0644) + if wrriteTempFileError != nil { + return wrriteTempFileError + } + path, err := exec.LookPath(vi) + if err != nil { + fmt.Printf("Error %s while looking up for %s!!", path, vi) + } + + editCommand := exec.Command(path, tmpFile.Name()) + editCommand.Stdin = os.Stdin + editCommand.Stdout = os.Stdout + editCommand.Stderr = os.Stderr + editCommandErr := editCommand.Start() + if editCommandErr != nil { + return editCommandErr + } + editCommandWaitError := editCommand.Wait() + if editCommandWaitError != nil { + return editCommandWaitError + } + + b, readTempFileError := util.UseSourceUrl(tmpFile.Name()) + if readTempFileError != nil { + return readTempFileError + } + Source := string(b) + isUpdated, updateError := restClient.Update(Type, Name, Source, SourceFileType, values) + if !isUpdated { + return updateError + } + fmt.Println(Type + " " + Name + " is edited") + return nil + }, +} + +func init() { + rootCmd.AddCommand(editCmd) + + editCmd.Flags().StringVarP(&SourceFileType, "input", "i", "yaml", "Resource file type yaml or json") +} diff --git a/cmd/root.go b/cmd/root.go index 17cd16b..b14dd8f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,7 +60,7 @@ var Hosts []string var kubeConfigPath string // version should be in format d.d.d where d is a decimal number -const Version string = "v0.0.26" +const Version string = "v0.0.27" var AppName string = InitAppName() diff --git a/models/models.go b/models/models.go index 89de38d..920b7a4 100644 --- a/models/models.go +++ b/models/models.go @@ -25,6 +25,10 @@ type Named struct { Name string `json:"name"` } +type WithSpecification struct { + Specification map[string]interface{} `json:"specification"` +} + type Versioned struct { Version string `json:"version"` }