From 9335e40d76059bf1851b92654b94e79d2ba2e5a1 Mon Sep 17 00:00:00 2001 From: Mohd Bilal Date: Mon, 28 Nov 2022 14:14:18 +0530 Subject: [PATCH] cli: remove `os.IsNotExist` and `os.IsExist` error checks PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7068 GitOrigin-RevId: 37735c0020df23857015871890d2f6774166d7b1 --- cli/cli.go | 12 +++++++----- cli/directory.go | 6 ++++-- cli/global_config.go | 6 ++++-- cli/plugins/scanner.go | 4 ++-- cli/util/copy.go | 16 ++++++++-------- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 254d111ca26..592ef1ed933 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -11,8 +11,10 @@ import ( "bytes" "context" "encoding/json" + stderrors "errors" "fmt" "io" + "io/fs" "io/ioutil" "net/http" "net/url" @@ -686,7 +688,7 @@ func (ec *ExecutionContext) Validate() error { // set name of migration directory ec.MigrationDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MigrationsDirectory) - if _, err := os.Stat(ec.MigrationDir); os.IsNotExist(err) { + if _, err := os.Stat(ec.MigrationDir); stderrors.Is(err, fs.ErrNotExist) { err = os.MkdirAll(ec.MigrationDir, os.ModePerm) if err != nil { return errors.E(op, fmt.Errorf("cannot create migrations directory: %w", err)) @@ -694,7 +696,7 @@ func (ec *ExecutionContext) Validate() error { } ec.SeedsDirectory = filepath.Join(ec.ExecutionDirectory, ec.Config.SeedsDirectory) - if _, err := os.Stat(ec.SeedsDirectory); os.IsNotExist(err) { + if _, err := os.Stat(ec.SeedsDirectory); stderrors.Is(err, fs.ErrNotExist) { err = os.MkdirAll(ec.SeedsDirectory, os.ModePerm) if err != nil { return errors.E(op, fmt.Errorf("cannot create seeds directory: %w", err)) @@ -704,7 +706,7 @@ func (ec *ExecutionContext) Validate() error { if ec.Config.Version >= V2 && ec.Config.MetadataDirectory != "" { if len(ec.Config.MetadataFile) > 0 { ec.MetadataFile = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataFile) - if _, err := os.Stat(ec.MetadataFile); os.IsNotExist(err) { + if _, err := os.Stat(ec.MetadataFile); stderrors.Is(err, fs.ErrNotExist) { if err := ioutil.WriteFile(ec.MetadataFile, []byte(""), os.ModePerm); err != nil { return errors.E(op, err) } @@ -720,7 +722,7 @@ func (ec *ExecutionContext) Validate() error { } // set name of metadata directory ec.MetadataDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataDirectory) - if _, err := os.Stat(ec.MetadataDir); os.IsNotExist(err) && !(len(ec.MetadataFile) > 0) { + if _, err := os.Stat(ec.MetadataDir); stderrors.Is(err, fs.ErrNotExist) && !(len(ec.MetadataFile) > 0) { err = os.MkdirAll(ec.MetadataDir, os.ModePerm) if err != nil { return errors.E(op, fmt.Errorf("cannot create metadata directory: %w", err)) @@ -952,7 +954,7 @@ func (ec *ExecutionContext) loadEnvfile() error { if ec.Envfile != ".env" { return errors.E(op, err) } - if !os.IsNotExist(err) { + if !stderrors.Is(err, fs.ErrNotExist) { ec.Logger.Warn(err) } } diff --git a/cli/directory.go b/cli/directory.go index 36457913132..ab13fc88d69 100644 --- a/cli/directory.go +++ b/cli/directory.go @@ -1,7 +1,9 @@ package cli import ( + stderrors "errors" "fmt" + "io/fs" "os" "path/filepath" "regexp" @@ -36,7 +38,7 @@ func (ec *ExecutionContext) validateDirectory() error { ed, err := os.Stat(ec.ExecutionDirectory) if err != nil { - if os.IsNotExist(err) { + if stderrors.Is(err, fs.ErrNotExist) { return errors.E(op, fmt.Errorf("did not find required directory. use 'init'?: %w", err)) } return errors.E(op, fmt.Errorf("error getting directory details: %w", err)) @@ -89,7 +91,7 @@ func ValidateDirectory(dir string) error { var op errors.Op = "cli.ValidateDirectory" notFound := []string{} for _, f := range filesRequired { - if _, err := os.Stat(filepath.Join(dir, f)); os.IsNotExist(err) { + if _, err := os.Stat(filepath.Join(dir, f)); stderrors.Is(err, fs.ErrNotExist) { relpath, e := filepath.Rel(dir, f) if e == nil { f = relpath diff --git a/cli/global_config.go b/cli/global_config.go index 4ffaf8dbf96..a821339dbdd 100644 --- a/cli/global_config.go +++ b/cli/global_config.go @@ -2,7 +2,9 @@ package cli import ( "encoding/json" + stderrors "errors" "fmt" + "io/fs" "io/ioutil" "os" "path/filepath" @@ -138,7 +140,7 @@ func (ec *ExecutionContext) setupGlobalConfig() error { // check if the global config file exist _, err = os.Stat(ec.GlobalConfigFile) - if os.IsNotExist(err) { + if stderrors.Is(err, fs.ErrNotExist) { // file does not exist, teat as first run and create it ec.Logger.Debug("global config file does not exist, this could be the first run, creating it...") @@ -162,7 +164,7 @@ func (ec *ExecutionContext) setupGlobalConfig() error { // also show a notice about telemetry ec.Logger.Info(TelemetryNotice) - } else if os.IsExist(err) || err == nil { + } else if stderrors.Is(err, fs.ErrExist) || err == nil { // file exists, verify contents ec.Logger.Debug("global config file exists, verifying contents") diff --git a/cli/plugins/scanner.go b/cli/plugins/scanner.go index 8a5df4bbb6d..ac719a82395 100644 --- a/cli/plugins/scanner.go +++ b/cli/plugins/scanner.go @@ -64,7 +64,7 @@ func (c *Config) LoadPluginListFromFS(indexDir string) (Plugins, error) { } // LoadPluginByName loads a plugins index file by its name. When plugin -// file not found, it returns an error that can be checked with os.IsNotExist. +// file not found, it returns an error that can be checked with stderrors.Is(err, fs.ErrNotExist) func (c *Config) LoadPluginByName(pluginName string) (*PluginVersions, error) { var op errors.Op = "plugins.Config.LoadPluginByName" c.Logger.Debugf("loading plugin %s", pluginName) @@ -123,7 +123,7 @@ func (c *Config) LoadPlugins(files []string, pluginName ...string) Plugins { } // ReadPluginFromFile loads a file from the FS. When plugin file not found, it -// returns an error that can be checked with os.IsNotExist. +// returns an error that can be checked with stderrors.Is(err, fs.ErrNotExist). func (c *Config) ReadPluginFromFile(path string) (Plugin, error) { var op errors.Op = "plugins.Config.ReadPluginFromFile" f, err := os.Open(path) diff --git a/cli/util/copy.go b/cli/util/copy.go index ac9c65ccdd1..118cdf37188 100644 --- a/cli/util/copy.go +++ b/cli/util/copy.go @@ -159,12 +159,12 @@ func CopyFileAfero(fs afero.Fs, src, dst string) error { } // CopyDir but with afero -func CopyDirAfero(fs afero.Fs, src string, dst string) error { +func CopyDirAfero(afs afero.Fs, src string, dst string) error { var op errors.Op = "util.CopyDirAfero" src = filepath.Clean(src) dst = filepath.Clean(dst) - si, err := fs.Stat(src) + si, err := afs.Stat(src) if err != nil { return errors.E(op, err) } @@ -172,20 +172,20 @@ func CopyDirAfero(fs afero.Fs, src string, dst string) error { return errors.E(op, fmt.Errorf("source is not a directory")) } - _, err = fs.Stat(dst) - if err != nil && !os.IsNotExist(err) { + _, err = afs.Stat(dst) + if err != nil && !stderrors.Is(err, fs.ErrNotExist) { return errors.E(op, err) } if err == nil { return errors.E(op, fmt.Errorf("destination already exists")) } - err = fs.MkdirAll(dst, si.Mode()) + err = afs.MkdirAll(dst, si.Mode()) if err != nil { return errors.E(op, err) } - entries, err := afero.ReadDir(fs, src) + entries, err := afero.ReadDir(afs, src) if err != nil { return errors.E(op, err) } @@ -195,7 +195,7 @@ func CopyDirAfero(fs afero.Fs, src string, dst string) error { dstPath := filepath.Join(dst, entry.Name()) if entry.IsDir() { - err = CopyDirAfero(fs, srcPath, dstPath) + err = CopyDirAfero(afs, srcPath, dstPath) if err != nil { return errors.E(op, err) } @@ -205,7 +205,7 @@ func CopyDirAfero(fs afero.Fs, src string, dst string) error { continue } - err = CopyFileAfero(fs, srcPath, dstPath) + err = CopyFileAfero(afs, srcPath, dstPath) if err != nil { return errors.E(op, err) }