bktide
Version:
Command-line interface for Buildkite CI/CD workflows with rich shell completions (Fish, Bash, Zsh) and Alfred workflow integration for macOS power users
211 lines • 7.97 kB
JavaScript
import { BaseTokenFormatter } from './Formatter.js';
/**
* JSON formatter for tokens
*/
export class JsonFormatter extends BaseTokenFormatter {
name = 'json';
/**
* Format token status information as JSON
*
* @param status The token status information
* @returns Formatted token status message as JSON string
*/
formatTokenStatus(status) {
const result = {
...status,
message: this.getStatusMessage(status)
};
return JSON.stringify(result, null, 2);
}
/**
* Format token storage result as JSON
*
* @param success Whether the token was successfully stored
* @returns Formatted token storage result message as JSON string
*/
formatTokenStorageResult(success) {
const result = {
success,
message: success
? 'Token successfully stored in system keychain'
: 'Failed to store token'
};
return JSON.stringify(result, null, 2);
}
/**
* Format token reset result as JSON
*
* @param success Whether the token was successfully reset
* @param hadToken Whether there was a token before reset
* @returns Formatted token reset result message as JSON string
*/
formatTokenResetResult(success, hadToken) {
const result = {
success,
hadToken,
message: this.getResetMessage(success, hadToken)
};
return JSON.stringify(result, null, 2);
}
/**
* Format token validation error as JSON
*
* @param validation The validation status for each API
* @param options Formatting options
* @returns Formatted token validation error message as JSON string
*/
formatTokenValidationError(validation) {
const error = {
...validation,
message: this.getValidationErrorMessage(validation)
};
return JSON.stringify(error, null, 2);
}
/**
* Format token validation status as JSON
*
* @param validation The validation status for each API
* @param options Formatting options
* @returns Formatted token validation status message as JSON string
*/
formatTokenValidationStatus(validation) {
const status = {
...validation,
isValid: validation.valid,
message: this.getValidationStatusMessage(validation)
};
return JSON.stringify(status, null, 2);
}
/**
* Format error message(s) as JSON
*
* @param operation The operation that failed (e.g., 'storing', 'resetting', 'validating')
* @param error The error that occurred, or an array of errors
* @returns Formatted error message(s) as JSON string
*/
formatError(operation, error) {
const errors = Array.isArray(error) ? error : [error];
const errorObjects = errors.map(error => {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
operation,
error: errorMessage,
message: `Error ${operation} token: ${errorMessage}`
};
});
return JSON.stringify(errorObjects, null, 2);
}
/**
* Format authentication error message(s) as JSON
*
* @param operation The authentication operation that failed (e.g., 'storing', 'validating')
* @param error The authentication error that occurred, or an array of errors
* @returns Formatted authentication error message(s) as JSON string
*/
formatAuthErrors(operation, error) {
const errors = Array.isArray(error) ? error : [error];
const errorObjects = errors.map(error => {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
operation,
error: errorMessage,
message: `Authentication error ${operation} token: ${errorMessage}`,
suggestion: 'Please check your token permissions and try again'
};
});
return JSON.stringify(errorObjects, null, 2);
}
/**
* Format multiple error messages as JSON
*
* @param operation The operation that failed (e.g., 'storing', 'resetting', 'validating')
* @param errors Array of errors that occurred
* @returns Formatted error messages as JSON string
*/
formatErrors(operation, errors) {
const errorObjects = errors.map(error => {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
operation,
error: errorMessage,
message: `Error ${operation} token: ${errorMessage}`
};
});
return JSON.stringify(errorObjects, null, 2);
}
/**
* Get a human-readable status message based on token status
*/
getStatusMessage(status) {
if (!status.hasToken) {
return 'No token found in system keychain';
}
if (status.isValid) {
const validOrgs = Object.entries(status.validation.organizations)
.filter(([_, status]) => status.graphql && status.builds && status.organizations)
.map(([org]) => org);
return `Token is valid for organizations: ${validOrgs.join(', ')}`;
}
if (!status.validation.canListOrganizations) {
return 'Token is invalid or does not have access to list organizations';
}
const validOrgs = Object.entries(status.validation.organizations)
.filter(([_, status]) => status.graphql && status.builds && status.organizations)
.map(([org]) => org);
if (validOrgs.length === 0) {
return 'Token has no valid organizations';
}
return `Token is valid for organizations: ${validOrgs.join(', ')}`;
}
/**
* Get a human-readable reset message based on reset result
*/
getResetMessage(success, hadToken) {
if (!hadToken) {
return 'No token found in system keychain';
}
if (success) {
return 'Token successfully deleted from system keychain';
}
else {
return 'Failed to delete token';
}
}
/**
* Get a human-readable validation error message
*/
getValidationErrorMessage(validation) {
if (validation.canListOrganizations) {
const invalidOrgs = Object.entries(validation.organizations)
.filter(([_, status]) => !status.graphql || !status.builds || !status.organizations)
.map(([org, status]) => {
const invalidApis = [];
if (!status.graphql)
invalidApis.push('GraphQL');
if (!status.builds)
invalidApis.push('Builds');
if (!status.organizations)
invalidApis.push('Organizations');
return `${org} (${invalidApis.join(', ')})`;
});
return `Token has limited access in some organizations: ${invalidOrgs.join(', ')}`;
}
return 'Token is invalid or does not have access to list organizations';
}
/**
* Get a human-readable validation status message
*/
getValidationStatusMessage(validation) {
if (!validation.canListOrganizations) {
return 'Token is invalid or does not have access to list organizations';
}
const validOrgs = Object.entries(validation.organizations)
.filter(([_, status]) => status.graphql && status.builds && status.organizations)
.map(([org]) => org);
if (validOrgs.length === 0) {
return 'Token has no valid organizations';
}
return `Token is valid for organizations: ${validOrgs.join(', ')}`;
}
}
//# sourceMappingURL=JsonFormatter.js.map