Skip to content

Commit

Permalink
feat(save): support save sync info
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 authored and Pengfei Jiang committed Mar 11, 2021
1 parent 8ef45f4 commit 7f53997
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 50 deletions.
44 changes: 22 additions & 22 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"github.com/joyme123/kubecm/pkg/types"
"log"

"github.com/joyme123/kubecm/pkg/loader"
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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.

Expand Down
20 changes: 20 additions & 0 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
24 changes: 12 additions & 12 deletions pkg/loader/local.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions pkg/loader/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

func IsSSH(path string) bool {
func isSSH(path string) bool {
if strings.HasPrefix(path, "ssh://") {
return true
}
Expand All @@ -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
Expand All @@ -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
Expand Down
44 changes: 32 additions & 12 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package manager

import (
"fmt"
"github.com/google/uuid"
"github.com/joyme123/kubecm/pkg/types"
"io/ioutil"
"os"
"path"
Expand All @@ -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 {
Expand All @@ -28,15 +30,15 @@ type impl struct {
configPath string
configDir string
kubePath string
conf *Configuration
conf *types.Configuration
}

func NewInterface(configDir string, configPath string, kubePath string) (Interface, error) {
i := &impl{
configDir: configDir,
configPath: configPath,
kubePath: kubePath,
conf: &Configuration{},
conf: &types.Configuration{},
}

err := i.init()
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion pkg/manager/types.go → pkg/types/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package manager
package types

import (
"fmt"
Expand All @@ -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() {
Expand Down

0 comments on commit 7f53997

Please sign in to comment.