2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
"github.com/mitchell/selfpass/cli/repositories"
|
|
|
|
"github.com/mitchell/selfpass/cli/types"
|
2019-05-28 01:16:50 +00:00
|
|
|
"github.com/mitchell/selfpass/credentials/commands"
|
2019-06-02 08:47:19 +00:00
|
|
|
credrepos "github.com/mitchell/selfpass/credentials/repositories"
|
2019-05-28 01:16:50 +00:00
|
|
|
credtypes "github.com/mitchell/selfpass/credentials/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
func Execute() {
|
2019-05-22 15:22:40 +00:00
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "spc",
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
cfgFile := rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.spc.toml)")
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
mgr := repositories.NewConfigManager(cfgFile)
|
|
|
|
clientInit := credrepos.NewCredentialServiceClient
|
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),
|
|
|
|
commands.MakeList(makeInitClient(mgr, clientInit)),
|
|
|
|
commands.MakeCreate(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
commands.MakeUpdate(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
commands.MakeGet(mgr, makeInitClient(mgr, clientInit)),
|
|
|
|
commands.MakeDelete(makeInitClient(mgr, clientInit)),
|
2019-07-09 00:45:01 +00:00
|
|
|
commands.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-06-02 08:47:19 +00:00
|
|
|
func makeInitClient(repo types.ConfigRepo, initClient credtypes.CredentialClientInit) commands.CredentialClientInit {
|
2019-05-28 01:16:50 +00:00
|
|
|
return func(ctx context.Context) credtypes.CredentialClient {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|