diff --git a/README.md b/README.md index 50a030d..d2f17d6 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ A key lifts those limits and unlocks every option, `request` and `sitemap`. Ther export STRING_API_KEY="your-api-key" ``` -`string whoami` shows which key commands use, the account it was saved for and the key's first characters, and checks it with String in one request that spends no credit. It exits 3 when String refuses the key, and 6 when it couldn't ask. `string whoami --offline` only says where the key comes from, as `whoami` did before. `string logout` deletes the saved key. +`string whoami` shows which key commands use, the account it was saved for and the key's first characters, and checks it with String in one request that spends no credit. It exits 3 when String refuses the key, and 6 when it couldn't ask. `string whoami --offline` only says where the key comes from, as `whoami` did before. With `--json`, either one prints one JSON object; only the checked one has `accepted`. `string logout` deletes the saved key. An agent working with nobody present can register for a trial key instead, where the server offers it. `string trial` solves a few seconds' proof of work and saves a key with a small balance in the same file. The key doesn't expire; it works until its credit runs out. Beside it, in `trial.json`, it keeps a claim token, which a person can use for 7 days to adopt the agent onto their String account. Neither is printed. Where trial keys are switched off, `string trial` exits 3. diff --git a/cmd/string/login.go b/cmd/string/login.go index 7fb3067..71570fb 100644 --- a/cmd/string/login.go +++ b/cmd/string/login.go @@ -853,7 +853,17 @@ func whoamiCommand(list []string, rt runtime) error { helpURL: helpURL, } } + source := "saved" + if key == env { + source = "STRING_API_KEY" + } + shown := map[string]any{"source": source, "credentials": path, "email": saved.Email, "key_prefix": keyPrefix(key)} if parsed.switches["offline"] { + // Nothing asked String, so the object says nothing of accepted. + if rt.json { + encoded, _ := json.Marshal(shown) + return write(rt.stdout, append(encoded, '\n')) + } return write(rt.stdout, []byte(said+".\n")) } // One request, which spends no credit, says whether String takes the key. @@ -873,12 +883,9 @@ func whoamiCommand(list []string, rt runtime) error { return &failure{exit: exitUpstream, code: "unverified", retryable: &yes, message: said + " (" + keyPrefix(key) + "), but string couldn't check it with String.", notes: []string{strings.TrimPrefix(asFailure(err).Error(), "error: ")}, nextStep: "run it again, or `string whoami --offline` to see the key without checking it"} } if rt.json { - source := "saved" - if key == env { - source = "STRING_API_KEY" - } - shown, _ := json.Marshal(map[string]any{"source": source, "credentials": path, "email": saved.Email, "key_prefix": keyPrefix(key), "accepted": true}) - return write(rt.stdout, append(shown, '\n')) + shown["accepted"] = true + encoded, _ := json.Marshal(shown) + return write(rt.stdout, append(encoded, '\n')) } return write(rt.stdout, []byte(said+" ("+keyPrefix(key)+"). String accepts it.\n")) } diff --git a/cmd/string/whoami_test.go b/cmd/string/whoami_test.go index 5e7c428..6a60050 100644 --- a/cmd/string/whoami_test.go +++ b/cmd/string/whoami_test.go @@ -72,6 +72,12 @@ func TestWhoamiChecksTheKeyWithString(t *testing.T) { if got := whoami(offline, "", true, "--offline"); got.status != 0 || !strings.HasPrefix(got.output, "Logged in as dev@localhost, with the key saved in ") || !strings.HasSuffix(got.output, "credentials.json.\n") { t.Errorf("--offline: %+v", got) } + // With --json, --offline gives the object too, without accepted, since + // nothing asked String. + got = whoami(offline, envKey, false, "--offline", "--json") + if shown, _ := decode(t, got.output).(map[string]any); got.status != 0 || shown["source"] != "STRING_API_KEY" || shown["key_prefix"] != "env-key-…" || shown["accepted"] != nil { + t.Errorf("--offline --json: %+v", got) + } if got := whoami(offline, "", false); got.status != exitAuth || !strings.HasPrefix(got.errors, "error: Not logged in.") { t.Errorf("no key: %+v", got) }