Skip to content

Commit

Permalink
Merge pull request #242 from buildpack/fix-windows-home
Browse files Browse the repository at this point in the history
Fix home env var value for windows
  • Loading branch information
ameyer-pivotal authored Jul 16, 2019
2 parents 324971b + 8795a9f commit 3123e32
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpack/pack/config"
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion commands/set_default_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpack/pack/config"
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion commands/set_run_image_mirrors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpack/pack/config"
Expand All @@ -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
}

Expand Down
18 changes: 13 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3123e32

Please sign in to comment.