Skip to content

Commit f3a5ab0

Browse files
Tiny refactor for common tsrelay args (#87)
We run tsrelay in sudo and non-sudo, this PR abstracts the common args passed to both class methods.
1 parent cf322bb commit f3a5ab0

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

src/tailscale/cli.ts

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,34 @@ export class Tailscale {
4444
return ts;
4545
}
4646

47-
async init(port?: string, nonce?: string) {
47+
defaultArgs() {
48+
const args = [];
49+
if (this._vscode.env.logLevel === LogLevel.Debug) {
50+
args.push('-v');
51+
}
52+
if (this.port) {
53+
args.push(`-port=${this.port}`);
54+
}
55+
if (this.nonce) {
56+
args.push(`-nonce=${this.nonce}`);
57+
}
58+
if (this.socket) {
59+
args.push(`-socket=${this.socket}`);
60+
}
61+
return args;
62+
}
63+
64+
async init() {
4865
return new Promise<null>((resolve) => {
4966
this.socket = vscode.workspace.getConfiguration(EXTENSION_NS).get<string>('socketPath');
5067
let binPath = this.tsrelayPath();
51-
let args = [];
52-
if (this._vscode.env.logLevel === LogLevel.Debug) {
53-
args.push('-v');
54-
}
68+
let args = this.defaultArgs();
5569
let cwd = __dirname;
5670
if (process.env.NODE_ENV === 'development') {
5771
binPath = '../tool/go';
5872
args = ['run', '.', ...args];
5973
cwd = path.join(cwd, '../tsrelay');
6074
}
61-
if (port) {
62-
args.push(`-port=${this.port}`);
63-
}
64-
if (nonce) {
65-
args.push(`-nonce=${this.nonce}`);
66-
}
67-
if (this.socket) {
68-
args.push(`-socket=${this.socket}`);
69-
}
7075
Logger.debug(`path: ${binPath}`, LOG_COMPONENT);
7176
Logger.debug(`args: ${args.join(' ')}`, LOG_COMPONENT);
7277

@@ -109,48 +114,11 @@ export class Tailscale {
109114
this.processStderr(this.childProcess);
110115
});
111116
}
112-
113-
processStderr(childProcess: cp.ChildProcess) {
114-
if (!childProcess.stderr) {
115-
Logger.error('childProcess.stderr is null', LOG_COMPONENT);
116-
throw new Error('childProcess.stderr is null');
117-
}
118-
let buffer = '';
119-
childProcess.stderr.on('data', (data: Buffer) => {
120-
buffer += data.toString(); // Append the data to the buffer
121-
122-
const lines = buffer.split('\n'); // Split the buffer into lines
123-
124-
// Process all complete lines except the last one
125-
for (let i = 0; i < lines.length - 1; i++) {
126-
const line = lines[i].trim();
127-
if (line.length > 0) {
128-
Logger.info(line, LOG_COMPONENT);
129-
}
130-
}
131-
132-
buffer = lines[lines.length - 1];
133-
});
134-
135-
childProcess.stderr.on('end', () => {
136-
// Process the remaining data in the buffer after the stream ends
137-
const line = buffer.trim();
138-
if (line.length > 0) {
139-
Logger.info(line, LOG_COMPONENT);
140-
}
141-
});
142-
}
143-
144117
async initSudo() {
145118
return new Promise<null>((resolve, err) => {
146119
const binPath = this.tsrelayPath();
147-
const args = [`-nonce=${this.nonce}`, `-port=${this.port}`];
148-
if (this._vscode.env.logLevel === LogLevel.Debug) {
149-
args.push('-v');
150-
}
151-
if (this.socket) {
152-
args.push(`-socket=${this.socket}`);
153-
}
120+
const args = this.defaultArgs();
121+
154122
Logger.info(`path: ${binPath}`, LOG_COMPONENT);
155123
this.notifyExit = () => {
156124
Logger.info('starting sudo tsrelay');
@@ -177,7 +145,7 @@ export class Tailscale {
177145
} else {
178146
this._vscode.window.showErrorMessage('Could not run authenticator, please check logs');
179147
}
180-
await this.init(this.port, this.nonce);
148+
await this.init();
181149
err('unauthenticated');
182150
});
183151
childProcess.on('error', (err) => {
@@ -200,6 +168,37 @@ export class Tailscale {
200168
});
201169
}
202170

171+
processStderr(childProcess: cp.ChildProcess) {
172+
if (!childProcess.stderr) {
173+
Logger.error('childProcess.stderr is null', LOG_COMPONENT);
174+
throw new Error('childProcess.stderr is null');
175+
}
176+
let buffer = '';
177+
childProcess.stderr.on('data', (data: Buffer) => {
178+
buffer += data.toString(); // Append the data to the buffer
179+
180+
const lines = buffer.split('\n'); // Split the buffer into lines
181+
182+
// Process all complete lines except the last one
183+
for (let i = 0; i < lines.length - 1; i++) {
184+
const line = lines[i].trim();
185+
if (line.length > 0) {
186+
Logger.info(line, LOG_COMPONENT);
187+
}
188+
}
189+
190+
buffer = lines[lines.length - 1];
191+
});
192+
193+
childProcess.stderr.on('end', () => {
194+
// Process the remaining data in the buffer after the stream ends
195+
const line = buffer.trim();
196+
if (line.length > 0) {
197+
Logger.info(line, LOG_COMPONENT);
198+
}
199+
});
200+
}
201+
203202
tsrelayPath(): string {
204203
let arch = process.arch;
205204
let platform: string = process.platform;

0 commit comments

Comments
 (0)