-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-cli.ts
More file actions
153 lines (136 loc) · 4.54 KB
/
Copy pathgithub-cli.ts
File metadata and controls
153 lines (136 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
CreatePlan,
DestroyPlan,
ModifyPlan,
ParameterChange,
Resource,
ResourceSettings,
SpawnStatus,
Utils,
getPty,
z,
} from '@codifycli/plugin-core';
import { OS } from '@codifycli/schemas';
import { exampleGithubCliBasic, exampleGithubCliFull } from './examples.js';
export const schema = z
.object({
gitProtocol: z
.enum(['https', 'ssh'])
.optional()
.describe('Default protocol for git operations (default: https)'),
editor: z
.string()
.optional()
.describe('Default text editor for gh commands'),
prompt: z
.enum(['enabled', 'disabled'])
.optional()
.describe('Whether interactive prompts are enabled (default: enabled)'),
pager: z
.string()
.optional()
.describe('Default pager program for gh output'),
browser: z
.string()
.optional()
.describe('Default web browser for opening URLs'),
interactiveLogin: z
.boolean()
.optional()
.describe('If true, runs gh auth login --web after installation for browser-based authentication. Use this as a shortcut instead of declaring a separate github-cli-auth block'),
})
.meta({ $comment: 'https://cli.github.com/manual/' })
.describe('GitHub CLI (gh) — installs gh and manages global configuration');
export type GithubCliConfig = z.infer<typeof schema>;
const CONFIG_KEY_MAP: Partial<Record<keyof GithubCliConfig, string>> = {
gitProtocol: 'git_protocol',
editor: 'editor',
prompt: 'prompt',
pager: 'pager',
browser: 'browser',
};
const defaultConfig: Partial<GithubCliConfig> = {
gitProtocol: 'https',
prompt: 'enabled',
};
export class GithubCliResource extends Resource<GithubCliConfig> {
getSettings(): ResourceSettings<GithubCliConfig> {
return {
id: 'github-cli',
defaultConfig,
exampleConfigs: {
example1: exampleGithubCliBasic,
example2: exampleGithubCliFull,
},
operatingSystems: [OS.Darwin, OS.Linux],
schema,
parameterSettings: {
gitProtocol: { canModify: true },
editor: { canModify: true },
prompt: { canModify: true },
pager: { canModify: true },
browser: { canModify: true },
interactiveLogin: { type: 'boolean', setting: true },
},
};
}
async refresh(_params: Partial<GithubCliConfig>): Promise<Partial<GithubCliConfig> | null> {
const $ = getPty();
const { status } = await $.spawnSafe('which gh');
if (status === SpawnStatus.ERROR) return null;
const { data, status: configStatus } = await $.spawnSafe('gh config list');
if (configStatus === SpawnStatus.ERROR) return {};
const configMap: Record<string, string> = {};
for (const line of data.split('\n').filter(Boolean)) {
const eqIdx = line.indexOf('=');
if (eqIdx === -1) continue;
const key = line.slice(0, eqIdx).trim();
const value = line.slice(eqIdx + 1).trim();
configMap[key] = value;
}
const result: Partial<GithubCliConfig> = {};
if (configMap['git_protocol']) {
result.gitProtocol = configMap['git_protocol'] as 'https' | 'ssh';
}
if (configMap['editor']) {
result.editor = configMap['editor'];
}
if (configMap['prompt']) {
result.prompt = configMap['prompt'] as 'enabled' | 'disabled';
}
if (configMap['pager']) {
result.pager = configMap['pager'];
}
if (configMap['browser']) {
result.browser = configMap['browser'];
}
return result;
}
async create(plan: CreatePlan<GithubCliConfig>): Promise<void> {
const $ = getPty();
await Utils.installViaPkgMgr('gh');
await this.applyConfig(plan.desiredConfig);
if (plan.desiredConfig.interactiveLogin) {
await $.spawn('gh auth login --web', { interactive: true, stdin: true });
}
}
async modify(pc: ParameterChange<GithubCliConfig>, _plan: ModifyPlan<GithubCliConfig>): Promise<void> {
const $ = getPty();
const ghKey = CONFIG_KEY_MAP[pc.name as keyof GithubCliConfig];
if (ghKey !== undefined && pc.newValue !== undefined) {
await $.spawn(`gh config set ${ghKey} "${pc.newValue}"`);
}
}
async destroy(_plan: DestroyPlan<GithubCliConfig>): Promise<void> {
await Utils.uninstallViaPkgMgr('gh');
}
private async applyConfig(config: Partial<GithubCliConfig>): Promise<void> {
const $ = getPty();
for (const [key, ghKey] of Object.entries(CONFIG_KEY_MAP) as Array<[keyof GithubCliConfig, string]>) {
const value = config[key];
if (value !== undefined) {
await $.spawn(`gh config set ${ghKey} "${value}"`);
}
}
}
}