Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/configgen/validate.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.22
6
7 package main
8
9 import (
10 "errors"
11 "fmt"
12 "go/version"
13
14 "golang.org/x/mod/semver"
15 "golang.org/x/telemetry/internal/chartconfig"
16 "golang.org/x/telemetry/internal/telemetry"
17 )
18
19 // ValidateChartConfig checks that a ChartConfig is complete and coherent,
20 // returning an error describing all problems encountered, or nil.
21 func ValidateChartConfig(cfg chartconfig.ChartConfig) error {
22 var errs []error
23 reportf := func(format string, args ...any) {
24 errs = append(errs, fmt.Errorf(format, args...))
25 }
26 if cfg.Title == "" {
27 reportf("title must be set")
28 }
29 if len(cfg.Issue) == 0 {
30 reportf("at least one issue is required")
31 }
32 if cfg.Program == "" {
33 reportf("program must be set")
34 }
35 if cfg.Counter == "" {
36 reportf("counter must be set")
37 }
38 if cfg.Type == "" {
39 reportf("type must be set")
40 }
41 if cfg.Depth < 0 {
42 reportf("invalid depth %d: must be non-negative", cfg.Depth)
43 }
44 if cfg.Depth != 0 && cfg.Type != "stack" {
45 reportf("depth can only be set for \"stack\" chart types")
46 }
47 valid := semver.IsValid
48 if telemetry.IsToolchainProgram(cfg.Program) {
49 valid = version.IsValid
50 }
51 if cfg.Version != "" && !valid(cfg.Version) {
52 reportf("%q is not a valid version (must be a go version or semver)", cfg.Version)
53 }
54 return errors.Join(errs...)
55 }