-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
李昌
committed
Feb 6, 2024
1 parent
16e0da8
commit 98f73a7
Showing
7 changed files
with
133 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters