|
| 1 | +package show |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/codefly-dev/cli/cmd/common" |
| 9 | + clicomposition "github.com/codefly-dev/cli/pkg/composition" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var showFixturesJSON bool |
| 14 | + |
| 15 | +// FixturesCmd lists the fixtures the workspace's composed packages declare, with |
| 16 | +// the principals each one seeds. A fixture is the contract a solution test |
| 17 | +// resolves an identity against instead of hardcoding a seeded login, so this |
| 18 | +// reports what `--fixture` can name and what authenticating as one gets you. |
| 19 | +var FixturesCmd = &cobra.Command{ |
| 20 | + Use: "fixtures", |
| 21 | + Short: "Show the fixtures the workspace's composed packages declare", |
| 22 | + Long: `List every fixture the packages this workspace composes declare, with the |
| 23 | +principals each seeds. |
| 24 | +
|
| 25 | +A fixture names the state a composed host boots with under CODEFLY__FIXTURE, so |
| 26 | +these are exactly the names "codefly run solution --fixture" accepts. Each |
| 27 | +principal is reported by id, email and role; role is the lookup key a test |
| 28 | +resolves an identity by. Seed tokens are not printed. |
| 29 | +
|
| 30 | +A package that cannot be read, and a name two packages both declare, are |
| 31 | +reported as problems — after the listing, so the fixtures that did resolve are |
| 32 | +still named. The command exits non-zero when it reports one. |
| 33 | +
|
| 34 | +Examples: |
| 35 | + codefly show fixtures |
| 36 | + codefly show fixtures --json`, |
| 37 | + Args: cobra.NoArgs, |
| 38 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 39 | + ctx, done := common.NewContext() |
| 40 | + defer done() |
| 41 | + |
| 42 | + // Deliberately the plain loader: materializing pinned modules writes the |
| 43 | + // workspace overlay and .gitignore, which an inspection command must not |
| 44 | + // do. Resolving a composed module below still materializes into the |
| 45 | + // content-addressed cache, which leaves the workspace untouched. |
| 46 | + workspace, err := common.LoadWorkspace(ctx) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("cannot load workspace: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + fixtures, problems := clicomposition.WorkspaceFixtures(ctx, workspace) |
| 52 | + |
| 53 | + report := fixturesReport{Workspace: workspace.Name, Fixtures: []fixtureReport{}, Problems: []string{}} |
| 54 | + for _, fixture := range fixtures { |
| 55 | + entry := fixtureReport{ |
| 56 | + Name: fixture.Name, |
| 57 | + Description: fixture.Description, |
| 58 | + Principals: []fixturePrincipalReport{}, |
| 59 | + } |
| 60 | + for _, principal := range fixture.Principals { |
| 61 | + entry.Principals = append(entry.Principals, fixturePrincipalReport{ |
| 62 | + ID: principal.ID, |
| 63 | + Email: principal.Email, |
| 64 | + Role: principal.Role, |
| 65 | + }) |
| 66 | + } |
| 67 | + report.Fixtures = append(report.Fixtures, entry) |
| 68 | + } |
| 69 | + for _, problem := range problems { |
| 70 | + report.Problems = append(report.Problems, problem.Error()) |
| 71 | + } |
| 72 | + |
| 73 | + out := cmd.OutOrStdout() |
| 74 | + if showFixturesJSON { |
| 75 | + encoder := json.NewEncoder(out) |
| 76 | + encoder.SetIndent("", " ") |
| 77 | + if err := encoder.Encode(report); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + return errors.Join(problems...) |
| 81 | + } |
| 82 | + |
| 83 | + if len(report.Fixtures) == 0 { |
| 84 | + fmt.Fprintf(out, "Workspace %q composes no package declaring a fixture.\n", report.Workspace) |
| 85 | + } else { |
| 86 | + fmt.Fprintf(out, "Fixtures declared by the packages workspace %q composes:\n\n", report.Workspace) |
| 87 | + for _, fixture := range report.Fixtures { |
| 88 | + fmt.Fprintf(out, "• %s", fixture.Name) |
| 89 | + if fixture.Description != "" { |
| 90 | + fmt.Fprintf(out, " — %s", fixture.Description) |
| 91 | + } |
| 92 | + fmt.Fprintln(out) |
| 93 | + if len(fixture.Principals) == 0 { |
| 94 | + fmt.Fprintln(out, " (seeds no principal)") |
| 95 | + continue |
| 96 | + } |
| 97 | + for _, principal := range fixture.Principals { |
| 98 | + fmt.Fprintf(out, " %-16s %-32s [%s]\n", principal.ID, principal.Email, principal.Role) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return errors.Join(problems...) |
| 103 | + }, |
| 104 | +} |
| 105 | + |
| 106 | +// fixturePrincipalReport omits the principal's token: the manifest ships inside |
| 107 | +// the package, so the token is a development seed credential rather than a |
| 108 | +// secret, but printing it on an inspection command puts it in scrollback and CI |
| 109 | +// logs for no one who needed it there. |
| 110 | +type fixturePrincipalReport struct { |
| 111 | + ID string `json:"id"` |
| 112 | + Email string `json:"email"` |
| 113 | + Role string `json:"role"` |
| 114 | +} |
| 115 | + |
| 116 | +type fixtureReport struct { |
| 117 | + Name string `json:"name"` |
| 118 | + Description string `json:"description,omitempty"` |
| 119 | + Principals []fixturePrincipalReport `json:"principals"` |
| 120 | +} |
| 121 | + |
| 122 | +type fixturesReport struct { |
| 123 | + Workspace string `json:"workspace"` |
| 124 | + Fixtures []fixtureReport `json:"fixtures"` |
| 125 | + Problems []string `json:"problems"` |
| 126 | +} |
| 127 | + |
| 128 | +func init() { |
| 129 | + FixturesCmd.Flags().BoolVar(&showFixturesJSON, "json", false, "Emit machine-readable JSON") |
| 130 | +} |
0 commit comments