Skip to content

Commit

Permalink
fix: git pull
Browse files Browse the repository at this point in the history
  • Loading branch information
李昌 committed Feb 6, 2024
1 parent 16e0da8 commit 98f73a7
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 11 deletions.
83 changes: 83 additions & 0 deletions cmds/delete.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ func init() {
RootCmd.AddCommand(PushCmd())
RootCmd.AddCommand(PullCmd())
RootCmd.AddCommand(InitCmd())
RootCmd.AddCommand(DelCmd())
}
2 changes: 1 addition & 1 deletion remote/git/git-remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions store/file-store/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions store/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions store/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
8 changes: 5 additions & 3 deletions store/sqlite-store/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sqlitestore
import (
"context"
"path/filepath"
"time"

"github.com/yangchnet/pm/config"
"github.com/yangchnet/pm/store"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 98f73a7

Please sign in to comment.