|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/Snider/Borg/pkg/console" |
| 9 | + "github.com/Snider/Borg/pkg/tim" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var consoleCmd = NewConsoleCmd() |
| 14 | + |
| 15 | +// NewConsoleCmd creates the console parent command. |
| 16 | +func NewConsoleCmd() *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "console", |
| 19 | + Short: "Manage encrypted PWA console demos", |
| 20 | + Long: `The Borg Console packages and serves encrypted PWA demos. |
| 21 | +
|
| 22 | +Build a console STIM: |
| 23 | + borg console build -p "password" -o console.stim |
| 24 | +
|
| 25 | +Serve with unlock page: |
| 26 | + borg console serve console.stim --open |
| 27 | +
|
| 28 | +Serve pre-unlocked: |
| 29 | + borg console serve console.stim -p "password" --open`, |
| 30 | + } |
| 31 | + |
| 32 | + cmd.AddCommand(NewConsoleBuildCmd()) |
| 33 | + cmd.AddCommand(NewConsoleServeCmd()) |
| 34 | + |
| 35 | + return cmd |
| 36 | +} |
| 37 | + |
| 38 | +// NewConsoleBuildCmd creates the build subcommand. |
| 39 | +func NewConsoleBuildCmd() *cobra.Command { |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "build", |
| 42 | + Short: "Build a console STIM from demo files", |
| 43 | + Long: `Packages HTML demo files into an encrypted STIM container. |
| 44 | +
|
| 45 | +By default, looks for files in js/borg-stmf/ directory. |
| 46 | +Required files: index.html, support-reply.html, stmf.wasm, wasm_exec.js`, |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + password, _ := cmd.Flags().GetString("password") |
| 49 | + output, _ := cmd.Flags().GetString("output") |
| 50 | + sourceDir, _ := cmd.Flags().GetString("source") |
| 51 | + |
| 52 | + if password == "" { |
| 53 | + return fmt.Errorf("password is required") |
| 54 | + } |
| 55 | + |
| 56 | + // Create new TIM |
| 57 | + m, err := tim.New() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("creating TIM: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + // Required demo files |
| 63 | + files := []string{ |
| 64 | + "index.html", |
| 65 | + "support-reply.html", |
| 66 | + "stmf.wasm", |
| 67 | + "wasm_exec.js", |
| 68 | + } |
| 69 | + |
| 70 | + // Add each file to the TIM |
| 71 | + for _, f := range files { |
| 72 | + path := filepath.Join(sourceDir, f) |
| 73 | + data, err := os.ReadFile(path) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("reading %s: %w", f, err) |
| 76 | + } |
| 77 | + m.RootFS.AddData(f, data) |
| 78 | + fmt.Printf(" + %s (%d bytes)\n", f, len(data)) |
| 79 | + } |
| 80 | + |
| 81 | + // Encrypt to STIM |
| 82 | + stim, err := m.ToSigil(password) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("encrypting STIM: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Write output |
| 88 | + if err := os.WriteFile(output, stim, 0644); err != nil { |
| 89 | + return fmt.Errorf("writing output: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Printf("\nBuilt: %s (%d bytes)\n", output, len(stim)) |
| 93 | + fmt.Println("Encrypted with ChaCha20-Poly1305") |
| 94 | + |
| 95 | + return nil |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + cmd.Flags().StringP("password", "p", "", "Encryption password (required)") |
| 100 | + cmd.Flags().StringP("output", "o", "console.stim", "Output file") |
| 101 | + cmd.Flags().StringP("source", "s", "js/borg-stmf", "Source directory") |
| 102 | + cmd.MarkFlagRequired("password") |
| 103 | + |
| 104 | + return cmd |
| 105 | +} |
| 106 | + |
| 107 | +// NewConsoleServeCmd creates the serve subcommand. |
| 108 | +func NewConsoleServeCmd() *cobra.Command { |
| 109 | + cmd := &cobra.Command{ |
| 110 | + Use: "serve [stim-file]", |
| 111 | + Short: "Serve an encrypted console STIM", |
| 112 | + Long: `Starts an HTTP server to serve encrypted STIM content. |
| 113 | +
|
| 114 | +Without a password, shows a dark-themed unlock page. |
| 115 | +With a password, decrypts immediately and serves content. |
| 116 | +
|
| 117 | +Examples: |
| 118 | + borg console serve demos.stim --open |
| 119 | + borg console serve demos.stim -p "password" --port 3000`, |
| 120 | + Args: cobra.ExactArgs(1), |
| 121 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 122 | + stimPath := args[0] |
| 123 | + password, _ := cmd.Flags().GetString("password") |
| 124 | + port, _ := cmd.Flags().GetString("port") |
| 125 | + openBrowser, _ := cmd.Flags().GetBool("open") |
| 126 | + |
| 127 | + // Create server |
| 128 | + server, err := console.NewServer(stimPath, password, port) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + // Print status |
| 134 | + fmt.Printf("Borg Console serving at %s\n", server.URL()) |
| 135 | + if password != "" { |
| 136 | + fmt.Println("Status: Unlocked (password provided)") |
| 137 | + } else { |
| 138 | + fmt.Println("Status: Locked (unlock page active)") |
| 139 | + } |
| 140 | + fmt.Println() |
| 141 | + |
| 142 | + // Open browser if requested |
| 143 | + if openBrowser { |
| 144 | + if err := console.OpenBrowser(server.URL()); err != nil { |
| 145 | + fmt.Printf("Warning: could not open browser: %v\n", err) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Start serving |
| 150 | + return server.Start() |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + cmd.Flags().StringP("password", "p", "", "Decryption password (skip unlock page)") |
| 155 | + cmd.Flags().String("port", "8080", "Port to serve on") |
| 156 | + cmd.Flags().Bool("open", false, "Auto-open browser") |
| 157 | + |
| 158 | + return cmd |
| 159 | +} |
| 160 | + |
| 161 | +func init() { |
| 162 | + RootCmd.AddCommand(consoleCmd) |
| 163 | +} |
0 commit comments