resolveInitIDStrategy returns the ID strategy config. If --id-strategy was provided, uses it. If TTY, prompts. Otherwise defaults to sequential.
(cmd *cobra.Command, isTTY bool)
| 360 | // resolveInitIDStrategy returns the ID strategy config. |
| 361 | // If --id-strategy was provided, uses it. If TTY, prompts. Otherwise defaults to sequential. |
| 362 | func resolveInitIDStrategy(cmd *cobra.Command, isTTY bool) (idStrategyConfig, error) { |
| 363 | cfg := idStrategyConfig{strategy: "sequential"} |
| 364 | |
| 365 | if cmd.Flags().Changed("id-strategy") { |
| 366 | if !isValidIDStrategy(projectInitIDStrategy) { |
| 367 | return cfg, fmt.Errorf("invalid --id-strategy %q: must be one of %s", |
| 368 | projectInitIDStrategy, strings.Join(validIDStrategies, ", ")) |
| 369 | } |
| 370 | cfg.strategy = projectInitIDStrategy |
| 371 | if cfg.strategy == "prefixed" { |
| 372 | prefix, err := resolveIDPrefix(cmd, isTTY) |
| 373 | if err != nil { |
| 374 | return cfg, err |
| 375 | } |
| 376 | cfg.prefix = prefix |
| 377 | } |
| 378 | return cfg, nil |
| 379 | } |
| 380 | |
| 381 | if isTTY { |
| 382 | strategy, err := promptIDStrategy() |
| 383 | if err != nil { |
| 384 | return cfg, err |
| 385 | } |
| 386 | cfg.strategy = strategy |
| 387 | if cfg.strategy == "prefixed" { |
| 388 | prefix, err := resolveIDPrefix(cmd, isTTY) |
| 389 | if err != nil { |
| 390 | return cfg, err |
| 391 | } |
| 392 | cfg.prefix = prefix |
| 393 | } |
| 394 | return cfg, nil |
| 395 | } |
| 396 | |
| 397 | return cfg, nil |
| 398 | } |
| 399 | |
| 400 | func isValidIDStrategy(s string) bool { |
| 401 | for _, v := range validIDStrategies { |
no test coverage detected