diff --git a/cmds/get.go b/cmds/get.go index 73962e6..7b04183 100644 --- a/cmds/get.go +++ b/cmds/get.go @@ -1,6 +1,7 @@ package cmds import ( + "errors" "fmt" "os" "text/tabwriter" @@ -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 { @@ -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) } diff --git a/cmds/init.go b/cmds/init.go index a66b65d..f0a6dbb 100644 --- a/cmds/init.go +++ b/cmds/init.go @@ -14,6 +14,10 @@ import ( ) func InitCmd() *cobra.Command { + var ( + onlyLocal bool + ) + var initCmd = &cobra.Command{ Use: "init", Short: "init config", @@ -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) @@ -82,6 +90,8 @@ func InitCmd() *cobra.Command { }, } + initCmd.Flags().BoolVarP(&onlyLocal, "only-local", "", false, "only use local storage") + return initCmd } diff --git a/cmds/new.go b/cmds/new.go index 6eecec6..8785676 100644 --- a/cmds/new.go +++ b/cmds/new.go @@ -17,6 +17,8 @@ func GenerateCmd() *cobra.Command { account string note string url string + passwd string + length int32 ) var generateCmd = &cobra.Command{ @@ -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() @@ -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 } diff --git a/remote/empty/empty-remote.go b/remote/empty/empty-remote.go new file mode 100644 index 0000000..597e962 --- /dev/null +++ b/remote/empty/empty-remote.go @@ -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 +} diff --git a/remote/remote.go b/remote/remote.go index d37e6a5..f1d3143 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/yangchnet/pm/remote/empty" gitremote "github.com/yangchnet/pm/remote/git" ) @@ -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) } diff --git a/store/sqlite.go b/store/sqlite.go index 32fb79a..f4dafab 100644 --- a/store/sqlite.go +++ b/store/sqlite.go @@ -8,6 +8,7 @@ import ( "github.com/yangchnet/pm/config" "gorm.io/driver/sqlite" "gorm.io/gorm" + gormlogger "gorm.io/gorm/logger" ) type SqliteStore struct { @@ -15,7 +16,9 @@ type SqliteStore struct { } 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") } @@ -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