forked from hashicorp/otto
/
gopath.go
78 lines (69 loc) · 2.43 KB
/
gopath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package goapp
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/otto/app"
)
// DetectImportPath will try to automatically determine the import path
// for the Go application under development.
//
// This is necessary to setup proper GOPATH directories for development
// and builds.
func DetectImportPath(ctx *app.Context) (string, error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
ctx.Ui.Message(
"Warning! GOPATH not set. Otto will be unable to automatically\n" +
"setup your application GOPATH for development and builds. While Otto\n" +
"sets up a development for you, your folder structure outside of Otto\n" +
"should still represent a proper Go environment. If you do this, then\n" +
"the development and build process function a lot smoother.\n\n" +
"For simple Go applications, this may not be necessary.\n\n" +
"This is just an informational message. This is not a bug.")
return "", nil
}
// Gopath should be absolute
gopath, err := filepath.Abs(gopath)
if err != nil {
return "", fmt.Errorf(
"Error expanding GOPATH to an absolute path: %s", err)
}
dir := filepath.Dir(ctx.Appfile.Path)
dir, err = filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf(
"Error expanding Appfile path to an absolute path: %s", err)
}
// If the directory to our Appfile is a symlink, resolve that symlink
// through. This makes this heuristic work for local dependencies.
if fi, err := os.Lstat(dir); err == nil {
if fi.Mode()&os.ModeSymlink != 0 {
newDir, err := os.Readlink(dir)
if err != nil {
return "", fmt.Errorf(
"Error reading symlink %s: %s", dir, err)
}
dir = newDir
}
}
// The directory has to be prefixed with the gopath
gopath = filepath.Join(gopath, "src")
if !strings.HasPrefix(dir, gopath) {
ctx.Ui.Message(
"Warning! It looks like your application is not within your set\n" +
"GOPATH. Otto will be unable to automatically setup the proper\n" +
"GOPATH structure within your development and build environments.\n\n" +
"To fix this, please put your application into the proper GOPATH\n" +
"location as according to standard Go development practices.")
return "", nil
}
detected := dir[len(gopath)+1:]
ctx.Ui.Message(fmt.Sprintf(
"Detected import path: %s\n\n"+
"Otto will use this import path to automatically setup your dev\n"+
"and build environments in the proper directories.",
detected))
return detected, nil
}