8
8
"fmt"
9
9
"io"
10
10
"io/fs"
11
+ "log"
11
12
"os"
12
13
"path/filepath"
13
14
"sort"
@@ -22,7 +23,10 @@ type MD struct {
22
23
}
23
24
24
25
func main () {
25
- produceManifest (os .Stdout , os .DirFS ("." ))
26
+ err := produceManifest (os .Stdout , os .DirFS ("." ))
27
+ if err != nil {
28
+ log .Fatal (err )
29
+ }
26
30
}
27
31
28
32
func produceManifest (out io.Writer , dir fs.FS ) error {
@@ -82,38 +86,62 @@ func produceManifest(out io.Writer, dir fs.FS) error {
82
86
}
83
87
}
84
88
85
- // It's not clear how to maintain a stable order of keys using the YAML serializer.
86
- // If it were, we could just through this map at the YAML serializer and call it a day.
87
- // Right now, we have to produce the YAML ourselves.
88
- var print func (m map [string ]interface {}, indent int ) error
89
- print = func (m map [string ]interface {}, indent int ) error {
90
- keys := make ([]string , 0 , len (m ))
91
- for v := range m {
92
- keys = append (keys , v )
89
+ vers , err := fs .Glob (dir , "**/versions.yaml" )
90
+ if err != nil {
91
+ return err
92
+ }
93
+ for _ , md := range vers {
94
+ b , err := fs .ReadFile (dir , md )
95
+ if err != nil {
96
+ return err
97
+ }
98
+ var versions struct {
99
+ Commit string `yaml:"commit"`
100
+ Version string `yaml:"version"`
101
+ Components map [string ]interface {} `yaml:"components"`
102
+ }
103
+ err = yaml .Unmarshal (b , & versions )
104
+ if err != nil {
105
+ return xerrors .Errorf ("cannot unmarshal %s: %w" , md , err )
93
106
}
94
- sort .Strings (keys )
95
-
96
- for _ , k := range keys {
97
- v := m [k ]
98
- fmt .Fprintf (out , "%s%s:" , strings .Repeat (" " , indent ), k )
99
- if c , ok := v .(map [string ]interface {}); ok {
100
- fmt .Fprintln (out )
101
- err := print (c , indent + 1 )
102
- if err != nil {
103
- return err
104
- }
105
- continue
106
- }
107
- if c , ok := v .(string ); ok {
108
- fmt .Fprintf (out , " %s\n " , c )
109
- fmt .Fprintln (out )
110
- continue
111
- }
112
107
113
- return xerrors .Errorf ("unknown value type - this should never happen" )
108
+ for k , v := range versions .Components {
109
+ res ["components" ].(map [string ]interface {})[k ] = v
114
110
}
115
- return nil
116
111
}
117
112
118
- return print (res , 0 )
113
+ return print (out , res , 0 )
114
+ }
115
+
116
+ // print serializes the given map to YAML.
117
+ // It's not clear how to maintain a stable order of keys using the YAML serializer.
118
+ // If it were, we could just through this map at the YAML serializer and call it a day.
119
+ // Right now, we have to produce the YAML ourselves.
120
+ func print (out io.Writer , m map [string ]interface {}, indent int ) error {
121
+ keys := make ([]string , 0 , len (m ))
122
+ for v := range m {
123
+ keys = append (keys , v )
124
+ }
125
+ sort .Strings (keys )
126
+
127
+ for _ , k := range keys {
128
+ v := m [k ]
129
+ fmt .Fprintf (out , "%s%s:" , strings .Repeat (" " , indent ), k )
130
+ if c , ok := v .(map [string ]interface {}); ok {
131
+ fmt .Fprintln (out )
132
+ err := print (out , c , indent + 1 )
133
+ if err != nil {
134
+ return err
135
+ }
136
+ continue
137
+ }
138
+ if c , ok := v .(string ); ok {
139
+ fmt .Fprintf (out , " %s\n " , c )
140
+ fmt .Fprintln (out )
141
+ continue
142
+ }
143
+
144
+ return xerrors .Errorf ("unknown value type - this should never happen" )
145
+ }
146
+ return nil
119
147
}
0 commit comments