-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support adding secrets in non-interactive mode (#559)
* feat: support adding secrets in non-interactive mode Now the user can pipe in the secret value to the `wrangler secret put` command. For example: ``` cat my-secret.txt | wrangler secret put secret-key --name worker-name ``` This requires that the user is logged in, and has only one account, or that the `account_id` has been set in `wrangler.toml`. Fixes #170
- Loading branch information
1 parent
6e9a219
commit 16fb5e6
Showing
6 changed files
with
413 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: support adding secrets in non-interactive mode | ||
|
||
Now the user can pipe in the secret value to the `wrangler secret put` command. | ||
For example: | ||
|
||
``` | ||
cat my-secret.txt | wrangler secret put secret-key --name worker-name | ||
``` | ||
|
||
This requires that the user is logged in, and has only one account, or that the `account_id` has been set in `wrangler.toml`. | ||
|
||
Fixes #170 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const ORIGINAL_STDIN = process.stdin; | ||
|
||
/** | ||
* Mock process.stdin so that we can pipe in text for non-interactive mode tests. | ||
*/ | ||
export function useMockStdin({ isTTY }: { isTTY: boolean }) { | ||
const mockStdin = new MockStdIn(isTTY); | ||
|
||
beforeEach(() => { | ||
mockStdin.reset(); | ||
Object.defineProperty(process, "stdin", { | ||
value: mockStdin, | ||
configurable: true, | ||
writable: false, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
Object.defineProperty(process, "stdin", { | ||
value: ORIGINAL_STDIN, | ||
configurable: true, | ||
writable: false, | ||
}); | ||
}); | ||
|
||
return mockStdin; | ||
} | ||
|
||
const failCallback: (value: unknown) => void = () => { | ||
throw new Error( | ||
"TEST FAILURE: stdin callback called before being initialized." | ||
); | ||
}; | ||
|
||
/** | ||
* A mock version of `process.std`, which can be used to simulate piping data | ||
* into the wrangler process in non-interactive mode. | ||
*/ | ||
class MockStdIn { | ||
private endCallback = failCallback; | ||
private errorCallback = failCallback; | ||
private chunks: string[] = []; | ||
private error: Error | undefined; | ||
|
||
/** | ||
* Set this to true if you want the stdin stream to error. | ||
*/ | ||
throwError(error: Error) { | ||
this.error = error; | ||
} | ||
|
||
/** | ||
* Call this to clean out the chunks that are queued for sending. | ||
*/ | ||
reset() { | ||
this.chunks.length = 0; | ||
this.error = undefined; | ||
} | ||
|
||
/** | ||
* Queue up some chunks to be sent. | ||
*/ | ||
send(...chunks: string[]) { | ||
this.chunks.push(...chunks); | ||
} | ||
|
||
constructor( | ||
/** | ||
* Used by wrangler to check whether stdin is interactive. | ||
*/ | ||
readonly isTTY: boolean | ||
) {} | ||
|
||
/** | ||
* Used by wrangler to add event listeners. | ||
*/ | ||
on(eventName: string, callback: () => void) { | ||
switch (eventName) { | ||
case "readable": | ||
setImmediate(callback); | ||
break; | ||
case "end": | ||
this.endCallback = callback; | ||
break; | ||
case "error": | ||
this.errorCallback = callback; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Used by wrangler to get the next chunk of data in the stream. | ||
*/ | ||
read() { | ||
if (this.error) { | ||
setImmediate(() => this.errorCallback(this.error)); | ||
} | ||
if (this.chunks.length === 0) { | ||
setImmediate(this.endCallback); | ||
} | ||
return this.chunks.shift() ?? null; | ||
} | ||
} |
Oops, something went wrong.