Skip to content

Commit

Permalink
Add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Jun 23, 2022
1 parent 7f2d31c commit 7711fe6
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ pip install cdktg
```

## How to use
Initialize a project:

```bash
mkdir threagile
cd threagile
cdktg init
```

### Threat Model written in typescript:
```typescript
Expand Down Expand Up @@ -127,15 +134,16 @@ The examples can be used with the [threagile playground](https://run.threagile.i
cdktg [command]

Commands:
cdktg init create a new cdk-threagile project
cdktg synth <filename> synthesize the models
cdktg ping ping the api
cdktg check check the models
cdktg analyze analyze the models
cdktg completion generate completion script

Options:
--help Show help [boolean]
--version Show version number [boolean]
--help Show help [boolean]
--version Show version number [boolean]
```
### Analyze outputs:
```sh
Expand Down
2 changes: 2 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as yargs from "yargs";

import { AnalyzeCommand } from "./analyze";
import { CheckCommand } from "./check";
import { InitCommand } from "./init";
import { PingCommand } from "./ping";
import { SynthCommand } from "./synth";

Expand All @@ -11,6 +12,7 @@ async function main() {

const ya = yargs;

ya.command(new InitCommand());
ya.command(new SynthCommand());
ya.command(new PingCommand());
ya.command(new CheckCommand());
Expand Down
121 changes: 121 additions & 0 deletions src/cli/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import * as path from "path";
import * as fs from "fs-extra";
import { CommandModule, Arguments, Argv, Options } from "yargs";

interface InitOptions extends Options {
language: string;
}

export class InitCommand<U extends InitOptions>
implements CommandModule<{}, U>
{
public command = "init";
public describe = "create a new cdk-threagile project";

builder = (args: Argv): Argv<U> => {
args.option("language", {
type: "string",
describe: "",
default: "typescript",
});

return args as unknown as Argv<U>;
};

public handler = async (args: Arguments<U>) => {
const gitIgnoreContent = [
"node_modules",
".env",
".venv",
"dist",
".cdktg.out",
".DS_Store",
];
const requirementsTxtContent = ["constructs>=10.0.0", "cdktg>=0.0.24"];
const envContent = "CDKTG_THREAGILE_BASE_URL=";

fs.writeFileSync(".env", envContent);

fs.writeFileSync(".gitignore", gitIgnoreContent.join("\n"));

if (args.language === "python") {
fs.writeFileSync("requirements.txt", requirementsTxtContent.join("\n"));

fs.writeJSONSync("package.json", createPackageJson(args.language), {
spaces: 4,
});

fs.writeFileSync(
"threagile.py",
`import cdktg
project = cdktg.Project()
# Define threat model here
project.synth()
`
);

return;
}

fs.writeJSONSync("package.json", createPackageJson(args.language), {
spaces: 4,
});

fs.writeFileSync(
"threagile.ts",
`import { Project } from 'cdktg'
const project = new Project();
// Define threat model here
project.synth();
`
);
};
}

interface CdktgPackageJson {
version: string;
scripts: Record<string, string>;
peerDependencies: Record<string, string>;
dependencies: Record<string, string>;
devDependencies: Record<string, string>;
}

function createPackageJson(language: string) {
const cdktgJson = fs.readJsonSync(
path.join(__dirname, "..", "..", "package.json")
) as CdktgPackageJson;

const scripts = {
cdktg: "cdktg",
synth: "cdktg synth threagile.ts",
ping: "cdktg ping",
"model:check": "cdktg check",
"model:analyze": "cdktg analyze",
};

if (language === "python") {
return {
scripts,
devDependencies: {
cdktg: `^${cdktgJson.version}`,
},
};
}

return {
scripts,
dependencies: {
cdktg: `^${cdktgJson.version}`,
constructs: cdktgJson.peerDependencies.constructs,
},
devDependencies: {
typescript: cdktgJson.devDependencies.typescript,
},
};
}

0 comments on commit 7711fe6

Please sign in to comment.