mirror of
https://github.com/mitchell/selfpass.git
synced 2025-12-14 13:27:21 +00:00
Add interactive prompt as the root command
This commit is contained in:
parent
e3f0e73460
commit
d2d972b9b1
3 changed files with 56 additions and 0 deletions
|
|
@ -2,7 +2,11 @@ package commands
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/c-bata/go-prompt"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
credrepos "github.com/mitchell/selfpass/services/credentials/repositories"
|
||||
|
|
@ -15,6 +19,7 @@ import (
|
|||
func Execute() {
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "sp",
|
||||
Run: run,
|
||||
Short: "This is the CLI client for Selfpass.",
|
||||
Long: `This is the CLI client for Selfpass, the self-hosted password manager. With this tool you
|
||||
can interact with the entire Selfpass API.`,
|
||||
|
|
@ -42,6 +47,50 @@ can interact with the entire Selfpass API.`,
|
|||
check(rootCmd.Execute())
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, _ []string) {
|
||||
ss := []prompt.Suggest{
|
||||
{Text: "exit", Description: "Exit selfpass prompt"},
|
||||
}
|
||||
|
||||
for _, subcmd := range cmd.Commands() {
|
||||
ss = append(ss, prompt.Suggest{
|
||||
Text: subcmd.Name(),
|
||||
Description: subcmd.Short,
|
||||
})
|
||||
}
|
||||
|
||||
completer := func(d prompt.Document) []prompt.Suggest {
|
||||
return prompt.FilterHasPrefix(ss, d.TextBeforeCursor(), true)
|
||||
}
|
||||
|
||||
executor := func(argstr string) {
|
||||
args := strings.Split(argstr, " ")
|
||||
|
||||
if len(args) > 0 && args[0] == "exit" {
|
||||
fmt.Print("Goodbye!\n\n")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
cmd.SetArgs(args)
|
||||
err := cmd.Execute()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
p := prompt.New(
|
||||
executor,
|
||||
completer,
|
||||
prompt.OptionPrefix("selfpass-> "),
|
||||
prompt.OptionPrefixTextColor(prompt.White),
|
||||
prompt.OptionSuggestionBGColor(prompt.DarkBlue),
|
||||
prompt.OptionDescriptionBGColor(prompt.Blue),
|
||||
)
|
||||
|
||||
p.Run()
|
||||
}
|
||||
|
||||
func makeInitClient(repo types.ConfigRepo, initClient credtypes.CredentialsClientInit) credentialsClientInit {
|
||||
return func(ctx context.Context) credtypes.CredentialsClient {
|
||||
_, cfg, err := repo.OpenConfig()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue