Skip to content

Issue 9 argparse #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 40 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ should work.

Then,

$ make
go: downloading github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
go: downloading github.com/pborman/getopt/v2 v2.1.0
go: downloading golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
go: downloading gorm.io/driver/sqlite v1.2.3
...
$ make
Building varuh
go: downloading github.com/akamensky/argparse v1.3.1
go: downloading golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
go: downloading github.com/atotto/clipboard v0.1.4
go: downloading github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
go: downloading github.com/pythonhacker/argparse v1.3.2
go: downloading gorm.io/driver/sqlite v1.2.3
...

$ sudo make install
Installing varuh...done
Expand All @@ -66,55 +69,43 @@ The binary will be installed in `/usr/local/bin` folder.
Usage
=====

$ varuh -h


SYNOPSIS

varuh [options] [flags]

OPTIONS

EDIT/CREATE ACTIONS:

-A --add Add a new entry
-I --init <path> Initialize a new database
-R --remove <id> Remove an entry
-e --encrypt Encrypt the current database
-d --decrypt <path> Decrypt password database
-C --clone <id> Clone an entry
-U --use-db <path> Set as active database
-E --edit <id> Edit entry by id

FIND/LIST ACTIONS:

-f --find <term> Search entries
-l --list-entry <id> List entry by id
-x --export <filename> Export all entries to <filename>
-p --path Show current database path
-a --list-all List all entries in current database

MISC ACTIONS:

-g --genpass <length> Generate password of given length

HELP ACTIONS:

-h --help Print this help message and exit
-v --version Show version information and exit

FLAGS:

-s --show Show passwords when listing entries
-c --copy Copy password to clipboard
$ ./varuh -h
usage: varuh [-h|--help] [-I|--init "<value>"] [-d|--decrypt "<value>"]
[-C|--clone "<value>"] [-R|--remove "<value>"] [-U|--use-db
"<value>"] [-f|--find "<value>"] [-E|--edit "<value>"]
[-l|--list-entry "<value>"] [-x|--export "<value>"] [-g|--genpass
"<value>"] [-e|--encrypt] [-A|--add] [-p|--path] [-a|--list-all]
[-s|--show] [-c|--copy] [-v|--version]

Password manager for the command line for Unix like operating
systems

Options:

-h --help Print help information
-I --init <path> Initialize a new database
-d --decrypt <path> Decrypt password database
-C --clone <id> Clone an entry with <id>
-R --remove <id> Remove an entry with <id>
-U --use-db <path> Set <path> as active database
-f --find <term> Search entries with <term>
-E --edit <id> Edit entry by <id>
-l --list-entry <id> List entry by <id>
-x --export <filename> Export all entries to <filename>
-g --genpass <length> Generate password of given <length>
-e --encrypt Encrypt the current database
-A --add Add a new entry
-p --path Show current database path
-a --list-all List all entries in current database
-s --show Show passwords when listing entries
-c --copy Copy password to clipboard
-v --version Show version information and exit


AUTHORS
Copyright (C) 2021 Anand B Pillai <[email protected]>


The command line flags are grouped into `Edit/Create`, `Find/List`, `Misc` and `Help` actions. The first group of actions allows you to work with password databases and perform create/edit as well as encrypt/decrypt actions. The second set of actions allows you to work with an active decrypted database and view/search/list entries.

Encryption and Security
=======================

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module varuh
go 1.16

require (
github.com/akamensky/argparse v1.3.1
github.com/atotto/clipboard v0.1.4
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/pborman/getopt/v2 v2.1.0
github.com/pythonhacker/argparse v1.3.2
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
gorm.io/driver/sqlite v1.2.3
gorm.io/gorm v1.22.2
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/akamensky/argparse v1.3.1 h1:kP6+OyvR0fuBH6UhbE6yh/nskrDEIQgEA1SUXDPjx4g=
github.com/akamensky/argparse v1.3.1/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -11,10 +13,10 @@ github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNq
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pborman/getopt/v2 v2.1.0 h1:eNfR+r+dWLdWmV8g5OlpyrTYHkhVNxHBdN2cCrJmOEA=
github.com/pborman/getopt/v2 v2.1.0/go.mod h1:4NtW75ny4eBw9fO1bhtNdYTlZKYX5/tBLtsOpwKIKd0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pythonhacker/argparse v1.3.2 h1:JOojnYFHk7oap+MQiFgiPAHlzvhJfqukErLneWaHR/M=
github.com/pythonhacker/argparse v1.3.2/go.mod h1:gdUstTr/g1ojhRwrF9gKFOVLwsNfwarBg8aCQRjtvo8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
105 changes: 80 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@ package main

import (
"fmt"
"strconv"
getopt "github.com/pborman/getopt/v2"
"github.com/pythonhacker/argparse"
"os"
"strconv"
)

const VERSION = 0.2
const VERSION = 0.3
const APP = "varuh"
const AUTHOR_EMAIL = "Anand B Pillai <[email protected]>"

const AUTHOR_INFO = `
AUTHORS
Copyright (C) 2021 Anand B Pillai <[email protected]>
`

type actionFunc func(string) error
type actionFunc2 func(string) (error, string)
type voidFunc func() error

// Structure to keep the options data
type CmdOption struct {
Short string
Long string
Help string
Path string
Default string
}

// Print the program's usage string and exit
func printUsage() error {
getopt.Usage()
// getopt.Usage()
os.Exit(0)

return nil
Expand All @@ -34,13 +47,13 @@ func printVersionInfo() error {
}

// Command-line wrapper to generateRandomPassword
func generatePassword(length string) (error, string) {
func genPass(length string) (error, string) {
var iLength int
var err error
var passwd string

iLength, _ = strconv.Atoi(length)
err, passwd = generateRandomPassword(iLength)
err, passwd = generatePassword(iLength)

if err != nil {
fmt.Printf("Error generating password - \"%s\"\n", err.Error())
Expand All @@ -53,12 +66,12 @@ func generatePassword(length string) (error, string) {
copyPasswordToClipboard(passwd)
fmt.Println("Password copied to clipboard")
}

return nil, passwd
}

// Perform an action by using the command line options map
func performAction(optMap map[string]interface{}, optionMap map[string]interface{}) {
// // Perform an action by using the command line options map
func performAction(optMap map[string]interface{}) {

var flag bool

Expand All @@ -84,7 +97,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface

stringActions2Map := map[string]actionFunc2{
"decrypt": decryptDatabase,
"genpass": generatePassword,
"genpass": genPass,
}

flagsActionsMap := map[string]voidFunc{
Expand Down Expand Up @@ -113,9 +126,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
}

for key, mappedFunc := range stringActionsMap {
option := optionMap[key].(Option)

if *optMap[key].(*string) != option.Path {
if *optMap[key].(*string) != "" {

var val = *(optMap[key].(*string))
mappedFunc(val)
Expand All @@ -129,10 +140,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
}

for key, mappedFunc := range stringActions2Map {
option := optionMap[key].(Option)

if *optMap[key].(*string) != option.Path {

if *optMap[key].(*string) != "" {
var val = *(optMap[key].(*string))
mappedFunc(val)
break
Expand All @@ -141,19 +149,66 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface

}

func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
var optMap map[string]interface{}

optMap = make(map[string]interface{})

stringOptions := []CmdOption{
{"I", "init", "Initialize a new database", "<path>", ""},
{"d", "decrypt", "Decrypt password database", "<path>", ""},
{"C", "clone", "Clone an entry with <id>", "<id>", ""},
{"R", "remove", "Remove an entry with <id>", "<id>", ""},
{"U", "use-db", "Set <path> as active database", "<path>", ""},
{"f", "find", "Search entries with <term>", "<term>", ""},
{"E", "edit", "Edit entry by <id>", "<id>", ""},
{"l", "list-entry", "List entry by <id>", "<id>", ""},
{"x", "export", "Export all entries to <filename>", "<filename>", ""},
{"g", "genpass", "Generate password of given <length>", "<length>", ""},
}

for _, opt := range stringOptions {
optMap[opt.Long] = parser.String(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
}

boolOptions := []CmdOption{
{"e", "encrypt", "Encrypt the current database", "", ""},
{"A", "add", "Add a new entry", "", ""},
{"p", "path", "Show current database path", "", ""},
{"a", "list-all", "List all entries in current database", "", ""},
{"s", "show", "Show passwords when listing entries", "", ""},
{"c", "copy", "Copy password to clipboard", "", ""},
{"v", "version", "Show version information and exit", "", ""},
{"h", "help", "Print this help message and exit", "", ""},
}

for _, opt := range boolOptions {
optMap[opt.Long] = parser.Flag(string(opt.Short), opt.Long, &argparse.Options{Help: opt.Help})
}

return optMap
}

// Main routine
func main() {
if len(os.Args) == 1 {
os.Args = append(os.Args, "-h")
}

optMap, optionMap := initializeCommandLine()
getopt.SetUsage(func() {
usageString(optionMap)
})
parser := argparse.NewParser("varuh",
"Password manager for the command line for Unix like operating systems",
AUTHOR_INFO,
)

optMap := initializeCmdLine(parser)

err := parser.Parse(os.Args)

if err != nil {
fmt.Println(parser.Usage(err))
}

getopt.Parse()
getOrCreateLocalConfig(APP)

performAction(optMap, optionMap)
performAction(optMap)
}
Loading