use std::io::{IsTerminal, Read};
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use dialoguer::{Confirm, Editor, Input, Password};
use serde_json::{Map, Value};
use servicenow_cli::api::{ApiError, DisplayValue, ListOptions, ServiceNowClient};
use servicenow_cli::commands::{
INCIDENT_LIST_FIELDS, build_body, parse_fields, print_record, print_records, record_sys_id,
};
use servicenow_cli::config::{
AuthType, Config, ProfileConfig, active_profile_name, config_path, init_document,
profile_summaries, remove_profile, save_profile, use_profile,
};
use servicenow_cli::credentials::{self, StoredCredential};
use servicenow_cli::incident;
use servicenow_cli::metadata::{self, ReferenceKind};
use servicenow_cli::output::{OutputConfig, OutputFormat, exit_code, print_error};
#[derive(Parser)]
#[command(
name = "servicenow",
version,
about = "Fast, safe, human-friendly ServiceNow operations from the terminal",
arg_required_else_help = true
)]
struct Cli {
#[arg(long, env = "SERVICENOW_INSTANCE")]
instance: Option<String>,
#[arg(long, env = "SERVICENOW_USERNAME")]
username: Option<String>,
#[arg(long, env = "SERVICENOW_PROFILE")]
profile: Option<String>,
#[arg(short, long, global = true, default_value = "auto")]
output: String,
#[arg(long, global = true, hide = true)]
json: bool,
#[arg(long, global = true)]
quiet: bool,
#[arg(long, global = true)]
no_color: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
#[command(subcommand)]
Auth(AuthCommand),
#[command(subcommand, visible_alias = "profiles")]
Profile(ProfileCommand),
#[command(subcommand, visible_alias = "incident")]
Incidents(IncidentsCommand),
#[command(subcommand, visible_alias = "table")]
Tables(TablesCommand),
#[command(subcommand)]
Config(ConfigCommand),
Doctor,
Schema {
table: Option<String>,
#[arg(long)]
refresh: bool,
},
Choices {
table: String,
field: String,
#[arg(long)]
refresh: bool,
},
Resolve {
#[arg(value_enum)]
kind: ReferenceKind,
value: String,
},
Completions {
shell: Shell,
},
}
#[derive(Subcommand)]
enum AuthCommand {
Login {
#[arg(default_value = "default")]
profile: String,
#[arg(long)]
instance: Option<String>,
#[arg(long, value_enum, default_value = "basic")]
method: AuthType,
#[arg(long)]
username: Option<String>,
#[arg(long)]
client_id: Option<String>,
#[arg(long, default_value = "useraccount")]
scope: String,
#[arg(long, default_value = "http://127.0.0.1:8484/callback")]
redirect_uri: String,
#[arg(long)]
secret_stdin: bool,
#[arg(long)]
no_browser: bool,
#[arg(long)]
read_only: bool,
},
Logout {
profile: Option<String>,
},
Status,
}
#[derive(Subcommand)]
enum ProfileCommand {
List,
Use { name: String },
Remove {
name: String,
#[arg(long)]
yes: bool,
},
}
#[derive(Subcommand)]
enum IncidentsCommand {
List {
#[arg(short, long)]
query: Option<String>,
#[arg(long)]
active: bool,
#[arg(short = 'n', long, default_value = "50")]
limit: usize,
#[arg(long, default_value = "0")]
offset: usize,
#[arg(long)]
all: bool,
#[arg(long)]
fields: Option<String>,
#[arg(long, value_enum, default_value = "false")]
display_value: DisplayValue,
},
Mine {
#[arg(short, long)]
query: Option<String>,
#[arg(short = 'n', long, default_value = "50")]
limit: usize,
#[arg(long)]
all: bool,
},
Show {
identifier: String,
#[arg(long)]
fields: Option<String>,
#[arg(long, value_enum, default_value = "all")]
display_value: DisplayValue,
},
Create {
#[arg(short, long)]
short_description: String,
#[arg(short, long)]
description: Option<String>,
#[arg(long)]
category: Option<String>,
#[arg(long)]
impact: Option<String>,
#[arg(long)]
urgency: Option<String>,
#[arg(long)]
assignment_group: Option<String>,
#[arg(long)]
assignee: Option<String>,
#[arg(long = "field")]
fields: Vec<String>,
},
Update {
identifier: String,
#[arg(long)]
short_description: Option<String>,
#[arg(long)]
description: Option<String>,
#[arg(long)]
state: Option<String>,
#[arg(long)]
assignee: Option<String>,
#[arg(long)]
work_notes: Option<String>,
#[arg(long = "field")]
fields: Vec<String>,
},
Edit {
identifier: String,
#[arg(long)]
file: Option<std::path::PathBuf>,
#[arg(long)]
dry_run: bool,
#[arg(long)]
yes: bool,
},
Note {
identifier: String,
text: Option<String>,
#[arg(long)]
file: Option<std::path::PathBuf>,
#[arg(long)]
dry_run: bool,
},
Assign {
identifier: String,
#[arg(long = "to")]
assignee: Option<String>,
#[arg(long)]
group: Option<String>,
#[arg(long)]
dry_run: bool,
},
Open {
identifier: String,
#[arg(long)]
print: bool,
},
Watch {
identifier: String,
#[arg(long, default_value = "5")]
interval: u64,
#[arg(long)]
count: Option<usize>,
#[arg(long)]
fields: Option<String>,
},
}
#[derive(Subcommand)]
enum TablesCommand {
List {
table: String,
#[arg(short, long)]
query: Option<String>,
#[arg(long)]
fields: Option<String>,
#[arg(short = 'n', long, default_value = "50")]
limit: usize,
#[arg(long, default_value = "0")]
offset: usize,
#[arg(long)]
all: bool,
#[arg(long, value_enum, default_value = "false")]
display_value: DisplayValue,
},
Get {
table: String,
sys_id: String,
#[arg(long)]
fields: Option<String>,
#[arg(long, value_enum, default_value = "all")]
display_value: DisplayValue,
},
Create {
table: String,
#[arg(long)]
data: Option<String>,
#[arg(long = "field")]
fields: Vec<String>,
},
Update {
table: String,
sys_id: String,
#[arg(long)]
data: Option<String>,
#[arg(long = "field")]
fields: Vec<String>,
},
Delete {
table: String,
sys_id: String,
#[arg(long)]
yes: bool,
},
}
#[derive(Subcommand)]
enum ConfigCommand {
Show,
Init,
Path,
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
let machine_errors = matches!(
cli.output.as_str(),
"json" | "jsonl" | "ndjson" | "yaml" | "yml" | "csv"
) || (cli.output == "auto"
&& (cli.json || !std::io::IsTerminal::is_terminal(&std::io::stdout())));
if let Err(error) = run(cli).await {
print_error(&error, machine_errors);
std::process::exit(exit_code(&error));
}
}
async fn run(cli: Cli) -> Result<(), ApiError> {
let output = OutputConfig::new(&cli.output, cli.json, cli.quiet, cli.no_color)?;
match cli.command {
Command::Auth(AuthCommand::Login {
profile,
instance,
method,
username,
client_id,
scope,
redirect_uri,
secret_stdin,
no_browser,
read_only,
}) => {
run_auth_login(
&output,
profile,
instance,
method,
username,
client_id,
scope,
redirect_uri,
secret_stdin,
no_browser,
read_only,
)
.await?;
return Ok(());
}
Command::Auth(AuthCommand::Logout { profile }) => {
let profile = profile.unwrap_or(active_profile_name()?);
let removed = credentials::delete(&profile)?;
let value = serde_json::json!({
"profile": profile,
"loggedOut": removed,
});
if output.json {
output.value(&value);
} else if removed {
println!("Logged out of profile {profile}.");
} else {
println!("Profile {profile} had no stored credential.");
}
return Ok(());
}
Command::Profile(ProfileCommand::List) => {
let profiles = profile_summaries()?;
if output.json {
output.value(&serde_json::json!({"result": profiles}));
} else {
let records = profiles
.into_iter()
.map(serde_json::to_value)
.collect::<Result<Vec<_>, _>>()
.map_err(|error| ApiError::Other(error.to_string()))?;
print_records(&records, None, output.color);
}
return Ok(());
}
Command::Profile(ProfileCommand::Use { name }) => {
use_profile(&name)?;
if output.json {
output.value(&serde_json::json!({"activeProfile": name}));
} else {
println!("Now using profile {name}.");
}
return Ok(());
}
Command::Profile(ProfileCommand::Remove { name, yes }) => {
let confirmed = yes
|| (std::io::stdin().is_terminal()
&& Confirm::new()
.with_prompt(format!(
"Remove profile '{name}' and its stored credential?"
))
.default(false)
.interact()
.map_err(|error| ApiError::Other(error.to_string()))?);
if !confirmed {
return Err(ApiError::InvalidInput(
"profile removal cancelled; use --yes for non-interactive removal".into(),
));
}
let removed = remove_profile(&name)?;
let credential_removed = credentials::delete(&name)?;
if !removed && !credential_removed {
return Err(ApiError::NotFound(format!("config profile '{name}'")));
}
if output.json {
output.value(&serde_json::json!({"removed": true, "profile": name}));
} else {
println!("Removed profile {name}.");
}
return Ok(());
}
Command::Schema { table: None, .. } => {
output.value(&command_schema(&Cli::command()));
return Ok(());
}
Command::Completions { shell } => {
let mut command = Cli::command();
clap_complete::generate(shell, &mut command, "servicenow", &mut std::io::stdout());
return Ok(());
}
Command::Config(ConfigCommand::Init) => {
if output.json {
output.value(&init_document());
} else {
let document = init_document();
println!(
"Config path: {}",
document["configPath"].as_str().unwrap_or("")
);
println!(
"\n{}",
toml::to_string_pretty(&document["example"])
.unwrap_or_else(|_| "[default]\ninstance = \"dev12345\"".into())
);
println!("Next: {}", document["setupCommand"].as_str().unwrap_or(""));
}
return Ok(());
}
Command::Config(ConfigCommand::Path) => {
if output.json {
output.value(&serde_json::json!({ "configPath": config_path() }));
} else {
println!("{}", config_path().display());
}
return Ok(());
}
_ => {}
}
let mut config = Config::load(cli.instance, cli.username, cli.profile)?;
if servicenow_cli::auth::refresh_if_needed(&mut config).await? {
output.message("OAuth access token refreshed.");
}
if matches!(cli.command, Command::Config(ConfigCommand::Show)) {
let masked = mask_secret(&config.secret);
let value = serde_json::json!({
"configPath": config_path(),
"profile": config.profile,
"instance": config.instance,
"username": config.username,
"authType": config.auth_type.as_str(),
"secretMasked": masked,
"readOnly": config.read_only,
});
if output.json {
output.value(&value);
} else {
print_record(&value, output.color);
}
return Ok(());
}
let client = ServiceNowClient::new(
&config.instance,
config.username.as_deref(),
&config.secret,
config.auth_type,
)?;
match cli.command {
Command::Incidents(command) => run_incidents(command, &client, &config, &output).await?,
Command::Tables(command) => run_tables(command, &client, &config, &output).await?,
Command::Schema {
table: Some(table),
refresh,
} => run_table_schema(&table, refresh, &client, &config, &output).await?,
Command::Choices {
table,
field,
refresh,
} => run_choices(&table, &field, refresh, &client, &config, &output).await?,
Command::Resolve { kind, value } => {
let record = metadata::resolve_reference(&client, kind, &value).await?;
emit_record(&output, record);
}
Command::Doctor | Command::Auth(AuthCommand::Status) => {
run_doctor(&client, &config, &output).await?
}
Command::Auth(_)
| Command::Profile(_)
| Command::Config(_)
| Command::Schema { .. }
| Command::Completions { .. } => {
unreachable!()
}
}
Ok(())
}
async fn run_table_schema(
table: &str,
refresh: bool,
client: &ServiceNowClient,
config: &Config,
output: &OutputConfig,
) -> Result<(), ApiError> {
let metadata = if refresh {
metadata::sync_table(client, &config.profile, table).await?
} else if let Some(metadata) = metadata::load(&config.profile, table)? {
metadata
} else {
output.message("No cached metadata found; fetching it from the instance.");
metadata::sync_table(client, &config.profile, table).await?
};
if output.json {
output.value(&serde_json::json!({"result": metadata}));
} else {
println!(
"{} {} fields {} choice fields\n",
output.heading(&metadata.table),
metadata.fields.len(),
metadata.choices.len()
);
let records = metadata::metadata_as_records(&metadata);
let fields = [
"field".into(),
"label".into(),
"type".into(),
"reference".into(),
"mandatory".into(),
"read_only".into(),
"choices".into(),
];
print_records(&records, Some(&fields), output.color);
}
Ok(())
}
async fn run_choices(
table: &str,
field: &str,
refresh: bool,
client: &ServiceNowClient,
config: &Config,
output: &OutputConfig,
) -> Result<(), ApiError> {
let metadata = if refresh {
metadata::sync_table(client, &config.profile, table).await?
} else if let Some(metadata) = metadata::load(&config.profile, table)? {
metadata
} else {
metadata::sync_table(client, &config.profile, table).await?
};
if metadata.field(field).is_none() {
return Err(ApiError::NotFound(format!("field {table}.{field}")));
}
let choices = metadata.choices.get(field).cloned().unwrap_or_default();
if output.json {
output.value(&serde_json::json!({
"table": table,
"field": field,
"result": choices,
}));
} else if choices.is_empty() {
println!("No configured choices for {table}.{field}.");
} else {
let records = choices
.into_iter()
.map(serde_json::to_value)
.collect::<Result<Vec<_>, _>>()
.map_err(|error| ApiError::Other(error.to_string()))?;
print_records(
&records,
Some(&["value".into(), "label".into(), "sequence".into()]),
output.color,
);
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn run_auth_login(
output: &OutputConfig,
profile: String,
instance: Option<String>,
method: AuthType,
username: Option<String>,
client_id: Option<String>,
scope: String,
redirect_uri: String,
secret_stdin: bool,
no_browser: bool,
read_only: bool,
) -> Result<(), ApiError> {
servicenow_cli::config::validate_profile_name(&profile)?;
credentials::available()?;
let instance = required_prompt(instance, "ServiceNow instance")?;
let mut username = username;
let mut configured_client_id = None;
let credential = match method {
AuthType::Basic => {
username = Some(required_prompt(username, "Username")?);
StoredCredential::Basic {
password: read_login_secret(secret_stdin, "Password", false)?,
}
}
AuthType::Bearer => StoredCredential::Bearer {
access_token: read_login_secret(secret_stdin, "Access token", false)?,
},
AuthType::OAuth => {
let id = required_prompt(client_id, "OAuth client ID")?;
configured_client_id = Some(id.clone());
let client_secret = read_login_secret(secret_stdin, "OAuth client secret", true)?;
servicenow_cli::auth::oauth_login(
&instance,
&id,
(!client_secret.is_empty()).then_some(client_secret),
Some(&scope),
&redirect_uri,
!no_browser,
)
.await?
}
};
let client =
ServiceNowClient::new(&instance, username.as_deref(), credential.secret(), method)?;
let users = client
.list_records(
"sys_user",
&ListOptions {
query: Some("sys_id=javascript:gs.getUserID()".into()),
fields: Some(vec!["sys_id".into(), "user_name".into(), "name".into()]),
limit: 1,
..ListOptions::default()
},
)
.await?;
if let Some(user) = users.first() {
username = field_text(user, "user_name").or(username);
}
credentials::store(&profile, &credential)?;
let mut profile_config = ProfileConfig::default();
profile_config.instance = Some(instance);
profile_config.username = username.clone();
profile_config.auth_type = Some(method.as_str().into());
profile_config.read_only = Some(read_only);
profile_config.credential_store = Some("keyring".into());
profile_config.client_id = configured_client_id;
profile_config.oauth_scope = matches!(method, AuthType::OAuth).then_some(scope);
profile_config.redirect_uri = matches!(method, AuthType::OAuth).then_some(redirect_uri);
if let Err(error) = save_profile(&profile, profile_config, true) {
let _ = credentials::delete(&profile);
return Err(error);
}
let result = serde_json::json!({
"profile": profile,
"instance": client.site_url(),
"username": username,
"authType": method.as_str(),
"readOnly": read_only,
"credentialStore": "os-keychain",
"authenticated": true,
});
if output.json {
output.value(&result);
} else {
println!("Signed in successfully.\n");
println!(
" Profile {}",
result["profile"].as_str().unwrap_or("")
);
println!(
" Instance {}",
result["instance"].as_str().unwrap_or("")
);
println!(
" User {}",
result["username"].as_str().unwrap_or("")
);
println!(
" Auth {}",
result["authType"].as_str().unwrap_or("")
);
println!(
" Safety {}",
if read_only {
"read-only"
} else {
"writes enabled"
}
);
println!(" Credentials OS keychain");
}
Ok(())
}
fn required_prompt(value: Option<String>, prompt: &str) -> Result<String, ApiError> {
if let Some(value) = value.filter(|value| !value.trim().is_empty()) {
return Ok(value.trim().into());
}
if !std::io::stdin().is_terminal() {
return Err(ApiError::InvalidInput(format!(
"{prompt} is required in non-interactive mode"
)));
}
Input::<String>::new()
.with_prompt(prompt)
.interact_text()
.map_err(|error| ApiError::Other(format!("failed to read {prompt}: {error}")))
.and_then(|value| {
(!value.trim().is_empty())
.then(|| value.trim().into())
.ok_or_else(|| ApiError::InvalidInput(format!("{prompt} cannot be empty")))
})
}
fn read_login_secret(
from_stdin: bool,
prompt: &str,
allow_empty: bool,
) -> Result<String, ApiError> {
let value = if from_stdin {
let mut value = String::new();
std::io::stdin()
.read_to_string(&mut value)
.map_err(|error| ApiError::Other(format!("failed to read secret: {error}")))?;
value.trim_end_matches(['\r', '\n']).to_string()
} else {
if !std::io::stdin().is_terminal() {
return Err(ApiError::InvalidInput(format!(
"{prompt} requires --secret-stdin in non-interactive mode"
)));
}
Password::new()
.with_prompt(prompt)
.allow_empty_password(allow_empty)
.interact()
.map_err(|error| ApiError::Other(format!("failed to read {prompt}: {error}")))?
};
if value.is_empty() && !allow_empty {
Err(ApiError::InvalidInput(format!("{prompt} cannot be empty")))
} else {
Ok(value)
}
}
async fn run_doctor(
client: &ServiceNowClient,
config: &Config,
output: &OutputConfig,
) -> Result<(), ApiError> {
let users = client
.list_records(
"sys_user",
&ListOptions {
query: Some("sys_id=javascript:gs.getUserID()".into()),
fields: Some(vec![
"sys_id".into(),
"user_name".into(),
"name".into(),
"active".into(),
]),
limit: 1,
..ListOptions::default()
},
)
.await?;
let user = users.first().ok_or_else(|| {
ApiError::Other("authentication succeeded, but the current user was not returned".into())
})?;
let username = field_text(user, "user_name").unwrap_or_else(|| "unknown".into());
client
.list_records(
"incident",
&ListOptions {
fields: Some(vec!["sys_id".into(), "number".into()]),
limit: 1,
..ListOptions::default()
},
)
.await?;
let checks = serde_json::json!([
{"name": "configuration", "ok": true, "detail": config.instance},
{"name": "authentication", "ok": true, "detail": username},
{"name": "table_api", "ok": true, "detail": "incident table is readable"},
{
"name": "write_safety",
"ok": true,
"detail": if config.read_only { "read-only mode enabled" } else { "write operations enabled" }
}
]);
let result = serde_json::json!({
"ok": true,
"instance": client.site_url(),
"checks": checks,
});
if output.json {
output.value(&result);
} else {
println!("ServiceNow connection\n");
for check in result["checks"].as_array().expect("checks are an array") {
println!(
" ✓ {:<16} {}",
check["name"].as_str().unwrap_or("check"),
check["detail"].as_str().unwrap_or("")
);
}
println!("\nReady.");
}
Ok(())
}
async fn run_incidents(
command: IncidentsCommand,
client: &ServiceNowClient,
config: &Config,
output: &OutputConfig,
) -> Result<(), ApiError> {
match command {
IncidentsCommand::List {
query,
active,
limit,
offset,
all,
fields,
display_value,
} => {
let query = combine_query(active.then_some("active=true"), query.as_deref());
let fields = parse_fields(fields.as_deref()).or_else(|| {
Some(
INCIDENT_LIST_FIELDS
.iter()
.map(|field| (*field).into())
.collect(),
)
});
let records = client
.list_records(
"incident",
&ListOptions {
query,
fields: fields.clone(),
limit,
offset,
all,
display_value,
},
)
.await?;
emit_records(output, records, fields.as_deref());
}
IncidentsCommand::Mine { query, limit, all } => {
let query = combine_query(
Some("assigned_to=javascript:gs.getUserID()"),
query.as_deref(),
);
let fields: Vec<String> = INCIDENT_LIST_FIELDS
.iter()
.map(|field| (*field).into())
.collect();
let records = client
.list_records(
"incident",
&ListOptions {
query,
fields: Some(fields.clone()),
limit,
all,
..ListOptions::default()
},
)
.await?;
emit_records(output, records, Some(&fields));
}
IncidentsCommand::Show {
identifier,
fields,
display_value,
} => {
let fields = parse_fields(fields.as_deref());
let record =
resolve_incident(client, &identifier, fields.clone(), display_value).await?;
emit_record(output, record);
}
IncidentsCommand::Create {
short_description,
description,
category,
impact,
urgency,
assignment_group,
assignee,
fields,
} => {
config.require_writable()?;
let mut body = build_body(None, &fields)?;
insert(&mut body, "short_description", Some(short_description));
insert(&mut body, "description", description);
insert(&mut body, "category", category);
insert(&mut body, "impact", impact);
insert(&mut body, "urgency", urgency);
insert(
&mut body,
"assignment_group",
resolve_reference_id(client, ReferenceKind::Group, assignment_group).await?,
);
insert(
&mut body,
"assigned_to",
resolve_reference_id(client, ReferenceKind::User, assignee).await?,
);
let record = client.create_record("incident", &body).await?;
output.success("Incident created.");
emit_record(output, record);
}
IncidentsCommand::Update {
identifier,
short_description,
description,
state,
assignee,
work_notes,
fields,
} => {
config.require_writable()?;
let mut body = build_body(None, &fields)?;
insert(&mut body, "short_description", short_description);
insert(&mut body, "description", description);
insert(&mut body, "state", state);
insert(
&mut body,
"assigned_to",
resolve_reference_id(client, ReferenceKind::User, assignee).await?,
);
insert(&mut body, "work_notes", work_notes);
if body.is_empty() {
return Err(ApiError::InvalidInput(
"at least one field must be supplied".into(),
));
}
let existing = resolve_incident(
client,
&identifier,
Some(vec!["sys_id".into()]),
DisplayValue::False,
)
.await?;
let sys_id = record_sys_id(&existing)?.to_string();
let record = client.update_record("incident", &sys_id, &body).await?;
output.success("Incident updated.");
emit_record(output, record);
}
IncidentsCommand::Edit {
identifier,
file,
dry_run,
yes,
} => {
let existing = resolve_incident(client, &identifier, None, DisplayValue::All).await?;
let sys_id = record_sys_id(&existing)?.to_string();
let original = incident::edit_document(
&existing,
metadata::load(&config.profile, "incident")?.as_ref(),
)?;
let edited = match file {
Some(path) => read_file_or_stdin(&path, "edited incident")?,
None if std::io::stdin().is_terminal() => Editor::new()
.extension(".yaml")
.edit(&original)
.map_err(|error| ApiError::Other(format!("failed to open editor: {error}")))?
.ok_or_else(|| ApiError::InvalidInput("incident edit cancelled".into()))?,
None => {
return Err(ApiError::InvalidInput(
"an interactive terminal or --file is required for incident edit".into(),
));
}
};
let body = incident::changed_fields(&existing, incident::parse_edit_document(&edited)?);
if body.is_empty() {
if output.json {
output.value(&serde_json::json!({
"changed": false,
"incident": identifier,
"changes": {},
}));
} else {
println!("No changes to apply.");
}
return Ok(());
}
if dry_run {
emit_mutation_plan(output, "update", &identifier, &body);
return Ok(());
}
config.require_writable()?;
if !yes {
if !std::io::stdin().is_terminal() {
return Err(ApiError::InvalidInput(
"confirmation requires an interactive terminal; rerun with --yes".into(),
));
}
let diff = incident::unified_diff(&original, &edited);
if !diff.is_empty() {
eprintln!("{diff}");
}
let confirmed = Confirm::new()
.with_prompt(format!(
"Apply {} changed field(s) to {identifier}?",
body.len()
))
.default(false)
.interact()
.map_err(|error| {
ApiError::Other(format!("failed to confirm update: {error}"))
})?;
if !confirmed {
return Err(ApiError::InvalidInput("incident edit cancelled".into()));
}
}
let record = client.update_record("incident", &sys_id, &body).await?;
output.success(&format!("Updated {identifier} ({} field(s)).", body.len()));
emit_record(output, record);
}
IncidentsCommand::Note {
identifier,
text,
file,
dry_run,
} => {
let note = match (text, file) {
(Some(_), Some(_)) => {
return Err(ApiError::InvalidInput(
"provide work note text or --file, not both".into(),
));
}
(Some(text), None) => text,
(None, Some(path)) => read_file_or_stdin(&path, "work note")?,
(None, None) => {
return Err(ApiError::InvalidInput(
"work note text or --file is required".into(),
));
}
};
if note.trim().is_empty() {
return Err(ApiError::InvalidInput("work note cannot be empty".into()));
}
let existing = resolve_incident(
client,
&identifier,
Some(vec!["sys_id".into()]),
DisplayValue::False,
)
.await?;
let mut body = Map::new();
body.insert("work_notes".into(), Value::String(note));
if dry_run {
emit_mutation_plan(output, "append_work_note", &identifier, &body);
return Ok(());
}
config.require_writable()?;
let record = client
.update_record("incident", record_sys_id(&existing)?, &body)
.await?;
output.success(&format!("Added a work note to {identifier}."));
emit_record(output, record);
}
IncidentsCommand::Assign {
identifier,
assignee,
group,
dry_run,
} => {
if assignee.is_none() && group.is_none() {
return Err(ApiError::InvalidInput(
"provide --to, --group, or both".into(),
));
}
let mut body = Map::new();
insert(
&mut body,
"assigned_to",
resolve_reference_id(client, ReferenceKind::User, assignee).await?,
);
insert(
&mut body,
"assignment_group",
resolve_reference_id(client, ReferenceKind::Group, group).await?,
);
let existing = resolve_incident(
client,
&identifier,
Some(vec!["sys_id".into()]),
DisplayValue::False,
)
.await?;
if dry_run {
emit_mutation_plan(output, "assign", &identifier, &body);
return Ok(());
}
config.require_writable()?;
let record = client
.update_record("incident", record_sys_id(&existing)?, &body)
.await?;
output.success(&format!("Assigned {identifier}."));
emit_record(output, record);
}
IncidentsCommand::Open { identifier, print } => {
let existing = resolve_incident(
client,
&identifier,
Some(vec!["sys_id".into(), "number".into()]),
DisplayValue::False,
)
.await?;
let url = client.record_url("incident", record_sys_id(&existing)?);
if output.json {
output.value(&serde_json::json!({
"incident": field_text(&existing, "number").unwrap_or(identifier),
"url": url,
}));
} else if print || !std::io::stdout().is_terminal() {
println!("{url}");
} else {
open::that(&url).map_err(|error| {
ApiError::Other(format!("failed to open ServiceNow in a browser: {error}"))
})?;
output.success("Opened incident in ServiceNow.");
}
}
IncidentsCommand::Watch {
identifier,
interval,
count,
fields,
} => {
if interval == 0 {
return Err(ApiError::InvalidInput(
"--interval must be greater than zero".into(),
));
}
if count == Some(0) {
return Err(ApiError::InvalidInput(
"--count must be greater than zero".into(),
));
}
if output.format == OutputFormat::Csv {
return Err(ApiError::InvalidInput(
"incident watch is a stream; use table, json, jsonl, or yaml output".into(),
));
}
let fields = parse_fields(fields.as_deref()).unwrap_or_else(|| {
[
"sys_id",
"number",
"short_description",
"state",
"priority",
"assigned_to",
"assignment_group",
"sys_updated_on",
]
.into_iter()
.map(str::to_string)
.collect()
});
let mut previous =
resolve_incident(client, &identifier, Some(fields.clone()), DisplayValue::All)
.await?;
let sys_id = record_sys_id(&previous)?.to_string();
emit_watch_event(output, &identifier, 0, &[], Some(&previous));
let mut polls = 0usize;
loop {
if count.is_some_and(|limit| polls >= limit) {
break;
}
tokio::select! {
_ = tokio::signal::ctrl_c() => {
output.message("Watch stopped.");
break;
}
_ = tokio::time::sleep(std::time::Duration::from_secs(interval)) => {}
}
polls += 1;
let current = client
.get_record("incident", &sys_id, Some(&fields), DisplayValue::All)
.await?;
let changes = incident::change_records(&previous, ¤t);
if !changes.is_empty() {
emit_watch_event(output, &identifier, polls, &changes, None);
}
previous = current;
}
}
}
Ok(())
}
async fn resolve_reference_id(
client: &ServiceNowClient,
kind: ReferenceKind,
value: Option<String>,
) -> Result<Option<String>, ApiError> {
let Some(value) = value else {
return Ok(None);
};
let record = metadata::resolve_reference(client, kind, &value).await?;
Ok(Some(record_sys_id(&record)?.to_string()))
}
fn read_file_or_stdin(path: &std::path::Path, label: &str) -> Result<String, ApiError> {
if path == std::path::Path::new("-") {
let mut value = String::new();
std::io::stdin()
.read_to_string(&mut value)
.map_err(|error| {
ApiError::Other(format!("failed to read {label} from stdin: {error}"))
})?;
Ok(value)
} else {
std::fs::read_to_string(path).map_err(|error| {
ApiError::Other(format!(
"failed to read {label} from {}: {error}",
path.display()
))
})
}
}
fn emit_mutation_plan(
output: &OutputConfig,
operation: &str,
identifier: &str,
body: &Map<String, Value>,
) {
let plan = serde_json::json!({
"dryRun": true,
"operation": operation,
"table": "incident",
"incident": identifier,
"changes": body,
});
if output.json {
output.value(&plan);
} else {
println!("Dry run: {operation} {identifier}\n");
print_record(&plan["changes"], output.color);
}
}
fn emit_watch_event(
output: &OutputConfig,
identifier: &str,
poll: usize,
changes: &[Value],
initial: Option<&Value>,
) {
let event = serde_json::json!({
"event": if initial.is_some() { "snapshot" } else { "change" },
"incident": identifier,
"poll": poll,
"changes": changes,
"record": initial,
});
match output.format {
OutputFormat::Json | OutputFormat::JsonLines => println!(
"{}",
serde_json::to_string(&event).expect("watch event is serializable")
),
OutputFormat::Yaml => print!(
"---\n{}",
serde_saphyr::to_string(&event).expect("watch event is serializable")
),
OutputFormat::Text => {
if let Some(record) = initial {
println!("Watching {identifier}. Press Ctrl-C to stop.\n");
print_record(record, output.color);
} else {
println!("\n{} changed", output.heading(identifier));
print_records(
changes,
Some(&["field".into(), "before".into(), "after".into()]),
output.color,
);
}
}
OutputFormat::Csv => unreachable!("CSV watch output is rejected before polling"),
}
}
async fn run_tables(
command: TablesCommand,
client: &ServiceNowClient,
config: &Config,
output: &OutputConfig,
) -> Result<(), ApiError> {
match command {
TablesCommand::List {
table,
query,
fields,
limit,
offset,
all,
display_value,
} => {
let fields = parse_fields(fields.as_deref());
let records = client
.list_records(
&table,
&ListOptions {
query,
fields: fields.clone(),
limit,
offset,
all,
display_value,
},
)
.await?;
emit_records(output, records, fields.as_deref());
}
TablesCommand::Get {
table,
sys_id,
fields,
display_value,
} => {
let fields = parse_fields(fields.as_deref());
let record = client
.get_record(&table, &sys_id, fields.as_deref(), display_value)
.await?;
emit_record(output, record);
}
TablesCommand::Create {
table,
data,
fields,
} => {
config.require_writable()?;
let body = build_body(data.as_deref(), &fields)?;
let record = client.create_record(&table, &body).await?;
output.message("Record created.");
emit_record(output, record);
}
TablesCommand::Update {
table,
sys_id,
data,
fields,
} => {
config.require_writable()?;
let body = build_body(data.as_deref(), &fields)?;
let record = client.update_record(&table, &sys_id, &body).await?;
output.message("Record updated.");
emit_record(output, record);
}
TablesCommand::Delete { table, sys_id, yes } => {
config.require_writable()?;
if !yes {
return Err(ApiError::InvalidInput(
"deletion is permanent; rerun with --yes to confirm".into(),
));
}
client.delete_record(&table, &sys_id).await?;
let result = serde_json::json!({
"deleted": true,
"table": table,
"sys_id": sys_id,
});
if output.json {
output.value(&result);
} else {
println!("Deleted {table}/{sys_id}.");
}
}
}
Ok(())
}
async fn resolve_incident(
client: &ServiceNowClient,
identifier: &str,
fields: Option<Vec<String>>,
display_value: DisplayValue,
) -> Result<Value, ApiError> {
if identifier.len() == 32 && identifier.bytes().all(|byte| byte.is_ascii_hexdigit()) {
client
.get_record("incident", identifier, fields.as_deref(), display_value)
.await
} else {
client
.find_one("incident", "number", identifier, fields, display_value)
.await
}
}
fn emit_records(output: &OutputConfig, records: Vec<Value>, fields: Option<&[String]>) {
if output.json {
output.value(&serde_json::json!({
"count": records.len(),
"result": records,
}));
} else {
print_records(&records, fields, output.color);
}
}
fn emit_record(output: &OutputConfig, record: Value) {
if output.json {
output.value(&serde_json::json!({ "result": record }));
} else {
print_record(&record, output.color);
}
}
fn combine_query(first: Option<&str>, second: Option<&str>) -> Option<String> {
match (
first.filter(|value| !value.is_empty()),
second.filter(|value| !value.is_empty()),
) {
(Some(first), Some(second)) => Some(format!("{first}^{second}")),
(Some(value), None) | (None, Some(value)) => Some(value.to_string()),
(None, None) => None,
}
}
fn insert(body: &mut Map<String, Value>, name: &str, value: Option<String>) {
if let Some(value) = value {
body.insert(name.into(), Value::String(value));
}
}
fn field_text(record: &Value, field: &str) -> Option<String> {
let value = record.get(field)?;
match value {
Value::String(value) => Some(value.clone()),
Value::Bool(value) => Some(value.to_string()),
Value::Number(value) => Some(value.to_string()),
Value::Object(value) => value
.get("display_value")
.and_then(Value::as_str)
.or_else(|| value.get("value").and_then(Value::as_str))
.map(str::to_string),
_ => None,
}
}
fn mask_secret(secret: &str) -> String {
if secret.chars().count() <= 4 {
"****".into()
} else {
let suffix: String = secret
.chars()
.rev()
.take(4)
.collect::<String>()
.chars()
.rev()
.collect();
format!("***{suffix}")
}
}
fn command_schema(command: &clap::Command) -> Value {
let args: Vec<Value> = command
.get_arguments()
.filter(|arg| arg.get_id() != "help" && arg.get_id() != "version")
.map(|arg| {
serde_json::json!({
"id": arg.get_id().as_str(),
"long": arg.get_long(),
"short": arg.get_short().map(|value| value.to_string()),
"required": arg.is_required_set(),
"help": arg.get_help().map(|value| value.to_string()),
})
})
.collect();
let commands: Vec<Value> = command.get_subcommands().map(command_schema).collect();
serde_json::json!({
"name": command.get_name(),
"about": command.get_about().map(|value| value.to_string()),
"arguments": args,
"commands": commands,
})
}