Skip to content

Commit

Permalink
feature: add empty remote
Browse files Browse the repository at this point in the history
  • Loading branch information
李昌 committed Feb 5, 2024
1 parent e574c38 commit 442bc58
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cmds/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmds

import (
"errors"
"fmt"
"os"
"text/tabwriter"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/yangchnet/pm/config"
"github.com/yangchnet/pm/store"
"gorm.io/gorm"
)

func GetCmd() *cobra.Command {
Expand Down Expand Up @@ -39,6 +41,10 @@ func GetCmd() *cobra.Command {

passwd, err := service.store.Get(cmd.Context(), args[0])
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
fmt.Println("password not found")
os.Exit(1)
}
fmt.Println(err)
os.Exit(1)
}
Expand Down
10 changes: 10 additions & 0 deletions cmds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
)

func InitCmd() *cobra.Command {
var (
onlyLocal bool
)

var initCmd = &cobra.Command{
Use: "init",
Short: "init config",
Expand All @@ -37,6 +41,10 @@ func InitCmd() *cobra.Command {
remoteType = args[0]
}

if onlyLocal {
remoteType = "empty"
}

remote, err := remote.NewRemote(cmd.Context(), remoteType, nil)
if err != nil {
fmt.Println("Error:", err)
Expand Down Expand Up @@ -82,6 +90,8 @@ func InitCmd() *cobra.Command {
},
}

initCmd.Flags().BoolVarP(&onlyLocal, "only-local", "", false, "only use local storage")

return initCmd
}

Expand Down
11 changes: 10 additions & 1 deletion cmds/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func GenerateCmd() *cobra.Command {
account string
note string
url string
passwd string
length int32
)

var generateCmd = &cobra.Command{
Expand All @@ -27,7 +29,10 @@ func GenerateCmd() *cobra.Command {
config.InitConfig()
},
Run: func(cmd *cobra.Command, args []string) {
password := generatePassword(12)
password := passwd
if password == "" {
password = generatePassword(int(length))
}

primaryKey := GetPrimaryKey()

Expand Down Expand Up @@ -79,6 +84,10 @@ func GenerateCmd() *cobra.Command {

generateCmd.Flags().StringVarP(&url, "url", "u", "", "相关的url")

generateCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "已有的密码")

generateCmd.Flags().Int32VarP(&length, "length", "l", 12, "生成的密码长度")

return generateCmd
}

Expand Down
23 changes: 23 additions & 0 deletions remote/empty/empty-remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package empty

import "context"

type EmptyRemote struct {
}

func NewEmptyRemote() *EmptyRemote {
return &EmptyRemote{}
}

func (r *EmptyRemote) Init(ctx context.Context) (string, error) {
return `remote:
type: empty`, nil
}

func (r *EmptyRemote) Pull(ctx context.Context) error {
return nil
}

func (r *EmptyRemote) Push(ctx context.Context, msg ...string) error {
return nil
}
3 changes: 3 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/yangchnet/pm/remote/empty"
gitremote "github.com/yangchnet/pm/remote/git"
)

Expand All @@ -23,6 +24,8 @@ func NewRemote(ctx context.Context, remoteType string, remoteMap map[string]any)
switch remoteType {
case "git":
remote = gitremote.NewGitRemote(ctx, remoteMap)
case "empty":
remote = empty.NewEmptyRemote()
default:
return nil, fmt.Errorf("未知的remote类型: %s", remoteType)
}
Expand Down
7 changes: 5 additions & 2 deletions store/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"github.com/yangchnet/pm/config"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)

type SqliteStore struct {
db *gorm.DB
}

func NewSqliteStore(ctx context.Context) *SqliteStore {
db, err := gorm.Open(sqlite.Open(filepath.Join(config.GetString("local.path"), "passwd.db")), &gorm.Config{})
db, err := gorm.Open(sqlite.Open(filepath.Join(config.GetString("local.path"), "passwd.db")), &gorm.Config{
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
})
if err != nil {
panic("failed to connect database")
}
Expand All @@ -39,7 +42,7 @@ func (s *SqliteStore) Save(ctx context.Context, passwd *Passwd) error {
func (s *SqliteStore) Get(ctx context.Context, name string) (*Passwd, error) {
var passwd *Passwd
if err := s.db.Model(&Passwd{}).Where("name = ?", name).First(&passwd).Error; err != nil {
return passwd, nil
return nil, err
}

return passwd, nil
Expand Down

0 comments on commit 442bc58

Please sign in to comment.