Skip to content

Commit

Permalink
refactor: always check for exact os.Stat error
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebradil committed Sep 22, 2023
1 parent 127dd3c commit 7480b58
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 53 deletions.
7 changes: 4 additions & 3 deletions internal/myks/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -59,7 +58,9 @@ func NewApplication(e *Environment, name string, prototypeName string) (*Applica
e: e,
}

if _, err := os.Stat(prototype); err != nil {
if ok, err := isExist(app.Prototype); err != nil {
return app, err
} else if !ok {
return app, errors.New("application prototype does not exist")
}

Expand Down Expand Up @@ -130,7 +131,7 @@ func (a *Application) collectDataFiles() {
a.yttDataFiles = append(a.yttDataFiles, environmentDataFiles...)

protoDataFile := filepath.Join(a.Prototype, a.e.g.ApplicationDataFileName)
if _, err := os.Stat(protoDataFile); err == nil {
if ok, err := isExist(protoDataFile); ok && err == nil {
a.yttDataFiles = append(a.yttDataFiles, protoDataFile)
}

Expand Down
26 changes: 11 additions & 15 deletions internal/myks/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,14 @@ func (g *Globe) createBaseFileStructure(force bool) error {
log.Debug().Str("myks config file", myksConfigFile).Msg("")

if !force {
if _, err := os.Stat(envDir); err == nil {
return ErrBootstrapTargetExists{target: envDir}
}
if _, err := os.Stat(protoDir); err == nil {
return ErrBootstrapTargetExists{target: protoDir}
}
if _, err := os.Stat(renderedDir); err == nil {
return ErrBootstrapTargetExists{target: renderedDir}
}
if _, err := os.Stat(envsGitignoreFile); err == nil {
return ErrBootstrapTargetExists{target: envsGitignoreFile}
}
if _, err := os.Stat(myksConfigFile); err == nil {
return ErrBootstrapTargetExists{target: myksConfigFile}
for _, path := range []string{envDir, protoDir, renderedDir, envsGitignoreFile, myksConfigFile} {
ok, err := isExist(path)
if err != nil {
return err
}
if ok {
return ErrBootstrapTargetExists{target: path}
}
}
}

Expand Down Expand Up @@ -139,7 +133,9 @@ func (g *Globe) createBaseFileStructure(force bool) error {

func (g *Globe) createDataSchemaFile() string {
dataSchemaFileName := filepath.Join(g.RootDir, g.ServiceDirName, g.TempDirName, g.DataSchemaFileName)
if _, err := os.Stat(dataSchemaFileName); err != nil {
if ok, err := isExist(dataSchemaFileName); err != nil {
log.Fatal().Err(err).Msg("Unable to stat data schema file")
} else if !ok {
log.Debug().Msg("Unable to find data schema file, creating one")
if err := os.MkdirAll(filepath.Dir(dataSchemaFileName), 0o750); err != nil {
log.Fatal().Err(err).Msg("Unable to create data schema file directory")
Expand Down
2 changes: 1 addition & 1 deletion internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (e *Environment) collectBySubpath(subpath string) []string {
for _, level := range levels {
currentPath = filepath.Join(currentPath, level)
item := filepath.Join(currentPath, subpath)
if _, err := os.Stat(item); err == nil {
if ok, err := isExist(item); ok && err == nil {
items = append(items, item)
}
}
Expand Down
9 changes: 6 additions & 3 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ func New(rootDir string) *Globe {
}

yttLibraryDir := filepath.Join(g.RootDir, g.YttLibraryDirName)
if _, err := os.Stat(yttLibraryDir); err == nil {
if ok, err := isExist(yttLibraryDir); err != nil {
log.Fatal().Err(err).Str("path", yttLibraryDir).Msg("Unable to stat ytt library directory")
} else if ok {
g.extraYttPaths = append(g.extraYttPaths, yttLibraryDir)
}

Expand Down Expand Up @@ -302,8 +304,9 @@ func (g *Globe) collectEnvironmentsInPath(searchPath string) []string {
return err
}
if d != nil && d.IsDir() {
_, err := os.Stat(filepath.Join(path, g.EnvironmentDataFileName))
if err == nil {
if ok, err := isExist(filepath.Join(path, g.EnvironmentDataFileName)); err != nil {
return err
} else if ok {
env, err := NewEnvironment(g, path)
if err == nil {
g.environments[path] = env
Expand Down
5 changes: 3 additions & 2 deletions internal/myks/plugin_argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package myks
import (
"bytes"
_ "embed"
"os"
"path/filepath"
"text/template"

Expand Down Expand Up @@ -78,7 +77,9 @@ func (a *Application) renderArgoCD() (err error) {
yttFiles = append(yttFiles, a.yttDataFiles...)
// 3. Use argocd-specific data values, schemas, and overlays from the prototype
prototypeArgoCDDir := filepath.Join(a.Prototype, a.e.g.ArgoCDDataDirName)
if _, err := os.Stat(prototypeArgoCDDir); err == nil {
if ok, err := isExist(prototypeArgoCDDir); err != nil {
return err
} else if ok {
yttFiles = append(yttFiles, prototypeArgoCDDir)
}
// 4. Collection of environment argocd-specific data values and schemas, and overlays
Expand Down
21 changes: 11 additions & 10 deletions internal/myks/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ func (a *Application) runSliceFormatStore(previousStepFile string) error {
filePath := filepath.Join(destinationDir, fileName)
// FIXME: If a file already exists, we should merge the two documents (probably).
// For now, we just overwrite the file and log a warning.
if _, err := os.Stat(filePath); err == nil {
if ok, err := isExist(filePath); err != nil {
return err
} else if ok {
log.Warn().Str("file", filePath).Msg(a.Msg(sliceStepName, "File already exists, check duplicated resources"))
}
err = writeFile(filePath, data.Bytes())
Expand Down Expand Up @@ -148,16 +150,13 @@ func genRenderedResourceFileName(resource map[string]interface{}) string {

func (a *Application) getVendoredDir(dirname string) (string, error) {
resourceDir := a.expandPath(filepath.Join(a.e.g.VendorDirName, dirname))
if _, err := os.Stat(resourceDir); err != nil {
if os.IsNotExist(err) {
return "", nil
}

log.Warn().Err(err).Msg(a.Msg(renderStepName, "Unable to find vendor directory: "+resourceDir))
if ok, err := isExist(resourceDir); err != nil {
return "", err
} else if ok {
return resourceDir, nil
} else {
return "", nil
}

return resourceDir, nil
}

// prepareValuesFile generates values.yaml file from ytt data files and ytt templates
Expand All @@ -168,7 +167,9 @@ func (a *Application) prepareValuesFile(dirName string, resourceName string) (st
var valuesFiles []string

prototypeValuesFile := filepath.Join(a.Prototype, valuesFileName)
if _, err := os.Stat(prototypeValuesFile); err == nil {
if ok, err := isExist(prototypeValuesFile); err != nil {
return "", err
} else if ok {
valuesFiles = append(valuesFiles, prototypeValuesFile)
}

Expand Down
9 changes: 6 additions & 3 deletions internal/myks/render_ytt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package myks

import (
"errors"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -31,12 +30,16 @@ func (y *Ytt) Render(previousStepFile string) (string, error) {
}

vendorYttDir := y.app.expandPath(filepath.Join(y.app.e.g.VendorDirName, y.app.e.g.YttStepDirName))
if _, err := os.Stat(vendorYttDir); err == nil {
if ok, err := isExist(vendorYttDir); err != nil {
return "", err
} else if ok {
yttFiles = append(yttFiles, vendorYttDir)
}

prototypeYttDir := filepath.Join(y.app.Prototype, y.app.e.g.YttStepDirName)
if _, err := os.Stat(prototypeYttDir); err == nil {
if ok, err := isExist(prototypeYttDir); err != nil {
return "", err
} else if ok {
yttFiles = append(yttFiles, prototypeYttDir)
}

Expand Down
8 changes: 6 additions & 2 deletions internal/myks/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (a *Application) prepareSync() error {
var yttFiles []string

protoVendirDir := filepath.Join(a.Prototype, "vendir")
if _, err := os.Stat(protoVendirDir); err == nil {
if ok, err := isExist(protoVendirDir); err != nil {
return err
} else if ok {
yttFiles = append(yttFiles, protoVendirDir)
}

Expand Down Expand Up @@ -186,7 +188,9 @@ func readVendirDirHashes(vendirConfigFilePath string) (vendirDirHashes, error) {
}

func readSyncFile(vendirSyncFile string) (vendirDirHashes, error) {
if _, err := os.Stat(vendirSyncFile); err != nil {
if ok, err := isExist(vendirSyncFile); err != nil {
return nil, err
} else if !ok {
return vendirDirHashes{}, nil
}

Expand Down
45 changes: 31 additions & 14 deletions internal/myks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ func copyFileSystemToPath(source fs.FS, sourcePath string, destinationPath strin
}

func unmarshalYamlToMap(filePath string) (map[string]interface{}, error) {
if _, err := os.Stat(filePath); err != nil {
log.Debug().Str("filePath", filePath).Msg("Yaml not found.")
ok, err := isExist(filePath)
if err != nil {
return nil, err
}
if !ok {
return make(map[string]interface{}), nil
}

Expand Down Expand Up @@ -192,27 +195,28 @@ func hashString(s string) string {
}

func createDirectory(dir string) error {
if _, err := os.Stat(dir); err != nil {
err := os.MkdirAll(dir, 0o750)
if err != nil {
log.Error().Err(err).Msg("Unable to create directory: " + dir)
return err
}
if ok, err := isExist(dir); err != nil {
return err
} else if ok {
return nil
}

if err := os.MkdirAll(dir, 0o750); err != nil {
log.Error().Err(err).Str("dir", dir).Msg("Unable to create directory")
return err
}
return nil
}

func writeFile(path string, content []byte) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(dir, 0o750)
if err != nil {
if ok, err := isExist(dir); err != nil {
return err
} else if !ok {
if err := os.MkdirAll(dir, 0o750); err != nil {
log.Error().Err(err).Msg("Unable to create directory")
return err
}
} else if err != nil {
log.Error().Err(err).Msg("Unable to stat directory")
return err
}

return os.WriteFile(path, content, 0o600)
Expand Down Expand Up @@ -303,3 +307,16 @@ func concatenate[T any](slices ...[]T) []T {
}
return result
}

func isExist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
log.Trace().Str("path", path).Msg("File does not exist")
return false, nil
}
log.Error().Err(err).Msg("Unable to stat file")
return false, err
}

0 comments on commit 7480b58

Please sign in to comment.