Skip to content

Commit 3889e2e

Browse files
committed
feat: Added ability to find all parameters for certain resources that allow multiple. General bug fixes and improvements
1 parent 1f124cb commit 3889e2e

File tree

10 files changed

+64
-25
lines changed

10 files changed

+64
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"ajv": "^8.12.0",
2424
"ajv-formats": "^2.1.1",
2525
"semver": "^7.6.0",
26-
"codify-plugin-lib": "1.0.166",
26+
"codify-plugin-lib": "1.0.170",
2727
"codify-schemas": "1.0.63",
2828
"chalk": "^5.3.0",
2929
"debug": "^4.3.4",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AsdfLocalResource } from './resources/asdf/asdf-local.js';
88
import { AsdfPluginResource } from './resources/asdf/asdf-plugin.js';
99
import { AwsCliResource } from './resources/aws-cli/cli/aws-cli.js';
1010
import { AwsProfileResource } from './resources/aws-cli/profile/aws-profile.js';
11-
import { GitCloneResource } from './resources/git/clone/git-clone.js';
11+
import { GitCloneResource } from './resources/git/clone/git-repository.js';
1212
import { GitResource } from './resources/git/git/git-resource.js';
1313
import { GitLfsResource } from './resources/git/lfs/git-lfs.js';
1414
import { HomebrewResource } from './resources/homebrew/homebrew.js';

src/resources/aws-cli/profile/aws-profile-schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
}
4141
},
4242
"oneOf": [
43-
{ "required": ["awsAccessKeyId", "awsSecretAccessKey", "region"]},
44-
{ "required": ["csvCredentials", "region"] }
43+
{ "required": ["awsAccessKeyId", "awsSecretAccessKey"]},
44+
{ "required": ["csvCredentials"] }
4545
],
4646
"additionalProperties": false
4747
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,30 @@ export class AwsProfileResource extends Resource<AwsProfileConfig> {
4040
csvCredentials: { type: 'directory', setting: true }, // Type setting means it won't be included in the plan calculation
4141
output: { default: 'json', canModify: true },
4242
profile: { default: 'default', canModify: true },
43-
metadataServiceNumAttempts: { canModify: true },
44-
metadataServiceTimeout: { canModify: true },
43+
metadataServiceNumAttempts: { canModify: true, setting: true },
44+
metadataServiceTimeout: { canModify: true, setting: true },
4545
},
4646
transformation: CSVCredentialsTransformation,
4747
importAndDestroy:{
48-
refreshKeys: ['output', 'profile', 'awsAccessKeyId', 'awsSecretAccessKey', 'region'],
4948
requiredParameters: ['profile']
5049
},
5150
allowMultiple: {
52-
identifyingParameters: ['profile']
51+
identifyingParameters: ['profile'],
52+
findAllParameters: async () => {
53+
const $ = getPty();
54+
const { status } = await $.spawnSafe('which aws');
55+
if (status === 'error') {
56+
return [];
57+
}
58+
59+
const { data } = await $.spawnSafe('aws configure list-profiles')
60+
61+
return data
62+
?.split(/\n/)
63+
?.filter(Boolean)
64+
?.map((profile) => ({ profile }))
65+
?? [];
66+
}
5367
}
5468
};
5569
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { codifySpawn } from '../../../utils/codify-spawn.js';
66
import { FileUtils } from '../../../utils/file-utils.js';
77
import Schema from './git-clone-schema.json';
88

9-
109
export interface GitCloneConfig extends ResourceConfig {
1110
autoVerifySSH: boolean
1211
directory?: string,
@@ -17,7 +16,7 @@ export interface GitCloneConfig extends ResourceConfig {
1716
export class GitCloneResource extends Resource<GitCloneConfig> {
1817
getSettings(): ResourceSettings<GitCloneConfig> {
1918
return {
20-
id: 'git-clone',
19+
id: 'git-repository',
2120
schema: Schema,
2221
parameterSettings: {
2322
parentDirectory: { type: 'directory' },
@@ -43,6 +42,16 @@ export class GitCloneResource extends Resource<GitCloneConfig> {
4342
}
4443

4544
return desiredPath === currentPath;
45+
},
46+
findAllParameters: async () => {
47+
const $ = getPty();
48+
const { data } = await $.spawnSafe('find ~ -type d \\( -path $HOME/Library -o -path $HOME/Pictures -o -path $HOME/Utilities -o -path "$HOME/.*" \\) -prune -o -name .git -print')
49+
50+
return data
51+
?.split(/\n/)?.filter(Boolean)
52+
?.map((p) => path.dirname(p))
53+
?.map((directory) => ({ directory }))
54+
?? [];
4655
}
4756
},
4857
dependencies: [

src/resources/scripting/file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export class FileResource extends Resource<FileConfig> {
1919
schema,
2020
parameterSettings: {
2121
path: { type: 'directory' },
22-
contents: { canModify: true }
22+
contents: { canModify: true },
23+
onlyCreate: { type: 'boolean', setting: true },
2324
},
2425
importAndDestroy:{
25-
refreshKeys: ['path', 'contents'],
2626
requiredParameters: ['path']
2727
},
2828
allowMultiple: {

src/resources/shell/alias/alias-resource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class AliasResource extends Resource<AliasConfig> {
3131
value: { canModify: true }
3232
},
3333
allowMultiple: {
34-
identifyingParameters: ['alias']
35-
}
34+
identifyingParameters: ['alias'],
35+
},
3636
}
3737
}
3838

src/resources/ssh/ssh-key.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {
22
CreatePlan,
33
DestroyPlan,
4-
getPty,
54
ModifyPlan,
65
ParameterChange,
76
Resource,
8-
ResourceSettings
7+
ResourceSettings,
8+
getPty
99
} from 'codify-plugin-lib';
1010
import { StringIndexedObject } from 'codify-schemas';
11+
import fs from 'node:fs/promises';
1112
import os from 'node:os';
1213
import path from 'node:path';
1314

@@ -60,6 +61,23 @@ export class SshKeyResource extends Resource<SshKeyConfig> {
6061
},
6162
allowMultiple: {
6263
identifyingParameters: ['fileName'],
64+
async findAllParameters() {
65+
try {
66+
const sshPublicKeys = await fs.readdir(path.join(os.homedir(), '.ssh'));
67+
68+
return sshPublicKeys.filter((p) => {
69+
if (!p.endsWith('.pub')) {
70+
return false;
71+
}
72+
73+
// We only want public / private pairs.
74+
const name = p.slice(0, - 4);
75+
return sshPublicKeys.includes(name)
76+
}).map((p) => ({ fileName: p.slice(0, - 4) }))
77+
} catch {
78+
return [];
79+
}
80+
}
6381
}
6482
}
6583
}
@@ -142,7 +160,7 @@ export class SshKeyResource extends Resource<SshKeyConfig> {
142160
// Passphrase can't be called in stateless mode because we don't know what the previous password is
143161
if (pc.name === 'passphrase') {
144162
await codifySpawn(`ssh-keygen -f ${plan.desiredConfig.fileName} -N ${pc.newValue} -P ${pc.previousValue} -p`)
145-
return;
163+
146164
}
147165
}
148166

src/utils/codify-spawn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Ajv } from 'ajv';
2-
import { SudoError } from 'codify-plugin-lib';
2+
import { SudoError, VerbosityLevel } from 'codify-plugin-lib';
33
import {
44
IpcMessage,
55
IpcMessageV2,
66
MessageCmd,
77
SudoRequestResponseData,
88
SudoRequestResponseDataSchema
99
} from 'codify-schemas';
10+
import { nanoid } from 'nanoid';
1011
import { SpawnOptions, spawn } from 'node:child_process';
1112
import stripAnsi from 'strip-ansi';
12-
import { nanoid } from 'nanoid';
1313

1414
const ajv = new Ajv({
1515
strict: true,
@@ -139,7 +139,7 @@ async function internalSpawn(
139139
// please node that this is not a full replacement for 'inherit'
140140
// the child process can and will detect if stdout is a pty and change output based on it
141141
// the terminal context is lost & ansi information (coloring) etc will be lost
142-
if (stdout && stderr) {
142+
if (stdout && stderr && VerbosityLevel.get() > 0) {
143143
stdout.pipe(process.stdout)
144144
stderr.pipe(process.stderr)
145145
}

src/utils/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ export const Utils = {
1818
},
1919

2020
async findInFolder(dir: string, search: string): Promise<string[]> {
21-
const { data } = await codifySpawn('ls -a', {
22-
cwd: dir,
23-
})
21+
const data = await fs.readdir(dir);
2422

25-
return data.split(/\n/)
23+
return data
2624
.filter((l) => l.includes(search))
2725
.map((l) => path.join(dir, l));
2826
},
@@ -58,7 +56,7 @@ export const Utils = {
5856
throw new Error(`Found file at ${path}. Cannot create a directory there`)
5957
}
6058

61-
await codifySpawn(`mkdir -p ${path}`)
59+
await fs.mkdir(path, { recursive: true })
6260
},
6361

6462
async findInstallLocation(name: string): Promise<null | string> {

0 commit comments

Comments
 (0)