0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-03-11 02:23:09 -05:00

feat(cli): better server info output (#8307)

* feat(cli): server-info command prints url and user email

* chore: clean up

---------

Co-authored-by: 澪 <mio@mio19.uk>
This commit is contained in:
Jason Rasmussen 2024-03-27 15:01:36 -04:00 committed by GitHub
parent 916603d2d4
commit 613b544bf0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 21 deletions

View file

@ -1,15 +1,24 @@
import { getAssetStatistics, getServerVersion, getSupportedMediaTypes } from '@immich/sdk'; import { getAssetStatistics, getMyUserInfo, getServerVersion, getSupportedMediaTypes } from '@immich/sdk';
import { BaseOptions, authenticate } from 'src/utils'; import { BaseOptions, authenticate } from 'src/utils';
export const serverInfo = async (options: BaseOptions) => { export const serverInfo = async (options: BaseOptions) => {
await authenticate(options); const { url } = await authenticate(options);
const versionInfo = await getServerVersion(); const [versionInfo, mediaTypes, stats, userInfo] = await Promise.all([
const mediaTypes = await getSupportedMediaTypes(); getServerVersion(),
const stats = await getAssetStatistics({}); getSupportedMediaTypes(),
getAssetStatistics({}),
getMyUserInfo(),
]);
console.log(`Server Version: ${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}`); console.log(`Server Info (via ${userInfo.email})`);
console.log(`Image Types: ${mediaTypes.image.map((extension) => extension.replace('.', ''))}`); console.log(` Url: ${url}`);
console.log(`Video Types: ${mediaTypes.video.map((extension) => extension.replace('.', ''))}`); console.log(` Version: ${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}`);
console.log(`Statistics:\n Images: ${stats.images}\n Videos: ${stats.videos}\n Total: ${stats.total}`); console.log(` Formats:`);
console.log(` Images: ${mediaTypes.image.map((extension) => extension.replace('.', ''))}`);
console.log(` Videos: ${mediaTypes.video.map((extension) => extension.replace('.', ''))}`);
console.log(` Statistics:`);
console.log(` Images: ${stats.images}`);
console.log(` Videos: ${stats.videos}`);
console.log(` Total: ${stats.total}`);
}; };

View file

@ -15,21 +15,25 @@ export interface BaseOptions {
export type AuthDto = { url: string; key: string }; export type AuthDto = { url: string; key: string };
type OldAuthDto = { instanceUrl: string; apiKey: string }; type OldAuthDto = { instanceUrl: string; apiKey: string };
export const authenticate = async (options: BaseOptions): Promise<void> => { export const authenticate = async (options: BaseOptions): Promise<AuthDto> => {
const { configDirectory: configDir, url, key } = options; const { configDirectory: configDir, url, key } = options;
// provided in command // provided in command
if (url && key) { if (url && key) {
await connect(url, key); return connect(url, key);
return;
} }
// fallback to auth file // fallback to auth file
const config = await readAuthFile(configDir); const config = await readAuthFile(configDir);
await connect(config.url, config.key); const auth = await connect(config.url, config.key);
if (auth.url !== config.url) {
await writeAuthFile(configDir, auth);
}
return auth;
}; };
export const connect = async (url: string, key: string): Promise<void> => { export const connect = async (url: string, key: string) => {
const wellKnownUrl = new URL('.well-known/immich', url); const wellKnownUrl = new URL('.well-known/immich', url);
try { try {
const wellKnown = await fetch(wellKnownUrl).then((response) => response.json()); const wellKnown = await fetch(wellKnownUrl).then((response) => response.json());
@ -50,6 +54,8 @@ export const connect = async (url: string, key: string): Promise<void> => {
logError(error, 'Failed to connect to server'); logError(error, 'Failed to connect to server');
process.exit(1); process.exit(1);
} }
return { url, key };
}; };
export const logError = (error: unknown, message: string) => { export const logError = (error: unknown, message: string) => {

View file

@ -11,13 +11,16 @@ describe(`immich server-info`, () => {
it('should return the server info', async () => { it('should return the server info', async () => {
const { stderr, stdout, exitCode } = await immichCli(['server-info']); const { stderr, stdout, exitCode } = await immichCli(['server-info']);
expect(stdout.split('\n')).toEqual([ expect(stdout.split('\n')).toEqual([
expect.stringContaining('Server Version:'), expect.stringContaining('Server Info (via admin@immich.cloud'),
expect.stringContaining('Image Types:'), ' Url: http://127.0.0.1:2283/api',
expect.stringContaining('Video Types:'), expect.stringContaining('Version:'),
'Statistics:', ' Formats:',
' Images: 0', expect.stringContaining('Images:'),
' Videos: 0', expect.stringContaining('Videos:'),
' Total: 0', ' Statistics:',
' Images: 0',
' Videos: 0',
' Total: 0',
]); ]);
expect(stderr).toBe(''); expect(stderr).toBe('');
expect(exitCode).toBe(0); expect(exitCode).toBe(0);