Skip to content

Commit b337678

Browse files
committed
fix(cli): honor --endpoint passed on the command line
Every subcommand reads the endpoint from `inputs.props.endpoint`, which is only populated from s.yaml. In `s cli` mode there is no yaml, so `--endpoint` was parsed into argv and then silently ignored: `s cli fc3 list --endpoint http://...` still went to the public `fcv3.<region>.aliyuncs.com` instead of the requested cluster, and only FC_CLIENT_CUSTOM_ENDPOINT worked. handlePreRun now reads --endpoint from argv and writes it into props.endpoint, so the command line takes precedence over yaml. The option is documented in the `list` help text.
1 parent 0120bc0 commit b337678

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

__tests__/ut/core/base_test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ describe('Base', () => {
8686
// Logger is mocked, so we can't verify specific calls
8787
});
8888

89+
it('should take endpoint from command line args', async () => {
90+
mockInputs.args = ['--endpoint', 'http://127.0.0.1:8080'];
91+
92+
await base.handlePreRun(mockInputs, false);
93+
94+
expect(mockInputs.props.endpoint).toBe('http://127.0.0.1:8080');
95+
});
96+
97+
it('should let command line endpoint win over yaml props', async () => {
98+
mockInputs.props.endpoint = 'https://fcv3.cn-hangzhou.aliyuncs.com';
99+
mockInputs.args = ['--endpoint', 'http://127.0.0.1:8080'];
100+
101+
await base.handlePreRun(mockInputs, false);
102+
103+
expect(mockInputs.props.endpoint).toBe('http://127.0.0.1:8080');
104+
});
105+
106+
it('should keep yaml endpoint when no endpoint arg is given', async () => {
107+
mockInputs.props.endpoint = 'https://fcv3.cn-hangzhou.aliyuncs.com';
108+
mockInputs.args = [];
109+
110+
await base.handlePreRun(mockInputs, false);
111+
112+
expect(mockInputs.props.endpoint).toBe('https://fcv3.cn-hangzhou.aliyuncs.com');
113+
});
114+
89115
it('should trim image whitespace for custom container', async () => {
90116
mockInputs.props.customContainerConfig = {
91117
image: ' test-image:latest ',

src/base.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable require-atomic-updates */
33
/* eslint-disable no-await-in-loop */
44
import _ from 'lodash';
5+
import { parseArgv } from '@serverless-devs/utils';
56
import { IInputs, INasConfig } from './interface';
67
// eslint-disable-next-line @typescript-eslint/no-shadow
78
import log from './logger';
@@ -30,6 +31,13 @@ export default class Base {
3031
// 在运行方法之前运行
3132
async handlePreRun(inputs: IInputs, needCredential: boolean) {
3233
log._set(this.logger);
34+
// --endpoint 只出现在命令行参数里(yaml 模式下走 props.endpoint),
35+
// s cli 模式没有 yaml,必须从 argv 取,命令行优先级高于 yaml
36+
const argvEndpoint = _.get(parseArgv(inputs.args || [], { string: ['endpoint'] }), 'endpoint');
37+
if (!_.isEmpty(argvEndpoint)) {
38+
log.debug(`use endpoint from command line: ${argvEndpoint}`);
39+
_.set(inputs, 'props.endpoint', argvEndpoint);
40+
}
3341
// fc组件镜像 trim 左右空格
3442
const image = _.get(inputs, 'props.customContainerConfig.image');
3543
if (!_.isEmpty(image)) {

src/commands-help/list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Example:
2121
'[Optional] Specify the next token for pagination, only works with --limit',
2222
],
2323
['--table', '[Optional] Specify if output the result as table format'],
24+
['--endpoint <endpoint>', '[Optional] Specify the fc endpoint, e.g. http://192.168.1.1:8080'],
2425
],
2526
},
2627
};

0 commit comments

Comments
 (0)