Add key subcommand to all 31 implementations

- Validate API keys via portal endpoint
- Show key status, tier, expiration with color-coded output
- Add --extend flag to open browser for key renewal
- Update .gitignore for compiled binaries
- Update README with key command documentation
This commit is contained in:
Russell Ballestrini 2025-12-27 11:50:34 -05:00
parent eb5d4ca8bf
commit 5380af54cb
32 changed files with 3140 additions and 70 deletions

110
un.v
View file

@ -46,12 +46,13 @@
import os
const (
api_base = 'https://api.unsandbox.com'
blue = '\x1b[34m'
red = '\x1b[31m'
green = '\x1b[32m'
yellow = '\x1b[33m'
reset = '\x1b[0m'
api_base = 'https://api.unsandbox.com'
portal_base = 'https://unsandbox.com'
blue = '\x1b[34m'
red = '\x1b[31m'
green = '\x1b[32m'
yellow = '\x1b[33m'
reset = '\x1b[0m'
)
fn detect_language(filename string) !string {
@ -99,6 +100,82 @@ fn exec_curl(cmd string) string {
return result.output
}
fn extract_json_string(json string, key string) string {
search := '"${key}":"'
start_idx := json.index(search) or { return '' }
start := start_idx + search.len
mut end := start
for end < json.len {
if json[end] == `"` && (end == 0 || json[end - 1] != `\\`) {
break
}
end++
}
if end > start {
raw := json[start..end]
// Unescape JSON string
return raw.replace('\\n', '\n')
.replace('\\r', '\r')
.replace('\\t', '\t')
.replace('\\"', '"')
.replace('\\\\', '\\')
}
return ''
}
fn cmd_key(extend bool, api_key string) {
cmd := "curl -s -X POST '${api_base}/keys/validate' -H 'Content-Type: application/json' -H 'Authorization: Bearer ${api_key}' -d '{}'"
result := exec_curl(cmd)
status := extract_json_string(result, 'status')
public_key := extract_json_string(result, 'public_key')
tier := extract_json_string(result, 'tier')
expired_at := extract_json_string(result, 'expired_at')
if extend && public_key != '' {
url := '${portal_base}/keys/extend?pk=${public_key}'
println('${yellow}Opening browser: ${url}${reset}')
// Try xdg-open (Linux), open (macOS), or start (Windows)
os.execute('xdg-open "${url}"') or {
os.execute('open "${url}"') or {
os.execute('cmd /c start "${url}"') or {
eprintln('${red}Error: Could not open browser${reset}')
}
}
}
return
}
match status {
'valid' {
println('${green}Valid${reset}')
println('Public Key: ${public_key}')
println('Tier: ${tier}')
if expired_at != '' {
println('Expires: ${expired_at}')
}
}
'expired' {
println('${red}Expired${reset}')
println('Public Key: ${public_key}')
println('Tier: ${tier}')
if expired_at != '' {
println('Expired: ${expired_at}')
}
println('${yellow}To renew: Visit ${portal_base}/keys/extend${reset}')
}
'invalid' {
println('${red}Invalid${reset}')
}
else {
println('${yellow}Unknown status: ${status}${reset}')
}
}
}
fn cmd_execute(source_file string, envs []string, artifacts bool, network string, vcpu int, api_key string) {
lang := detect_language(source_file) or {
eprintln('${red}Error: ${err}${reset}')
@ -263,6 +340,7 @@ fn main() {
eprintln('Usage: ${os.args[0]} [options] <source_file>')
eprintln(' ${os.args[0]} session [options]')
eprintln(' ${os.args[0]} service [options]')
eprintln(' ${os.args[0]} key [--extend]')
exit(1)
}
@ -391,6 +469,26 @@ fn main() {
return
}
if os.args[1] == 'key' {
mut extend := false
mut i := 2
for i < os.args.len {
match os.args[i] {
'--extend' { extend = true }
'-k' {
i++
api_key = os.args[i]
}
else {}
}
i++
}
cmd_key(extend, api_key)
return
}
// Execute mode
mut envs := []string{}
mut artifacts := false