Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 06cf128

Browse files
adding cdktf version to the terraform user agent (#321)
1 parent 267d2b1 commit 06cf128

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

packages/cdktf-cli/bin/cmds/ui/models/terraform.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path';
2-
import { exec } from '../../../../lib/util'
2+
import { exec, readCDKTFVersion } from '../../../../lib/util'
33

44
const terraformBinaryName = process.env.TERRAFORM_BINARY_NAME || 'terraform'
55

@@ -83,6 +83,12 @@ export class Terraform {
8383
}
8484

8585
public async init(): Promise<void> {
86+
// Read the cdktf version from the 'cdk.tf.json' file
87+
// and set the user agent.
88+
const version = await readCDKTFVersion(this.workdir)
89+
if (version != "") {
90+
process.env.TF_APPEND_USER_AGENT = "cdktf " + version + " (+https://github.com/hashicorp/terraform-cdk)";
91+
}
8692
await exec(terraformBinaryName, ['init'], { cwd: this.workdir, env: process.env })
8793
}
8894

packages/cdktf-cli/lib/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,14 @@ export const exec = async (command: string, args: string[], options: SpawnOption
7070
});
7171
});
7272
}
73+
74+
export async function readCDKTFVersion(outputDir: string): Promise<string> {
75+
const outputFile = path.join(outputDir, "cdk.tf.json");
76+
if (fs.existsSync(outputFile)) {
77+
const outputJSON = fs.readFileSync(outputFile, "utf8");
78+
const data = JSON.parse(outputJSON);
79+
return data["//"].metadata.version
80+
}
81+
82+
return ""
83+
}

0 commit comments

Comments
 (0)