Skip to content

Commit

Permalink
Merge pull request #2 from gdanko/use-config-file
Browse files Browse the repository at this point in the history
Use config file
  • Loading branch information
gdanko authored Aug 24, 2024
2 parents 4aaea42 + c9799e3 commit f551f57
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GOPATH := $(shell go env GOPATH)
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
ENPASS_VERSION := "0.4.10"
ENPASS_VERSION := "0.5.0"

GOOS ?= $(shell uname | tr '[:upper:]' '[:lower:]')
GOARCH ?=$(shell arch)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ I wrote some Python scripts to manipulate my Enpass database, but I wanted to do
* darwin or linux OS

## Installation
Clone the repository and from within the repository directory, type `make build`. This will create a directory with the given value of `GOOS` and install the binary there. It will also create a tarball which will eventually be used for Homebrew formulae.
* Clone the repository and from within the repository directory
* Type `make build`. This will create a directory with the given value of `GOOS` and install the binary there. It will also create a tarball which will eventually be used for Homebrew formulae.
* Copy <repo_root>/enpass.yml.SAMPLE to ~/.enpass.yml

## Features
* List all entries from the database
Expand All @@ -22,7 +24,7 @@ Clone the repository and from within the repository directory, type `make build`
* Filter by multiple logins (subtitles), titles, categories, or uuids using wildcards
* Colorize output for JSON, YAML, list, and default views
* Colors are defined centrally so all colorized outputs use the same color scheme
* And more....
* Basic options can be set in ~/.enpass.yml, please see enpass.yml.SAMPLE

## Usage
```
Expand Down
15 changes: 1 addition & 14 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ func copyPreRunCmd(cmd *cobra.Command, args []string) {
}

func copyRunCmd(cmd *cobra.Command, args []string) {
if vaultPath == "" {
vaultPath, err = enpass.FindDefaultVaultPath()
if err != nil {
logger.Error(err)
logger.Exit(2)
}
}

err = enpass.ValidateVaultPath(vaultPath)
if err != nil {
logger.Error(err)
logger.Exit(2)
}

vaultPath := enpass.DetermineVaultPath(logger, vaultPathFlag)
vault, credentials, err = enpass.OpenVault(logger, pinEnable, nonInteractive, vaultPath, keyFilePath, logLevel, nocolorFlag)
if err != nil {
logger.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func getListShowFlags(cmd *cobra.Command) {
}

func GetPersistenFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&vaultPath, "vault", "v", "", "Path to your Enpass vault.")
cmd.PersistentFlags().StringVarP(&vaultPathFlag, "vault", "v", "", "Path to your Enpass vault.")
cmd.PersistentFlags().StringVar(&cardType, "type", "password", "The type of your card. (password, ...)")
cmd.PersistentFlags().StringArrayVarP(&recordTitle, "title", "t", []string{}, "Filter based on record title. Wildcards (%) are allowed. Can be used multiple times.")
cmd.PersistentFlags().StringArrayVarP(&recordCategory, "category", "c", []string{}, "Filter based on record category. Wildcards (%) are allowed. Can be used multiple times.")
Expand Down
15 changes: 1 addition & 14 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,7 @@ func listPreRunCmd(cmd *cobra.Command, args []string) {
}

func listRunCmd(cmd *cobra.Command, args []string) {
if vaultPath == "" {
vaultPath, err = enpass.FindDefaultVaultPath()
if err != nil {
logger.Error(err)
logger.Exit(2)
}
}

err = enpass.ValidateVaultPath(vaultPath)
if err != nil {
logger.Error(err)
logger.Exit(2)
}

vaultPath := enpass.DetermineVaultPath(logger, vaultPathFlag)
vault, credentials, err = enpass.OpenVault(logger, pinEnable, nonInteractive, vaultPath, keyFilePath, logLevel, nocolorFlag)
if err != nil {
logger.Error(err)
Expand Down
15 changes: 1 addition & 14 deletions cmd/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@ func passPreRunCmd(cmd *cobra.Command, args []string) {
}

func passRunCmd(cmd *cobra.Command, args []string) {
if vaultPath == "" {
vaultPath, err = enpass.FindDefaultVaultPath()
if err != nil {
logger.Error(err)
logger.Exit(2)
}
}

err = enpass.ValidateVaultPath(vaultPath)
if err != nil {
logger.Error(err)
logger.Exit(2)
}

vaultPath := enpass.DetermineVaultPath(logger, vaultPathFlag)
vault, credentials, err = enpass.OpenVault(logger, pinEnable, nonInteractive, vaultPath, keyFilePath, logLevel, nocolorFlag)
if err != nil {
logger.Error(err)
Expand Down
43 changes: 36 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package cmd

import (
"path/filepath"
"strings"

"github.com/gdanko/enpass/globals"
"github.com/gdanko/enpass/pkg/enpass"
"github.com/gdanko/enpass/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -10,8 +15,10 @@ var (
cardType string
caseSensitive bool
clipboardPrimary bool
configPath string
credentials *enpass.VaultCredentials
defaultLogLevel = "info"
enpassConfig globals.EnpassConfig
err error
keyFilePath string
jsonFlag bool
Expand Down Expand Up @@ -40,13 +47,12 @@ var (
Short: "enpass is a command line interface for the Enpass password manager",
Long: "enpass is a command line interface for the Enpass password manager",
}
tableFlag bool
trashedFlag bool

vault *enpass.Vault
vaultPath string
versionFull bool
yamlFlag bool
tableFlag bool
trashedFlag bool
vault *enpass.Vault
vaultPathFlag string
versionFull bool
yamlFlag bool
)

func Execute() error {
Expand All @@ -55,4 +61,27 @@ func Execute() error {

func init() {
GetPersistenFlags(rootCmd)

logLevel = logLevelMap[logLevelStr]
logger = util.ConfigureLogger(logLevel, nocolorFlag)

// Set the home directory in globals
err = globals.SetHomeDirectory()
if err != nil {
logger.Error(err)
logger.Exit(2)
}

// Parse the config file and set the config object in globals
configPath = filepath.Join(globals.GetHomeDirectory(), "enpass.yml")
enpassConfig, err = util.ParseConfig(configPath)
if err == nil {
globals.SetConfig(enpassConfig)
} else {
if strings.Contains(err.Error(), "does not exist") {
logger.Infof("%s, using the default configuration", err)
} else if strings.Contains(err.Error(), "failed to read") || strings.Contains(err.Error(), "failed to parse") {
logger.Warningf("%s, using the default configuration", err)
}
}
}
15 changes: 1 addition & 14 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,7 @@ func showPreRunCmd(cmd *cobra.Command, args []string) {
}

func showRunCmd(cmd *cobra.Command, args []string) {
if vaultPath == "" {
vaultPath, err = enpass.FindDefaultVaultPath()
if err != nil {
logger.Error(err)
logger.Exit(2)
}
}

err = enpass.ValidateVaultPath(vaultPath)
if err != nil {
logger.Error(err)
logger.Exit(2)
}

vaultPath := enpass.DetermineVaultPath(logger, vaultPathFlag)
vault, credentials, err = enpass.OpenVault(logger, pinEnable, nonInteractive, vaultPath, keyFilePath, logLevel, nocolorFlag)
if err != nil {
logger.Error(err)
Expand Down
27 changes: 27 additions & 0 deletions enpass.yml.SAMPLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# Valid colors
# black-bold
# black
# blue-bold
# blue
# cyan-bold
# cyan
# green-bold
# green
# magenta-bold
# magenta
# red-bold
# red
# white-bold
# white
# yellow-bold
# yellow
colors:
alias_color: yellow-bold
anchor_color: yellow-bold
bool_color: yellow-bold
key_color: cyan-bold
null_color: black-bold
number_color: magenta-bold
string_color: green-bold
vault_path: ~/Documents/Enpass/Vaults/primary
72 changes: 59 additions & 13 deletions globals/globals.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
package globals

import (
"fmt"
"os/user"
"sync"

"github.com/fatih/color"
)

type Colors struct {
AliasColor string `yaml:"alias_color"`
AnchorColor string `yaml:"anchor_color"`
BoolColor string `yaml:"bool_color"`
KeyColor string `yaml:"key_color"`
NullColor string `yaml:"null_color"`
NumberColor string `yaml:"number_color"`
StringColor string `yaml:"string_color"`
}

type EnpassConfig struct {
VaultPath string `yaml:"vault_path"`
VaultPassword string `yaml:"vault_password"`
Colors Colors `yaml:"colors"`
}

var (
colorMap = map[string]color.Attribute{
"AliasColor": color.FgHiYellow,
"AnchorColor": color.FgHiYellow,
"BoolColor": color.FgHiYellow,
"KeyColor": color.FgHiCyan,
"NullColor": color.FgHiBlack,
"NumberColor": color.FgHiMagenta,
"StringColor": color.FgHiGreen,
enpassConfig = EnpassConfig{
VaultPath: "~/Documents/Enpass/Vaults/primary",
Colors: Colors{
AliasColor: "yellow-bold",
AnchorColor: "yellow-bold",
BoolColor: "yellow-bold",
KeyColor: "cyan-bold",
NullColor: "black-bold",
NumberColor: "magenta-bold",
StringColor: "green-bold",
},
}
mu sync.RWMutex
homeDir string
mu sync.RWMutex
)

func GetColorMap() (x map[string]color.Attribute) {
// Set and get pairs
func SetConfig(x EnpassConfig) {
mu.Lock()
enpassConfig = x
mu.Unlock()
}

func GetConfig() (x EnpassConfig) {
mu.Lock()
x = enpassConfig
mu.Unlock()
return x
}

func SetHomeDirectory() (err error) {
mu.Lock()
userObj, err := user.Current()
if err != nil {
mu.Unlock()
return fmt.Errorf("failed to determine the path of your home directory: %s", err)
}
homeDir = userObj.HomeDir
mu.Unlock()
return nil
}

func GetHomeDirectory() (x string) {
mu.Lock()
x = colorMap
x = homeDir
mu.Unlock()
return x
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/thoas/go-funk v0.9.3
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.26.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.25.11
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
Expand Down
Loading

0 comments on commit f551f57

Please sign in to comment.