-
Notifications
You must be signed in to change notification settings - Fork 69
/
recipe_file_fetcher.go
110 lines (87 loc) · 2.72 KB
/
recipe_file_fetcher.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package recipes
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"gopkg.in/yaml.v2"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
type RecipeFileFetcher struct {
HTTPGetFunc func(string) (*http.Response, error)
readFileFunc func(string) ([]byte, error)
Paths []string
}
func NewRecipeFileFetcher(paths []string) *RecipeFileFetcher {
f := RecipeFileFetcher{}
f.HTTPGetFunc = defaultHTTPGetFunc
f.readFileFunc = defaultReadFileFunc
f.Paths = paths
return &f
}
func (rff *RecipeFileFetcher) FetchLibraryVersion(ctx context.Context) string {
return ""
}
func (rff *RecipeFileFetcher) FetchRecipes(ctx context.Context) ([]*types.OpenInstallationRecipe, error) {
var recipesFromPath []*types.OpenInstallationRecipe
for _, recipePath := range rff.Paths {
recipeURL, parseErr := url.Parse(recipePath)
isURL := parseErr == nil && recipeURL.Scheme != "" && strings.HasPrefix(strings.ToLower(recipeURL.Scheme), "http")
var recipe *types.OpenInstallationRecipe
var err error
if isURL {
log.Debugf("Loading recipe from URL:%s", recipeURL)
recipe, err = rff.FetchRecipeFile(recipeURL)
if err != nil {
return recipesFromPath, fmt.Errorf("could not fetch file %s: %s", recipePath, err)
}
} else {
log.Debugf("Loading recipe from path:%s", recipePath)
recipe, err = rff.LoadRecipeFile(recipePath)
if err != nil {
return recipesFromPath, fmt.Errorf("could not load file %s: %s", recipePath, err)
}
}
recipesFromPath = append(recipesFromPath, recipe)
}
return recipesFromPath, nil
}
func defaultHTTPGetFunc(recipeURL string) (*http.Response, error) {
return http.Get(recipeURL)
}
func defaultReadFileFunc(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func NewRecipeFile(recipeFileString string) (*types.OpenInstallationRecipe, error) {
var f types.OpenInstallationRecipe
err := yaml.Unmarshal([]byte(recipeFileString), &f)
if err != nil {
return nil, err
}
return &f, nil
}
func (rff *RecipeFileFetcher) FetchRecipeFile(recipeURL *url.URL) (*types.OpenInstallationRecipe, error) {
response, err := rff.HTTPGetFunc(recipeURL.String())
if err != nil {
return nil, err
}
if response.StatusCode < 200 || response.StatusCode > 299 {
return nil, fmt.Errorf("received non-2xx Status code %d when retrieving recipe", response.StatusCode)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return NewRecipeFile(string(body))
}
func (rff *RecipeFileFetcher) LoadRecipeFile(filename string) (*types.OpenInstallationRecipe, error) {
out, err := rff.readFileFunc(filename)
if err != nil {
return nil, err
}
return NewRecipeFile(string(out))
}