Skip to content

Commit

Permalink
Merge pull request #21 from cgwalters/fcos-fetch-stream
Browse files Browse the repository at this point in the history
fedoracoreos: Add FetchStream API
  • Loading branch information
cgwalters committed Apr 26, 2021
2 parents 63bc4d1 + e45baac commit 0ea1c73
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
27 changes: 6 additions & 21 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,30 +51,18 @@ func printAMI(fcosstable stream.Stream) error {
}

func run() error {
streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamStable)
resp, err := http.Get(streamurl.String())
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
}
if len(os.Args) != 2 {
return fmt.Errorf("usage: example aws-ami|download-iso")
}
arg := os.Args[1]
fcosstable, err := fedoracoreos.FetchStream(fedoracoreos.StreamStable)
if err != nil {
return err
}
if arg == "aws-ami" {
return printAMI(fcosstable)
return printAMI(*fcosstable)
} else if arg == "download-iso" {
return downloadISO(fcosstable)
return downloadISO(*fcosstable)
} else {
return fmt.Errorf("invalid operation %s", arg)
}
Expand Down
33 changes: 33 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,32 @@ func GetStreamURL(stream string) url.URL {
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 := ioutil.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
}

0 comments on commit 0ea1c73

Please sign in to comment.