2025-07-22 10:52:40 -04:00
/ * *
* @license
* Copyright 2025 Google LLC
* SPDX - License - Identifier : Apache - 2.0
* /
2025-08-26 00:04:53 +02:00
import type { Config } from '@google/gemini-cli-core' ;
import { AuthType } from '@google/gemini-cli-core' ;
2025-07-22 10:52:40 -04:00
import { USER_SETTINGS_PATH } from './config/settings.js' ;
import { validateAuthMethod } from './config/auth.js' ;
2025-07-25 10:19:38 -07:00
function getAuthTypeFromEnv ( ) : AuthType | undefined {
2025-08-17 12:43:21 -04:00
if ( process . env [ 'GOOGLE_GENAI_USE_GCA' ] === 'true' ) {
2025-07-25 10:19:38 -07:00
return AuthType . LOGIN_WITH_GOOGLE ;
}
2025-08-17 12:43:21 -04:00
if ( process . env [ 'GOOGLE_GENAI_USE_VERTEXAI' ] === 'true' ) {
2025-07-25 10:19:38 -07:00
return AuthType . USE_VERTEX_AI ;
}
2025-08-17 12:43:21 -04:00
if ( process . env [ 'GEMINI_API_KEY' ] ) {
2025-07-25 10:19:38 -07:00
return AuthType . USE_GEMINI ;
}
return undefined ;
}
2025-07-22 10:52:40 -04:00
export async function validateNonInteractiveAuth (
configuredAuthType : AuthType | undefined ,
2025-08-01 11:49:03 -07:00
useExternalAuth : boolean | undefined ,
2025-07-22 10:52:40 -04:00
nonInteractiveConfig : Config ,
) {
2025-07-25 10:19:38 -07:00
const effectiveAuthType = configuredAuthType || getAuthTypeFromEnv ( ) ;
2025-07-22 10:52:40 -04:00
if ( ! effectiveAuthType ) {
console . error (
2025-07-25 10:19:38 -07:00
` Please set an Auth method in your ${ USER_SETTINGS_PATH } or specify one of the following environment variables before running: GEMINI_API_KEY, GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA ` ,
2025-07-22 10:52:40 -04:00
) ;
process . exit ( 1 ) ;
}
2025-08-01 11:49:03 -07:00
if ( ! useExternalAuth ) {
const err = validateAuthMethod ( effectiveAuthType ) ;
if ( err != null ) {
console . error ( err ) ;
process . exit ( 1 ) ;
}
2025-07-22 10:52:40 -04:00
}
await nonInteractiveConfig . refreshAuth ( effectiveAuthType ) ;
return nonInteractiveConfig ;
}