|
| 1 | +package export |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/mozilla-ai/mcpd/v2/internal/cmd" |
| 9 | + "github.com/mozilla-ai/mcpd/v2/internal/cmd/options" |
| 10 | + cmdopts "github.com/mozilla-ai/mcpd/v2/internal/cmd/options" |
| 11 | + "github.com/mozilla-ai/mcpd/v2/internal/config" |
| 12 | + "github.com/mozilla-ai/mcpd/v2/internal/context" |
| 13 | + "github.com/mozilla-ai/mcpd/v2/internal/flags" |
| 14 | +) |
| 15 | + |
| 16 | +type Cmd struct { |
| 17 | + *cmd.BaseCmd |
| 18 | + Format cmd.ExportFormat |
| 19 | + ContractOutput string |
| 20 | + ContextOutput string |
| 21 | + cfgLoader config.Loader |
| 22 | + ctxLoader context.Loader |
| 23 | +} |
| 24 | + |
| 25 | +func NewCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) { |
| 26 | + opts, err := cmdopts.NewOptions(opt...) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + c := &Cmd{ |
| 32 | + BaseCmd: baseCmd, |
| 33 | + Format: cmd.FormatDotEnv, // Default to dotenv |
| 34 | + cfgLoader: opts.ConfigLoader, |
| 35 | + ctxLoader: opts.ContextLoader, |
| 36 | + } |
| 37 | + |
| 38 | + cobraCmd := &cobra.Command{ |
| 39 | + Use: "export", |
| 40 | + Short: "Exports current configuration, generating a pair of safe and portable configuration files", |
| 41 | + Long: "Exports current configuration, generating a pair of safe and portable configuration files.\n\n" + |
| 42 | + "Using a project's required configuration (e.g. .mcpd.toml) and the locally configured runtime values " + |
| 43 | + "from the execution context file (e.g. ~/.config/mcpd/secrets.dev.toml), outputs both an 'Environment Contract' " + |
| 44 | + "and 'Portable Execution Context' file." + |
| 45 | + "These files are safe to check into version control if required.\n\n" + |
| 46 | + "This allows running an mcpd project in any environment, cleanly separating the configuration structure " + |
| 47 | + "from the secret values.", |
| 48 | + RunE: c.run, |
| 49 | + } |
| 50 | + |
| 51 | + // Portable Execution Context: |
| 52 | + // |
| 53 | + // A new secrets.toml file that defines the runtime args and env sections for each server, |
| 54 | + // using the placeholders from the environment contract. |
| 55 | + cobraCmd.Flags().StringVar( |
| 56 | + &c.ContextOutput, |
| 57 | + "context-output", |
| 58 | + "secrets.prod.toml", |
| 59 | + "Optional, specify the output path for the templated execution context config file", |
| 60 | + ) |
| 61 | + |
| 62 | + // Environment Contract: |
| 63 | + // |
| 64 | + // Lists all required and configured environment variables as secure, namespaced placeholders |
| 65 | + // e.g. MCPD__{SERVER_NAME}__{ENV_VAR} |
| 66 | + // Creates placeholders for command line arguments to be populated with env vars |
| 67 | + // e.g. MCPD__{SERVER_NAME}__ARG_{ARG_NAME} |
| 68 | + // This file is intended for the platform operator or CI/CD system. |
| 69 | + cobraCmd.Flags().StringVar( |
| 70 | + &c.ContractOutput, |
| 71 | + "contract-output", |
| 72 | + ".env", |
| 73 | + "Optional, specify the output path for the templated environment file", |
| 74 | + ) |
| 75 | + |
| 76 | + allowed := cmd.AllowedFormats() |
| 77 | + cobraCmd.Flags().Var( |
| 78 | + &c.Format, |
| 79 | + "format", |
| 80 | + fmt.Sprintf("Specify the format of the contract output file (one of: %s)", allowed.String()), |
| 81 | + ) |
| 82 | + |
| 83 | + return cobraCmd, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *Cmd) run(cmd *cobra.Command, args []string) error { |
| 87 | + contextPath := c.ContextOutput |
| 88 | + // contractPath := c.ContractOutput |
| 89 | + |
| 90 | + if err := exportPortableExecutionContext(c.ctxLoader, flags.RuntimeFile, contextPath); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + fmt.Fprintf(cmd.OutOrStdout(), "✓ Portable Execution Context exported: %s\n", contextPath) |
| 95 | + |
| 96 | + // Export 'Environment Contract' |
| 97 | + // TODO: export to contractPath based on format |
| 98 | + // fmt.Fprintf(cmd.OutOrStdout(), "✓ Environment Contract exported: %s\n", contractPath) |
| 99 | + |
| 100 | + fmt.Fprintf(cmd.OutOrStdout(), "✓ Export completed successfully!\n") |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func exportPortableExecutionContext(loader context.Loader, src string, dest string) error { |
| 106 | + mod, err := loader.Load(src) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("failed to load execution context config: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + exp, ok := mod.(context.Exporter) |
| 112 | + if !ok { |
| 113 | + return fmt.Errorf("execution context config does not support exporting") |
| 114 | + } |
| 115 | + |
| 116 | + return exp.Export(dest) |
| 117 | +} |
0 commit comments