-
Notifications
You must be signed in to change notification settings - Fork 248
/
main.go
121 lines (109 loc) · 3.54 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
// generate-registry-metadata creates a metadata.json file for a step registry directory
// that contains extra information useful for the configresolver's web UI
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/test-infra/prow/repoowners"
"sigs.k8s.io/yaml"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/load"
"github.com/openshift/ci-tools/pkg/util/gzip"
)
type options struct {
registry string
}
func (o *options) Validate() error {
if o.registry == "" {
return errors.New("--registry is required")
}
return nil
}
func gatherOptions() (options, error) {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&o.registry, "registry", "", "Path to the step registry directory.")
if err := fs.Parse(os.Args[1:]); err != nil {
return options{}, fmt.Errorf("could not parse input: %w", err)
}
return o, nil
}
func generateMetadata(registryPath string) (api.RegistryMetadata, error) {
metadata := make(map[string]api.RegistryInfo)
var errs []error
err := filepath.WalkDir(registryPath, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info != nil && !info.IsDir() && filepath.Ext(info.Name()) == ".yaml" {
relpath, err := filepath.Rel(registryPath, path)
if err != nil {
return fmt.Errorf("failed to determine relative path for %s: %w", path, err)
}
ownersPath := filepath.Join(filepath.Dir(path), "OWNERS")
// all step registry components are required to have an owners file in the same directory as the component
owners, err := gzip.ReadFileMaybeGZIP(ownersPath)
if err != nil {
if os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("missing OWNERS file at %s", ownersPath))
} else {
errs = append(errs, fmt.Errorf("failed to read OWNERS file for component %s at %s: %w", info.Name(), ownersPath, err))
}
return nil
}
var ownersConfig repoowners.Config
err = yaml.Unmarshal(owners, &ownersConfig)
if err != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal OWNERS file at %s: %w", ownersPath, err))
return nil
}
metadata[info.Name()] = api.RegistryInfo{
Path: relpath,
Owners: ownersConfig,
}
}
return nil
})
if err := utilerrors.NewAggregate(append(errs, err)); err != nil {
return api.RegistryMetadata{}, fmt.Errorf("Failed to update registry metadata: %w", err)
}
return metadata, nil
}
func writeMetadata(registryPath string, metadata api.RegistryMetadata) error {
for filename, data := range metadata {
metadataPath := filepath.Join(registryPath, filepath.Dir(data.Path), fmt.Sprintf("%s%s", strings.TrimSuffix(filename, ".yaml"), load.MetadataSuffix))
output, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return fmt.Errorf("Failed to marshal metadata file `%s`: %w", metadataPath, err)
}
if err := os.WriteFile(metadataPath, output, 0644); err != nil {
return fmt.Errorf("Failed to write metadata file `%s`: %w", metadataPath, err)
}
}
return nil
}
func main() {
o, err := gatherOptions()
if err != nil {
logrus.WithError(err).Fatal("failed to gather options")
}
if err := o.Validate(); err != nil {
logrus.WithError(err).Fatal("invalid options")
}
metadata, err := generateMetadata(o.registry)
if err != nil {
log.Fatalf("Failed to update metadata: %v", err)
}
if err := writeMetadata(o.registry, metadata); err != nil {
log.Fatalf("Failed to write metadata: %v", err)
}
}