Skip to content

Commit

Permalink
user errors.Wrap to give context to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking authored and czimergebot committed Jun 28, 2018
1 parent 93c11d4 commit a13aa46
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
31 changes: 16 additions & 15 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/chanzuckerberg/fogg/templates"
"github.com/chanzuckerberg/fogg/util"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/spf13/afero"
)

Expand All @@ -20,22 +21,22 @@ const rootPath = "terraform"
func Apply(fs afero.Fs, configFile string, tmp *templates.T) error {
p, err := plan.Eval(fs, configFile)
if err != nil {
return err
return errors.Wrap(err, "unable to evaluate plan")
}

e := applyRepo(fs, p, &tmp.Repo)
if e != nil {
return e
return errors.Wrap(e, "unable to apply repo")
}

e = applyAccounts(fs, p, &tmp.Account)
if e != nil {
return e
return errors.Wrap(e, "unable to apply accounts")
}

e = applyEnvs(fs, p, &tmp.Env, &tmp.Component)
if e != nil {
return e
return errors.Wrap(e, "unable to apply envs")
}

e = applyGlobal(fs, p.Global, &tmp.Global)
Expand Down Expand Up @@ -66,11 +67,11 @@ func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) {
path := fmt.Sprintf("%s/accounts/%s", rootPath, account)
e = fs.MkdirAll(path, 0755)
if e != nil {
return e
return errors.Wrap(e, "unable to make directories for accounts")
}
e = applyTree(accountBox, afero.NewBasePathFs(fs, path), accountPlan)
if e != nil {
return e
return errors.Wrap(e, "unable to apply templates to account")
}
}
return nil
Expand All @@ -81,21 +82,21 @@ func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr
path := fmt.Sprintf("%s/envs/%s", rootPath, env)
e = fs.MkdirAll(path, 0755)
if e != nil {
return e
return errors.Wrap(e, "unable to make directies for envs")
}
e := applyTree(envBox, afero.NewBasePathFs(fs, path), envPlan)
if e != nil {
return e
return errors.Wrap(e, "unable to apply templates to env")
}
for component, componentPlan := range envPlan.Components {
path := fmt.Sprintf("%s/envs/%s/%s", rootPath, env, component)
e = fs.MkdirAll(path, 0755)
if e != nil {
return e
return errors.Wrap(e, "unable to make directories for component")
}
e := applyTree(componentBox, afero.NewBasePathFs(fs, path), componentPlan)
if e != nil {
return e
return errors.Wrap(e, "unable to apply templates for component")
}
}
}
Expand All @@ -110,7 +111,7 @@ func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) {

err := applyTemplate(sourceFile, dest, basename, subst)
if err != nil {
return err
return errors.Wrap(err, "unable to apply template")
}

// if dest.endswith('.tf'):
Expand All @@ -129,7 +130,7 @@ func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) {
log.Printf("copying %s", path)
e = afero.WriteReader(dest, path, sourceFile)
if e != nil {
return e
return errors.Wrap(e, "unable to copy file")
}
}
return nil
Expand All @@ -143,7 +144,7 @@ func touchFile(dest afero.Fs, path string) error {
log.Printf("touching %s", path)
_, err = dest.Create(path)
if err != nil {
return err
return errors.Wrap(err, "unable to touch file")
}
} else {
log.Printf("skipping touch on existing file %s", path)
Expand All @@ -157,7 +158,7 @@ func createFile(dest afero.Fs, path string, sourceFile io.Reader) error {
log.Printf("creating %s", path)
err = afero.WriteReader(dest, path, sourceFile)
if err != nil {
return err
return errors.Wrap(err, "unable to create file")
}
} else {
log.Printf("skipping create on existing file %s", path)
Expand All @@ -173,7 +174,7 @@ func applyTemplate(sourceFile io.Reader, dest afero.Fs, path string, overrides i
log.Printf("templating %s", path)
writer, err := dest.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
return errors.Wrap(err, "unable to open file")
}
t := util.OpenTemplate(sourceFile)
return t.Execute(writer, overrides)
Expand Down
19 changes: 10 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -110,21 +111,21 @@ func InitConfig(project, region, bucket, awsProfile, owner string) *Config {

func ReadConfig(f io.Reader) (*Config, error) {
c := &Config{}
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
b, e := ioutil.ReadAll(f)
if e != nil {
return nil, errors.Wrap(e, "unable to read config")
}
err2 := json.Unmarshal(b, c)
if err2 != nil {
return nil, err2
e = json.Unmarshal(b, c)
if e != nil {
return nil, errors.Wrap(e, "unable to parse json config file")
}
return c, nil
}

func FindAndReadConfig(fs afero.Fs, configFile string) (*Config, error) {
f, err := fs.Open(configFile)
if err != nil {
return nil, err
f, e := fs.Open(configFile)
if e != nil {
return nil, errors.Wrap(e, "unable to open config file")
}
reader := io.ReadCloser(f)
defer reader.Close()
Expand Down
9 changes: 5 additions & 4 deletions init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/chanzuckerberg/fogg/config"
"github.com/pkg/errors"
prompt "github.com/segmentio/go-prompt"
"github.com/spf13/afero"
)
Expand All @@ -21,11 +22,11 @@ func userPrompt() (string, string, string, string, string) {
func writeConfig(fs afero.Fs, config *config.Config) error {
json, e := json.MarshalIndent(config, "", " ")
if e != nil {
return e
return errors.Wrap(e, "unable to marshal json")
}
configFile, e2 := fs.Create("fogg.json")
if e2 != nil {
return e2
configFile, e := fs.Create("fogg.json")
if e != nil {
return errors.Wrap(e, "unable to create config file fogg.json")
}
_, e3 := configFile.Write(json)
return e3
Expand Down
14 changes: 8 additions & 6 deletions util/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package util
import (
"fmt"
"strconv"

"github.com/pkg/errors"
)

var (
Version = "undefined"
GitSha = "undefined"
Release = "false"
Dirty = "true"
Version = "undefined"
GitSha = "undefined"
Release = "false"
Dirty = "true"
)

func VersionString() (string, error) {
release, e := strconv.ParseBool(Release)
if e != nil {
return "", e
return "", errors.Wrapf(e, "unable to parse version release field %s", Release)
}
dirty, e := strconv.ParseBool(Dirty)
if e != nil {
return "", e
return "", errors.Wrapf(e, "unable to parse version dirty field %s", Dirty)
}
return versionString(Version, GitSha, release, dirty), nil
}
Expand Down

0 comments on commit a13aa46

Please sign in to comment.