Skip to content

feat: set FTP and power zones - #329

Merged
bpauli merged 3 commits into
mainfrom
feat/328-set-power-zones
Nov 4, 2026
Merged

bpauli merged 3 commits into
mainfrom
feat/328-set-power-zones

Conversation

@bpauli

@bpauli bpauli commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Closes #328.

Summary

You can now set the FTP and the power zones of a sport from the CLI:

gccli profile power-zones set cycling --ftp 280
gccli profile power-zones set running --ftp 180 --floors 116,144,163,180,207

The command sends one PUT /biometric-service/powerZones/all with a single sport object. Garmin changes only that sport. It also adds a MANUAL FTP record, so gccli health cycling-ftp shows the new value.

Decisions on the open questions in #328

  • Host and auth. The PUT works on connectapi.garmin.com with the gccli OAuth2 token. An empty array returns 400 powerZonesDTO cannot be null. A valid body returns 204.
  • Default floors. Cycling, rowing and cross-country skiing use 0, 55, 75, 90, 105, 120 and 150 % of FTP, rounded half up. This rule gives the floors that Garmin stores for these three sports on the owner account (FTP 279, 160 and 207). TestDefaultPowerZoneFloors pins those values. --floors sets exact values.
  • Running. The running floors match no simple percent rule. Running therefore needs --floors with 5 values, and the command sends 0 for zone6Floor and zone7Floor, as Garmin stores them. Without --floors, running exits 2 before it sends a request.
  • No read before the write. The body holds every field that Garmin stores for a sport, so the command does not read the current zones first.

Before it sends a request, the command checks that FTP is greater than 0, that the number of floors is correct for the sport, that no floor is negative, and that each floor is higher than the one before. The default floors get the same order check, so an FTP below 7 W without --floors is refused. An unknown sport or a missing --ftp gets Kong's own usage error. Each failed check exits 2.

Verification

  • make fmt-check, make lint (0 issues), go vet -tags e2e ./... and make test pass.
  • 14 goldens cover the defaults, explicit floors, running, the hyphenated sport name, each validation error, a bad or missing sport, a missing --ftp, a too low FTP and a 400 from the PUT. The second independent verifier injected 21 defects, and each made a test fail. Examples are a wrong percent, truncation instead of rounding, a missing zero fill for running, and the sport sent in lowercase.
  • Live, on the owner account with an isolated login, using values that were already stored: set cycling --ftp 279 printed Set cycling FTP to 279 W, zone floors 0, 153, 209, 251, 293, 335, 419 W, and set running --ftp 179 --floors 116,143,162,179,206 --json printed {"ok": true}. profile power-zones list was the same before and after. Each write added a MANUAL FTP record with the same value.

Add `gccli profile power-zones set <sport> --ftp WATTS [--floors F1,...]`.
It sends one PUT to /biometric-service/powerZones/all with the sport, the
FTP and the zone floors. Garmin changes only that sport and also records
the FTP.

Without --floors, the floors of cycling, rowing and cross-country skiing
are 0, 55, 75, 90, 105, 120 and 150 percent of FTP, rounded half up. This
rule gives the floors that Garmin stored for the FTP values in the tests.
Running has 5 zones and no default rule, so it needs --floors with 5
values. zone6Floor and zone7Floor are sent as 0 for running.

The command checks the FTP and the floors before it sends a request: FTP
greater than 0, the zone count of the sport, no negative floor, and each
floor higher than the one before.
@bpauli

bpauli commented Nov 3, 2026

Copy link
Copy Markdown
Owner Author

Independent verification: PASS+NOTES

  • Head: afd7cd51204a5cb23f66fb84a17827a989a999db
  • Base (merge-base): 02dbdc5
  • Also tested: head merged into origin/main 8a832a9 (local merge only, not pushed)

I did not write this change. I made no live Garmin calls. All binary runs used a local HTTPS CONNECT mock (HTTPS_PROXY, SSL_CERT_FILE to its CA), with HOME and XDG_CONFIG_HOME in a temp dir, GCCLI_KEYRING_BACKEND=file, a fake token from auth import and BROWSER=/bin/false.

Gate on head

  • GOTOOLCHAIN=go1.26.0 make tools: ok
  • make fmt-check: ok
  • GOTOOLCHAIN=go1.26.0 make lint: 0 issues
  • go vet -tags e2e ./...: ok
  • make test: ok, all packages pass

Head merged into origin/main 8a832a9

  • Clean merge. go build ok. make test: ok, all 20 package runs pass.
  • The merge binary gives the same output as the head binary for set cycling --ftp 279 and --json set running ... --floors .... It gives the same output as base for power-zones list and power-zones sport cycling (stdout, stderr, exit code and requests).

Base vs head binary on the mock

These match byte for byte (stdout, stderr, exit code and the request log): profile power-zones, profile power-zones list in table, --json and --plain mode, profile power-zones sport cycling in table and --json mode, sport swimming (exit 2), and sport rowing with a 404 (exit 4).

All differences are ones the PR names:

  • Group help is now "Show and set power zones.".
  • profile power-zones set is a new row in root, profile and power-zones help.
  • The bash, zsh, fish and powershell completions have the new set command and --ftp and --floors. In bash, the global flag list gets --floors and --ftp and the command list gets "profile power-zones set".
  • set cycling --ftp 279 gives "unexpected argument set" (exit 2) on base. On head it sends the PUT.

New command on the mock

The PUT bodies that the mock received:

  • set cycling --ftp 279: exit 0. Body [{"functionalThresholdPower":279,"sport":"CYCLING","userLocalTime":null,"zone1Floor":0,"zone2Floor":153,"zone3Floor":209,"zone4Floor":251,"zone5Floor":293,"zone6Floor":335,"zone7Floor":419}]. The message on stderr is "Set cycling FTP to 279 W, zone floors 0, 153, 209, 251, 293, 335, 419 W". --plain gives the same.
  • --ftp 290 gives 160/218/261/305/348/435. This is half-up rounding (159.5 to 160, 304.5 to 305).
  • rowing --ftp 160 gives 0/88/120/144/168/192/240. cross-country-skiing --ftp 207 sends CROSS_COUNTRY_SKIING with 0/114/155/186/217/248/311.
  • cycling --ftp 300 --floors 0,160,220,270,310,360,450 sends the floors as given.
  • running --ftp 179 --floors 116,143,162,179,206 sends zone1..5 as given and zone6Floor and zone7Floor as 0.
  • --json on a 204 prints {"ok": true} on stdout, exit 0.
  • When the PUT gets a 400, the exit code is 1. stderr is set power zones: garmin api: 400 Bad Request: .... With --json it is the JSON error object with "status":400.
  • These exit 2 with no request: running without --floors, running with 7 floors, --ftp 0, --ftp=-5, 6 floors for cycling, --floors=-1,..., equal floors (209,209) and floors that go down.

Injected defects

I put in 16 defects one at a time. The tests caught all 16, and I reverted each one. I ran go test ./internal/cmd ./internal/garminapi with HOME and XDG_CONFIG_HOME in a temp dir.

  • wrong percent (55 to 56): TestDefaultPowerZoneFloors and TestGolden
  • truncation instead of rounding: TestDefaultPowerZoneFloors and TestGolden
  • no zero fill for running: TestGolden @running
  • lowercase sport on the wire: TestGolden
  • --ftp 0 accepted: TestGolden @bad-ftp and TestBadValuesAreCheckedBeforeAccount
  • no floor count check: @floors-count
  • no negative check: @floors-negative
  • equal floors accepted: @floors-order
  • a default rule for running: @running-no-floors
  • verb "update power zones": @error
  • userLocalTime dropped: TestGolden
  • zone6Floor from the wrong index: TestGolden
  • rowing missing from the rules: TestPowerZoneRulesCoverSportEnum, TestDefaultPowerZoneFloors and @floors
  • PUT changed to POST: TestGolden
  • FTP off by one: TestGolden
  • different floor separator in the message: TestGolden

PR Verification claims rerun

  • Gate passes: confirmed (see above).
  • Ten goldens: confirmed, the main file plus 9 cases.
  • Injected defects make a golden fail: confirmed with my own set of 16 (see above).
  • The default floors for FTP 279, 160 and 207 match the values in the PR: confirmed on the binary.
  • I did not rerun the live claims, because no live calls were approved for this verification.

Notes (none blocks the merge)

  1. Wrong error text for a bad sport or a missing flag. Kong runs Validate() before it checks enums and required values. So Validate runs with a sport that has no rule and with FTP 0. All of these exit 2 and send no request, but the message is misleading:

    • set swimming --ftp 279 gives swimming has no default zone floors: give --floors with 0 values.
    • set CYCLING --ftp 279 gives CYCLING has no default zone floors: give --floors with 0 values.
    • set --ftp 279 gives has no default zone floors: give --floors with 0 values.
    • set cycling without --ftp gives --ftp must be greater than 0, not a missing-flag error.

    For comparison, power-zones sport swimming and set swimming --ftp 279 --floors "" give Kong's enum error. A possible fix: return nil from Validate when powerZoneRules[c.Sport].zones == 0, so that Kong reports the enum error or the missing argument. No golden pins these inputs.

  2. Default floors are not checked for order. set cycling --ftp 5 sends floors 0,3,4,5,5,6,8 (exit 0). The same values given with --floors exit 2. This happens only for an FTP of a few watts, so it is unlikely in real use.

CI on this head: all 6 checks pass (test, conformance, darwin-cgo-build, two runs each).

Kong runs a command's Validate before it checks the enum of a required
argument and the required flags. So Validate saw a sport with no rule
or an FTP of 0 and gave a misleading message.

Validate now returns nil for a sport with no rule and for a missing
--ftp, which is a pointer now. Kong then gives its enum, missing-flag
or missing-argument error.
An FTP of 1 to 6 W gives default floors with equal values, such as
0, 3, 4, 5, 5, 6, 8 for 5 W. The command sent them, but the same
values given with --floors are a usage error.

Validate now runs the order check on the floors that Run sends, and
for the default floors it asks for --floors.
@bpauli

bpauli commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Independent verification: PASS+NOTES

  • Head: 52c633ac4c26e2b8753c26f3b2483916b1b84873
  • Base (merge-base): 02dbdc5
  • Also tested: head merged into origin/main 8a832a9 (local merge only, not pushed). origin/main was still 8a832a9 after the runs.

I did not write this change. I made no live Garmin calls. All binary runs used a local HTTPS CONNECT mock (HTTPS_PROXY, SSL_CERT_FILE to its CA) that logs each request and its body. HOME and XDG_CONFIG_HOME pointed to a temp dir, with GCCLI_KEYRING_BACKEND=file, a fake token from auth import and BROWSER=/bin/false. All test runs also used a temp HOME and XDG_CONFIG_HOME.

Gate on head

  • GOTOOLCHAIN=go1.26.0 make tools: ok
  • make fmt-check: ok
  • GOTOOLCHAIN=go1.26.0 make lint: 0 issues
  • go vet -tags e2e ./...: ok
  • make test: ok, all packages pass

Head merged into origin/main 8a832a9

  • Clean merge. go build ok. make test: ok, all packages pass.
  • The merge binary and the head binary give the same stdout, stderr, exit code and requests for 8 cases: set with defaults, running with --json, a bad sport, a missing --ftp, --ftp 5, power-zones list, power-zones sport cycling and power-zones --help.

Base vs head binary on the mock

These match byte for byte (stdout, stderr, exit code and the request log): profile power-zones, power-zones list in table, --json and --plain mode, power-zones sport cycling in table and --json mode, sport running --plain, sport cross-country-skiing, sport swimming (exit 2), sport CYCLING (exit 2), sport without an argument (exit 2), list --help, sport --help, profile and profile hr-zones.

All differences are ones the PR names:

  • The group help is now "Show and set power zones.".
  • profile power-zones set is a new row in the root, profile and power-zones help.
  • The bash, zsh, fish and powershell completions have the new set command and --ftp and --floors. In bash, the two global flag lists get only --floors and --ftp, and the command list gets only "profile power-zones set".
  • set cycling --ftp 279 gives "unexpected argument set" (exit 2) on base. On head it sends the PUT.

New command on the mock: bad input

Each of these exits 2 and sends no request:

  • set swimming --ftp 279, set CYCLING --ftp 279, set Cycling --ftp 279 and --json set swimming --ftp 279: Kong's enum error <sport> must be one of "cycling","running","rowing","cross-country-skiing" but got .... This fixes note 1 of the previous verdict.
  • set --ftp 279: expected "<sport>".
  • set cycling, set and set swimming: missing flags: --ftp=WATTS.
  • --ftp 0, --ftp=-5, running --ftp 0 and --json ... --ftp 0: --ftp must be greater than 0. --ftp -5 and --ftp abc get Kong's parse errors.
  • --ftp 1, --ftp 5 and --ftp 6: --ftp N is too low for increasing default zone floors: give --floors. This fixes note 2 of the previous verdict.
  • The smallest FTP that passes is 7. It sends floors 0, 4, 5, 6, 7, 8, 11 (exit 0). --ftp 8 sends 0, 4, 6, 7, 8, 10, 12.
  • 6 or 8 floors for cycling, 6 for rowing, 4 or 7 for running, and --floors "": --floors needs N values for <sport>, got M.
  • --floors=-1,...: --floors must not be negative. --floors 0,a,...: Kong's parse error.
  • Equal floors (160,160) and floors that go down (160,150): --floors must increase from zone to zone.
  • running --ftp 179 without floors: running has no default zone floors: give --floors with 5 values.

New command on the mock: good paths

The PUT bodies that the mock received:

  • set cycling --ftp 279: exit 0, one PUT /biometric-service/powerZones/all with [{"functionalThresholdPower":279,"sport":"CYCLING","userLocalTime":null,"zone1Floor":0,"zone2Floor":153,"zone3Floor":209,"zone4Floor":251,"zone5Floor":293,"zone6Floor":335,"zone7Floor":419}]. stderr is "Set cycling FTP to 279 W, zone floors 0, 153, 209, 251, 293, 335, 419 W". --plain gives the same. --json prints {"ok": true} on stdout.
  • --ftp 290 gives 0/160/218/261/305/348/435 (half up: 159.5 to 160, 304.5 to 305).
  • rowing --ftp 160 gives 0/88/120/144/168/192/240. cross-country-skiing --ftp 207 sends CROSS_COUNTRY_SKIING with 0/114/155/186/217/248/311.
  • cycling --ftp 300 --floors 0,160,220,270,310,360,450 sends the floors as given, also with the flags in the other order.
  • running --ftp 179 --floors 116,143,162,179,206 sends zone1 to zone5 as given and zone6Floor and zone7Floor as 0.
  • A 400 from the PUT: exit 1, set power zones: garmin api: 400 Bad Request: ... in table and --plain mode, and the JSON error object with "status":400 in --json mode. A 500 from the PUT: exit 1 after one request, so the PUT is not retried.

Injected defects

I put in 21 defects one at a time in a separate worktree and ran go test ./internal/cmd ./internal/garminapi. A test failed for each, and I reverted each one (the worktree was clean at the end).

New goldens from 2063e52 and 52c633a:

  • Validate refuses an unknown sport again (!ok check gone): @bad-sport and @no-sport
  • a missing --ftp is treated as 0 again: @no-ftp
  • Validate returns its own error for an empty sport: @no-sport
  • no order check for default floors: @bad-low-ftp and TestBadValuesAreCheckedBeforeAccount
  • Validate checks only c.Floors, not the computed floors: @bad-low-ftp and TestBadValuesAreCheckedBeforeAccount
  • default floors get the --floors message: @bad-low-ftp
  • equal floors accepted (< for <=): @floors-order and @bad-low-ftp

Earlier tests:

  • 55 to 56 percent: TestDefaultPowerZoneFloors, TestGolden and TestWriteCommandOutput
  • truncation instead of rounding: TestDefaultPowerZoneFloors and TestGolden
  • sport sent in lowercase: TestGolden
  • zone6Floor from the wrong index: TestGolden (@running too)
  • userLocalTime dropped: TestGolden
  • verb "update power zones": @error
  • PUT changed to POST: TestProfileEndpoints, TestGolden and TestWriteCommandOutput
  • rowing missing from the rules: TestDefaultPowerZoneFloors and TestPowerZoneRulesCoverSportEnum
  • --ftp 0 accepted: @bad-ftp
  • no negative check: @floors-negative
  • no floor count check: @floors-count
  • a default rule for running: @running-no-floors
  • FTP off by one in the body: TestGolden
  • other floor separator in the message: TestGolden and TestWriteCommandOutput

PR Verification claims rerun

  • Gate passes: confirmed (see above).
  • Goldens: the head has 14, not ten: the main file plus 13 cases. See note 2.
  • Injected defects make a golden fail: confirmed with my own set of 21.
  • The default floors for FTP 279, 160 and 207 match the values in the PR: confirmed on the binary.
  • I did not rerun the live claims, because no live calls were approved for this verification.

Notes (none blocks the merge)

  1. A very large --ftp overflows the default floor math. set cycling --ftp 100000000000000000 exits 2 with --floors must not be negative, but no --floors was given. --ftp 9223372036854775807 exits 2 with --ftp 9223372036854775807 is too low for increasing default zone floors. Neither sends a request. There is no upper limit on FTP, so a mistyped value such as --ftp 2790 is sent. Real input will not reach the overflow. A possible fix is an upper limit on --ftp.
  2. The PR description is out of date. The Verification section says "Ten goldens" and "Eleven injected defects". After 2063e52 and 52c633a the head has 14 goldens.

CI on this head: all 6 checks pass (test, conformance and darwin-cgo-build, two runs each).

@bpauli
bpauli merged commit e51f9e2 into main Nov 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: set FTP and power zones

1 participant