|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/open-source-cloud/devstack/internal/ide" |
| 9 | +) |
| 10 | + |
| 11 | +// newIdeCmd wires `devstack ide` — the spec-17 editor/IDE generation sink. It loads |
| 12 | +// the workspace and authors, deterministically, the artifacts that point editors at |
| 13 | +// devstack's already-generated compose stacks: per-repo .devcontainer/devcontainer.json, |
| 14 | +// a multi-root <name>.code-workspace, and per-repo .vscode/{launch,settings}.json. |
| 15 | +// |
| 16 | +// It is pure file authorship (no Docker, no ledger, no flock), so it mirrors the |
| 17 | +// generate command's --check/--json contract. Targets are selected with |
| 18 | +// --devcontainer / --vscode; --all (the no-flag default) emits both. |
| 19 | +func newIdeCmd(g *GlobalOpts) *cobra.Command { |
| 20 | + var ( |
| 21 | + devcontainer bool |
| 22 | + vscode bool |
| 23 | + all bool |
| 24 | + check bool |
| 25 | + ) |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "ide", |
| 28 | + Short: "Generate devcontainer/.code-workspace/launch editor configs", |
| 29 | + Long: "ide authors, from the same resolved config as `generate`, the editor artifacts\n" + |
| 30 | + "that point at devstack's already-generated compose stacks:\n\n" + |
| 31 | + " * <repo>/.devcontainer/devcontainer.json (attach the IDE to the SAME\n" + |
| 32 | + " devstack-<name> compose project + shared network devstack up runs)\n" + |
| 33 | + " * <workspace-root>/<name>.code-workspace (VS Code multi-root)\n" + |
| 34 | + " * <repo>/.vscode/{launch,settings}.json (debugger + schema-map stubs)\n\n" + |
| 35 | + "Select targets with --devcontainer / --vscode; --all (the default when no target\n" + |
| 36 | + "flag is given) emits both. Output is byte-deterministic (writeIfChanged); --check\n" + |
| 37 | + "reports drift without writing (CI-friendly).", |
| 38 | + Args: cobra.NoArgs, |
| 39 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 40 | + m, err := loadWorkspace() |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + targets := ide.Targets{Devcontainer: devcontainer, VSCode: vscode} |
| 45 | + if all || (!devcontainer && !vscode) { |
| 46 | + targets = ide.All() |
| 47 | + } |
| 48 | + gen := ide.New(m) |
| 49 | + arts, err := gen.Build(targets) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + if check { |
| 54 | + return reportIdeCheck(cmd, g, arts) |
| 55 | + } |
| 56 | + results, err := ide.Write(arts) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return reportIdeWrite(cmd, g, results) |
| 61 | + }, |
| 62 | + } |
| 63 | + cmd.Flags().BoolVar(&devcontainer, "devcontainer", false, "emit per-repo .devcontainer/devcontainer.json only") |
| 64 | + cmd.Flags().BoolVar(&vscode, "vscode", false, "emit the .code-workspace + per-repo .vscode/ configs only") |
| 65 | + cmd.Flags().BoolVar(&all, "all", false, "emit every target (default when no target flag is given)") |
| 66 | + cmd.Flags().BoolVar(&check, "check", false, "report drift without writing (CI)") |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +func reportIdeWrite(cmd *cobra.Command, g *GlobalOpts, results []ide.WriteResult) error { |
| 71 | + if g.JSON { |
| 72 | + return writeJSON(cmd, map[string]any{"ok": true, "artifacts": results}) |
| 73 | + } |
| 74 | + if g.Quiet { |
| 75 | + return nil |
| 76 | + } |
| 77 | + w := cmd.OutOrStdout() |
| 78 | + for _, r := range results { |
| 79 | + state := "unchanged" |
| 80 | + if r.Changed { |
| 81 | + state = "updated" |
| 82 | + } |
| 83 | + fmt.Fprintf(w, "%s → %s (%s)\n", r.Kind, r.Path, state) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func reportIdeCheck(cmd *cobra.Command, g *GlobalOpts, arts []ide.Artifact) error { |
| 89 | + upToDate := ide.UpToDate(arts) |
| 90 | + type entry struct { |
| 91 | + Path string `json:"path"` |
| 92 | + Kind string `json:"kind"` |
| 93 | + } |
| 94 | + paths := make([]entry, 0, len(arts)) |
| 95 | + for _, a := range arts { |
| 96 | + paths = append(paths, entry{Path: a.Rel, Kind: a.Kind}) |
| 97 | + } |
| 98 | + if g.JSON { |
| 99 | + if err := writeJSON(cmd, map[string]any{"ok": upToDate, "artifacts": paths}); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } else if !g.Quiet { |
| 103 | + w := cmd.OutOrStdout() |
| 104 | + for _, p := range paths { |
| 105 | + fmt.Fprintf(w, "%s: %s\n", p.Kind, p.Path) |
| 106 | + } |
| 107 | + } |
| 108 | + if !upToDate { |
| 109 | + return fmt.Errorf("IDE artifacts are stale; run `%s ide`", rootName(cmd)) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
0 commit comments