@@ -11,15 +11,14 @@ import (
1111 "io/ioutil"
1212 "log"
1313 "os"
14- "os/user"
15- "path"
1614 "path/filepath"
1715 "strings"
1816
1917 "github.com/google/uuid"
2018 "github.com/logrusorgru/aurora"
2119 "github.com/pkg/errors"
2220 "github.com/prysmaticlabs/prysm/shared/bls"
21+ "github.com/prysmaticlabs/prysm/shared/fileutil"
2322 "github.com/prysmaticlabs/prysm/shared/params"
2423 "github.com/prysmaticlabs/prysm/shared/promptutil"
2524 v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
@@ -92,7 +91,7 @@ func decrypt(cliCtx *cli.Context) error {
9291 if keystorePath == "" {
9392 return errors .New ("--keystore must be set" )
9493 }
95- fullPath , err := expandPath (keystorePath )
94+ fullPath , err := fileutil . ExpandPath (keystorePath )
9695 if err != nil {
9796 return errors .Wrapf (err , "could not expand path: %s" , keystorePath )
9897 }
@@ -104,7 +103,7 @@ func decrypt(cliCtx *cli.Context) error {
104103 return nil
105104 })
106105 }
107- isDir , err := hasDir (fullPath )
106+ isDir , err := fileutil . HasDir (fullPath )
108107 if err != nil {
109108 return errors .Wrapf (err , "could not check if path exists: %s" , fullPath )
110109 }
@@ -149,15 +148,11 @@ func encrypt(cliCtx *cli.Context) error {
149148 if outputPath == "" {
150149 return errors .New ("--output-path must be set" )
151150 }
152- fullPath , err := expandPath (outputPath )
151+ fullPath , err := fileutil . ExpandPath (outputPath )
153152 if err != nil {
154153 return errors .Wrapf (err , "could not expand path: %s" , outputPath )
155154 }
156- exists , err := fileExists (fullPath )
157- if err != nil {
158- return errors .Wrapf (err , "could not check if file exists: %s" , fullPath )
159- }
160- if exists {
155+ if fileutil .FileExists (fullPath ) {
161156 response , err := promptutil .ValidatePrompt (
162157 fmt .Sprintf ("file at path %s already exists, are you sure you want to overwrite it? [y/n]" , fullPath ),
163158 func (s string ) error {
@@ -263,58 +258,3 @@ func readAndDecryptKeystore(fullPath string, password string) error {
263258 fmt .Printf ("Pubkey: %#x\n " , au .BrightGreen (pubKeyBytes ))
264259 return nil
265260}
266-
267- // Checks if the item at the specified path exists and is a directory.
268- func hasDir (dirPath string ) (bool , error ) {
269- fullPath , err := expandPath (dirPath )
270- if err != nil {
271- return false , err
272- }
273- info , err := os .Stat (fullPath )
274- if err != nil {
275- if os .IsNotExist (err ) {
276- return false , nil
277- }
278- return false , err
279- }
280- return info .IsDir (), nil
281- }
282-
283- // Check if a file at the specified path exists.
284- func fileExists (filePath string ) (bool , error ) {
285- fullPath , err := expandPath (filePath )
286- if err != nil {
287- return false , err
288- }
289- if _ , err := os .Stat (fullPath ); err != nil {
290- if os .IsNotExist (err ) {
291- return false , nil
292- }
293- return false , err
294- }
295- return true , nil
296- }
297-
298- // Expands a file path
299- // 1. replace tilde with users home dir
300- // 2. expands embedded environment variables
301- // 3. cleans the path, e.g. /a/b/../c -> /a/c
302- // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
303- func expandPath (p string ) (string , error ) {
304- if strings .HasPrefix (p , "~/" ) || strings .HasPrefix (p , "~\\ " ) {
305- if home := homeDir (); home != "" {
306- p = home + p [1 :]
307- }
308- }
309- return filepath .Abs (path .Clean (os .ExpandEnv (p )))
310- }
311-
312- func homeDir () string {
313- if home := os .Getenv ("HOME" ); home != "" {
314- return home
315- }
316- if usr , err := user .Current (); err == nil {
317- return usr .HomeDir
318- }
319- return ""
320- }
0 commit comments