|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/raystack/salt/cli/printer" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +// protocols maps CLI protocol names to their doc filenames. |
| 13 | +var protocols = map[string]string{ |
| 14 | + "spp": "spp.md", |
| 15 | + "epp": "epp.md", |
| 16 | + "tm": "tm.md", |
| 17 | + "tc": "tc.md", |
| 18 | + "time": "time.md", |
| 19 | + "cadu": "cadu.md", |
| 20 | + "cltu": "cltu.md", |
| 21 | +} |
| 22 | + |
| 23 | +func manualCmd(docsFS embed.FS) *cobra.Command { |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "manual [protocol]", |
| 26 | + Short: "Display protocol reference manual", |
| 27 | + Long: "Display the CLI reference manual for a protocol. Run without arguments to list available topics.", |
| 28 | + Annotations: map[string]string{ |
| 29 | + "group": "help", |
| 30 | + }, |
| 31 | + ValidArgs: protocolNames(), |
| 32 | + Args: cobra.MaximumNArgs(1), |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + if len(args) == 0 { |
| 35 | + return printManualIndex() |
| 36 | + } |
| 37 | + return printManual(docsFS, args[0]) |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func printManualIndex() error { |
| 45 | + var sb strings.Builder |
| 46 | + sb.WriteString("# Astro Manual\n\n") |
| 47 | + sb.WriteString("Available protocol manuals:\n\n") |
| 48 | + sb.WriteString("| Protocol | Command |\n") |
| 49 | + sb.WriteString("|----------|---------|\n") |
| 50 | + sb.WriteString("| Space Packet Protocol | `astro manual spp` |\n") |
| 51 | + sb.WriteString("| Encapsulation Packet Protocol | `astro manual epp` |\n") |
| 52 | + sb.WriteString("| TM Transfer Frames | `astro manual tm` |\n") |
| 53 | + sb.WriteString("| TC Transfer Frames | `astro manual tc` |\n") |
| 54 | + sb.WriteString("| Time Code Formats | `astro manual time` |\n") |
| 55 | + sb.WriteString("| Channel Access Data Units | `astro manual cadu` |\n") |
| 56 | + sb.WriteString("| Command Link Transmission Units | `astro manual cltu` |\n") |
| 57 | + |
| 58 | + out, err := printer.Markdown(sb.String()) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + fmt.Print(out) |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func printManual(docsFS embed.FS, protocol string) error { |
| 67 | + filename, ok := protocols[protocol] |
| 68 | + if !ok { |
| 69 | + return fmt.Errorf("unknown protocol %q — run 'astro manual' to see available topics", protocol) |
| 70 | + } |
| 71 | + |
| 72 | + content, err := docsFS.ReadFile("docs/cli/" + filename) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("reading manual for %s: %w", protocol, err) |
| 75 | + } |
| 76 | + |
| 77 | + out, err := printer.Markdown(string(content)) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + fmt.Print(out) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func protocolNames() []string { |
| 86 | + names := make([]string, 0, len(protocols)) |
| 87 | + for k := range protocols { |
| 88 | + names = append(names, k) |
| 89 | + } |
| 90 | + return names |
| 91 | +} |
0 commit comments