summaryrefslogtreecommitdiffstats
path: root/fedoracoreos/fcos.go
blob: 9fc2b3e2f2d6cedd8a323b1d82585a3b5db417f3 (plain)
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
// Package fedoracoreos contains APIs defining well-known
// streams for Fedora CoreOS and a method to retrieve
// the URL for a stream endpoint.
package fedoracoreos

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"

	"github.com/coreos/stream-metadata-go/fedoracoreos/internals"
	"github.com/coreos/stream-metadata-go/stream"
)

const (
	// StreamStable is the default stream
	StreamStable = "stable"
	// StreamTesting is what is intended to land in stable
	StreamTesting = "testing"
	// StreamNext usually tracks the next Fedora major version
	StreamNext = "next"
)

// GetStreamURL returns the URL for the given stream
func GetStreamURL(stream string) url.URL {
	u := internals.GetBaseURL()
	u.Path = fmt.Sprintf("streams/%s.json", stream)
	return u
}

func getStream(u url.URL) (*stream.Stream, error) {
	resp, err := http.Get(u.String())
	if err != nil {
		return nil, err
	}
	body, err := io.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		return nil, err
	}

	var s stream.Stream
	err = json.Unmarshal(body, &s)
	if err != nil {
		return nil, err
	}
	return &s, nil
}

// FetchStream fetches and parses stream metadata for a stream
func FetchStream(streamName string) (*stream.Stream, error) {
	u := GetStreamURL(streamName)
	s, err := getStream(u)
	if err != nil {
		return nil, fmt.Errorf("failed to fetch stream %s: %w", streamName, err)
	}
	return s, nil
}