summaryrefslogtreecommitdiff
path: root/cmd/ponzu/add.go
blob: 579f65b58c516dab9444cff84c425006f84b8eba (plain)
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
package main

import (
	"errors"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

// Use `go get` to download addon and add to $GOPATH/src, useful
// for IDE auto-import and code completion, then copy entire directory
// tree to project's ./addons folder
func getAddon(args []string) error {

	// error return
	errorFunc := func(err error) error {
		return errors.New("Ponzu add failed. " + "\n" + err.Error())
	}

	var cmdOptions []string
	var addonPath = args[1]

	// Go get
	cmdOptions = append(cmdOptions, "get", addonPath)
	get := exec.Command(gocmd, cmdOptions...)
	get.Stderr = os.Stderr
	get.Stdout = os.Stdout

	err := get.Start()
	if err != nil {
		errorFunc(err)
	}
	err = get.Wait()
	if err != nil {
		errorFunc(err)
	}

	// Copy to ./addons folder
	// GOPATH can be a list delimited by ":" on Linux or ";" on Windows
	// `go get` uses the first, this should parse out the first whatever the OS
	gopath := resolveGOPATH()

	pwd, err := os.Getwd()
	if err != nil {
		errorFunc(err)
	}

	src := filepath.Join(gopath, addonPath)
	dest := filepath.Join(pwd, "addons", addonPath)

	err = copyAll(src, dest)
	if err != nil {
		errorFunc(err)
	}
	return nil
}

// GOPATH can be a list delimited by ":" on Linux or ";" on Windows
// `go get` uses saves packages to the first entry, so this function
// should parse out the first whatever the OS
func resolveGOPATH() string {
	envGOPATH := os.Getenv("GOPATH")
	gopaths := strings.Split(envGOPATH, ":")
	gopath := gopaths[0]
	gopaths = strings.Split(envGOPATH, ";")
	gopath = gopaths[0]
	return gopath
}