Skip to content

Commit

Permalink
refactor: change base create/delete/get command
Browse files Browse the repository at this point in the history
  • Loading branch information
itnpeople committed May 9, 2022
1 parent ec859b1 commit 9f8bf12
Show file tree
Hide file tree
Showing 30 changed files with 2,077 additions and 2,138 deletions.
348 changes: 248 additions & 100 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type conf struct {
}

type ConfigContext struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Urls struct {
MCKS string `yaml:"mcks"`
Expand Down Expand Up @@ -58,6 +59,7 @@ func OnConfigInitialize(cfgFile string) error {

// set default
viper.SetDefault("current-context", "")
viper.SetDefault("contexts.local.name", "local")
viper.SetDefault("contexts.local.namespace", "")
viper.SetDefault("contexts.local.urls", map[string]string{
"mcks": "http://localhost:1470/mcks",
Expand Down
41 changes: 41 additions & 0 deletions app/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,47 @@ var (
BuildTime string = ""
)

type IOptions interface {
GetFilename() string
}

type Options struct {
OutStream *os.File // output stream
ConfigFile string // config file
Output string // output format (json/yaml)
Filename string // file
Namespace string // cloud-barista namespace
Name string // object name
}

func (o *Options) GetFilename() string {
return o.Filename
}

func (o *Options) Println(format string, params ...interface{}) {
msg := fmt.Sprintf(format+"\n", params...)
if o.OutStream != nil {
o.OutStream.WriteString(msg)
} else {
os.Stdout.WriteString(msg)
}
}
func (o *Options) PrintlnError(err error) {
o.Println("%+v\n", err)
}

func (o *Options) WriteBody(json []byte) {
if o.Output == OUTPUT_JSON {
o.OutStream.Write(utils.ToPrettyJSON(json))
} else {
if d, err := yaml.JSONToYAML(json); err == nil {
o.OutStream.Write(d)
} else {
o.OutStream.Write(json)
}
}
}

type IOStreams struct {
In *os.File
Out *os.File
Expand Down
54 changes: 46 additions & 8 deletions app/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package app

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"

"github.com/ghodss/yaml"
"github.com/spf13/cobra"

"github.com/itnpeople/cbctl/utils"
)

func ValidateError(err error) {
func ValidateError(c *cobra.Command, err error) {

if err != nil {
msg := err.Error()
c.Help()
msg := "\n" + err.Error()
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
Expand All @@ -21,16 +28,47 @@ func ValidateError(err error) {

}

func ValidCommandArgs(idx int, value *string) func(c *cobra.Command, args []string) error {
func BindCommandArgs(values ...*string) func(c *cobra.Command, args []string) error {

return func(c *cobra.Command, args []string) error {
if len(args) > idx && len(args[idx]) > 0 {
*value = args[idx]
}
if *value == "" {
return fmt.Errorf("arguemnt[%d] is empty", idx)

for i, v := range args {
if len(values) > i {
*values[i] = v
}
}
return nil
}

}

func GetBody(o IOptions, tpl string) (buf []byte, err error) {

//o := opt.(Options)

// -f 옵션
fileName := o.GetFilename()
if len(fileName) > 0 {
switch {
case fileName == "-": // standard-in
buf, err = ioutil.ReadAll(os.Stdin)
case strings.Index(fileName, "http://") == 0 || strings.Index(fileName, "https://") == 0: // http
if _, err = url.Parse(fileName); err == nil {
if resp, err := http.Get(fileName); err == nil {
defer resp.Body.Close()
buf, err = ioutil.ReadAll(resp.Body)
}
}
default:
buf, err = ioutil.ReadFile(fileName) // local file
}

if err == nil {
buf, err = yaml.YAMLToJSON(buf)
}
} else {
buf, err = utils.ToTemplateBytes(tpl, o)
}

return
}
103 changes: 103 additions & 0 deletions cmd/clean/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package clean

import (
"fmt"

"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"

"github.com/itnpeople/cbctl/app"
"github.com/itnpeople/cbctl/utils"
)

// a struct to support command
type CleanOptions struct {
*app.Options
}

// validates
func (o *CleanOptions) Validate() error {
o.Namespace = utils.NVL(o.Namespace, app.Config.GetCurrentContext().Namespace)
if o.Namespace == "" {
return fmt.Errorf("Namespace is required.")
}
return nil
}

func (o *CleanOptions) RunCleanupMCIS() error {

if o.Namespace == "" {
return fmt.Errorf("Namespace is required.")
}

url := fmt.Sprintf("%s/ns/%s", app.Config.GetCurrentContext().Urls.Tumblebug, o.Namespace)
http := resty.New().SetDisableWarn(true).R().SetBasicAuth("default", "default")

// mcis
if resp, err := http.Delete(url + "/mcis"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
// vpc
if resp, err := http.Delete(url + "/resources/vNet"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
// securityGroup
if resp, err := http.Delete(url + "/resources/securityGroup"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
// sshKey
if resp, err := http.Delete(url + "/resources/sshKey"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
// image
if resp, err := http.Delete(url + "/resources/image"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
// spec
if resp, err := http.Delete(url + "/resources/spec"); err != nil {
return err
} else {
o.WriteBody(resp.Body())
}
return nil
}

// returns a cobra command
func NewCommandClean(options *app.Options) *cobra.Command {

o := &CleanOptions{
Options: options,
}

// clean
cmd := &cobra.Command{
Use: "clean [mcir]",
Short: "Clean up objects",
Args: app.BindCommandArgs(&o.Name),
DisableFlagsInUseLine: true,
Run: func(c *cobra.Command, args []string) {
app.ValidateError(c, o.Validate())
app.ValidateError(c, func() error {
switch o.Name {
case "mcir":
return o.RunCleanupMCIS()
default:
c.Help()
return nil
}
}())
},
}

return cmd
}
Loading

0 comments on commit 9f8bf12

Please sign in to comment.