-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
134 lines (116 loc) · 3.57 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"github.com/tobischo/gokeepasslib/v3"
"github.com/spf13/cobra"
)
const (
app = "pk2"
timeFormat = "2006-01-02 15:04:05 -0700"
)
var usePassword bool
var keyFile string
var filePath string
var groupFlag bool
var changed = false
var db *gokeepasslib.Database
func main() {
// var cmdAdd = &cobra.Command{
// Use: "add [selector]",
// Short: "adds a new entry at the given location",
// Long: `add builds a new entry at the given location and ` +
// `asks for the information required`,
// PreRunE: loadDatabaseCmd,
// RunE: addCmd,
// }
// cmdAdd.
// Flags().
// BoolVarP(&groupFlag, "group", "g", false, when true adds a group instead of an entry")
var cmdBrowse = &cobra.Command{
Use: "browse",
Short: "allows interactive access to the database",
Long: `interactive allows to execute multiple operations on the file`,
PreRunE: loadDatabaseCmd,
RunE: browseCmd,
}
var cmdCopy = &cobra.Command{
Use: "copy [selector]",
Short: "copies the password into the clipboard",
Long: `copy is for selecting the entry and copying the password into the clipboard`,
PreRunE: loadDatabaseCmd,
RunE: copyCmd,
}
var cmdGeneratePassword = &cobra.Command{
Use: "generate",
Short: "generates a new password",
Long: `generate builds a new password for the selected entry and copies it into the clipboard`,
RunE: generateCmd,
}
var cmdInfo = &cobra.Command{
Use: "info [selector]",
Short: "shows the information for an entry",
Long: `info is for listing all relevant information for a command (except the password)`,
PreRunE: loadDatabaseCmd,
RunE: infoCmd,
}
var cmdInit = &cobra.Command{
Use: "init [no options!]",
Short: "initializes a new kdbx file",
Long: `init is for creating a new basic keepass file at the given location. ` +
`It will fail if a file already exists`,
RunE: initCmd,
}
// var cmdMove = &cobra.Command{
// Use: "move [sourceSelector] [targetSelector]",
// Short: "moves an entry within the file",
// Long: `move takes an entry from the position given with [sourceSelector] ` +
// `and moves it to the group given at [targetSelector]`,
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("move")
// },
// }
// var cmdRemove = &cobra.Command{
// Use: "remove [selector]",
// Short: "removes an entry from the keepass file",
// Long: `remove takes an entry out of the Keepass file. ` +
// `It asks for confirmation before persisting the file`,
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("remove")
// },
// }
var cmdVersion = &cobra.Command{
Use: "version",
Short: "shows the version",
Long: "version shows the version of this tool",
Run: versionCmd,
}
var rootCmd = &cobra.Command{
Use: "kp2",
Short: "tool to access Keepass2 files form the command line",
PersistentPreRun: paramSetupCmd,
PersistentPostRun: persistDatabaseIfChanged,
}
rootCmd.PersistentFlags().BoolVarP(
&usePassword, "password", "p", false,
"true by default, false when using key param, add when using key and password authentication",
)
rootCmd.PersistentFlags().StringVarP(
&keyFile, "key", "k", "",
"path to the key file to use for auth",
)
rootCmd.PersistentFlags().StringVarP(
&filePath, "file", "f", "",
"Keepass2 file to be loaded, setting KP2FILE allows omitting this flag",
)
rootCmd.AddCommand(
// cmdAdd,
cmdBrowse,
cmdCopy,
cmdGeneratePassword,
cmdInfo,
cmdInit,
// cmdMove,
// cmdRemove,
cmdVersion,
)
rootCmd.Execute()
}