Repository navigation
Conversation
nh device export writes a product's devices as the CSV NervesHub's new GET .../devices/export sends: identifier, description, tags, product and org, plus certificates with --include-certificates. --filter, --query and --sort work as for nh device list, and --file writes the file whole or not at all. nh device import now takes that CSV as well as a Trust&Go manifest, reading the format from the file or --format. It checks every row the way the server reads it before sending, and checks first that the server can import a CSV, since NervesHub 2.5.4 and earlier accept one and then drop it.
Rummager review of
|
u7d-bot
left a comment
There was a problem hiding this comment.
Inline findings for 8a42db5c70. The summary is in the rummager comment.
| nh checks every entry first, the way the server reads it, and sends nothing | ||
| unless every entry is good, since older NervesHub servers accept a file | ||
| they can't read and then drop it without saying so. --dry-run does the check | ||
| and stops; it needs no login, org or product. |
There was a problem hiding this comment.
[WARNING] The rewritten help no longer says why nh checks every entry first: one malformed entry can make NervesHub drop the whole file without saying so. Prose, comments and docs · likely
The new reason is that "older NervesHub servers accept a file they can't read". That describes the old-server CSV case, and nh handles that case with the SupportsDeviceExport probe, not with the row checks. The per-entry check exists because the server abandons the whole import over one bad entry and never reports it. That fact was in the old help text and is now gone.
| nh checks every entry first, the way the server reads it, and sends nothing | |
| unless every entry is good, since older NervesHub servers accept a file | |
| they can't read and then drop it without saying so. --dry-run does the check | |
| and stops; it needs no login, org or product. | |
| nh checks every entry first, the way the server reads it, and sends nothing | |
| unless every entry is good: a malformed entry can make NervesHub abandon the | |
| whole file without saying so. --dry-run does the check and stops; it needs | |
| no login, org or product. |
| if err != nil { | ||
| return fmt.Errorf("creating temporary file: %w", err) | ||
| } | ||
| tmpName := tmp.Name() |
There was a problem hiding this comment.
[SUGGESTION] The export's temporary file is not registered with interrupt.RemoveOnExit, so a second Ctrl-C or the grace timeout leaves a hidden .nh-export-* file next to the destination. Correctness · likely
When interrupt.Run gives up on the work, it calls os.Exit without running the cleanup on lines 164-179. Only files registered with interrupt.RemoveOnExit are removed on that path (signedTempPath registers its file this way). That breaks the PR's promise that a failed export leaves nothing behind. Add defer interrupt.RemoveOnExit(tmpName)() after this line and import github.com/nerves-hub/nh/internal/interrupt.
| if strings.EqualFold(filepath.Ext(path), ".csv") { | ||
| return api.DeviceImportFormatCSV | ||
| } | ||
| trimmed := bytes.TrimSpace(bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))) |
There was a problem hiding this comment.
[SUGGESTION] detectImportFormat writes the UTF-8 BOM bytes out by hand, while internal/api already defines them as utf8BOM. Code quality · definite
The same constant now lives in two packages. Export it from api (for example as api.UTF8BOM) and use that here, so the format detection and the CSV parser cannot drift apart.
| entries := "entries" | ||
| if isCSV { | ||
| entries = "rows" | ||
| } |
There was a problem hiding this comment.
[SUGGESTION] renderDeviceImport picks the rows/entries noun with its own branch instead of calling the new importEntries helper. Code quality · definite
Two helpers now choose the word for an import's entries and already disagree ("manifest entries" in importEntries, "entries" here). Keep one source, for example entries := importEntries(r.Format), and drop this branch.
| entries := "entries" | |
| if isCSV { | |
| entries = "rows" | |
| } | |
| entries := importEntries(r.Format) |
NervesHub is adding a devices CSV export to its API, and a CSV import that reads the file back, in nerves-hub/nerves_hub_web#3427. This PR adds both to nh, so a product's devices can be copied to another product or instance with their descriptions, tags and certificates:
nh device exportGET .../devices/exportto standard output, or to a file with-f/--file. A file is written beside the destination and renamed once complete, so a failed export leaves nothing behind. With a file, nh then says how many devices it exported, as JSON with-o json.--filter,--queryand--sortare the same flags, with the same checks, asnh device list. They're factored out ofdevice listwithout changing it.--include-certificatesadds the certificates column.Accept: text/csv, application/json;q=0.9. A server without the export only answers JSON, so offering JSON lets it answer 404 instead of 406. nh reports "needs a release newer than v2.5.4" when that 404 comes for a product that exists, or when a 200 isn't CSV.nh device import.csvfile, or content that doesn't start like JSON, is CSV.--format csv|microchip_trust_and_gooverrides it.text/csvwithFormat: csv.--tagadds tags on top of each row's own.--dry-runonly checks.Identifiers are unique across a whole NervesHub instance, deleted devices included. An import therefore only creates devices the instance doesn't already have. The help text and README say so and point to
nh device movefor moving devices between products.Verified
go vet,go buildandgo test -race ./...pass, with new tests for both commands and the CSV reader.nh device export --include-certificates -fand a dry run.nh device importinto another product.