Skip to content

Commit

Permalink
Fix CLI formatting on windows (#119435)
Browse files Browse the repository at this point in the history
* Provide more guidance on enrollment token

* Added suggestions from copy review

* Simplify copy

* format bash commands correctly

* Added suggestions from code review
  • Loading branch information
thomheymann authored Nov 23, 2021
1 parent 94136d6 commit f58cd5c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { euiThemeVars } from '@kbn/ui-shared-deps-src/theme';

import type { Certificate } from '../common';
import { DocLink } from './doc_link';
import { getCommandLineSnippet } from './get_command_line_snippet';
import { SubmitErrorCallout } from './submit_error_callout';
import { TextTruncate } from './text_truncate';
import type { ValidationErrors } from './use_form';
Expand Down Expand Up @@ -511,7 +512,7 @@ export const ForgotPasswordPopover: FunctionComponent<ForgotPasswordPopoverProps
/>
</p>
<EuiCodeBlock language="bash" paddingSize="m" isCopyable>
bin/elasticsearch-reset-password --username {username}
{getCommandLineSnippet('elasticsearch-reset-password', `--username ${username}`)}
</EuiCodeBlock>
</EuiText>
</EuiPopover>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { FormattedMessage } from '@kbn/i18n/react';

import type { EnrollmentToken } from '../common';
import { DocLink } from './doc_link';
import { getCommandLineSnippet } from './get_command_line_snippet';
import { SubmitErrorCallout } from './submit_error_callout';
import { TextTruncate } from './text_truncate';
import type { ValidationErrors } from './use_form';
Expand Down Expand Up @@ -260,7 +261,7 @@ export const EnrollmentTokenHelpPopover = () => {
/>
</p>
<EuiCodeBlock language="bash" paddingSize="m" isCopyable>
bin/elasticsearch-create-enrollment-token --scope kibana
{getCommandLineSnippet('elasticsearch-create-enrollment-token', '--scope kibana')}
</EuiCodeBlock>
</EuiText>
<EuiPopoverFooter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { getCommandLineSnippet } from './get_command_line_snippet';

describe('getCommandLineSnippet', () => {
const originalNavigator = window.navigator;

it('should format windows correctly', () => {
Object.defineProperty(window, 'navigator', {
value: { userAgent: 'Windows' },
writable: true,
});
expect(getCommandLineSnippet('kibana')).toEqual('bin\\kibana.bat');
expect(getCommandLineSnippet('kibana', '--silent')).toEqual('bin\\kibana.bat --silent');
});

it('should format unix correctly', () => {
Object.defineProperty(window, 'navigator', {
value: { userAgent: 'Linux' },
writable: true,
});
expect(getCommandLineSnippet('kibana')).toEqual('bin/kibana');
expect(getCommandLineSnippet('kibana', '--silent')).toEqual('bin/kibana --silent');
});

afterAll(function () {
Object.defineProperty(window, 'navigator', {
value: originalNavigator,
writable: true,
});
});
});
12 changes: 12 additions & 0 deletions src/plugins/interactive_setup/public/get_command_line_snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export function getCommandLineSnippet(command: string, args?: string) {
const isWindows = window.navigator.userAgent.includes('Win');
return `${isWindows ? `bin\\${command}.bat` : `bin/${command}`}${args ? ` ${args}` : ''}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FormattedMessage } from '@kbn/i18n/react';
import type { IHttpFetchError, ResponseErrorBody } from 'kibana/public';

import { VERIFICATION_CODE_LENGTH } from '../common';
import { getCommandLineSnippet } from './get_command_line_snippet';
import { SingleCharsField } from './single_chars_field';
import { SubmitErrorCallout } from './submit_error_callout';
import type { ValidationErrors } from './use_form';
Expand Down Expand Up @@ -118,9 +119,7 @@ export const VerificationCodeForm: FunctionComponent<VerificationCodeFormProps>
values={{
command: (
<EuiCode language="bash">
{window.navigator.userAgent.includes('Win')
? 'bin\\kibana-verification-code.bat'
: 'bin/kibana-verification-code'}
{getCommandLineSnippet('kibana-verification-code')}
</EuiCode>
),
}}
Expand Down

0 comments on commit f58cd5c

Please sign in to comment.