-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
230 lines (197 loc) · 5.77 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
"github.com/coreos/stream-metadata-go/release"
"github.com/coreos/stream-metadata-go/stream"
)
// Substituted by Makefile
var Version = "was not built properly"
var generator = "fedora-coreos-stream-generator " + Version
var errReleaseIndexMissing = errors.New("Please specify release index url or release override")
// getReleaseURL gets path for latest release.json available
func getReleaseURL(releaseIndexURL string) (string, error) {
var relIndex release.Index
parsedURL, err := url.Parse(releaseIndexURL)
if err != nil {
return "", err
}
var decoder *json.Decoder
if parsedURL.Scheme == "" {
// It is most likely a local file.
releases, err := os.Open(releaseIndexURL)
if err != nil {
return "", err
}
defer releases.Close()
decoder = json.NewDecoder(releases)
} else {
resp, err := http.Get(releaseIndexURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
decoder = json.NewDecoder(resp.Body)
}
if err := decoder.Decode(&relIndex); err != nil {
return "", err
}
if len(relIndex.Releases) < 1 {
return "", fmt.Errorf("No release available to process")
}
return relIndex.Releases[len(relIndex.Releases)-1].MetadataURL, nil
}
func overrideData(original, override interface{}) interface{} {
switch override1 := override.(type) {
case map[string]interface{}:
original1, ok := original.(map[string]interface{})
if !ok {
return override1
}
for key, value1 := range original1 {
if value2, ok := override1[key]; ok {
override1[key] = overrideData(value1, value2)
} else {
override1[key] = value1
}
}
case nil:
original1, ok := original.(map[string]interface{})
if ok {
return original1
}
}
return override
}
func run() error {
var releasesURL string
flag.StringVar(&releasesURL, "releases", "", "Release index location for the required stream")
var overrideReleasePath string
flag.StringVar(&overrideReleasePath, "release", "", "Override release metadata location")
var overrideFilename string
flag.StringVar(&overrideFilename, "override", "", "Override file location for the required stream")
var outputFile string
flag.StringVar(&outputFile, "output-file", "", "Save output into a file")
var prettyPrint bool
flag.BoolVar(&prettyPrint, "pretty-print", false, "Pretty-print output")
var version bool
flag.BoolVar(&version, "version", false, "Show version")
flag.Parse()
if version {
fmt.Println(generator)
return nil
}
var releasePath string
if releasesURL == "" && overrideReleasePath == "" {
return errReleaseIndexMissing
} else if releasesURL != "" && overrideReleasePath != "" {
return fmt.Errorf("Can't specify both -releases and -release")
} else if overrideReleasePath != "" {
releasePath = overrideReleasePath
} else {
var err error
releasePath, err = getReleaseURL(releasesURL)
if err != nil {
return fmt.Errorf("Error with Release Index: %v", err)
}
}
parsedURL, err := url.Parse(releasePath)
if err != nil {
return fmt.Errorf("Error while parsing release path: %v", err)
}
var decoder *json.Decoder
if parsedURL.Scheme == "" {
// It is most likely a local file.
releaseMetadataFile, err := os.Open(releasePath)
if err != nil {
return fmt.Errorf("Error opening file: %v", err)
}
defer releaseMetadataFile.Close()
decoder = json.NewDecoder(releaseMetadataFile)
} else {
resp, err := http.Get(releasePath)
if err != nil {
return fmt.Errorf("Error while fetching: %v", err)
}
defer resp.Body.Close()
decoder = json.NewDecoder(resp.Body)
}
var rel release.Release
if err = decoder.Decode(&rel); err != nil {
return fmt.Errorf("Error while decoding json: %v", err)
}
streamMetadata := stream.Stream{
Stream: rel.Stream,
Metadata: stream.Metadata{
LastModified: time.Now().UTC().Format(time.RFC3339),
Generator: generator,
},
Architectures: rel.ToStreamArchitectures(),
}
if overrideFilename != "" {
overrideFile, err := os.Open(overrideFilename)
if err != nil {
return fmt.Errorf("Can't open file %s: %v", overrideFilename, err)
}
defer overrideFile.Close()
streamMetadataJSON, err := json.Marshal(&streamMetadata)
if err != nil {
return fmt.Errorf("Error during Marshal: %v", err)
}
streamMetadataMap := make(map[string]interface{})
if err = json.Unmarshal(streamMetadataJSON, &streamMetadataMap); err != nil {
return fmt.Errorf("Error during Unmarshal: %v", err)
}
overrideMap := make(map[string]interface{})
overrideDecoder := json.NewDecoder(overrideFile)
if err = overrideDecoder.Decode(&overrideMap); err != nil {
return fmt.Errorf("Error while decoding: %v", err)
}
streamMetadataInterface := overrideData(streamMetadataMap, overrideMap)
streamMetadataMap = streamMetadataInterface.(map[string]interface{})
// We are doing Marshal and Unmarshal of streamMetadataMap to keep json in ordered way
streamMetadataJSON, err = json.Marshal(streamMetadataMap)
if err != nil {
return fmt.Errorf("Error during Marshal: %v", err)
}
if err = json.Unmarshal(streamMetadataJSON, &streamMetadata); err != nil {
return fmt.Errorf("Error during Unmarshal: %v", err)
}
}
var out io.Writer
// If outputFile option not specified print on stdout
if outputFile != "" {
streamFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("Can't open file: %v", err)
}
defer streamFile.Close()
out = streamFile
} else {
out = os.Stdout
}
encoder := json.NewEncoder(out)
if prettyPrint {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(&streamMetadata); err != nil {
return fmt.Errorf("Error while encoding: %v", err)
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Println(err)
if err == errReleaseIndexMissing {
flag.Usage()
}
os.Exit(1)
}
}