-
Notifications
You must be signed in to change notification settings - Fork 834
feat: expose minimal library crate for programmatic API access #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Configuration directory resolution. | ||
| //! | ||
| //! Provides [`config_dir()`] to locate the gws config directory, respecting | ||
| //! the `GOOGLE_WORKSPACE_CLI_CONFIG_DIR` environment variable and falling back | ||
| //! to `~/.config/gws` (or the OS-specific legacy path for existing installs). | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
| /// Returns the gws configuration directory. | ||
| /// | ||
| /// Resolution order: | ||
| /// 1. `GOOGLE_WORKSPACE_CLI_CONFIG_DIR` environment variable (if set) | ||
| /// 2. `~/.config/gws` (if it exists — primary location) | ||
| /// 3. OS-specific config dir / `gws` (legacy fallback for existing installs) | ||
| /// 4. `~/.config/gws` (default for new installs) | ||
| pub fn config_dir() -> PathBuf { | ||
| if let Ok(dir) = std::env::var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR") { | ||
| return PathBuf::from(dir); | ||
| } | ||
|
|
||
| // Use ~/.config/gws on all platforms for a consistent, user-friendly path. | ||
| let primary = dirs::home_dir() | ||
| .unwrap_or_else(|| PathBuf::from(".")) | ||
| .join(".config") | ||
| .join("gws"); | ||
| if primary.exists() { | ||
| return primary; | ||
| } | ||
|
|
||
| // Backward compat: fall back to OS-specific config dir for existing installs | ||
| // (e.g. ~/Library/Application Support/gws on macOS, %APPDATA%\gws on Windows). | ||
| let legacy = dirs::config_dir() | ||
| .unwrap_or_else(|| PathBuf::from(".")) | ||
| .join("gws"); | ||
| if legacy.exists() { | ||
| return legacy; | ||
| } | ||
|
|
||
| primary | ||
| } | ||
|
Comment on lines
+30
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function For a robust library API, it's better to return a pub fn config_dir() -> Result<PathBuf, GwsError> {
if let Ok(dir) = std::env::var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR") {
return Ok(PathBuf::from(dir));
}
let home_dir = dirs::home_dir().ok_or_else(|| {
GwsError::Validation(
"Could not determine home directory. Ensure $HOME is set.".to_string(),
)
})?;
// Use ~/.config/gws on all platforms for a consistent, user-friendly path.
let primary = home_dir.join(".config").join("gws");
if primary.exists() {
return Ok(primary);
}
// Backward compat: fall back to OS-specific config dir for existing installs
// (e.g. ~/Library/Application Support/gws on macOS, %APPDATA%\gws on Windows).
if let Some(config_dir) = dirs::config_dir() {
let legacy = config_dir.join("gws");
if legacy.exists() {
return Ok(legacy);
}
}
Ok(primary)
} |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Google Workspace CLI — library crate. | ||
| //! | ||
| //! This crate exposes a focused subset of `gws` internals for programmatic | ||
| //! access to Google Workspace APIs. Library consumers can introspect API | ||
| //! schemas, resolve service names, validate inputs, and build authenticated | ||
| //! HTTP clients without depending on any CLI-specific code (clap, ratatui, | ||
| //! interactive OAuth flows, etc.). | ||
| //! | ||
| //! # Exposed modules | ||
| //! | ||
| //! | Module | Purpose | | ||
| //! |---|---| | ||
| //! | [`discovery`] | `RestDescription`, `RestMethod`, `RestResource` — introspect Google APIs | | ||
| //! | [`error`] | `GwsError` — unified error type | | ||
| //! | [`config`] | `config_dir()` — resolve the gws configuration directory | | ||
| //! | [`services`] | `resolve_service()`, `SERVICES` — service name to API mapping | | ||
| //! | [`validate`] | `validate_api_identifier()`, input safety helpers | | ||
| //! | [`client`] | `build_client()`, `send_with_retry()` — HTTP with retry | | ||
|
|
||
| pub mod client; | ||
| pub mod config; | ||
| pub mod discovery; | ||
| pub mod error; | ||
| pub mod services; | ||
| pub mod validate; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support returning a
Resultfromconfig_dirand provide more specific error information,GwsErrorshould be imported.