From 613b544bf0ebddfc410e11bd8c47ccc9d75c9b73 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Wed, 27 Mar 2024 15:01:36 -0400 Subject: [PATCH] feat(cli): better server info output (#8307) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(cli): server-info command prints url and user email * chore: clean up --------- Co-authored-by: 澪 --- cli/src/commands/server-info.ts | 27 +++++++++++++++-------- cli/src/utils.ts | 16 +++++++++----- e2e/src/cli/specs/server-info.e2e-spec.ts | 17 ++++++++------ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/cli/src/commands/server-info.ts b/cli/src/commands/server-info.ts index a7de804df9..074513bd61 100644 --- a/cli/src/commands/server-info.ts +++ b/cli/src/commands/server-info.ts @@ -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'; export const serverInfo = async (options: BaseOptions) => { - await authenticate(options); + const { url } = await authenticate(options); - const versionInfo = await getServerVersion(); - const mediaTypes = await getSupportedMediaTypes(); - const stats = await getAssetStatistics({}); + const [versionInfo, mediaTypes, stats, userInfo] = await Promise.all([ + getServerVersion(), + getSupportedMediaTypes(), + getAssetStatistics({}), + getMyUserInfo(), + ]); - console.log(`Server Version: ${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}`); - console.log(`Image Types: ${mediaTypes.image.map((extension) => extension.replace('.', ''))}`); - console.log(`Video Types: ${mediaTypes.video.map((extension) => extension.replace('.', ''))}`); - console.log(`Statistics:\n Images: ${stats.images}\n Videos: ${stats.videos}\n Total: ${stats.total}`); + console.log(`Server Info (via ${userInfo.email})`); + console.log(` Url: ${url}`); + console.log(` Version: ${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}`); + 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}`); }; diff --git a/cli/src/utils.ts b/cli/src/utils.ts index c17ad69038..b2d34bbb48 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -15,21 +15,25 @@ export interface BaseOptions { export type AuthDto = { url: string; key: string }; type OldAuthDto = { instanceUrl: string; apiKey: string }; -export const authenticate = async (options: BaseOptions): Promise => { +export const authenticate = async (options: BaseOptions): Promise => { const { configDirectory: configDir, url, key } = options; // provided in command if (url && key) { - await connect(url, key); - return; + return connect(url, key); } // fallback to auth file 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 => { +export const connect = async (url: string, key: string) => { const wellKnownUrl = new URL('.well-known/immich', url); try { const wellKnown = await fetch(wellKnownUrl).then((response) => response.json()); @@ -50,6 +54,8 @@ export const connect = async (url: string, key: string): Promise => { logError(error, 'Failed to connect to server'); process.exit(1); } + + return { url, key }; }; export const logError = (error: unknown, message: string) => { diff --git a/e2e/src/cli/specs/server-info.e2e-spec.ts b/e2e/src/cli/specs/server-info.e2e-spec.ts index f207f1fa2e..13eefd3df4 100644 --- a/e2e/src/cli/specs/server-info.e2e-spec.ts +++ b/e2e/src/cli/specs/server-info.e2e-spec.ts @@ -11,13 +11,16 @@ describe(`immich server-info`, () => { it('should return the server info', async () => { const { stderr, stdout, exitCode } = await immichCli(['server-info']); expect(stdout.split('\n')).toEqual([ - expect.stringContaining('Server Version:'), - expect.stringContaining('Image Types:'), - expect.stringContaining('Video Types:'), - 'Statistics:', - ' Images: 0', - ' Videos: 0', - ' Total: 0', + expect.stringContaining('Server Info (via admin@immich.cloud'), + ' Url: http://127.0.0.1:2283/api', + expect.stringContaining('Version:'), + ' Formats:', + expect.stringContaining('Images:'), + expect.stringContaining('Videos:'), + ' Statistics:', + ' Images: 0', + ' Videos: 0', + ' Total: 0', ]); expect(stderr).toBe(''); expect(exitCode).toBe(0);