diff --git a/client.go b/client.go index fdb8895435..8fb44417d9 100644 --- a/client.go +++ b/client.go @@ -5,6 +5,7 @@ import ( "path/filepath" dockerClient "github.com/docker/docker/client" + "github.com/pkg/errors" "github.com/buildpack/pack/build" "github.com/buildpack/pack/buildpack" @@ -58,7 +59,11 @@ func NewClient(opts ...ClientOption) (*Client, error) { } } - downloader := NewDownloader(client.logger, filepath.Join(config.PackHome(), "download-cache")) + packHome, err := config.PackHome() + if err != nil { + return nil, errors.Wrap(err, "getting pack home") + } + downloader := NewDownloader(client.logger, filepath.Join(packHome, "download-cache")) client.imageFetcher = image.NewFetcher(client.logger, client.docker) client.buildpackFetcher = buildpack.NewFetcher(downloader) client.lifecycleFetcher = lifecycle.NewFetcher(downloader) diff --git a/cmd/pack/main.go b/cmd/pack/main.go index a9559c8d02..9c899ad441 100644 --- a/cmd/pack/main.go +++ b/cmd/pack/main.go @@ -1,10 +1,10 @@ package main import ( - "fmt" "os" "github.com/fatih/color" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/buildpack/pack" @@ -24,7 +24,11 @@ func main() { logger := clilogger.NewLogWithWriters() cobra.EnableCommandSorting = false - cfg := initConfig() + cfg, err := initConfig() + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } rootCmd := &cobra.Command{ Use: "pack", @@ -72,13 +76,17 @@ func main() { } } -func initConfig() config.Config { - cfg, err := config.Read(config.DefaultConfigPath()) +func initConfig() (config.Config, error) { + path, err := config.DefaultConfigPath() + if err != nil { + return config.Config{}, errors.Wrap(err, "getting config path") + } + + cfg, err := config.Read(path) if err != nil { - fmt.Printf("WARN: %s\n", err.Error()) - return config.Config{} + return config.Config{}, errors.Wrap(err, "reading pack config") } - return cfg + return cfg, nil } func initClient(logger logging.Logger) pack.Client { diff --git a/commands/completion.go b/commands/completion.go index c82cdfad6e..56ad6ad865 100644 --- a/commands/completion.go +++ b/commands/completion.go @@ -3,6 +3,7 @@ package commands import ( "path/filepath" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/buildpack/pack/config" @@ -20,7 +21,11 @@ To configure your bash shell to load completions for each session, add the follo . $(pack completion) `, RunE: logError(logger, func(cmd *cobra.Command, args []string) error { - completionPath := filepath.Join(config.PackHome(), "completion") + packHome, err := config.PackHome() + if err != nil { + return errors.Wrap(err, "getting pack home") + } + completionPath := filepath.Join(packHome, "completion") if err := cmd.Parent().GenBashCompletionFile(completionPath); err != nil { return err diff --git a/commands/set_default_builder.go b/commands/set_default_builder.go index 00ed066576..f55c71bd22 100644 --- a/commands/set_default_builder.go +++ b/commands/set_default_builder.go @@ -3,6 +3,7 @@ package commands import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/buildpack/pack/config" @@ -44,7 +45,11 @@ func SetDefaultBuilder(logger logging.Logger, cfg config.Config, client PackClie } cfg.DefaultBuilder = imageName - if err := config.Write(cfg, config.DefaultConfigPath()); err != nil { + configPath, err := config.DefaultConfigPath() + if err != nil { + return errors.Wrap(err, "getting config path") + } + if err := config.Write(cfg, configPath); err != nil { return err } logger.Infof("Builder %s is now the default builder", style.Symbol(imageName)) diff --git a/commands/set_run_image_mirrors.go b/commands/set_run_image_mirrors.go index 9d61137c9f..85d14a4587 100644 --- a/commands/set_run_image_mirrors.go +++ b/commands/set_run_image_mirrors.go @@ -1,6 +1,7 @@ package commands import ( + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/buildpack/pack/config" @@ -18,7 +19,11 @@ func SetRunImagesMirrors(logger logging.Logger, cfg config.Config) *cobra.Comman RunE: logError(logger, func(cmd *cobra.Command, args []string) error { runImage := args[0] cfg = config.SetRunImageMirrors(cfg, runImage, mirrors) - if err := config.Write(cfg, config.DefaultConfigPath()); err != nil { + configPath, err := config.DefaultConfigPath() + if err != nil { + return errors.Wrap(err, "getting config path") + } + if err := config.Write(cfg, configPath); err != nil { return err } diff --git a/config/config.go b/config/config.go index cae7038c68..f93a3e1cdc 100644 --- a/config/config.go +++ b/config/config.go @@ -18,16 +18,24 @@ type RunImage struct { Mirrors []string `toml:"mirrors"` } -func DefaultConfigPath() string { - return filepath.Join(PackHome(), "config.toml") +func DefaultConfigPath() (string, error) { + home, err := PackHome() + if err != nil { + return "", errors.Wrap(err, "getting pack home") + } + return filepath.Join(home, "config.toml"), nil } -func PackHome() string { +func PackHome() (string, error) { packHome := os.Getenv("PACK_HOME") if packHome == "" { - packHome = filepath.Join(os.Getenv("HOME"), ".pack") + home, err := os.UserHomeDir() + if err != nil { + return "", errors.Wrap(err, "getting user home") + } + packHome = filepath.Join(home, ".pack") } - return packHome + return packHome, nil } func Read(path string) (Config, error) {