-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
92 lines (76 loc) · 2.41 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pvwnthem/gopwd/cmd/config"
"github.com/pvwnthem/gopwd/cmd/vault"
"github.com/pvwnthem/gopwd/constants"
"github.com/pvwnthem/gopwd/pkg/util"
"github.com/spf13/cobra"
)
var configFile string
var (
Path string
Version string
Copy bool
Force bool
)
var rootCmd = &cobra.Command{
Use: "gopwd",
Short: "A cli password manager written in go",
Long: "gopwd is an encrypted cli password manager (similar to password-store) written in golang",
Version: Version,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Check if the vault exists
vaultPath := Path
pathExists, err := util.Exists(vaultPath)
if err != nil {
return fmt.Errorf(constants.ErrVaultExistence, err)
}
if !pathExists {
return fmt.Errorf(constants.ErrVaultDoesNotExist, vaultPath)
}
isVault, err := util.Exists(filepath.Join(vaultPath, ".vault"))
if err != nil {
return fmt.Errorf("failed to check if the provided path is a vault %w", err)
}
if !isVault {
return fmt.Errorf("provided path is not a vault, does it have a .vault file?")
}
nestedDirs, err := util.GetNestedDirectories(Path)
if err != nil {
return fmt.Errorf("error getting nested directories of vault %w", err)
}
if nestedDirs == nil {
return fmt.Errorf("provided vault was found but is empty")
}
fmt.Println(strings.Split(Path, "/")[len(strings.Split(Path, "/"))-1]) // print name of vault on top of dir structure
err = util.PrintDirectoryTree(Path, "")
if err != nil {
return fmt.Errorf("error printing directory tree: %w", err)
}
return nil
},
}
func Execute() {
Path, configFile, _ = util.InitConfig(Path, configFile)
rootCmd.Version = Version
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func addSubcommandPalettes() {
rootCmd.AddCommand(vault.VaultCmd)
rootCmd.AddCommand(config.ConfigCmd)
}
func init() {
addSubcommandPalettes()
rootCmd.PersistentFlags().StringVarP(&Path, "path", "p", "", "The path of the vault")
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Config file (default is $HOME/.gopwd/config.json)")
rootCmd.PersistentFlags().BoolVarP(&Copy, "copy", "c", false, "copy the password to the clipboard and don't print it to stdout")
rootCmd.PersistentFlags().BoolVarP(&Force, "yes", "y", false, "if the command asks for confirmation, force it to yes")
}