-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetter.go
78 lines (64 loc) · 1.71 KB
/
getter.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 hclconfig
import (
"context"
"fmt"
"os"
"path"
"github.com/flytam/filenamify"
getter "github.com/hashicorp/go-getter"
)
type Getter interface {
// Get fetches the source files from src and downloads them to the
// given folder. If the files already exist at the given location
// Get does nothing unless ignoreCache is true when source will be
// downloaded regarless of cache.
//
// Get returns a string with the full path of the downloaded source
// this contains any url characters in src correctly encoded for
// a filepath.
Get(src, destFolder string, ignoreCache bool) (string, error)
}
type GoGetter struct {
get func(src, dest, working string) error
}
func NewGoGetter() Getter {
return &GoGetter{
get: func(src, dest, working string) error {
c := &getter.Client{
Ctx: context.Background(),
Src: src,
Dst: dest,
Pwd: working,
Mode: getter.ClientModeAny,
Options: []getter.ClientOption{},
}
err := c.Get()
if err != nil {
return fmt.Errorf("unable to fetch files from %s: %s", src, err)
}
return nil
},
}
}
func (g *GoGetter) Get(src, dest string, ignoreCache bool) (string, error) {
// check to see if a folder exists at the destination and exit if exists
pwd, err := os.Getwd()
if err != nil {
return "", err
}
// ensure the output folder is correctly encoded
output, err := filenamify.Filenamify(src, filenamify.Options{
Replacement: "_",
})
if err != nil {
return "", err
}
downloadPath := path.Join(dest, output)
// check to see if the destination exists
_, err = os.Stat(downloadPath)
if err == nil && !ignoreCache {
return downloadPath, nil
}
err = g.get(src, downloadPath, pwd)
return downloadPath, err
}