Skip to content

Commit 02eadd7

Browse files
committed
feat: add download command for environment variables with JSON output
1 parent 902c33e commit 02eadd7

4 files changed

Lines changed: 100 additions & 0 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

src/core/command-runner.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,57 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
613613
fullCommandName: command.fullCommandName,
614614
data: responseData,
615615
});
616+
} else if(command.fullCommandName === `${PROGRAM_NAME}-build-variable-download`){
617+
const spinner = createOra('Downloading environment variables...').start();
618+
try {
619+
const variableGroups = await getEnvironmentVariableGroups();
620+
const variableGroup = variableGroups.find((group: any) => group.id === params.variableGroupId);
621+
622+
if (!variableGroup) {
623+
spinner.fail(`Variable group with ID ${params.variableGroupId} not found`);
624+
throw new Error(`Variable group not found`);
625+
}
626+
627+
const responseData = await getEnvironmentVariables(params);
628+
629+
let formattedVariables = responseData.map((variable: any) => ({
630+
key: variable.key,
631+
value: variable.value,
632+
isSecret: variable.isSecret,
633+
isFile: variable.isFile || false,
634+
id: variable.key
635+
}));
636+
637+
formattedVariables.sort((a: any, b: any) => {
638+
const aKey = a.key;
639+
const bKey = b.key;
640+
return bKey.localeCompare(aKey);
641+
});
642+
643+
const timestamp = Date.now();
644+
const fileName = `${variableGroup.name}_${timestamp}.json`;
645+
let filePath = params.path || process.cwd();
646+
647+
if (filePath.includes('~')) {
648+
filePath = filePath.replace(/~/g, os.homedir());
649+
}
650+
651+
filePath = path.resolve(filePath);
652+
653+
if (!fs.existsSync(filePath)) {
654+
fs.mkdirSync(filePath, { recursive: true });
655+
}
656+
657+
if (fs.statSync(filePath).isDirectory()) {
658+
filePath = path.join(filePath, fileName);
659+
}
660+
661+
fs.writeFileSync(filePath, JSON.stringify(formattedVariables));
662+
spinner.succeed(`Environment variables downloaded successfully to ${filePath}`);
663+
} catch (e) {
664+
spinner.fail('Failed to download environment variables');
665+
throw e;
666+
}
616667
} else if(command.fullCommandName === `${PROGRAM_NAME}-build-variable-create`){
617668
const spinner = createOra('Creating environment variable').start();
618669
try {

src/core/commands.ts

Lines changed: 20 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: []

0 commit comments

Comments
 (0)