-
Notifications
You must be signed in to change notification settings - Fork 24
/
getter.go
86 lines (73 loc) · 1.82 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
79
80
81
82
83
84
85
86
package clients
import (
"context"
"os"
"github.com/hashicorp/go-getter"
"golang.org/x/xerrors"
)
// Getter is an interface which defines interations for
// downloading remote folders
type Getter interface {
Get(uri, dst string) error
SetForce(force bool)
}
// GetterImpl is a concrete implementation of the Getter interface
type GetterImpl struct {
//
force bool
get func(uri, dst, pwd string) error
}
// NewGetter creates a new Getter
func NewGetter(force bool) *GetterImpl {
gi := &GetterImpl{
force,
func(uri, dst, pwd string) error {
// if the argument is a url fetch it first
c := &getter.Client{
Ctx: context.Background(),
Src: uri,
Dst: dst,
Pwd: pwd,
Mode: getter.ClientModeAny,
Options: []getter.ClientOption{},
}
return c.Get()
},
}
return gi
}
// SetForce sets the force flag causing all downloads to overwrite the destination
func (g *GetterImpl) SetForce(force bool) {
g.force = force
}
// Get attempts to retrieve a folder
// from a remote location and stores it at the destination.
//
// If force was set to true when creating a Getter then
// the destination folder will automatically be overwritten.
//
// Returns error on failure
func (g *GetterImpl) Get(uri, dst string) error {
// check to see if a folder exists at the destination and exit if force is not
// equal to true
_, err := os.Stat(dst)
if err == nil {
// we already have files at the destination do we want to overwrite?
if g.force == false {
return nil
}
err := os.RemoveAll(dst)
if err != nil {
return xerrors.Errorf("Destination folder exists, unable to delete: %w", err)
}
}
pwd, err := os.Getwd()
if err != nil {
return err
}
err = g.get(uri, dst, pwd)
if err != nil {
return xerrors.Errorf("unable to fetch files from %s: %w", uri, err)
}
return nil
}