Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,22 @@ release can deliver to devices)
- `list`, `show <serial>`, `delete <serial>` — `list` and `show` include each
CA's JITP product, tags and description

**`nh checklist [product]`** — report which NervesHub features a product is
using, the same report as the Utilization tab of the product's settings
- opens with the score (recommended features in use, such as `(5/13)`) and how
the product's devices authenticate: a method to move away from, such as a
secret shared by the whole product, in orange, the others in green and unused
ones dimmed
- then each recommended feature (`✓` in use, `!` set up but risky or not showing up yet, `○` not in
use, `-` not available on this server), and after those the optional ones,
which don't count towards the score
- recommended features not in use are followed by a link to their documentation;
`--verbose` describes and links every feature
- `--color auto|always|never` (auto colours a terminal, and honours `NO_COLOR`);
`-o json` prints the report as the API gives it
- the product defaults to `--product` / `nh config set product`; needs a
NervesHub server with the utilization report, and says so on an older one

**`nh status`** — check that the configured server is up (`GET /status/alive`,
no sign-in needed); `--timeout` sets how long to wait

Expand Down
284 changes: 284 additions & 0 deletions cmd/checklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/*
Copyright © 2026 NervesHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package cmd

import (
"fmt"
"io"
"os"
"strings"

"github.com/nerves-hub/nh/internal/api"
"github.com/nerves-hub/nh/internal/config"
"github.com/spf13/cobra"
)

// checklistCmd implements `nh checklist [product]`.
var checklistCmd = &cobra.Command{
Use: "checklist [product]",
Short: "Report which NervesHub features a product is using",
Long: `Report how much of NervesHub a product is using, as a checklist: the same
report as the Utilization tab of the product's settings.

It opens with the score, the recommended features in use out of those the
server offers, and how the product's devices authenticate: a secret shared by
the whole product is marked as one to move away from, and certificates
(LocalCert, NervesKey) or per-device secrets as where to be. Then each
recommended feature, and after those the optional ones, which are situational
and don't count towards the score.

✓ in use
! set up, but risky or not showing up yet (a warning)
○ not in use yet (never a warning)
- not available on this server (and so not scored)

Recommended features that aren't in use are followed by a link to their
documentation; --verbose describes every feature and links them all. The product defaults to
the one set with --product, NERVES_HUB_PRODUCT or ` + "`nh config set product`" + `.

The server judges each feature from what it can see of the product, so
firmware configuration that leaves no trace there is not on the list.`,
Example: ` nh checklist thermostat
nh checklist thermostat --verbose
nh checklist thermostat -o json`,
Args: atMostOneArg("too many arguments: provide a single product name"),
RunE: runChecklist,
}

func init() {
checklistCmd.Flags().BoolP("verbose", "v", false, "describe every feature and link its documentation")
checklistCmd.Flags().String("color", "auto", "colour the report: auto (when writing to a terminal), always or never")
rootCmd.AddCommand(checklistCmd)
}

func runChecklist(cmd *cobra.Command, args []string) error {
cfg := config.From(cmd.Context())
org, err := requireOrg(cfg)
if err != nil {
return err
}

var product string
if len(args) == 1 {
product = args[0]
} else if product, err = requireProduct(cfg); err != nil {
return err
}

w := cmd.OutOrStdout()
colorMode, _ := cmd.Flags().GetString("color")
color, err := useColor(colorMode, cmd)
if err != nil {
return err
}

client, err := newAuthedClient(cfg)
if err != nil {
return err
}

report, err := client.GetUtilization(cmd.Context(), org, product)
if err != nil {
return err
}

if cfg.Output == config.OutputJSON {
return printJSON(w, report)
}

verbose, _ := cmd.Flags().GetBool("verbose")
return printChecklist(w, org, product, report, checklistStyle{color: color, verbose: verbose})
}

// useColor resolves --color. "auto" colours only a terminal, and never when
// NO_COLOR is set (https://no-color.org).
func useColor(mode string, cmd *cobra.Command) (bool, error) {
switch mode {
case "always":
return true, nil
case "never":
return false, nil
case "auto":
if os.Getenv("NO_COLOR") != "" {
return false, nil
}
return stdoutIsTerminal(cmd), nil
default:
return false, fmt.Errorf("invalid --color %q: use auto, always or never", mode)
}
}

// ANSI colours for the report. Orange is from the 256-colour palette, which
// has no orange among its first sixteen.
const (
ansiReset = "\x1b[0m"
ansiBold = "\x1b[1m"
ansiDim = "\x1b[2m"
ansiGreen = "\x1b[32m"
ansiOrange = "\x1b[38;5;208m"
)

type checklistStyle struct {
color bool
verbose bool
}

func (s checklistStyle) paint(code, text string) string {
if !s.color {
return text
}
return code + text + ansiReset
}

// mark is the symbol for an item's status, coloured to match.
func (s checklistStyle) mark(status string) string {
switch status {
case api.UtilizationDone:
return s.paint(ansiGreen, "✓")
case api.UtilizationAttention:
return s.paint(ansiOrange, "!")
case api.UtilizationUnavailable:
return s.paint(ansiDim, "-")
default:
return s.paint(ansiDim, "○")
}
}

// printChecklist writes the report: the score, device authentication, the
// recommended items and then the optional ones.
func printChecklist(w io.Writer, org, product string, report *api.Utilization, style checklistStyle) error {
var b strings.Builder

fmt.Fprintf(&b, "%s %s\n",
style.paint(ansiBold, fmt.Sprintf("Utilization of %s/%s", org, product)),
style.paint(ansiBold, fmt.Sprintf("(%d/%d)", report.Score.Done, report.Score.Total)))

width := titleWidth(report.Items)

auth := itemsIn(report.Items, api.UtilizationGroupAuthentication)
fmt.Fprintf(&b, "\n%s\n", style.paint(ansiBold, "Device authentication"))
if len(report.DeviceAuth) > 0 {
fmt.Fprintf(&b, " %s\n", authMethods(report.DeviceAuth, style))
}
writeItems(&b, auth, width, style)

recommended := itemsIn(report.Items, api.UtilizationGroupRecommended)
fmt.Fprintf(&b, "\n%s %s\n",
style.paint(ansiBold, "Recommended"),
style.paint(ansiDim, fmt.Sprintf("(%d/%d)", doneCount(recommended), availableCount(recommended))))
writeItems(&b, recommended, width, style)
Comment on lines +185 to +189

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] The Recommended section's (done/total) uses len(recommended) as the total, so it counts unavailable items that the headline score leaves out. Correctness · likely

The code's own docs say an unavailable feature is not available on the server and is not scored. With the test fixture this prints Recommended (1/3) even though only 2 of those features can be adopted, so the section suggests the product is further behind than the headline score does. Divide by the number of recommended items whose status isn't api.UtilizationUnavailable (giving (1/2) here) and update the expected test output to match.


if optional := itemsIn(report.Items, api.UtilizationGroupOptional); len(optional) > 0 {
fmt.Fprintf(&b, "\n%s %s\n",
style.paint(ansiBold, "Optional bonuses"),
style.paint(ansiDim, fmt.Sprintf("(%d in use, not scored)", doneCount(optional))))
writeItems(&b, optional, width, style)
}

_, err := io.WriteString(w, b.String())
return err
}

// authMethods renders how devices authenticate on one line, such as
// "Shared Secret 2 | LocalCert 10 | NervesKey 0": a method in use takes its
// level's colour (orange for a warning, green otherwise), an unused one is dim.
func authMethods(methods []api.UtilizationAuthMethod, style checklistStyle) string {
parts := make([]string, 0, len(methods))
for _, m := range methods {
text := fmt.Sprintf("%s %d", m.Label, m.Devices)
switch {
case m.Devices == 0:
text = style.paint(ansiDim, text)
case m.Level == api.UtilizationLevelWarning:
text = style.paint(ansiOrange, text)
default:
text = style.paint(ansiGreen, text)
}
parts = append(parts, text)
}
return strings.Join(parts, style.paint(ansiDim, " | "))
}

// writeItems writes one line per item: its mark, title and what the server
// found. A scored feature that isn't in use, and that the server can offer,
// is followed by its documentation link; verbose adds the description and
// links every item, the optional ones included.
func writeItems(b *strings.Builder, items []api.UtilizationItem, width int, style checklistStyle) {
const indent = " "
for _, item := range items {
// Padded by hand: the tabwriter would count colour codes as width.
pad := strings.Repeat(" ", width-len([]rune(item.Title)))
fmt.Fprintf(b, " %s %s%s %s\n", style.mark(item.Status), item.Title, pad, item.Summary)

if style.verbose && item.Description != "" {
fmt.Fprintf(b, "%s%s\n", indent, style.paint(ansiDim, item.Description))
}
wanted := item.Group != api.UtilizationGroupOptional &&
(item.Status == api.UtilizationTodo || item.Status == api.UtilizationAttention)
if item.DocsURL != "" && (style.verbose || wanted) {
fmt.Fprintf(b, "%s%s\n", indent, style.paint(ansiDim, item.DocsURL))
}
}
}

func itemsIn(items []api.UtilizationItem, group string) []api.UtilizationItem {
var out []api.UtilizationItem
for _, item := range items {
if item.Group == group {
out = append(out, item)
}
}
return out
}

// availableCount is how many of the items the server can offer. The rest are
// not scored, so a section's total leaves them out as the score does.
func availableCount(items []api.UtilizationItem) int {
n := 0
for _, item := range items {
if item.Status != api.UtilizationUnavailable {
n++
}
}
return n
}

func doneCount(items []api.UtilizationItem) int {
n := 0
for _, item := range items {
if item.Status == api.UtilizationDone {
n++
}
}
return n
}

func titleWidth(items []api.UtilizationItem) int {
width := 0
for _, item := range items {
if n := len([]rune(item.Title)); n > width {
width = n
}
}
return width
}
Loading
Loading