| // Copyright 2023 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "log" |
| "path/filepath" |
| |
| "github.com/maruel/subcommands" |
| "go.fuchsia.dev/infra/cmd/roller-configurator/proto" |
| ) |
| |
| func cmdValidate() *subcommands.Command { |
| return &subcommands.Command{ |
| UsageLine: "validate [-config <config-path>]", |
| ShortDesc: "Validate a rollers.textproto file.", |
| LongDesc: "Validate a rollers.textproto file.", |
| CommandRun: func() subcommands.CommandRun { |
| c := &validateRun{} |
| c.Init() |
| return c |
| }, |
| } |
| } |
| |
| type validateRun struct { |
| subcommands.CommandRunBase |
| configPath string |
| } |
| |
| func (c *validateRun) Init() { |
| c.Flags.StringVar(&c.configPath, "config", "rollers.textproto", "Path to the config file to validate.") |
| } |
| |
| func (c *validateRun) Run(a subcommands.Application, args []string, env subcommands.Env) int { |
| config, err := readConfig(c.configPath) |
| if err != nil { |
| fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| return 1 |
| } |
| // rollers.textproto files must be located in the repository root. |
| repoRoot := filepath.Dir(c.configPath) |
| if err := validate(context.Background(), repoRoot, config); err != nil { |
| fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| return 1 |
| } |
| return 0 |
| } |
| |
| func validate(ctx context.Context, repoRoot string, config *proto.Config) error { |
| for i, roller := range config.GetRollers() { |
| toRollDesc := roller.ProtoReflect().Descriptor().Oneofs().ByName("to_roll") |
| field := roller.ProtoReflect().WhichOneof(toRollDesc) |
| if field == nil { |
| return fmt.Errorf("entry %d is missing an entity to roll", i) |
| } |
| |
| var toValidate interface { |
| Validate(ctx context.Context, repoRoot string) error |
| } |
| switch field.Name() { |
| case "submodule": |
| toValidate = roller.GetSubmodule() |
| case "cipd_ensure_file": |
| toValidate = roller.GetCipdEnsureFile() |
| case "jiri_project": |
| toValidate = roller.GetJiriProject() |
| case "jiri_packages": |
| toValidate = roller.GetJiriPackages() |
| default: |
| log.Panicf("unknown to_roll type: %q", field.Name()) |
| } |
| |
| if err := toValidate.Validate(ctx, repoRoot); err != nil { |
| return err |
| } |
| } |
| return nil |
| } |