Skip to content

Commit 7d31808

Browse files
authored
fix(cli): fixed --non-interactive flag (#3666)
1 parent d32fa03 commit 7d31808

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

genkit-tools/cli/src/cli.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ export async function startCLI(): Promise<void> {
6969
.description('Genkit CLI')
7070
.version(version)
7171
.option('--no-update-notification', 'Do not show update notification')
72-
.hook('preAction', async (_, actionCommand) => {
73-
await notifyAnalyticsIfFirstRun();
74-
72+
.option(
73+
'--non-interactive',
74+
'Run in non-interactive mode. All interactions will use the default choice.'
75+
)
76+
.hook('preAction', async (command, actionCommand) => {
7577
// For now only record known command names, to avoid tools plugins causing
7678
// arbitrary text to get recorded. Once we launch tools plugins, we'll have
7779
// to give this more thought
@@ -87,6 +89,14 @@ export async function startCLI(): Promise<void> {
8789
} else {
8890
commandName = 'unknown';
8991
}
92+
93+
if (
94+
!process.argv.includes('--non-interactive') &&
95+
commandName !== 'config'
96+
) {
97+
await notifyAnalyticsIfFirstRun();
98+
}
99+
90100
const { isCompiledBinary } = detectCLIRuntime();
91101
await record(
92102
new RunCommandEvent(commandName, isCompiledBinary ? 'binary' : 'node')

genkit-tools/common/src/utils/analytics.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export async function record(event: GAEvent): Promise<void> {
120120

121121
/** Displays a notification that analytics are in use. */
122122
export async function notifyAnalyticsIfFirstRun(): Promise<void> {
123-
if (!isAnalyticsEnabled()) return;
123+
if (isAnalyticsOptedOut()) return;
124124

125125
if (configstore.get(NOTIFICATION_ACKED)) {
126126
return;
@@ -132,7 +132,7 @@ export async function notifyAnalyticsIfFirstRun(): Promise<void> {
132132
input: process.stdin,
133133
output: process.stdout,
134134
});
135-
await readline.question('Press "Enter" to continue');
135+
await readline.question('Press "Enter" to acknowledge and continue');
136136
readline.close();
137137

138138
configstore.set(NOTIFICATION_ACKED, true);
@@ -169,7 +169,12 @@ const ANALYTICS_NOTIFICATION =
169169
'Genkit CLI and Developer UI use cookies and ' +
170170
'similar technologies from Google\nto deliver and enhance the quality of its ' +
171171
'services and to analyze usage.\n' +
172-
'Learn more at https://policies.google.com/technologies/cookies';
172+
'Learn more at https://policies.google.com/technologies/cookies\n' +
173+
'\n' +
174+
'If running in non-interactive environments set --non-interactive flag. Ex:\n' +
175+
' genkit start --non-interactive -- <cmd>\n' +
176+
'To opt out of analytics run:\n' +
177+
' genkit config set analyticsOptOut true\n';
173178
const NOTIFICATION_ACKED = 'analytics_notification';
174179
const CONFIGSTORE_CLIENT_KEY = 'genkit-tools-ga-id';
175180

@@ -217,14 +222,16 @@ function isValidateOnly(): boolean {
217222
return !!process.env['GENKIT_GA_VALIDATE'];
218223
}
219224

220-
// For now, this is default false unless GENKIT_GA_DEBUG or GENKIT_GA_VALIDATE
221-
// are set. Once we have opt-out and we're ready for public preview this will
222-
// get updated.
225+
function isAnalyticsAcknowledged(): boolean {
226+
return configstore.get(NOTIFICATION_ACKED) === true;
227+
}
228+
229+
function isAnalyticsOptedOut(): boolean {
230+
return getUserSettings()[ANALYTICS_OPT_OUT_CONFIG_TAG] === true;
231+
}
232+
223233
function isAnalyticsEnabled(): boolean {
224-
return (
225-
!process.argv.includes('--non-interactive') &&
226-
!getUserSettings()[ANALYTICS_OPT_OUT_CONFIG_TAG]
227-
);
234+
return isAnalyticsAcknowledged() && !isAnalyticsOptedOut();
228235
}
229236

230237
async function recordInternal(

0 commit comments

Comments
 (0)