From 7f539979749fce54def36e26009858bd0273be62 Mon Sep 17 00:00:00 2001 From: jiangpengfei Date: Wed, 10 Mar 2021 23:33:28 +0800 Subject: [PATCH] feat(save): support save sync info --- cmd/import.go | 44 ++++++++++++++++----------------- pkg/loader/loader.go | 20 +++++++++++++++ pkg/loader/local.go | 24 +++++++++--------- pkg/loader/ssh.go | 6 ++--- pkg/manager/manager.go | 44 ++++++++++++++++++++++++--------- pkg/{manager => types}/types.go | 10 +++++++- 6 files changed, 98 insertions(+), 50 deletions(-) create mode 100644 pkg/loader/loader.go rename pkg/{manager => types}/types.go (61%) diff --git a/cmd/import.go b/cmd/import.go index 55d22af..f56e233 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -16,6 +16,7 @@ limitations under the License. package cmd import ( + "github.com/joyme123/kubecm/pkg/types" "log" "github.com/joyme123/kubecm/pkg/loader" @@ -27,9 +28,10 @@ type importOptions struct { name string from string password string - publicKey string privateKey string sshPort int + save bool + override bool } var importOpt importOptions @@ -52,32 +54,28 @@ func runImport(opt importOptions) { log.Fatalf("fatal: %v", err) } - var data []byte - if loader.IsLocal(opt.from) { - data, err = loader.LocalGet(opt.from) - if err != nil { - log.Fatalf("get config from local error: %v", err) - } - } else if loader.IsSSH(opt.from) { - if len(opt.password) > 0 { - data, err = loader.SSHGetWithPassword(opt.from, opt.sshPort, opt.password) - if err != nil { - log.Fatalf("get config from ssh with password error: %v", err) - } - } else { - data, err = loader.SSHGetWithPrivateKey(opt.from, opt.sshPort, opt.privateKey) - if err != nil { - log.Fatalf("get config from ssh with private key error: %v", err) - } - } - } else { - log.Fatalf("unsupport path: %s", opt.from) + syncInfo := &types.Sync{ + From: opt.from, + SSHPort: opt.sshPort, + Password: opt.password, + PrivateKey: opt.privateKey, + } + + data, err := loader.Load(syncInfo) + if err != nil { + log.Fatalf("load error: %v", err) } - err = m.Import(opt.name, data) + err = m.Import(opt.name, data, opt.override) if err != nil { log.Fatalf("import config error: %v", err) } + + if opt.save { + if err := m.SaveSyncInfo(opt.name, syncInfo); err != nil { + log.Fatalf("save sync info error: %v", err) + } + } } func init() { @@ -91,6 +89,8 @@ func init() { importCmd.Flags().StringVarP(&importOpt.password, "password", "p", "", "ssh password") importCmd.Flags().StringVarP(&importOpt.privateKey, "privateKey", "k", privateKeyPath, "ssh private key") importCmd.Flags().IntVarP(&importOpt.sshPort, "port", "", 22, "ssh server port") + importCmd.Flags().BoolVarP(&importOpt.save, "save", "", false, "save info for sync or not") + importCmd.Flags().BoolVarP(&importOpt.override, "override", "", false, "override exist config") // Here you will define your flags and configuration settings. diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go new file mode 100644 index 0000000..e1fa108 --- /dev/null +++ b/pkg/loader/loader.go @@ -0,0 +1,20 @@ +package loader + +import ( + "fmt" + "github.com/joyme123/kubecm/pkg/types" +) + +func Load(syncInfo *types.Sync) ([]byte, error) { + if isLocal(syncInfo.From) { + return localGet(syncInfo.From) + } else if isSSH(syncInfo.From) { + if len(syncInfo.Password) > 0 { + return sshGetWithPassword(syncInfo.From, syncInfo.SSHPort, syncInfo.Password) + } else { + return sshGetWithPrivateKey(syncInfo.From, syncInfo.SSHPort, syncInfo.PrivateKey) + } + } else { + return nil, fmt.Errorf("unsupport path: %s", syncInfo.From) + } +} diff --git a/pkg/loader/local.go b/pkg/loader/local.go index 14953f8..e897adf 100644 --- a/pkg/loader/local.go +++ b/pkg/loader/local.go @@ -1,21 +1,21 @@ package loader import ( - "io/ioutil" - "strings" + "io/ioutil" + "strings" ) -func IsLocal(path string) bool { - if strings.HasPrefix(path, "file://") { - return true - } else if !strings.Contains(path, "://") { - return true - } +func isLocal(path string) bool { + if strings.HasPrefix(path, "file://") { + return true + } else if !strings.Contains(path, "://") { + return true + } - return false + return false } -func LocalGet(path string) ([]byte, error) { - path = strings.TrimLeft(path, "file://") - return ioutil.ReadFile(path) +func localGet(path string) ([]byte, error) { + path = strings.TrimLeft(path, "file://") + return ioutil.ReadFile(path) } diff --git a/pkg/loader/ssh.go b/pkg/loader/ssh.go index 12f3df8..a1ce14c 100644 --- a/pkg/loader/ssh.go +++ b/pkg/loader/ssh.go @@ -10,7 +10,7 @@ import ( "strings" ) -func IsSSH(path string) bool { +func isSSH(path string) bool { if strings.HasPrefix(path, "ssh://") { return true } @@ -27,7 +27,7 @@ func DefaultSSHKeyPath() (string, error) { return privateKeyPath, nil } -func SSHGetWithPassword(path string, port int, password string) ([]byte, error) { +func sshGetWithPassword(path string, port int, password string) ([]byte, error) { params, err := getParams(path) if err != nil { return nil, err @@ -45,7 +45,7 @@ func SSHGetWithPassword(path string, port int, password string) ([]byte, error) return sshGet(config, params.ip, port, params.path) } -func SSHGetWithPrivateKey(filepath string, port int, privateKeyPath string) ([]byte, error) { +func sshGetWithPrivateKey(filepath string, port int, privateKeyPath string) ([]byte, error) { params, err := getParams(filepath) if err != nil { return nil, err diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 8a07375..e8a7a2b 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -2,6 +2,8 @@ package manager import ( "fmt" + "github.com/google/uuid" + "github.com/joyme123/kubecm/pkg/types" "io/ioutil" "os" "path" @@ -11,15 +13,15 @@ import ( "github.com/joyme123/kubecm/pkg/util" "github.com/ghodss/yaml" - "github.com/google/uuid" ) type Interface interface { - List() (*Configuration, error) - Import(name string, configData []byte) error + List() (*types.Configuration, error) + Import(name string, configData []byte, override bool) error Remove(name string) error Rename(src string, dst string) error Use(name string) error + SaveSyncInfo(name string, syncInfo *types.Sync) error } type impl struct { @@ -28,7 +30,7 @@ type impl struct { configPath string configDir string kubePath string - conf *Configuration + conf *types.Configuration } func NewInterface(configDir string, configPath string, kubePath string) (Interface, error) { @@ -36,7 +38,7 @@ func NewInterface(configDir string, configPath string, kubePath string) (Interfa configDir: configDir, configPath: configPath, kubePath: kubePath, - conf: &Configuration{}, + conf: &types.Configuration{}, } err := i.init() @@ -71,31 +73,40 @@ func (i *impl) init() error { return nil } -func (i *impl) List() (*Configuration, error) { +func (i *impl) List() (*types.Configuration, error) { return i.conf, nil } -func (i *impl) Import(name string, configData []byte) error { +func (i *impl) Import(name string, configData []byte, override bool) error { i.m.Lock() defer i.m.Unlock() index := i.search(name) - if index >= 0 { + if !override && index >= 0 { return NameConflictError } - id := uuid.New().String() - newLocation := path.Join(i.configDir, id) + var newLocation string + if index >= 0 { + newLocation = i.conf.Items[index].Location + } else { + id := uuid.New().String() + newLocation = path.Join(i.configDir, id) + } if err := util.Copy(configData, newLocation); err != nil { return err } - item := ConfigItem{ + item := types.ConfigItem{ Name: name, Location: newLocation, TimeStamp: time.Now(), } - i.conf.Items = append(i.conf.Items, item) + if index >= 0 { + i.conf.Items[index] = item + } else { + i.conf.Items = append(i.conf.Items, item) + } return i.write() } @@ -180,6 +191,15 @@ func (i *impl) Use(name string) error { return i.write() } +func (i *impl) SaveSyncInfo(name string, syncInfo *types.Sync) error { + k := i.search(name) + if k < 0 { + return nil + } + i.conf.Items[k].Sync = syncInfo + return i.write() +} + func (i *impl) search(name string) int { for k, item := range i.conf.Items { if item.Name == name { diff --git a/pkg/manager/types.go b/pkg/types/types.go similarity index 61% rename from pkg/manager/types.go rename to pkg/types/types.go index 6d862e6..b38e794 100644 --- a/pkg/manager/types.go +++ b/pkg/types/types.go @@ -1,4 +1,4 @@ -package manager +package types import ( "fmt" @@ -16,6 +16,14 @@ type ConfigItem struct { Name string `json:"name" yaml:"name"` Location string `json:"location" yaml:"location"` TimeStamp time.Time `json:"timestamp" yaml:"timestamp"` + Sync *Sync `json:"sync,omitempty" yaml:"sync,omitempty"` +} + +type Sync struct { + From string `json:"from,omitempty" yaml:"from,omitempty"` + SSHPort int `json:"sshPort,omitempty" yaml:"sshPort,omitempty"` + Password string `json:"password,omitempty" yaml:"password,omitempty"` + PrivateKey string `json:"privateKey,omitempty" yaml:"privateKey,omitempty"` } func (c *Configuration) Print() {