Skip to content

Commit 96bbae9

Browse files
committed
[CODE-16] Add git resource
1 parent efbc10f commit 96bbae9

8 files changed

Lines changed: 145 additions & 2 deletions

File tree

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Plugin, runPlugin } from 'codify-plugin-lib';
33
import { AwsCliResource } from './resources/aws-cli/cli/aws-cli.js';
44
import { AwsProfileResource } from './resources/aws-cli/profile/aws-profile.js';
55
import { GitCloneResource } from './resources/git/clone/git-clone.js';
6+
import { GitResource } from './resources/git/git/git-resource.js';
67
import { GitLfsResource } from './resources/git/lfs/git-lfs.js';
78
import { HomebrewResource } from './resources/homebrew/homebrew.js';
89
import { JenvResource } from './resources/java/jenv/jenv.js';
@@ -18,6 +19,7 @@ import { XcodeToolsResource } from './resources/xcode-tools/xcode-tools.js';
1819
runPlugin(Plugin.create(
1920
'default',
2021
[
22+
new GitResource(),
2123
new XcodeToolsResource(),
2224
new PathResource(),
2325
new AliasResource(),

src/resources/aws-cli/profile/aws-profile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class AwsProfileResource extends Resource<AwsProfileConfig> {
2828
awsAccessKeyId: { modifyOnChange: true },
2929
awsSecretAccessKey: { modifyOnChange: true },
3030
csvCredentials: { transformParameter: new CSVCredentialsParameter() },
31+
output: { default: 'json' },
3132
profile: { default: 'default' },
3233
},
3334
schema: Schema,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { StatefulParameter } from 'codify-plugin-lib';
2+
3+
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
4+
import { GitConfig } from './git-resource.js';
5+
6+
export class GitEmailParameter extends StatefulParameter<GitConfig, string> {
7+
8+
async refresh(): Promise<null | string> {
9+
const { data: email, status } = await codifySpawn('git config --global user.email', { throws: false })
10+
11+
return status === SpawnStatus.ERROR ? null : email.trim()
12+
}
13+
14+
async applyAdd(valueToAdd: string): Promise<void> {
15+
await codifySpawn(`git config --global user.email "${valueToAdd}"`)
16+
}
17+
18+
async applyModify(newValue: string): Promise<void> {
19+
await codifySpawn(`git config --global user.email "${newValue}"`)
20+
}
21+
22+
async applyRemove(): Promise<void> {
23+
await codifySpawn('git config --global --unset user.email')
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { StatefulParameter } from 'codify-plugin-lib';
2+
3+
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
4+
import { GitConfig } from './git-resource.js';
5+
6+
export class GitNameParameter extends StatefulParameter<GitConfig, string> {
7+
8+
async refresh(): Promise<null | string> {
9+
const { data: email, status } = await codifySpawn('git config --global user.name', { throws: false })
10+
11+
return status === SpawnStatus.ERROR ? null : email.trim()
12+
}
13+
14+
async applyAdd(valueToAdd: string): Promise<void> {
15+
await codifySpawn(`git config --global user.name "${valueToAdd}"`)
16+
}
17+
18+
async applyModify(newValue: string): Promise<void> {
19+
await codifySpawn(`git config --global user.name "${newValue}"`)
20+
}
21+
22+
async applyRemove(): Promise<void> {
23+
await codifySpawn('git config --global --unset user.name')
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { CreatePlan, DestroyPlan, Resource } from 'codify-plugin-lib';
2+
import { StringIndexedObject } from 'codify-schemas';
3+
4+
import { SpawnStatus, codifySpawn } from '../../../utils/codify-spawn.js';
5+
import { GitEmailParameter } from './git-email-paramater.js';
6+
import { GitNameParameter } from './git-name-parameter.js';
7+
8+
export interface GitConfig extends StringIndexedObject {
9+
email?: string,
10+
username?: string,
11+
// TODO: Allow upgrading git to the latest version in the future. This means installing git using homebrew
12+
}
13+
14+
export class GitResource extends Resource<GitConfig> {
15+
constructor() {
16+
super({
17+
callStatefulParameterRemoveOnDestroy: true,
18+
parameterOptions: {
19+
email: { statefulParameter: new GitEmailParameter(), },
20+
username: { statefulParameter: new GitNameParameter() },
21+
},
22+
type: 'git'
23+
});
24+
}
25+
26+
async refresh(): Promise<Partial<GitConfig> | null> {
27+
const { status } = await codifySpawn('which git', { throws: false })
28+
return status === SpawnStatus.ERROR ? null : {}
29+
}
30+
31+
async applyCreate(): Promise<void> {
32+
// Git should always be installed with xcode tools. Nothing to do here.
33+
}
34+
35+
async applyDestroy(): Promise<void> {
36+
// Don't uninstall git. It will break things.
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://www.codifycli.com/git.json",
4+
"title": "Jenv resource",
5+
"type": "object",
6+
"properties": {
7+
"email": {
8+
"type": "string",
9+
"description": "The global email to set for git"
10+
},
11+
"username": {
12+
"type": "string",
13+
"description": "The global username to set for git"
14+
}
15+
},
16+
"additionalProperties": false
17+
}

src/resources/java/jenv/jenv-schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
3-
"$id": "https://www.codifycli.com/nvm.json",
4-
"title": "Nvm resource",
3+
"$id": "https://www.codifycli.com/jenv.json",
4+
"title": "Jenv resource",
55
"type": "object",
66
"properties": {
77
"add": {

test/git/git.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { afterEach, beforeEach, describe, it } from 'vitest';
2+
import { PluginTester } from 'codify-plugin-test';
3+
import * as path from 'node:path';
4+
5+
describe('Git integration tests', async () => {
6+
let plugin: PluginTester;
7+
8+
beforeEach(() => {
9+
plugin = new PluginTester(path.resolve('./src/index.ts'));
10+
})
11+
12+
it('Can add global user name and email', { timeout: 300000 }, async () => {
13+
await plugin.fullTest([
14+
{
15+
type: 'git',
16+
username: 'test',
17+
email: 'test@test.com'
18+
}
19+
]);
20+
})
21+
22+
it('Can modify git user name and email', { timeout: 300000 }, async () => {
23+
await plugin.fullTest([
24+
{
25+
type: 'git',
26+
username: 'test2',
27+
email: 'test2@test.com'
28+
}
29+
]);
30+
})
31+
32+
afterEach(() => {
33+
plugin.kill();
34+
})
35+
})

0 commit comments

Comments
 (0)