ValidateFlags validates all the command line flags and environment variables.
(w *world.World)
| 13 | |
| 14 | // ValidateFlags validates all the command line flags and environment variables. |
| 15 | func ValidateFlags(w *world.World) error { |
| 16 | // Set access token from environment if not provided |
| 17 | if w.AccessToken == "" { |
| 18 | w.AccessToken = os.Getenv("BYTEBASE_ACCESS_TOKEN") |
| 19 | } |
| 20 | // Set service account and secret from environment if not provided |
| 21 | if w.ServiceAccount == "" { |
| 22 | w.ServiceAccount = os.Getenv("BYTEBASE_SERVICE_ACCOUNT") |
| 23 | } |
| 24 | if w.ServiceAccountSecret == "" { |
| 25 | w.ServiceAccountSecret = os.Getenv("BYTEBASE_SERVICE_ACCOUNT_SECRET") |
| 26 | } |
| 27 | |
| 28 | // Set platform if not specified |
| 29 | if w.Platform == world.UnspecifiedPlatform { |
| 30 | w.Platform = world.GetJobPlatform() |
| 31 | } |
| 32 | |
| 33 | // Validate authentication: either access-token or service-account+secret |
| 34 | if w.AccessToken == "" { |
| 35 | if w.ServiceAccount == "" { |
| 36 | return errors.Errorf("service-account is required and cannot be empty") |
| 37 | } |
| 38 | if w.ServiceAccountSecret == "" { |
| 39 | return errors.Errorf("service-account-secret is required and cannot be empty") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Validate URL: must be a non-empty absolute URL. |
| 44 | if w.URL == "" { |
| 45 | return errors.Errorf("--url is required") |
| 46 | } |
| 47 | u, err := url.Parse(w.URL) |
| 48 | if err != nil { |
| 49 | return errors.Wrapf(err, "invalid URL format: %s", w.URL) |
| 50 | } |
| 51 | if u.Scheme == "" || u.Host == "" { |
| 52 | return errors.Errorf("--url must be an absolute URL (e.g. https://bytebase.example.com), got %q", w.URL) |
| 53 | } |
| 54 | w.URL = strings.TrimSuffix(u.String(), "/") // update the URL to the canonical form |
| 55 | |
| 56 | // Validate project format |
| 57 | if !strings.HasPrefix(w.Project, "projects/") { |
| 58 | return errors.Errorf("invalid project format, must be projects/{project}") |
| 59 | } |
| 60 | |
| 61 | // Validate targets format |
| 62 | return validateTargets(w.Targets) |
| 63 | } |
| 64 | |
| 65 | // validateTargets validates the targets format and ensures consistency. |
| 66 | func validateTargets(targets []string) error { |
no test coverage detected