Skip to content

Commit 0f49c0a

Browse files
authored
Merge pull request #43 from eco-infra/feat/update-cli-to-handle-state-and-plan
feat: update cli to handle state and plan flows
2 parents 9fbf32a + 6abab90 commit 0f49c0a

File tree

9 files changed

+546
-169
lines changed

9 files changed

+546
-169
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,34 @@ Run the tool in a supported CI environment
3232
[GitHub Actions](https://github.com/marketplace/actions/eco-infra-action)
3333
```yaml
3434
- name: Eco-Infra
35-
uses: ecoinfra/ecoinfra-action@v1.1.2
35+
uses: ecoinfra/ecoinfra-action@v1.2.0
3636
with:
3737
token: 'TOKEN'
3838
project-name: 'my-project'
39-
plan-file: './terraform'
39+
file: './terraform/state.json'
4040
```
4141
4242
Run the tool from your command line interface (CLI) or terminal.
4343
4444
```bash
45-
$ ecoinfra-PLATFORM --token {{Token}} --project-name {{Unique Project Name}} --plan-file {{Plan JSON File}}
45+
$ ecoinfra-PLATFORM --token {{Token}} --project-name {{Unique Project Name}} --file {{Plan JSON File}}
4646
```
4747

4848
Example:
4949

5050
```bash
51-
# Generate the Terraform plan JSON file
51+
# Generate the Terraform JSON file
5252
$ terraform plan -out=plan.out
53+
# get the plan json
5354
$ terraform show -json plan.out > plan.json
55+
# or state json
56+
$ terraform show -json terraform.tftate > state.json
5457

55-
# Analyze with ecoinfra
56-
$ ecoinfra-PLATFORM --token c3dc55b6-78a0-43ad-2513-a751e76553de --project-name "Production Account" --plan-file plan.json
58+
# Analyze plan with ecoinfra
59+
$ ecoinfra-PLATFORM --token c3dc55b6-78a0-43ad-2513-a751e76553de --project-name "Production Account" --file plan.json
60+
61+
# Analyze state with ecoinfra
62+
$ ecoinfra-PLATFORM --token c3dc55b6-78a0-43ad-2513-a751e76553de --project-name "Production Account" --file state.json
5763
```
5864
---
5965
## 📖 Documentation
@@ -62,7 +68,7 @@ $ ecoinfra-PLATFORM --token c3dc55b6-78a0-43ad-2513-a751e76553de --project-name
6268

6369
- `--token` - Your unique API key.
6470
- `--project-name` - A unique name for your project.
65-
- `--plan-file` - Path to the Terraform plan JSON file.
71+
- `-fill` - Path to the Terraform plan JSON file.
6672

6773
### Optional Parameters
6874

@@ -105,7 +111,7 @@ terraform plan -out=plan.out
105111
terraform show -json plan.out > plan.json
106112

107113
# Run the tool
108-
./build/ecoinfra-PLATFORM --token {{Token}} --project-name {{Unique Project Name}} --plan-file plan.json
114+
./build/ecoinfra-PLATFORM --token {{Token}} --project-name {{Unique Project Name}} -fill plan.json
109115
```
110116

111117
# Contributing

app/cli/main.cli.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default class MainCli {
77

88
private projectName: string;
99

10-
private planFile: string;
10+
private inputFile: string;
11+
12+
private fileType: 'plan' | 'state' = 'plan';
1113

1214
public args: arg.Result<any>;
1315

@@ -18,11 +20,27 @@ export default class MainCli {
1820

1921
if (!this.args['--project-name']) throw new Error('Specify project name --project-name');
2022

21-
if (!this.args['--plan-file']) throw new Error('Specify plan file --plan-file <path>');
23+
// Determine input file - prefer new --file, fallback to legacy options
24+
const file = this.args['--file'];
25+
const planFile = this.args['--plan-file'];
26+
const stateFile = this.args['--state-file'];
27+
28+
// Count how many file options are provided
29+
const fileOptions = [file, planFile, stateFile].filter(Boolean);
30+
31+
if (fileOptions.length === 0) {
32+
throw new Error('Specify input file with --file <path> (or legacy --plan-file/--state-file)');
33+
}
34+
35+
if (fileOptions.length > 1) {
36+
throw new Error('Specify only one input file option');
37+
}
38+
39+
// Use whichever file option was provided
40+
this.inputFile = file || planFile || stateFile!;
2241

2342
this.token = this.args['--token'];
2443
this.breakdown = this.args['--breakdown'] ?? false;
25-
this.planFile = this.args['--plan-file'];
2644
this.projectName = this.args['--project-name'];
2745
}
2846

@@ -38,7 +56,24 @@ export default class MainCli {
3856
return this.breakdown;
3957
}
4058

41-
getPlanFile(): string {
42-
return this.planFile;
59+
getInputFile(): string {
60+
return this.inputFile;
61+
}
62+
63+
// Legacy methods for backward compatibility
64+
getPlanFile(): string | null {
65+
return this.args['--plan-file'] || null;
66+
}
67+
68+
getStateFile(): string | null {
69+
return this.args['--state-file'] || null;
70+
}
71+
72+
getFileType(): 'plan' | 'state' {
73+
return this.fileType;
74+
}
75+
76+
setFileType(fileType: 'plan' | 'state') {
77+
this.fileType = fileType;
4378
}
4479
}

0 commit comments

Comments
 (0)