Skip to content

Commit

Permalink
Use default GOPATH as a fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
kujtimiihoxha committed Apr 10, 2017
1 parent be49181 commit 42e7dc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (sg *ServiceInitGenerator) generateHttpTransport(name string, iface *parser
defaultFs := fs.Get()
handlerFile := parser.NewFile()
handlerFile.Package = "http"
gosrc := viper.GetString("GOPATH") + "/src/"
gosrc := utils.GetGOPATH() + "/src/"
gosrc = strings.Replace(gosrc, "\\", "/", -1)
pwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -372,7 +372,7 @@ func (sg *ServiceInitGenerator) generateEndpoints(name string, iface *parser.Int
file.Structs = []parser.Struct{
parser.NewStruct("Endpoints", []parser.NamedTypeValue{}),
}
gosrc := viper.GetString("GOPATH") + "/src/"
gosrc := utils.GetGOPATH() + "/src/"
gosrc = strings.Replace(gosrc, "\\", "/", -1)
pwd, err := os.Getwd()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"github.com/spf13/viper"
"os"
"strings"
"github.com/kujtimiihoxha/gk/utils"
)

func main() {
viper.AutomaticEnv()
gosrc := viper.GetString("GOPATH") + "/src/"
gosrc := utils.GetGOPATH() + "/src/"
pwd, err := os.Getwd()
if err != nil {
logrus.Error(err)
Expand Down
29 changes: 29 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package utils

import (
"github.com/alioygur/godash"
"github.com/spf13/viper"
"os"
"path/filepath"
"runtime"
"strings"
)

Expand All @@ -18,3 +22,28 @@ func ToLowerSnakeCase(s string) string {
func ToCamelCase(s string) string {
return godash.ToCamelCase(s)
}

func GetGOPATH() string {
if viper.GetString("GOPATH") != "" {
return viper.GetString("GOPATH")
}
return defaultGOPATH()
}
func defaultGOPATH() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
if home := os.Getenv(env); home != "" {
def := filepath.Join(home, "go")
if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
// Don't set the default GOPATH to GOROOT,
// as that will trigger warnings from the go tool.
return ""
}
return def
}
return ""
}

0 comments on commit 42e7dc8

Please sign in to comment.