Initial Commit
This commit is contained in:
68
cmd/get.go
Normal file
68
cmd/get.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.bullercodeworks.com/brian/keepass-cli/models"
|
||||
"git.bullercodeworks.com/brian/keepass-cli/util"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
kp "github.com/tobischo/gokeepasslib"
|
||||
)
|
||||
|
||||
// getCmd represents the get command
|
||||
var getCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "Get the password for an entry",
|
||||
RunE: runGetCmd,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getCmd)
|
||||
}
|
||||
|
||||
func runGetCmd(cmd *cobra.Command, args []string) error {
|
||||
var path []string
|
||||
for i := range args {
|
||||
path = append(path, strings.Split(args[i], "/")...)
|
||||
}
|
||||
var db *models.KeePassDB
|
||||
pass, err := util.PromptUserForPassword("Master Password")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var entry *kp.Entry
|
||||
for entry == nil {
|
||||
var entryErr error
|
||||
list := db.GetGroupsAndEntriesFromRoot(path)
|
||||
if len(list) == 0 {
|
||||
return errors.New("Invalid Path")
|
||||
} else if len(list) > 1 {
|
||||
for i := range list {
|
||||
fmt.Printf("%d. %s\n", i+1, list[i])
|
||||
}
|
||||
idx := util.PromptUserInt("Choice:")
|
||||
path = list[idx-1]
|
||||
} else {
|
||||
// Only one... Could be a single group nested, or it's an entry
|
||||
entry, entryErr = db.FindEntryFromRoot(path)
|
||||
if entryErr == nil {
|
||||
fmt.Println(entry.GetPassword())
|
||||
return util.WriteToClipboard(entry.GetPassword())
|
||||
}
|
||||
// It's a group
|
||||
path = list[0]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
123
cmd/rofi.go
Normal file
123
cmd/rofi.go
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.bullercodeworks.com/brian/keepass-cli/models"
|
||||
"git.bullercodeworks.com/brian/keepass-cli/util"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// rofiCmd represents the rofi command
|
||||
var rofiCmd = &cobra.Command{
|
||||
Use: "rofi",
|
||||
Short: "Rofi Menufication of keepass-cli",
|
||||
RunE: runRofi,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(rofiCmd)
|
||||
}
|
||||
|
||||
func runRofi(cmd *cobra.Command, args []string) error {
|
||||
var path []string
|
||||
for i := range args {
|
||||
path = append(path, strings.Split(args[i], "/")...)
|
||||
}
|
||||
var db *models.KeePassDB
|
||||
var havePassword bool
|
||||
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
|
||||
pass, err := util.ReadFile(pwFile)
|
||||
if err != nil {
|
||||
havePassword = false
|
||||
// See if we have a password...
|
||||
if len(args) == 1 {
|
||||
pass = args[0]
|
||||
} else {
|
||||
fmt.Println("Enter Master Password")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
havePassword = true
|
||||
}
|
||||
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !havePassword {
|
||||
if err = util.WritePWFile(pwFile, pass); err != nil {
|
||||
return err
|
||||
}
|
||||
path = []string{}
|
||||
}
|
||||
if len(path) == 0 {
|
||||
list := db.GetAllEntriesFromRoot()
|
||||
for i := range list {
|
||||
fmt.Println(strings.Join(list[i], "/"))
|
||||
}
|
||||
} else {
|
||||
// Only one... Could be a single group nested, or it's an entry
|
||||
if entry, entryErr := db.FindEntryFromRoot(path); entryErr != nil {
|
||||
return entryErr
|
||||
} else {
|
||||
return util.WriteToClipboard(entry.GetPassword())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runRofiSteps(cmd *cobra.Command, args []string) error {
|
||||
var path []string
|
||||
for i := range args {
|
||||
path = append(path, strings.Split(args[i], "/")...)
|
||||
}
|
||||
var db *models.KeePassDB
|
||||
var havePassword bool
|
||||
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
|
||||
pass, err := util.ReadFile(pwFile)
|
||||
if err != nil {
|
||||
havePassword = false
|
||||
// See if we have a password...
|
||||
if len(args) == 1 {
|
||||
pass = args[0]
|
||||
} else {
|
||||
fmt.Println("Enter Master Password")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
havePassword = true
|
||||
}
|
||||
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !havePassword {
|
||||
if err = util.WritePWFile(pwFile, pass); err != nil {
|
||||
return err
|
||||
}
|
||||
path = []string{}
|
||||
}
|
||||
list := db.GetGroupsAndEntriesFromRoot(path)
|
||||
if len(list) == 0 {
|
||||
return errors.New("Invalid Path")
|
||||
} else if len(list) > 1 {
|
||||
for i := range list {
|
||||
fmt.Println(strings.Join(list[i], "/"))
|
||||
}
|
||||
} else {
|
||||
// Only one... Could be a single group nested, or it's an entry
|
||||
entry, entryErr := db.FindEntryFromRoot(path)
|
||||
if entryErr == nil {
|
||||
return util.WriteToClipboard(entry.GetPassword())
|
||||
}
|
||||
// It's a group
|
||||
fmt.Println(strings.Join(list[0], "/"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
109
cmd/root.go
Normal file
109
cmd/root.go
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.bullercodeworks.com/brian/keepass-cli/cli"
|
||||
"git.bullercodeworks.com/brian/keepass-cli/util"
|
||||
gap "github.com/muesli/go-app-paths"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"golang.design/x/clipboard"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var (
|
||||
Version = "1.0"
|
||||
Build = "1"
|
||||
Name = "keepass-cli"
|
||||
configFile string
|
||||
ConfigDir = ""
|
||||
program cli.Program
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "keepass-cli",
|
||||
Short: "CLI for Managing KeePass Files",
|
||||
}
|
||||
)
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
program = cli.Program{}
|
||||
err := program.Initialize()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = rootCmd.Execute()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
err := clipboard.Init()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
rootCmd.Version = Version
|
||||
initConfig()
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
var firstDir string
|
||||
if configFile != "" {
|
||||
viper.SetConfigFile(configFile)
|
||||
} else {
|
||||
scope := gap.NewScope(gap.User, Name)
|
||||
dirs, err := scope.ConfigDirs()
|
||||
if err != nil {
|
||||
fmt.Println("Can't retrieve default config")
|
||||
os.Exit(1)
|
||||
}
|
||||
firstDir = dirs[0]
|
||||
for _, v := range dirs {
|
||||
viper.AddConfigPath(v)
|
||||
}
|
||||
viper.SetConfigName(Name)
|
||||
viper.SetConfigType("yaml")
|
||||
}
|
||||
configName := fmt.Sprintf("%s.yaml", Name)
|
||||
var createConfig bool
|
||||
ConfigDir = fmt.Sprintf("%s%s", firstDir, string(os.PathSeparator))
|
||||
configPath := fmt.Sprintf("%s%s", ConfigDir, configName)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
createConfig = true
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
fmt.Println("Config file not found.")
|
||||
} else {
|
||||
fmt.Println("Config file found, but some other error occurred.")
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
if createConfig {
|
||||
_, err := os.Stat(firstDir)
|
||||
if os.IsNotExist(err) {
|
||||
err := os.Mkdir(firstDir, 0755)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if err = viper.WriteConfigAs(configPath); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
db := viper.GetString("database")
|
||||
if db == "" {
|
||||
db = util.PromptUserTrimmed("Path to KeePass Database:")
|
||||
viper.Set("database", db)
|
||||
}
|
||||
viper.WriteConfig()
|
||||
}
|
37
cmd/test.go
Normal file
37
cmd/test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// testCmd represents the test command
|
||||
var testCmd = &cobra.Command{
|
||||
Use: "test",
|
||||
Short: "Run Tests",
|
||||
RunE: runTestCmd,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(testCmd)
|
||||
}
|
||||
|
||||
func runTestCmd(cmd *cobra.Command, args []string) error {
|
||||
/*
|
||||
var db *models.KeePassDB
|
||||
pass, err := util.PromptUserForPassword("Master Password")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err = models.NewKeePassDB("/home/brbuller/Nextcloud/Apps/keys/keys.copy.kdbx", pass)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range db.GetAllEntries() {
|
||||
fmt.Println(v)
|
||||
}
|
||||
*/
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user