|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/kernel/hypeman-go" |
| 11 | + "github.com/kernel/hypeman-go/option" |
| 12 | + "github.com/tidwall/gjson" |
| 13 | + "github.com/urfave/cli/v3" |
| 14 | +) |
| 15 | + |
| 16 | +var capabilitiesCmd = cli.Command{ |
| 17 | + Name: "capabilities", |
| 18 | + Aliases: []string{"capability"}, |
| 19 | + Usage: "Show machine-readable host capabilities", |
| 20 | + Description: `Report server and API version, host OS/architecture, every runtime available on |
| 21 | +this host with its per-runtime feature IDs, the configured default runtime and |
| 22 | +whether it is available, guest networking model and host gateway, supported |
| 23 | +image platforms, and stable server-level feature IDs. |
| 24 | +
|
| 25 | +Runtime-derived values reflect the actual host (for example, snapshot and |
| 26 | +standby support on macOS is gated on the host OS version), so clients can gate |
| 27 | +behavior on capabilities without hard-coding hypervisor knowledge. |
| 28 | +
|
| 29 | +Examples: |
| 30 | + # Show capabilities (default table format) |
| 31 | + hypeman capabilities |
| 32 | +
|
| 33 | + # Show capabilities as JSON |
| 34 | + hypeman capabilities --format json |
| 35 | +
|
| 36 | + # Show only the runtimes this host supports |
| 37 | + hypeman capabilities --transform runtimes`, |
| 38 | + Action: handleCapabilities, |
| 39 | + HideHelpCommand: true, |
| 40 | +} |
| 41 | + |
| 42 | +func handleCapabilities(ctx context.Context, cmd *cli.Command) error { |
| 43 | + client := hypeman.NewClient(getDefaultRequestOptions(cmd)...) |
| 44 | + |
| 45 | + var opts []option.RequestOption |
| 46 | + if cmd.Root().Bool("debug") { |
| 47 | + opts = append(opts, debugMiddlewareOption) |
| 48 | + } |
| 49 | + |
| 50 | + var res []byte |
| 51 | + opts = append(opts, option.WithResponseBodyInto(&res)) |
| 52 | + _, err := client.Capabilities.Get(ctx, opts...) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + format := cmd.Root().String("format") |
| 58 | + transform := cmd.Root().String("transform") |
| 59 | + |
| 60 | + if format == "auto" || format == "" { |
| 61 | + return showCapabilities(os.Stdout, res) |
| 62 | + } |
| 63 | + |
| 64 | + obj := gjson.ParseBytes(res) |
| 65 | + return ShowJSON(os.Stdout, "capabilities", obj, format, transform) |
| 66 | +} |
| 67 | + |
| 68 | +func showCapabilities(w io.Writer, data []byte) error { |
| 69 | + obj := gjson.ParseBytes(data) |
| 70 | + |
| 71 | + server := obj.Get("server") |
| 72 | + fmt.Fprintln(w, "SERVER") |
| 73 | + fmt.Fprintf(w, " Version: %s\n", orDash(server.Get("version").String())) |
| 74 | + fmt.Fprintf(w, " API version: %s\n", orDash(server.Get("api_version").String())) |
| 75 | + |
| 76 | + host := obj.Get("host") |
| 77 | + fmt.Fprintln(w) |
| 78 | + fmt.Fprintln(w, "HOST") |
| 79 | + fmt.Fprintf(w, " OS: %s\n", orDash(host.Get("os").String())) |
| 80 | + fmt.Fprintf(w, " Arch: %s\n", orDash(host.Get("arch").String())) |
| 81 | + |
| 82 | + defaultRuntime := obj.Get("default_runtime") |
| 83 | + fmt.Fprintln(w) |
| 84 | + fmt.Fprintln(w, "DEFAULT RUNTIME") |
| 85 | + fmt.Fprintf(w, " Name: %s\n", orDash(defaultRuntime.Get("name").String())) |
| 86 | + fmt.Fprintf(w, " Available: %s\n", yesNo(defaultRuntime.Get("available").Bool())) |
| 87 | + |
| 88 | + runtimes := obj.Get("runtimes") |
| 89 | + if runtimes.IsArray() && len(runtimes.Array()) > 0 { |
| 90 | + fmt.Fprintln(w) |
| 91 | + fmt.Fprintln(w, "RUNTIMES") |
| 92 | + table := NewTableWriter(w, "NAME", "AVAILABLE", "FEATURES") |
| 93 | + table.TruncOrder = []int{2} |
| 94 | + runtimes.ForEach(func(_, value gjson.Result) bool { |
| 95 | + table.AddRow( |
| 96 | + value.Get("name").String(), |
| 97 | + yesNo(value.Get("available").Bool()), |
| 98 | + orDash(joinStrings(value.Get("features"))), |
| 99 | + ) |
| 100 | + return true |
| 101 | + }) |
| 102 | + table.Render() |
| 103 | + } |
| 104 | + |
| 105 | + images := obj.Get("images") |
| 106 | + fmt.Fprintln(w) |
| 107 | + fmt.Fprintln(w, "IMAGES") |
| 108 | + fmt.Fprintf(w, " Default platform: %s\n", orDash(images.Get("default_platform").String())) |
| 109 | + fmt.Fprintf(w, " Platforms: %s\n", orDash(joinStrings(images.Get("platforms")))) |
| 110 | + |
| 111 | + network := obj.Get("network") |
| 112 | + fmt.Fprintln(w) |
| 113 | + fmt.Fprintln(w, "NETWORK") |
| 114 | + fmt.Fprintf(w, " Model: %s\n", orDash(network.Get("model").String())) |
| 115 | + fmt.Fprintf(w, " Gateway: %s\n", orDash(network.Get("gateway").String())) |
| 116 | + fmt.Fprintf(w, " Subnet: %s\n", orDash(network.Get("subnet").String())) |
| 117 | + fmt.Fprintf(w, " Guest to guest: %s\n", yesNo(network.Get("guest_to_guest").Bool())) |
| 118 | + |
| 119 | + fmt.Fprintln(w) |
| 120 | + fmt.Fprintln(w, "SERVER FEATURES") |
| 121 | + fmt.Fprintf(w, " %s\n", orDash(joinStrings(obj.Get("features")))) |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func joinStrings(arr gjson.Result) string { |
| 127 | + if !arr.IsArray() { |
| 128 | + return "" |
| 129 | + } |
| 130 | + values := make([]string, 0, len(arr.Array())) |
| 131 | + arr.ForEach(func(_, value gjson.Result) bool { |
| 132 | + values = append(values, value.String()) |
| 133 | + return true |
| 134 | + }) |
| 135 | + return strings.Join(values, ", ") |
| 136 | +} |
| 137 | + |
| 138 | +func orDash(s string) string { |
| 139 | + if s == "" { |
| 140 | + return "-" |
| 141 | + } |
| 142 | + return s |
| 143 | +} |
| 144 | + |
| 145 | +func yesNo(b bool) string { |
| 146 | + if b { |
| 147 | + return "yes" |
| 148 | + } |
| 149 | + return "no" |
| 150 | +} |
0 commit comments