Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/readme-source.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version = 1
sha256 = "671c20fbff6a084e6b665dd5d5a7755fd7768d282c2c406e45e1c7ce4e798ae9"
sha256 = "3fce97e7eef296ae3e7cfa494fbf1ab6cb5fe132b4ccc51f4a4f182f308964fe"

[history]
forbidden_commits = ["3cbdabb71650fbca119b17e89ceed577f6bf81cd"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Generated from private documentation source. Do not edit directly. Source SHA256: 6e721770cc84307b99637887871aa189f05040f2d2ef027401669233aa94b63f -->
<!-- Generated from private documentation source. Do not edit directly. Source SHA256: 1d83b6adbe2331ad365f614932124446c2e38210640df6e6f93f5c7b715cd572 -->

# pxlb

Expand Down Expand Up @@ -71,7 +71,7 @@ export PIXELLAB_API_KEY="..."
pxlb balance
```

You can also put `PIXELLAB_API_KEY=...` in a `.env` file in the directory where you run `pxlb`, or pass `--token <key>` for a single command.
Supply `PIXELLAB_API_KEY` through your process environment, including an approved Preview credential injected by `ghenv dev -- pxlb balance`. Keep public development defaults in Mise. Pass `--token <key>` to override the process value for a single command.

## Command Model

Expand Down Expand Up @@ -125,7 +125,7 @@ pxlb --version

| Flag | Purpose |
|---|---|
| `--token <key>` | PixelLab API key. Overrides `PIXELLAB_API_KEY` and `.env`. |
| `--token <key>` | PixelLab API key. Overrides `PIXELLAB_API_KEY`. |
| `--base-url <url>` | API base URL. Defaults to `https://api.pixellab.ai/v2`. |
| `--http-method <method>` | Override inferred method for colliding routes. Usually only needed for delete. |
| `--json` | Print raw JSON response. |
Expand Down
27 changes: 3 additions & 24 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Result;
use serde::Deserialize;
use std::{collections::BTreeMap, fs, time::Duration};
use std::{collections::BTreeMap, time::Duration};

#[derive(Clone, Deserialize)]
pub struct Endpoint {
Expand Down Expand Up @@ -74,28 +74,7 @@ fn duration(value: &str) -> Result<Duration> {
})
}
pub fn token() -> String {
if let Ok(token) = std::env::var("PIXELLAB_API_KEY")
&& !token.is_empty()
{
return token;
}
let Ok(text) = fs::read_to_string(".env") else {
return String::new();
};
let mut token = String::new();
for line in text.lines() {
let line = line.trim();
if line.starts_with('#') {
continue;
}
let line = line.strip_prefix("export ").unwrap_or(line);
if let Some((key, value)) = line.split_once('=')
&& key.trim() == "PIXELLAB_API_KEY"
{
token = value.trim().trim_matches(['\"', '\'']).into();
}
}
token
std::env::var("PIXELLAB_API_KEY").unwrap_or_default()
}
impl Config {
pub fn parse(args: &[String]) -> Result<Self> {
Expand Down Expand Up @@ -247,7 +226,7 @@ pub fn display(path: &str) -> String {
.join("/")
}
pub fn help() -> String {
let mut text="pxlb - PixelLab v2 API CLI\n\nUsage:\n pxlb <api-route> [flags]\n pxlb --version\n\nGlobal flags:\n --token <key> PixelLab API key; overrides PIXELLAB_API_KEY and .env.\n --base-url <url> API base URL; defaults to https://api.pixellab.ai/v2.\n --http-method <method> Override inferred HTTP method for colliding routes.\n --body-json <json|file> Send a raw JSON request body.\n --wait Poll returned background jobs until completion.\n --poll-interval <time> Job polling interval, default 5s.\n --timeout <time> Optional job polling timeout.\n --out <path> Save returned binary or base64 image outputs.\n --quiet Suppress normal JSON output.\n\nRoutes:\n".to_owned();
let mut text="pxlb - PixelLab v2 API CLI\n\nUsage:\n pxlb <api-route> [flags]\n pxlb --version\n\nGlobal flags:\n --token <key> PixelLab API key; overrides PIXELLAB_API_KEY.\n --base-url <url> API base URL; defaults to https://api.pixellab.ai/v2.\n --http-method <method> Override inferred HTTP method for colliding routes.\n --body-json <json|file> Send a raw JSON request body.\n --wait Poll returned background jobs until completion.\n --poll-interval <time> Job polling interval, default 5s.\n --timeout <time> Optional job polling timeout.\n --out <path> Save returned binary or base64 image outputs.\n --quiet Suppress normal JSON output.\n\nRoutes:\n".to_owned();
let mut entries: Vec<_> = endpoints()
.into_iter()
.map(|spec| {
Expand Down
8 changes: 6 additions & 2 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ def test_timeout_after_response_headers_is_identified(self):
self.assertNotEqual(result.returncode,0)
self.assertIn('timeout',result.stderr)
self.assertEqual(len(calls),2)
def test_token_precedence_flag_environment_then_dotenv(self):
def test_token_precedence_flag_environment_and_no_dotenv_fallback(self):
(self.root/'.env').write_text('export PIXELLAB_API_KEY="file-token"\n')
with api() as (url,calls):
for flags,env,expected in [([],dict(PIXELLAB_API_KEY=''),'file-token'),([],dict(PIXELLAB_API_KEY='env-token'),'env-token'),(['--token','flag-token'],dict(PIXELLAB_API_KEY='env-token'),'flag-token')]:
result=self.request(url,'balance',PIXELLAB_API_KEY='')
self.assertEqual(result.returncode,0,result.stderr)
self.assertIsNone(calls[-1]['auth'])
self.assertNotIn('file-token',result.stdout+result.stderr)
for flags,env,expected in [([],dict(PIXELLAB_API_KEY='env-token'),'env-token'),(['--token','flag-token'],dict(PIXELLAB_API_KEY='env-token'),'flag-token')]:
result=self.request(url,'balance',*flags,**env)
self.assertEqual(result.returncode,0,result.stderr)
self.assertEqual(calls[-1]['auth'],'Bearer '+expected)
Expand Down
Loading