Skip to content

Commit c51cdd3

Browse files
authored
Merge pull request #61 from appcircleio/feature/be-6106-download
#BE-6106 feat: add download command for environment variables with JSON output
2 parents 629404e + 27b8fb6 commit c51cdd3

6 files changed

Lines changed: 206 additions & 2 deletions

File tree

docs/build/variable/download.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `appcircle build variable download`
2+
3+
Download environment variables as JSON
4+
5+
```plaintext
6+
appcircle build variable download [options]
7+
```
8+
9+
## Examples
10+
11+
```plaintext
12+
$ appcircle build variable download --variableGroupId "Variable Group ID"
13+
14+
$ appcircle build variable download --variableGroupId "Variable Group ID" --path "/path/to/save"
15+
```
16+
17+
## Options
18+
19+
```plaintext
20+
--variableGroupId <uuid> Variable Groups ID
21+
--path <string> [OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)
22+
```
23+
24+
## Options inherited from parent commands
25+
26+
```plaintext
27+
--help Show help for command
28+
```

docs/build/variable/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ appcircle build variable [command] [options]
1717
- [`group`](group/index.md)
1818
- [`create`](create.md)
1919
- [`view`](view.md)
20+
- [`download`](download.md)
2021

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `appcircle publish variable group download`
2+
3+
Download publish environment variables as JSON
4+
5+
```plaintext
6+
appcircle publish variable group download [options]
7+
```
8+
9+
## Examples
10+
11+
```plaintext
12+
$ appcircle publish variable group download --publishVariableGroupId "Variable Group ID"
13+
14+
$ appcircle publish variable group download --publishVariableGroupId "Variable Group ID" --path "/path/to/save"
15+
```
16+
17+
## Options
18+
19+
```plaintext
20+
--publishVariableGroupId <uuid> Variable Group ID
21+
--path <string> [OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)
22+
```
23+
24+
## Options inherited from parent commands
25+
26+
```plaintext
27+
--help Show help for command
28+
```

docs/publish/variable/group/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ appcircle publish profile variable group [command] [options]
1616

1717
- [`list`](list.md)
1818
- [`view`](view.md)
19+
- [`download`](download.md)
1920

src/core/command-runner.ts

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,60 @@ const handlePublishCommand = async (command: ProgramCommand, params: any) => {
453453
fullCommandName: command.fullCommandName,
454454
data: variables.variables,
455455
});
456-
}else if(command.fullCommandName === `${PROGRAM_NAME}-publish-profile-version-list`){
456+
} else if (command.fullCommandName === `${PROGRAM_NAME}-publish-variable-group-download`) {
457+
const spinner = createOra('Downloading publish environment variables...').start();
458+
try {
459+
const variableGroups = await getPublishVariableGroups();
460+
const variableGroup = variableGroups.find((group: any) => group.id === params.publishVariableGroupId);
461+
462+
if (!variableGroup) {
463+
spinner.fail(`Variable group with ID ${params.publishVariableGroupId} not found`);
464+
throw new Error(`Variable group not found`);
465+
}
466+
467+
const variables = await getPublishVariableListByGroupId(params);
468+
469+
let formattedVariables = variables.variables.map((variable: any) => ({
470+
key: variable.key,
471+
value: variable.value,
472+
isSecret: variable.isSecret,
473+
isFile: variable.isFile || false,
474+
id: variable.key
475+
}));
476+
477+
formattedVariables.sort((a: any, b: any) => {
478+
const aKey = a.key;
479+
const bKey = b.key;
480+
return bKey.localeCompare(aKey);
481+
});
482+
483+
const timestamp = Date.now();
484+
const fileName = `${variableGroup.name}_${timestamp}.json`;
485+
486+
let filePath = params.path || process.cwd();
487+
488+
if (filePath.includes('~')) {
489+
filePath = filePath.replace(/~/g, os.homedir());
490+
}
491+
492+
filePath = path.resolve(filePath);
493+
494+
if (!fs.existsSync(filePath)) {
495+
fs.mkdirSync(filePath, { recursive: true });
496+
}
497+
498+
if (fs.statSync(filePath).isDirectory()) {
499+
filePath = path.join(filePath, fileName);
500+
}
501+
502+
fs.writeFileSync(filePath, JSON.stringify(formattedVariables));
503+
504+
spinner.succeed(`Publish environment variables downloaded successfully to ${filePath}`);
505+
} catch (e) {
506+
spinner.fail('Failed to download publish environment variables');
507+
throw e;
508+
}
509+
} else if(command.fullCommandName === `${PROGRAM_NAME}-publish-profile-version-list`){
457510
const spinner = createOra('Fetching...').start();
458511
const appVersions = await getAppVersions(params);
459512
spinner.succeed();
@@ -532,7 +585,7 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
532585
fullCommandName: command.fullCommandName,
533586
data: responseData,
534587
});
535-
}else if(command.fullCommandName === `${PROGRAM_NAME}-build-profile-branch-list`) {
588+
}else if(command.fullCommandName === `${PROGRAM_NAME}-build-profile-branch-list`){
536589
const spinner = createOra('Fetching...').start();
537590
const responseData = await getBranches(params);
538591
spinner.succeed();
@@ -624,6 +677,57 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
624677
fullCommandName: command.fullCommandName,
625678
data: responseData,
626679
});
680+
} else if(command.fullCommandName === `${PROGRAM_NAME}-build-variable-download`){
681+
const spinner = createOra('Downloading environment variables...').start();
682+
try {
683+
const variableGroups = await getEnvironmentVariableGroups();
684+
const variableGroup = variableGroups.find((group: any) => group.id === params.variableGroupId);
685+
686+
if (!variableGroup) {
687+
spinner.fail(`Variable group with ID ${params.variableGroupId} not found`);
688+
throw new Error(`Variable group not found`);
689+
}
690+
691+
const responseData = await getEnvironmentVariables(params);
692+
693+
let formattedVariables = responseData.map((variable: any) => ({
694+
key: variable.key,
695+
value: variable.value,
696+
isSecret: variable.isSecret,
697+
isFile: variable.isFile || false,
698+
id: variable.key
699+
}));
700+
701+
formattedVariables.sort((a: any, b: any) => {
702+
const aKey = a.key;
703+
const bKey = b.key;
704+
return bKey.localeCompare(aKey);
705+
});
706+
707+
const timestamp = Date.now();
708+
const fileName = `${variableGroup.name}_${timestamp}.json`;
709+
let filePath = params.path || process.cwd();
710+
711+
if (filePath.includes('~')) {
712+
filePath = filePath.replace(/~/g, os.homedir());
713+
}
714+
715+
filePath = path.resolve(filePath);
716+
717+
if (!fs.existsSync(filePath)) {
718+
fs.mkdirSync(filePath, { recursive: true });
719+
}
720+
721+
if (fs.statSync(filePath).isDirectory()) {
722+
filePath = path.join(filePath, fileName);
723+
}
724+
725+
fs.writeFileSync(filePath, JSON.stringify(formattedVariables));
726+
spinner.succeed(`Environment variables downloaded successfully to ${filePath}`);
727+
} catch (e) {
728+
spinner.fail('Failed to download environment variables');
729+
throw e;
730+
}
627731
} else if(command.fullCommandName === `${PROGRAM_NAME}-build-variable-create`){
628732
const spinner = createOra('Creating environment variable').start();
629733
try {

src/core/commands.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,26 @@ export const Commands: CommandType[] = [
547547
valueType: 'uuid',
548548
},
549549
],
550+
},
551+
{
552+
command: "download",
553+
description: 'Download environment variables as JSON',
554+
params: [
555+
{
556+
name: 'variableGroupId',
557+
description: 'Variable Groups ID',
558+
type: CommandParameterTypes.SELECT,
559+
valueType: 'uuid',
560+
},
561+
{
562+
name: 'path',
563+
description: '[OPTIONAL] The path for JSON file to be downloaded',
564+
longDescription:'[OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)',
565+
type: CommandParameterTypes.STRING,
566+
valueType: 'string',
567+
required: false,
568+
}
569+
],
550570
}
551571
],
552572
params: []
@@ -1461,6 +1481,28 @@ export const Commands: CommandType[] = [
14611481
required: true
14621482
}
14631483
],
1484+
},
1485+
{
1486+
command: "download",
1487+
description: 'Download publish environment variables as JSON',
1488+
longDescription: 'Download publish environment variables as JSON file',
1489+
params: [
1490+
{
1491+
name: 'publishVariableGroupId',
1492+
description: 'Variable Group ID',
1493+
type: CommandParameterTypes.SELECT,
1494+
valueType: 'uuid',
1495+
required: true
1496+
},
1497+
{
1498+
name: 'path',
1499+
description: '[OPTIONAL] The path for JSON file to be downloaded',
1500+
longDescription:'[OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)',
1501+
type: CommandParameterTypes.STRING,
1502+
valueType: 'string',
1503+
required: false,
1504+
}
1505+
],
14641506
}
14651507
]
14661508
}

0 commit comments

Comments
 (0)