Skip to content

Commit 950f47a

Browse files
committed
Add example code, update README.md
A quick example is very useful for people.
1 parent 52b38c2 commit 950f47a

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# Go library for parsing Fedora CoreOS streams
22

3-
See https://github.com/coreos/fedora-coreos-tracker/issues/98#issuecomment-683150940
3+
See the [Fedora CoreOS documentation](https://docs.fedoraproject.org/en-US/fedora-coreos/getting-started/)
4+
for basic information about streams.
5+
6+
This is a Go library which exposes API to decode streams into Go structs,
7+
as well as a convenience API to find the URL for a given stream.
8+
9+
# Example usage
10+
11+
See [example code here](example/example.go).

example/example.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Package main contains an example use of this library; it
2+
// prints the current Fedora CoreOS EC2(AWS) x86_64 AMI in the
3+
// us-east-2 region.
4+
package main
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"io/ioutil"
10+
"net/http"
11+
"os"
12+
13+
"github.com/coreos/stream-metadata-go/fedoracoreos"
14+
"github.com/coreos/stream-metadata-go/stream"
15+
)
16+
17+
const (
18+
targetArch = "x86_64"
19+
region = "us-east-2"
20+
)
21+
22+
func run() error {
23+
streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamStable)
24+
resp, err := http.Get(streamurl.String())
25+
if err != nil {
26+
return err
27+
}
28+
body, err := ioutil.ReadAll(resp.Body)
29+
resp.Body.Close()
30+
if err != nil {
31+
return err
32+
}
33+
34+
var fcosstable stream.Stream
35+
err = json.Unmarshal(body, &fcosstable)
36+
if err != nil {
37+
return err
38+
}
39+
arch, ok := fcosstable.Architectures[targetArch]
40+
if !ok {
41+
return fmt.Errorf("No %s architecture in stream", targetArch)
42+
}
43+
awsimages := arch.Images.Aws
44+
if awsimages == nil {
45+
return fmt.Errorf("No %s AWS images in stream", targetArch)
46+
}
47+
var regionVal stream.AwsRegionImage
48+
if regionVal, ok = awsimages.Regions[region]; !ok {
49+
return fmt.Errorf("No %s AWS images in region %s", targetArch, region)
50+
}
51+
fmt.Printf("%s\n", regionVal.Image)
52+
53+
return nil
54+
}
55+
56+
func main() {
57+
if err := run(); err != nil {
58+
fmt.Fprintf(os.Stderr, "%v\n", err)
59+
os.Exit(1)
60+
}
61+
}

0 commit comments

Comments
 (0)