-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
47 lines (37 loc) · 1.02 KB
/
remote.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
package template
import (
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
type RemoteSource[T TemplateType] struct {
cacheOpts *CacheSourceOpts
url string
}
func (src *RemoteSource[T]) Parse() (*RawTemplate, error) {
return nil, errors.New("not implemented")
}
func (src *RemoteSource[T]) Validate() ([]*TemplateValidated, error) {
return nil, errors.New("not implemented")
}
func (src *RemoteSource[T]) Read() (*TemplateInfo[T], error) {
return nil, errors.New("not implemented")
}
// TODO not used e.g. https://raw.githubusercontent.com/hckops/megalopolis/main/box/base/alpine.yml
func httpGetString(url string) (string, error) {
// TODO context with timeout
resp, err := http.Get(url)
if err != nil {
return "", errors.Wrap(err, "network error")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return "", fmt.Errorf("not found")
}
data, err := io.ReadAll(resp.Body)
if err != nil || len(data) == 0 {
return "", errors.Wrap(err, "invalid body")
}
return string(data), nil
}