2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-31 00:17:29 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-08-31 00:17:29 +00:00
|
|
|
"github.com/c-bata/go-prompt"
|
2019-05-22 15:22:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-07-11 02:33:22 +00:00
|
|
|
credrepos "github.com/mitchell/selfpass/services/credentials/repositories"
|
|
|
|
credtypes "github.com/mitchell/selfpass/services/credentials/types"
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/repositories"
|
|
|
|
"github.com/mitchell/selfpass/sp/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-08-12 18:51:39 +00:00
|
|
|
// Execute is the main entrypoint for the `sp` CLI tool.
|
2019-06-02 08:47:19 +00:00
|
|
|
func Execute() {
|
2019-05-22 15:22:40 +00:00
|
|
|
rootCmd := &cobra.Command{
|
2019-07-11 06:05:59 +00:00
|
|
|
Use: "sp",
|
2019-08-31 00:17:29 +00:00
|
|
|
Run: run,
|
2019-05-22 15:22:40 +00:00
|
|
|
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.`,
|
2019-05-28 01:16:50 +00:00
|
|
|
Version: "v0.1.0",
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
cfgFile := rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.sp.toml)")
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
mgr := repositories.NewConfigManager(cfgFile)
|
2019-07-12 01:02:46 +00:00
|
|
|
clientInit := credrepos.NewCredentialsClient
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-06-30 04:17:22 +00:00
|
|
|
rootCmd.AddCommand(
|
|
|
|
makeInit(mgr),
|
|
|
|
makeEncrypt(mgr),
|
|
|
|
makeDecrypt(mgr),
|
|
|
|
makeDecryptCfg(mgr),
|
2019-07-11 06:05:59 +00:00
|
|
|
makeList(makeInitClient(mgr, clientInit)),
|
|
|
|
makeCreate(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
makeUpdate(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
makeGet(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
makeDelete(makeInitClient(mgr, clientInit)),
|
|
|
|
makeGCMToCBC(mgr, makeInitClient(mgr, clientInit)),
|
2019-06-30 04:17:22 +00:00
|
|
|
)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
check(rootCmd.Execute())
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:17:29 +00:00
|
|
|
func run(cmd *cobra.Command, _ []string) {
|
|
|
|
ss := []prompt.Suggest{
|
|
|
|
{Text: "exit", Description: "Exit selfpass prompt"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subcmd := range cmd.Commands() {
|
2019-08-31 01:18:44 +00:00
|
|
|
if subcmd.Hidden {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:17:29 +00:00
|
|
|
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,
|
2019-08-31 01:18:44 +00:00
|
|
|
prompt.OptionPrefix("-> "),
|
|
|
|
prompt.OptionPrefixTextColor(prompt.Green),
|
|
|
|
prompt.OptionInputTextColor(prompt.LightGray),
|
2019-08-31 00:17:29 +00:00
|
|
|
prompt.OptionSuggestionBGColor(prompt.DarkBlue),
|
|
|
|
prompt.OptionDescriptionBGColor(prompt.Blue),
|
2019-08-31 01:18:44 +00:00
|
|
|
prompt.OptionSelectedSuggestionBGColor(prompt.Blue),
|
|
|
|
prompt.OptionSelectedDescriptionBGColor(prompt.DarkBlue),
|
|
|
|
prompt.OptionScrollbarThumbColor(prompt.LightGray),
|
|
|
|
prompt.OptionScrollbarBGColor(prompt.DarkGray),
|
|
|
|
prompt.OptionPreviewSuggestionTextColor(prompt.Red),
|
2019-08-31 00:17:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
p.Run()
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:51:39 +00:00
|
|
|
func makeInitClient(repo types.ConfigRepo, initClient credtypes.CredentialsClientInit) credentialsClientInit {
|
2019-07-12 01:02:46 +00:00
|
|
|
return func(ctx context.Context) credtypes.CredentialsClient {
|
2019-06-02 08:47:19 +00:00
|
|
|
_, cfg, err := repo.OpenConfig()
|
|
|
|
check(err)
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
connConfig := cfg.GetStringMapString(types.KeyConnConfig)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
client, err := initClient(
|
|
|
|
ctx,
|
|
|
|
connConfig["target"],
|
|
|
|
connConfig["ca"],
|
|
|
|
connConfig["cert"],
|
|
|
|
connConfig["key"],
|
|
|
|
)
|
|
|
|
check(err)
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
return client
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
|
|
|
}
|