@@ -10,14 +10,15 @@ import (
1010
1111 "github.com/goccy/go-yaml"
1212 "github.com/tjblackheart/andcli/v2/internal/buildinfo"
13+ "github.com/tjblackheart/andcli/v2/internal/vaults"
1314)
1415
1516type (
1617 Config struct {
17- File string `yaml:"file"`
18- Type string `yaml:"type"`
19- ClipboardCmd string `yaml:"clipboard_cmd"`
20- Options * Opts `yaml:"options"`
18+ File string `yaml:"file"`
19+ Type vaults. Type `yaml:"type"`
20+ ClipboardCmd string `yaml:"clipboard_cmd"`
21+ Options * Opts `yaml:"options"`
2122 //
2223 path string
2324 passwordFromStdin bool
@@ -70,81 +71,80 @@ func create(dir string) (*Config, error) {
7071}
7172
7273// Writes the current configuration to a yaml file.
73- func (c Config ) Persist () error {
74- b , err := yaml .Marshal (c )
74+ func (cfg Config ) Persist () error {
75+ b , err := yaml .Marshal (cfg )
7576 if err != nil {
7677 return err
7778 }
7879
79- return os .WriteFile (c .path , b , 0600 )
80+ return os .WriteFile (cfg .path , b , 0o600 )
8081}
8182
8283// Returns true if the flag option "passwd-stdin" was set.
83- func (c Config ) PasswdStdin () bool {
84- return c .passwordFromStdin
84+ func (cfg Config ) PasswdStdin () bool {
85+ return cfg .passwordFromStdin
8586}
8687
8788// Reads an possibly existing config file and merges the content
8889// into the current config.
89- func (c * Config ) mergeExisting () error {
90- if _ , err := os .Stat (c .path ); os .IsNotExist (err ) {
90+ func (cfg * Config ) mergeExisting () error {
91+ if _ , err := os .Stat (cfg .path ); os .IsNotExist (err ) {
9192 return nil
9293 }
9394
94- b , err := os .ReadFile (c .path )
95+ b , err := os .ReadFile (cfg .path )
9596 if err != nil {
9697 return err
9798 }
9899
99- var existing = new (Config )
100+ existing : = new (Config )
100101 if err := yaml .Unmarshal (b , existing ); err != nil {
101102 return err
102103 }
103104
104- c .File = existing .File
105- c .Type = existing .Type
106- c .ClipboardCmd = existing .ClipboardCmd
105+ cfg .File = existing .File
106+ cfg .Type = existing .Type
107+ cfg .ClipboardCmd = existing .ClipboardCmd
107108
108109 if existing .Options != nil {
109- c .Options = existing .Options
110+ cfg .Options = existing .Options
110111 }
111112
112113 return nil
113114}
114115
115116// Validates the current configuration.
116- func (c * Config ) validate () error {
117-
118- if c .File == "" {
117+ func (cfg * Config ) validate () error {
118+ if cfg .File == "" {
119119 return errors .New ("no vault file specified" )
120120 }
121121
122- if c .Type == "" {
122+ if cfg .Type == "" {
123123 return errors .New ("no vault type specified" )
124124 }
125125
126126 var err error
127- if c .File , err = filepath .Abs (c .File ); err != nil {
128- return fmt .Errorf ("%s: %s" , c .File , err )
127+ if cfg .File , err = filepath .Abs (cfg .File ); err != nil {
128+ return fmt .Errorf ("%s: %s" , cfg .File , err )
129129 }
130130
131- fi , err := os .Stat (c .File )
131+ fi , err := os .Stat (cfg .File )
132132 if err != nil {
133- return fmt .Errorf ("%s: %s" , c .File , err )
133+ return fmt .Errorf ("%s: %s" , cfg .File , err )
134134 }
135135
136136 if fi .IsDir () {
137- return fmt .Errorf ("%s: is a directory, not a vault file" , c .File )
137+ return fmt .Errorf ("%s: is a directory, not a vault file" , cfg .File )
138138 }
139139
140140 // if set, check if the basic clipboard cmd is available in system PATH.
141141 // the option parsing is done at a later time.
142- if parts := strings .SplitN (c .ClipboardCmd , " " , 2 ); parts [0 ] != "" {
142+ if parts := strings .SplitN (cfg .ClipboardCmd , " " , 2 ); parts [0 ] != "" {
143143 path , err := exec .LookPath (parts [0 ])
144144 if err != nil {
145145 return fmt .Errorf ("%s: %s" , parts [0 ], err )
146146 }
147- c .ClipboardCmd = strings .ReplaceAll (c .ClipboardCmd , parts [0 ], path )
147+ cfg .ClipboardCmd = strings .ReplaceAll (cfg .ClipboardCmd , parts [0 ], path )
148148 }
149149
150150 return nil
0 commit comments