-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
162 additions
and
53 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
|
||
import { parseArgs } from "node:util"; | ||
|
||
import { getInput } from '@actions/core' | ||
|
||
import { env } from "./env"; | ||
|
||
export const args = (prompt: string) => { | ||
// https://stackoverflow.com/questions/29655760/convert-a-string-into-shell-arguments | ||
const re = /"[^"]+"|'[^']+'|\S+/g | ||
if (env.github() && !env.mock()) { | ||
prompt = getInput("transmute") | ||
} | ||
return parseArgs({ | ||
allowPositionals: true, | ||
args: prompt.match(re) || [], | ||
options: { | ||
verbose: { | ||
type: 'boolean' as "boolean", | ||
short: 'v' | ||
}, | ||
alg: { | ||
type: 'string' as "string", | ||
short: 'a' | ||
}, | ||
}, | ||
}) | ||
} |
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,5 @@ | ||
const github = () => !!process.env.GITHUB_ACTION | ||
|
||
const mock = () => process.env.GITHUB_ACTION === 'jest-mock' | ||
|
||
export const env = { github, mock } |
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,16 @@ | ||
import { PositionalArguments } from '../types' | ||
|
||
import * as jose from '../jose' | ||
|
||
const commands = { jose } | ||
|
||
export const handler = async (args: PositionalArguments) => { | ||
const [command] = args.positionals | ||
if (commands[command]) { | ||
await commands[command].handler(args) | ||
} else { | ||
const message = `😕 Unknown Command` | ||
console.error(message) | ||
throw new Error(message) | ||
} | ||
} |
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,3 @@ | ||
export * from './env' | ||
export * from './facade' | ||
export * from './handler' |
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 @@ | ||
export * from './action' |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { facade } from './action/facade' | ||
|
||
const cli = { facade } | ||
|
||
export default cli | ||
export * from './types' | ||
export * from './cli' |
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,44 @@ | ||
import * as jose from 'jose' | ||
|
||
import { PositionalArguments } from "../types" | ||
|
||
import { setSecret, setOutput, debug } from '@actions/core' | ||
|
||
import { env } from '../action' | ||
|
||
const prettyKey = (k: jose.JWK) => { | ||
const { kid, kty, crv, alg, x, y, d } = k | ||
return JSON.stringify({ kid, kty, crv, alg, x, y, d }, null, 2) | ||
} | ||
|
||
export const handler = async function ({ positionals, values }: PositionalArguments) { | ||
const [resource, _action] = positionals.slice(1) | ||
switch (resource) { | ||
case 'keygen': { | ||
const alg = values.alg || 'ES256' | ||
const crv = values.crv || 'Ed25519' | ||
const verbose = values.verbose || false | ||
const k = await jose.generateKeyPair(alg, { crv }) | ||
const privateKey = await jose.exportJWK(k.privateKey) | ||
privateKey.kid = await jose.calculateJwkThumbprint(privateKey) | ||
if (verbose) { | ||
const message = `🔑 ${privateKey.kid}` | ||
debug(message) | ||
} | ||
const output = prettyKey(privateKey) | ||
if (env.github()) { | ||
setSecret(output) | ||
setOutput('json', output) | ||
} else { | ||
console.log(output) | ||
} | ||
break | ||
} | ||
default: { | ||
const message = `😕 Unknown Command` | ||
console.error(message) | ||
throw new Error(message) | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
export * from './handler' |
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 @@ | ||
export type PositionalArguments = { positionals: string[], values: Record<string, any> } |
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import * as core from '@actions/core' | ||
|
||
import { facade } from '../src' | ||
|
||
let debug: jest.SpiedFunction<typeof core.debug> | ||
let output: jest.SpiedFunction<typeof core.setOutput> | ||
let secret: jest.SpiedFunction<typeof core.setSecret> | ||
|
||
beforeEach(() => { | ||
process.env.GITHUB_ACTION = 'jest-mock' | ||
jest.clearAllMocks() | ||
debug = jest.spyOn(core, 'debug').mockImplementation() | ||
output = jest.spyOn(core, 'setOutput').mockImplementation() | ||
secret = jest.spyOn(core, 'setSecret').mockImplementation() | ||
}) | ||
|
||
it('keygen', async () => { | ||
await facade(`jose keygen --alg ES256 --verbose`) | ||
expect(debug).toHaveBeenCalledTimes(1) | ||
expect(secret).toHaveBeenCalledTimes(1) | ||
expect(output).toHaveBeenCalledTimes(1) | ||
}) |