-
Notifications
You must be signed in to change notification settings - Fork 24
/
get.go
89 lines (72 loc) · 2.11 KB
/
get.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
87
88
89
package cmd
import (
"errors"
"fmt"
"os"
"golang.org/x/xerrors"
"context"
getter "github.com/hashicorp/go-getter"
"github.com/shipyard-run/shipyard/pkg/utils"
"github.com/spf13/cobra"
)
// ErrorInvalidBlueprintURI is returned when the URI for a blueprint can not be parsed
var ErrorInvalidBlueprintURI = errors.New("error invalid Blueprint URI, blueprints should be formatted 'github.com/org/repo//blueprint'")
var getCmd = &cobra.Command{
Use: "get [remote blueprint]",
Short: "Download the blueprint to the Shipyard config folder",
Long: `Download the blueprint to the Shipyard configuration folder`,
Example: `
# Fetch a blueprint from GitHub
yard get github.com/shipyard-run/blueprints//vault-k8s
`,
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
dst := args[0]
fmt.Println("Fetching blueprint from: ", dst)
fmt.Println("")
// create a logger
log := createLogger()
// create the shipyard home
os.MkdirAll(utils.ShipyardHome(), os.FileMode(0755))
if utils.IsLocalFolder(dst) {
log.Error("Parameter is not a remote blueprint, e.g. github.com/shipyard-run/blueprints//vault-k8s")
os.Exit(1)
}
// fetch the remote server from github
dst, err = pullRemoteBlueprint(dst)
if err != nil {
log.Error("Unable to retrieve blueprint", "error", err)
os.Exit(1)
}
},
}
// pullRemoteBlueprint attempts to retrieve a blueprint
// from a remote location
// returns the local path if successful or error on
// failure
func pullRemoteBlueprint(uri string) (string, error) {
bpFolder, err := utils.GetBlueprintFolder(uri)
if err != nil {
return "", err
}
dst := fmt.Sprintf("%s/.shipyard/blueprints/%s", os.Getenv("HOME"), bpFolder)
pwd, err := os.Getwd()
if err != nil {
return "", err
}
// 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{},
}
err = c.Get()
if err != nil {
return "", xerrors.Errorf("unable to fetch blueprint from %s: %w", uri, err)
}
return dst, nil
}