forked from coreos/stream-metadata-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
61 lines (54 loc) · 1.28 KB
/
example.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Package main contains an example use of this library; it
// prints the current Fedora CoreOS EC2(AWS) x86_64 AMI in the
// us-east-2 region.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/coreos/stream-metadata-go/fedoracoreos"
"github.com/coreos/stream-metadata-go/stream"
)
const (
targetArch = "x86_64"
region = "us-east-2"
)
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
}
arch, ok := fcosstable.Architectures[targetArch]
if !ok {
return fmt.Errorf("No %s architecture in stream", targetArch)
}
awsimages := arch.Images.Aws
if awsimages == nil {
return fmt.Errorf("No %s AWS images in stream", targetArch)
}
var regionVal stream.AwsRegionImage
if regionVal, ok = awsimages.Regions[region]; !ok {
return fmt.Errorf("No %s AWS images in region %s", targetArch, region)
}
fmt.Printf("%s\n", regionVal.Image)
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}