// Package cmd implements the CLI command tree. package cmd import ( "encoding/json" "fmt" "io" "os" "github.com/spf13/cobra" ) // --------------------------------------------------------------------------- // Bundle command group (PLATFORM-MANAGEMENT-API.md ยง5(b)): // molecule bundle export [--file out.json] // molecule bundle import --file bundle.json (or - for stdin) // Tenant host, Org API Key (AdminAuth). // --------------------------------------------------------------------------- var bundleCmd = &cobra.Command{ Use: "bundle", Short: "Export and import workspace bundles", } func init() { bundleCmd.AddCommand(bundleExportCmd, bundleImportCmd) bundleExportCmd.Flags().StringVar(&bundleExportFile, "file", "", "Write bundle JSON to this file instead of stdout") bundleImportCmd.Flags().StringVar(&bundleImportFile, "file", "", "Read bundle JSON from this file (- for stdin)") } var ( bundleExportFile string bundleImportFile string ) var bundleExportCmd = &cobra.Command{ Use: "export ", Short: "Export a workspace as a bundle", Args: cobra.ExactArgs(1), RunE: runBundleExport, } func runBundleExport(_ *cobra.Command, args []string) error { raw, err := newClient().ExportBundle(args[0]) if err != nil { return fmt.Errorf("bundle export: %w", err) } if bundleExportFile != "" { if err := os.WriteFile(bundleExportFile, raw, 0o600); err != nil { return fmt.Errorf("bundle export: write %s: %w", bundleExportFile, err) } fmt.Printf("Bundle written to %s\n", bundleExportFile) return nil } return printRaw(raw) } var bundleImportCmd = &cobra.Command{ Use: "import --file ", Short: "Import a workspace bundle from a file or stdin", RunE: runBundleImport, } func runBundleImport(_ *cobra.Command, _ []string) error { if bundleImportFile == "" { return &exitError{code: 2, msg: "bundle import: --file is required"} } var data []byte var err error if bundleImportFile == "-" { data, err = io.ReadAll(os.Stdin) } else { data, err = os.ReadFile(bundleImportFile) } if err != nil { return fmt.Errorf("bundle import: read: %w", err) } if !json.Valid(data) { return &exitError{code: 2, msg: "bundle import: input is not valid JSON"} } raw, err := newClient().ImportBundle(json.RawMessage(data)) if err != nil { return fmt.Errorf("bundle import: %w", err) } return printRaw(raw) }