Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
Added Edit command LAM-227
Browse files Browse the repository at this point in the history
  • Loading branch information
bgokden committed May 3, 2019
1 parent 81b01e5 commit 42908ab
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
48 changes: 48 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
105 changes: 105 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright © 2018 Developer [email protected]
//
// 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")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 42908ab

Please sign in to comment.