Skip to content

Commit

Permalink
fedoracoreos: Add FetchStream API
Browse files Browse the repository at this point in the history
Most callers will want this.  Additionally, if we ever add
more integrity for the stream such as GPG signing, this will
be the place to do it.

xref coreos/fedora-coreos-tracker#774
  • Loading branch information
cgwalters committed Mar 26, 2021
1 parent 63bc4d1 commit 11ff29b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
19 changes: 2 additions & 17 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/coreos/stream-metadata-go/fedoracoreos"
Expand Down Expand Up @@ -54,21 +51,9 @@ func printAMI(fcosstable stream.Stream) error {
}

func run() error {
streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamStable)
resp, err := http.Get(streamurl.String())
fcosstable, err := fedoracoreos.FetchStream(fedoracoreos.StreamStable)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}

var fcosstable stream.Stream
err = json.Unmarshal(body, &fcosstable)
if err != nil {
return err
return fmt.Errorf("failed to fetch stream: %w", err)
}
if len(os.Args) != 2 {
return fmt.Errorf("usage: example aws-ami|download-iso")
Expand Down
25 changes: 25 additions & 0 deletions fedoracoreos/fcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
package fedoracoreos

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

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

const (
Expand All @@ -25,3 +29,24 @@ func GetStreamURL(stream string) url.URL {
u.Path = fmt.Sprintf("streams/%s.json", stream)
return u
}

// FetchStream downloads the Fedora CoreOS stream data and parses it.
func FetchStream(streamName string) (stream.Stream, error) {
streamurl := GetStreamURL(streamName)
resp, err := http.Get(streamurl.String())
if err != nil {
return stream.Stream{}, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return stream.Stream{}, err
}

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

0 comments on commit 11ff29b

Please sign in to comment.