Skip to content

Commit e38366c

Browse files
committed
feat: Add file and action resources
1 parent 4b59fcc commit e38366c

7 files changed

Lines changed: 313 additions & 1 deletion

File tree

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { JenvResource } from './resources/java/jenv/jenv.js';
1616
import { NvmResource } from './resources/node/nvm/nvm.js';
1717
import { PgcliResource } from './resources/pgcli/pgcli.js';
1818
import { PyenvResource } from './resources/python/pyenv/pyenv.js';
19+
import { ActionResource } from './resources/scripting/action.js';
20+
import { FileResource } from './resources/scripting/file.js';
1921
import { AliasResource } from './resources/shell/alias/alias-resource.js';
2022
import { PathResource } from './resources/shell/path/path-resource.js';
2123
import { SshAddResource } from './resources/ssh/ssh-add.js';
@@ -51,6 +53,8 @@ runPlugin(Plugin.create(
5153
new AsdfInstallResource(),
5254
new SshKeyResource(),
5355
new SshConfigFileResource(),
54-
new SshAddResource()
56+
new SshAddResource(),
57+
new ActionResource(),
58+
new FileResource()
5559
])
5660
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "https://www.codifycli.com/action.json",
4+
"title": "Action resource",
5+
"type": "object",
6+
"properties": {
7+
"condition": {
8+
"type": "string"
9+
},
10+
"action": {
11+
"type": "string"
12+
},
13+
"cwd": {
14+
"type": "string"
15+
}
16+
},
17+
"required": ["action"],
18+
"additionalProperties": false
19+
}

src/resources/scripting/action.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { CreatePlan, DestroyPlan, Resource, ResourceSettings } from 'codify-plugin-lib';
2+
import { StringIndexedObject } from 'codify-schemas';
3+
4+
import { SpawnStatus, codifySpawn } from '../../utils/codify-spawn.js';
5+
import schema from './action-schema.json'
6+
7+
export interface ActionConfig extends StringIndexedObject {
8+
condition?: string;
9+
action: string;
10+
cwd?: string;
11+
}
12+
13+
export class ActionResource extends Resource<ActionConfig> {
14+
15+
getSettings(): ResourceSettings<ActionConfig> {
16+
return {
17+
id: 'action',
18+
schema,
19+
parameterSettings: {
20+
cwd: { type: 'directory' },
21+
}
22+
}
23+
}
24+
25+
async refresh(parameters: Partial<ActionConfig>): Promise<Partial<ActionConfig> | Partial<ActionConfig>[] | null> {
26+
// Always run if condition doesn't exist
27+
// TODO: Remove hack. Right now we're returning null to simulate CREATE and a value for NO-OP
28+
if (!parameters.condition) {
29+
return null;
30+
}
31+
32+
const { condition, action, cwd } = parameters;
33+
const { status } = await codifySpawn(condition, { throws: false, cwd: cwd ?? undefined });
34+
35+
return status === SpawnStatus.ERROR
36+
? null
37+
: {
38+
...(condition ? { condition } : undefined),
39+
...(action ? { action } : undefined),
40+
...(cwd ? { cwd } : undefined),
41+
};
42+
}
43+
44+
async create(plan: CreatePlan<ActionConfig>): Promise<void> {
45+
await codifySpawn(plan.desiredConfig.action, { cwd: plan.desiredConfig.cwd ?? undefined });
46+
}
47+
48+
async destroy(plan: DestroyPlan<ActionConfig>): Promise<void> {}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "https://www.codifycli.com/file.json",
4+
"title": "File resource",
5+
"type": "object",
6+
"properties": {
7+
"path": {
8+
"type": "string"
9+
},
10+
"contents": {
11+
"type": "string"
12+
},
13+
"onlyCreate": {
14+
"type": "boolean"
15+
}
16+
},
17+
"required": ["path", "contents"],
18+
"additionalProperties": false
19+
}

src/resources/scripting/file.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { CreatePlan, DestroyPlan, ModifyPlan, ParameterChange, Resource, ResourceSettings } from 'codify-plugin-lib';
2+
import { StringIndexedObject } from 'codify-schemas';
3+
import fs from 'node:fs/promises';
4+
import path from 'node:path';
5+
6+
import { FileUtils } from '../../utils/file-utils.js';
7+
import schema from './file-schema.json'
8+
9+
export interface FileConfig extends StringIndexedObject {
10+
path: string;
11+
contents: string;
12+
onlyCreate: boolean;
13+
}
14+
15+
export class FileResource extends Resource<FileConfig> {
16+
getSettings(): ResourceSettings<FileConfig> {
17+
return {
18+
id: 'file',
19+
schema,
20+
parameterSettings: {
21+
path: { type: 'directory' },
22+
contents: { canModify: true },
23+
onlyCreate: { type: 'boolean' }
24+
},
25+
import: {
26+
requiredParameters: ['path']
27+
}
28+
}
29+
}
30+
31+
async refresh(parameters: Partial<FileConfig>): Promise<Partial<FileConfig> | Partial<FileConfig>[] | null> {
32+
const filePath = parameters.path!;
33+
34+
if (!(await FileUtils.exists(filePath))) {
35+
return null;
36+
}
37+
38+
const isFile = (await fs.lstat(filePath)).isFile()
39+
if (!isFile) {
40+
throw new Error(`A directory exists at ${filePath}`)
41+
}
42+
43+
// If we only care that the contents of the file is created then there's no point checking the contents
44+
const { onlyCreate } = parameters;
45+
if (onlyCreate) {
46+
return parameters;
47+
}
48+
49+
const contents = (await fs.readFile(filePath)).toString('utf8');
50+
return {
51+
path: parameters.path,
52+
contents,
53+
onlyCreate,
54+
}
55+
}
56+
57+
async create(plan: CreatePlan<FileConfig>): Promise<void> {
58+
const { contents, path } = plan.desiredConfig;
59+
60+
await fs.writeFile(path, contents, 'utf8');
61+
}
62+
63+
async modify(pc: ParameterChange<FileConfig>, plan: ModifyPlan<FileConfig>): Promise<void> {
64+
const filePath = path.resolve(plan.desiredConfig.path!);
65+
const { contents } = plan.desiredConfig;
66+
67+
await fs.writeFile(filePath, contents, 'utf8');
68+
}
69+
70+
async destroy(plan: DestroyPlan<FileConfig>): Promise<void> {
71+
await fs.rm(path.resolve(plan.currentConfig.path));
72+
}
73+
}

test/scripting/action.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { PluginTester } from 'codify-plugin-test';
3+
import path from 'node:path';
4+
import { ResourceOperation } from 'codify-schemas';
5+
import fs from 'node:fs';
6+
import os from 'node:os';
7+
8+
describe('Action tests', () => {
9+
let plugin: PluginTester;
10+
11+
beforeEach(() => {
12+
plugin = new PluginTester(path.resolve('./src/index.ts'));
13+
})
14+
15+
it('Can run an action if the condition returns as non-zero', { timeout: 300000 }, async () => {
16+
await plugin.fullTest([
17+
{ type: 'action', condition: '[ -d ~/tmp ]', action: 'mkdir ~/tmp; touch ~/tmp/testFile' }
18+
], {
19+
skipUninstall: true,
20+
validateApply: (plans) => {
21+
expect(plans[0]).toMatchObject({
22+
operation: ResourceOperation.CREATE,
23+
})
24+
25+
const dir = fs.readdirSync(path.resolve(os.homedir(), 'tmp'))
26+
expect(dir[0]).to.eq('testFile')
27+
}
28+
})
29+
})
30+
31+
it('It will return NO-OP when the return is 0', { timeout: 300000 }, async () => {
32+
await plugin.fullTest([
33+
{ type: 'action', condition: 'exit 0;', action: 'mkdir ~/tmp; touch ~/tmp/testFile' }
34+
], {
35+
skipUninstall: true,
36+
validatePlan: (plans) => {
37+
expect(plans[0]).toMatchObject({
38+
operation: ResourceOperation.NOOP,
39+
})
40+
}
41+
})
42+
})
43+
44+
it('It can use the cwd parameter to run all commands from a specific directory', { timeout: 300000 }, async () => {
45+
fs.mkdirSync(path.resolve(os.homedir(), 'tmp2'))
46+
await plugin.fullTest([
47+
{ type: 'action', condition: '[ -e testFile ]', action: 'touch testFile', cwd: '~/tmp2' }
48+
], {
49+
skipUninstall: true,
50+
validatePlan: (plans) => {
51+
expect(plans[0]).toMatchObject({
52+
operation: ResourceOperation.CREATE,
53+
})
54+
}
55+
})
56+
57+
expect(fs.existsSync(path.resolve(os.homedir(), 'tmp2', 'testFile'))).to.be.true;
58+
59+
await plugin.fullTest([
60+
{ type: 'action', condition: '[ -e testFile ]', action: 'touch testFile', cwd: '~/tmp2' }
61+
], {
62+
skipUninstall: true,
63+
validatePlan: (plans) => {
64+
expect(plans[0]).toMatchObject({
65+
operation: ResourceOperation.NOOP,
66+
})
67+
}
68+
})
69+
})
70+
})

test/scripting/file.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { PluginTester } from 'codify-plugin-test';
3+
import path from 'node:path';
4+
import { ResourceOperation } from 'codify-schemas';
5+
import fs from 'node:fs';
6+
import os from 'node:os';
7+
8+
describe('File resource tests', () => {
9+
let plugin: PluginTester;
10+
11+
beforeEach(() => {
12+
plugin = new PluginTester(path.resolve('./src/index.ts'));
13+
})
14+
15+
it('Can create a file, modify the contents and delete it', { timeout: 300000 }, async () => {
16+
const contents = 'AWS_ACCESS_KEY_ID=\n' +
17+
'AWS_SECRET_ACCESS_KEY=\n' +
18+
'AWS_S3_ENDPOINT=\n' +
19+
'AWS_REGION=\n';
20+
21+
await plugin.fullTest([
22+
{ type: 'file',
23+
path: '~/.env',
24+
contents
25+
}
26+
], {
27+
validateApply: (plans) => {
28+
expect(plans[0]).toMatchObject({
29+
operation: ResourceOperation.CREATE,
30+
})
31+
32+
expect(fs.readFileSync(path.resolve(os.homedir(), '.env'), 'utf-8')).to.eq(contents);
33+
},
34+
testModify: {
35+
modifiedConfigs: [{
36+
type: 'file',
37+
path: '~/.env',
38+
contents: 'testing\ntest',
39+
}],
40+
validateModify: (plans) => {
41+
expect(plans[0]).toMatchObject({
42+
operation: ResourceOperation.MODIFY,
43+
})
44+
expect(fs.readFileSync(path.resolve(os.homedir(), '.env'), 'utf-8')).to.eq('testing\ntest');
45+
}
46+
},
47+
validateDestroy: () => {
48+
expect(fs.existsSync(path.resolve(os.homedir(), '.env'))).to.be.false;
49+
}
50+
})
51+
})
52+
53+
it('Will throw an error if the path given is a directory', { timeout: 300000 }, async () => {
54+
fs.mkdirSync(path.resolve(os.homedir(), 'tmp'))
55+
56+
await expect(async () => plugin.fullTest([
57+
{ type: 'file', path: '~/tmp', contents: 'anything' }
58+
])).rejects.toThrow();
59+
})
60+
61+
it('Will not modify a file if already created and onlyCreate is set to true', { timeout: 300000 }, async () => {
62+
const filePath = path.resolve(os.homedir(), 'testFile');
63+
fs.writeFileSync(filePath, 'this is the previous file', 'utf-8')
64+
65+
await plugin.fullTest([
66+
{ type: 'file', path: filePath, contents: 'anything', onlyCreate: true }
67+
], {
68+
skipUninstall: true,
69+
validatePlan(plans) {
70+
expect(plans[0]).toMatchObject({
71+
operation: ResourceOperation.NOOP,
72+
})
73+
}
74+
})
75+
76+
expect(fs.readFileSync(filePath, 'utf-8')).to.eq('this is the previous file');
77+
})
78+
})

0 commit comments

Comments
 (0)