From 98f73a7f5f39bd16a8c8af751c0f08153feeaf54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8C?= Date: Tue, 6 Feb 2024 15:26:18 +0800 Subject: [PATCH] fix: git pull --- cmds/delete.go | 83 ++++++++++++++++++++++++++++++++++++ cmds/root.go | 1 + remote/git/git-remote.go | 2 +- store/file-store/file.go | 33 ++++++++++++++ store/interface.go | 3 ++ store/model.go | 14 +++--- store/sqlite-store/sqlite.go | 8 ++-- 7 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 cmds/delete.go diff --git a/cmds/delete.go b/cmds/delete.go new file mode 100644 index 0000000..7e3941a --- /dev/null +++ b/cmds/delete.go @@ -0,0 +1,83 @@ +package cmds + +import ( + "errors" + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/yangchnet/pm/config" + "gorm.io/gorm" +) + +func DelCmd() *cobra.Command { + var getCmd = &cobra.Command{ + Use: "del [name]", + Short: "delete password for [name] from store", + Args: cobra.ExactArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + config.InitConfig() + + service, err := NewService(cmd.Context()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if err := service.remote.Pull(cmd.Context()); err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + Run: func(cmd *cobra.Command, args []string) { + service, err := NewService(cmd.Context()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + _ = GetPrimaryKey() + + passwd, err := service.store.Get(cmd.Context(), args[0]) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + fmt.Println(err) + os.Exit(1) + } + + if errors.Is(err, gorm.ErrRecordNotFound) { + return + } + + var inputName string + fmt.Printf("Are you sure you want to delete the password named %s? If yes, please enter %s: ", passwd.Name, passwd.Name) + fmt.Scanln(&inputName) + + if inputName != passwd.Name { + fmt.Println("input name not match") + os.Exit(1) + } + + if err := service.store.Delete(cmd.Context(), passwd.Name); err != nil { + fmt.Println(err) + os.Exit(1) + } + }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + config.InitConfig() + service, err := NewService(cmd.Context()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + nameList, err := service.store.SearchName(cmd.Context(), toComplete) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + return nameList, cobra.ShellCompDirectiveKeepOrder + }, + } + + return getCmd +} diff --git a/cmds/root.go b/cmds/root.go index 9bb9ef3..5c80840 100644 --- a/cmds/root.go +++ b/cmds/root.go @@ -10,4 +10,5 @@ func init() { RootCmd.AddCommand(PushCmd()) RootCmd.AddCommand(PullCmd()) RootCmd.AddCommand(InitCmd()) + RootCmd.AddCommand(DelCmd()) } diff --git a/remote/git/git-remote.go b/remote/git/git-remote.go index 4fddd06..1944ba4 100644 --- a/remote/git/git-remote.go +++ b/remote/git/git-remote.go @@ -200,7 +200,7 @@ func (gr *GitRemote) Pull(ctx context.Context) error { } if errors.Is(err, git.ErrRepositoryNotExists) { - _, err = git.PlainClone(config.GetString("local.path"), false, &git.CloneOptions{ + r, err = git.PlainClone(config.GetString("local.path"), false, &git.CloneOptions{ URL: config.GetString("remote.url"), RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, Auth: auth, diff --git a/store/file-store/file.go b/store/file-store/file.go index 1a9b17c..3184a52 100644 --- a/store/file-store/file.go +++ b/store/file-store/file.go @@ -120,6 +120,39 @@ func (s *FileStore) Delete(ctx context.Context, name string) error { return os.Remove(filepath.Join(s.localPath, name+".passwd")) } +// Update 更新一个记录 +func (s *FileStore) Update(ctx context.Context, name string, passwd *store.Passwd) error { + files, err := readAllPasswd(s.localPath) + if err != nil { + return err + } + + path, ok := files[name+".passwd"] + if !ok { + return store.ErrNotFound + } + + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + + passwdByte, err := json.Marshal(passwd) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + encoded := base64.StdEncoding.EncodeToString(passwdByte) + _, err = f.WriteString(encoded) + if err != nil { + return err + } + + return nil +} + func readAllPasswd(dir string) (map[string]string, error) { files := make(map[string]string) err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { diff --git a/store/interface.go b/store/interface.go index 47af803..4cbcb5a 100644 --- a/store/interface.go +++ b/store/interface.go @@ -19,4 +19,7 @@ type Store interface { // Delete 删除一个记录 Delete(ctx context.Context, name string) error + + // Update 更新一个记录 + Update(ctx context.Context, name string, passwd *Passwd) error } diff --git a/store/model.go b/store/model.go index f6a89f0..caf0538 100644 --- a/store/model.go +++ b/store/model.go @@ -4,11 +4,11 @@ import "time" type Passwd struct { Id int - Name string `gorm:"index:idx_name,unique"` // Name is unique,供用户查找密码 - Url string // 用户该密码对应的的网址 - UserName string // 用户在该网站上的用户名 - CryptedPasswd []byte // 用户密码,加密后的 - Note string // 备注 - CreateTime time.Time - UpdateTime time.Time + Name string `gorm:"index:idx_name,unique"` // Name is unique,供用户查找密码 + Url string // 用户该密码对应的的网址 + UserName string // 用户在该网站上的用户名 + CryptedPasswd []byte // 用户密码,加密后的 + Note string // 备注 + CreateTime time.Time `gorm:"autoCreateTime"` + UpdateTime time.Time `gorm:"autoUpdateTime"` } diff --git a/store/sqlite-store/sqlite.go b/store/sqlite-store/sqlite.go index 50ca015..35cecd7 100644 --- a/store/sqlite-store/sqlite.go +++ b/store/sqlite-store/sqlite.go @@ -3,7 +3,6 @@ package sqlitestore import ( "context" "path/filepath" - "time" "github.com/yangchnet/pm/config" "github.com/yangchnet/pm/store" @@ -39,8 +38,6 @@ func (s *SqliteStore) Init(ctx context.Context) (string, error) { // Save 在使用cryptFunc对密码密文进行存储 func (s *SqliteStore) Save(ctx context.Context, passwd *store.Passwd) error { - passwd.CreateTime = time.Now() - passwd.UpdateTime = time.Now() return s.db.Save(passwd).Error } @@ -71,3 +68,8 @@ func (s *SqliteStore) Delete(ctx context.Context, name string) error { } return s.db.Delete(&passwd).Error } + +// Update 更新一个记录 +func (s *SqliteStore) Update(ctx context.Context, name string, passwd *store.Passwd) error { + return s.db.Model(&store.Passwd{}).Where("name = ?", name).Updates(passwd).Error +}