ParseCheckoutConfigs converts a raw frontmatter value (single map or array of maps) into a slice of CheckoutConfig entries. Returns (nil, nil) if the value is nil; for non-nil values, invalid types or shapes result in a non-nil error.
(raw any)
| 15 | // Returns (nil, nil) if the value is nil; for non-nil values, invalid types or shapes |
| 16 | // result in a non-nil error. |
| 17 | func ParseCheckoutConfigs(raw any) ([]*CheckoutConfig, error) { |
| 18 | if raw == nil { |
| 19 | return nil, nil |
| 20 | } |
| 21 | checkoutManagerLog.Printf("Parsing checkout configuration: type=%T", raw) |
| 22 | |
| 23 | var configs []*CheckoutConfig |
| 24 | |
| 25 | // Try single object first |
| 26 | if singleMap, ok := raw.(map[string]any); ok { |
| 27 | cfg, err := checkoutConfigFromMap(singleMap) |
| 28 | if err != nil { |
| 29 | return nil, fmt.Errorf("invalid checkout configuration: %w", err) |
| 30 | } |
| 31 | configs = []*CheckoutConfig{cfg} |
| 32 | } else if arr, ok := raw.([]any); ok { |
| 33 | // Try array of objects |
| 34 | configs = make([]*CheckoutConfig, 0, len(arr)) |
| 35 | for i, item := range arr { |
| 36 | itemMap, ok := item.(map[string]any) |
| 37 | if !ok { |
| 38 | return nil, fmt.Errorf("checkout[%d]: expected object, got %T", i, item) |
| 39 | } |
| 40 | cfg, err := checkoutConfigFromMap(itemMap) |
| 41 | if err != nil { |
| 42 | return nil, fmt.Errorf("checkout[%d]: %w", i, err) |
| 43 | } |
| 44 | configs = append(configs, cfg) |
| 45 | } |
| 46 | } else { |
| 47 | return nil, fmt.Errorf("checkout must be an object or an array of objects, got %T", raw) |
| 48 | } |
| 49 | |
| 50 | // Validate that at most one logical checkout target has current: true. |
| 51 | // Multiple current checkouts are not allowed since only one repo/path pair can be |
| 52 | // the primary target for the agent at a time. Multiple configs that merge into the |
| 53 | // same (repository, path, wiki) tuple are treated as a single logical checkout. |
| 54 | currentTargets := make(map[string]struct{}) |
| 55 | for _, cfg := range configs { |
| 56 | if !cfg.Current { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | repo := strings.TrimSpace(cfg.Repository) |
| 61 | path := strings.TrimSpace(cfg.Path) |
| 62 | wiki := "false" |
| 63 | if cfg.Wiki { |
| 64 | wiki = "true" |
| 65 | } |
| 66 | key := repo + "\x00" + path + "\x00" + wiki |
| 67 | |
| 68 | currentTargets[key] = struct{}{} |
| 69 | } |
| 70 | if len(currentTargets) > 1 { |
| 71 | checkoutManagerLog.Printf("Rejecting checkout config: %d distinct current targets, only one allowed", len(currentTargets)) |
| 72 | return nil, fmt.Errorf("only one checkout target may have current: true, found %d", len(currentTargets)) |
| 73 | } |
| 74 |