From adcedc48559e5ed4b8ce19e18a7736722eacec68 Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Wed, 29 Jun 2016 06:57:59 +0100 Subject: [PATCH] use USERPROFILE instead of HOME for windows, rework paths to use filepath.Join for better cross platform support --- .gitignore | 2 ++ cli.go | 11 ++++++----- util.go | 25 +++++++------------------ 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 5647188d..cd633f94 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ src/github.com/docopt/ src/github.com/mgutz/ src/github.com/op/ src/gopkg.in/ +jira +jira.exe \ No newline at end of file diff --git a/cli.go b/cli.go index 5c3ee62f..99ca8da8 100644 --- a/cli.go +++ b/cli.go @@ -15,13 +15,14 @@ import ( "os" "os/exec" "path" + "path/filepath" "runtime" "strings" "time" ) var ( - log = logging.MustGetLogger("jira") + log = logging.MustGetLogger("jira") VERSION string ) @@ -33,7 +34,7 @@ type Cli struct { } func New(opts map[string]interface{}) *Cli { - homedir := os.Getenv("HOME") + homedir := homedir() cookieJar, _ := cookiejar.New(nil) endpoint, _ := opts["endpoint"].(string) url, _ := url.Parse(strings.TrimRight(endpoint, "/")) @@ -53,7 +54,7 @@ func New(opts map[string]interface{}) *Cli { cli := &Cli{ endpoint: url, opts: opts, - cookieFile: fmt.Sprintf("%s/.jira.d/cookies.js", homedir), + cookieFile: filepath.Join(homedir, ".jira.d", "cookies.js"), ua: &http.Client{ Jar: cookieJar, Transport: transport, @@ -214,7 +215,7 @@ func (c *Cli) GetTemplate(name string) string { } func getLookedUpTemplate(name string, dflt string) string { - if file, err := FindClosestParentPath(fmt.Sprintf(".jira.d/templates/%s", name)); err == nil { + if file, err := FindClosestParentPath(filepath.Join(".jira.d", "templates", name)); err == nil { return readFile(file) } if _, err := os.Stat(fmt.Sprintf("/etc/go-jira/templates/%s", name)); err == nil { @@ -250,7 +251,7 @@ func (f NoChangesFound) Error() string { func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData map[string]interface{}, templateProcessor func(string) error) error { - tmpdir := fmt.Sprintf("%s/.jira.d/tmp", os.Getenv("HOME")) + tmpdir := filepath.Join(homedir(), ".jira.d", "tmp") if err := mkdir(tmpdir); err != nil { return err } diff --git a/util.go b/util.go index 82b3e927..77a5935d 100644 --- a/util.go +++ b/util.go @@ -6,25 +6,24 @@ import ( "encoding/json" "errors" "fmt" - "path/filepath" "github.com/mgutz/ansi" "gopkg.in/coryb/yaml.v2" "io" "io/ioutil" "net/http" "os" - "strings" + "path/filepath" "runtime" + "strings" "text/template" "time" ) func homedir() string { - log.Errorf("GOOS: %s", runtime.GOOS) - if runtime.GOOS == "windows" { - return os.Getenv("USERPROFILE") - } - return os.Getenv("HOME") + if runtime.GOOS == "windows" { + return os.Getenv("USERPROFILE") + } + return os.Getenv("HOME") } func FindParentPaths(fileName string) []string { @@ -41,7 +40,6 @@ func FindParentPaths(fileName string) []string { } } - path := filepath.Join(cwd, fileName) if _, err := os.Stat(path); err == nil { paths = append(paths, path) @@ -52,19 +50,10 @@ func FindParentPaths(fileName string) []string { if _, err := os.Stat(path); err == nil { paths = append(paths, path) } - if cwd[len(cwd)-1]== filepath.Separator { + if cwd[len(cwd)-1] == filepath.Separator { break } } - // for _, part := range strings.Split(cwd, string(os.PathSeparator)) { - // if dir == "/" { - // dir = fmt.Sprintf("/%s", part) - // } else { - // dir = fmt.Sprintf("%s/%s", dir, part) - // } - // file := fmt.Sprintf("%s/%s", dir, fileName) - // } - log.Errorf("PATHS: %#v", paths) return paths }