-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
67 lines (54 loc) · 1.53 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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/pvwnthem/gopwd/cmd/config"
"github.com/pvwnthem/gopwd/cmd/vault"
"github.com/pvwnthem/gopwd/util"
"github.com/spf13/cobra"
)
var configFile string
var (
Path string
Name string
)
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",
Run: func(cmd *cobra.Command, args []string) {
// Check if the vault exists
vaultExists, err := util.Exists(filepath.Join(Path, Name))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !vaultExists {
fmt.Println("Vault does not exist")
os.Exit(1)
}
fmt.Println(Name) // print name of vault on top of dir structure
err = util.PrintDirectoryTree(filepath.Join(Path, Name), "")
if err != nil {
fmt.Printf("Error printing directory tree: %v\n", err)
}
},
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func addSubcommandPalettes() {
rootCmd.AddCommand(vault.VaultCmd)
rootCmd.AddCommand(config.ConfigCmd)
}
func init() {
cobra.OnInitialize(func() { Path, Name, configFile, _ = util.InitConfig(Path, Name, configFile) })
addSubcommandPalettes()
rootCmd.PersistentFlags().StringVarP(&Path, "path", "p", "", "The path of the vault")
rootCmd.PersistentFlags().StringVarP(&Name, "name", "n", "", "The name of the vault")
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Config file (default is $HOME/.gopwd/config.json)")
}