cli: remove os.IsNotExist and os.IsExist error checks

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7068
GitOrigin-RevId: 37735c0020df23857015871890d2f6774166d7b1
This commit is contained in:
Mohd Bilal 2022-11-28 14:14:18 +05:30 committed by hasura-bot
parent b74c2952dd
commit 9335e40d76
5 changed files with 25 additions and 19 deletions

View File

@ -11,8 +11,10 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
stderrors "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -686,7 +688,7 @@ func (ec *ExecutionContext) Validate() error {
// set name of migration directory // set name of migration directory
ec.MigrationDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MigrationsDirectory) 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) err = os.MkdirAll(ec.MigrationDir, os.ModePerm)
if err != nil { if err != nil {
return errors.E(op, fmt.Errorf("cannot create migrations directory: %w", err)) 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) 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) err = os.MkdirAll(ec.SeedsDirectory, os.ModePerm)
if err != nil { if err != nil {
return errors.E(op, fmt.Errorf("cannot create seeds directory: %w", err)) 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 ec.Config.Version >= V2 && ec.Config.MetadataDirectory != "" {
if len(ec.Config.MetadataFile) > 0 { if len(ec.Config.MetadataFile) > 0 {
ec.MetadataFile = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataFile) 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 { if err := ioutil.WriteFile(ec.MetadataFile, []byte(""), os.ModePerm); err != nil {
return errors.E(op, err) return errors.E(op, err)
} }
@ -720,7 +722,7 @@ func (ec *ExecutionContext) Validate() error {
} }
// set name of metadata directory // set name of metadata directory
ec.MetadataDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataDirectory) 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) err = os.MkdirAll(ec.MetadataDir, os.ModePerm)
if err != nil { if err != nil {
return errors.E(op, fmt.Errorf("cannot create metadata directory: %w", err)) 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" { if ec.Envfile != ".env" {
return errors.E(op, err) return errors.E(op, err)
} }
if !os.IsNotExist(err) { if !stderrors.Is(err, fs.ErrNotExist) {
ec.Logger.Warn(err) ec.Logger.Warn(err)
} }
} }

View File

@ -1,7 +1,9 @@
package cli package cli
import ( import (
stderrors "errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -36,7 +38,7 @@ func (ec *ExecutionContext) validateDirectory() error {
ed, err := os.Stat(ec.ExecutionDirectory) ed, err := os.Stat(ec.ExecutionDirectory)
if err != nil { 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("did not find required directory. use 'init'?: %w", err))
} }
return errors.E(op, fmt.Errorf("error getting directory details: %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" var op errors.Op = "cli.ValidateDirectory"
notFound := []string{} notFound := []string{}
for _, f := range filesRequired { 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) relpath, e := filepath.Rel(dir, f)
if e == nil { if e == nil {
f = relpath f = relpath

View File

@ -2,7 +2,9 @@ package cli
import ( import (
"encoding/json" "encoding/json"
stderrors "errors"
"fmt" "fmt"
"io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -138,7 +140,7 @@ func (ec *ExecutionContext) setupGlobalConfig() error {
// check if the global config file exist // check if the global config file exist
_, err = os.Stat(ec.GlobalConfigFile) _, 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 // 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...") 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 // also show a notice about telemetry
ec.Logger.Info(TelemetryNotice) ec.Logger.Info(TelemetryNotice)
} else if os.IsExist(err) || err == nil { } else if stderrors.Is(err, fs.ErrExist) || err == nil {
// file exists, verify contents // file exists, verify contents
ec.Logger.Debug("global config file exists, verifying contents") ec.Logger.Debug("global config file exists, verifying contents")

View File

@ -64,7 +64,7 @@ func (c *Config) LoadPluginListFromFS(indexDir string) (Plugins, error) {
} }
// LoadPluginByName loads a plugins index file by its name. When plugin // 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) { func (c *Config) LoadPluginByName(pluginName string) (*PluginVersions, error) {
var op errors.Op = "plugins.Config.LoadPluginByName" var op errors.Op = "plugins.Config.LoadPluginByName"
c.Logger.Debugf("loading plugin %s", pluginName) 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 // 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) { func (c *Config) ReadPluginFromFile(path string) (Plugin, error) {
var op errors.Op = "plugins.Config.ReadPluginFromFile" var op errors.Op = "plugins.Config.ReadPluginFromFile"
f, err := os.Open(path) f, err := os.Open(path)

View File

@ -159,12 +159,12 @@ func CopyFileAfero(fs afero.Fs, src, dst string) error {
} }
// CopyDir but with afero // 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" var op errors.Op = "util.CopyDirAfero"
src = filepath.Clean(src) src = filepath.Clean(src)
dst = filepath.Clean(dst) dst = filepath.Clean(dst)
si, err := fs.Stat(src) si, err := afs.Stat(src)
if err != nil { if err != nil {
return errors.E(op, err) 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")) return errors.E(op, fmt.Errorf("source is not a directory"))
} }
_, err = fs.Stat(dst) _, err = afs.Stat(dst)
if err != nil && !os.IsNotExist(err) { if err != nil && !stderrors.Is(err, fs.ErrNotExist) {
return errors.E(op, err) return errors.E(op, err)
} }
if err == nil { if err == nil {
return errors.E(op, fmt.Errorf("destination already exists")) return errors.E(op, fmt.Errorf("destination already exists"))
} }
err = fs.MkdirAll(dst, si.Mode()) err = afs.MkdirAll(dst, si.Mode())
if err != nil { if err != nil {
return errors.E(op, err) return errors.E(op, err)
} }
entries, err := afero.ReadDir(fs, src) entries, err := afero.ReadDir(afs, src)
if err != nil { if err != nil {
return errors.E(op, err) 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()) dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() { if entry.IsDir() {
err = CopyDirAfero(fs, srcPath, dstPath) err = CopyDirAfero(afs, srcPath, dstPath)
if err != nil { if err != nil {
return errors.E(op, err) return errors.E(op, err)
} }
@ -205,7 +205,7 @@ func CopyDirAfero(fs afero.Fs, src string, dst string) error {
continue continue
} }
err = CopyFileAfero(fs, srcPath, dstPath) err = CopyFileAfero(afs, srcPath, dstPath)
if err != nil { if err != nil {
return errors.E(op, err) return errors.E(op, err)
} }