mirror of
https://github.com/pesde-pkg/tooling.git
synced 2025-01-23 12:58:04 +00:00
feat(lib): use gh
CLI to get token and condition bar start
* Looks for `$GITHUB_TOKEN` env var, and if not found, tries to run `gh auth token` to get the token from the GitHub CLI. * Made a conditional `start` method for the bar, which was previously missed.
This commit is contained in:
parent
0726169df8
commit
bb6278407d
1 changed files with 19 additions and 6 deletions
|
@ -126,10 +126,24 @@ local function makeConditionalBar()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
next = makeConditional(bar.nextStage),
|
next = makeConditional(bar.nextStage),
|
||||||
|
start = makeConditional(bar.start),
|
||||||
stop = makeConditional(bar.stop),
|
stop = makeConditional(bar.stop),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function getGithubToken(): Option<string>
|
||||||
|
return Option.from(process.env.GITHUB_TOKEN):orElse(function()
|
||||||
|
local child = process.spawn("gh", { "auth", "token" })
|
||||||
|
if not child.ok then
|
||||||
|
return Option.None
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Remove newlines and other control characters
|
||||||
|
local token = string.gsub(child.stdout, "%s+", "")
|
||||||
|
return Option.Some(token)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
_G.interactive = false
|
_G.interactive = false
|
||||||
function installTool(tool: ToolId, installPath: pathfs.Path): number
|
function installTool(tool: ToolId, installPath: pathfs.Path): number
|
||||||
local barFns = makeConditionalBar()
|
local barFns = makeConditionalBar()
|
||||||
|
@ -138,8 +152,7 @@ function installTool(tool: ToolId, installPath: pathfs.Path): number
|
||||||
local client = Github.new(
|
local client = Github.new(
|
||||||
tool.repo,
|
tool.repo,
|
||||||
Option.Some({
|
Option.Some({
|
||||||
-- TODO: Maybe use the `gh auth token` command to get the token
|
authToken = getGithubToken(),
|
||||||
authToken = Option.from(process.env.GITHUB_TOKEN) :: Option<string>,
|
|
||||||
retries = Option.None :: Option<number>,
|
retries = Option.None :: Option<number>,
|
||||||
}) :: Option<Github.Config>
|
}) :: Option<Github.Config>
|
||||||
)
|
)
|
||||||
|
@ -188,7 +201,6 @@ function installTool(tool: ToolId, installPath: pathfs.Path): number
|
||||||
end
|
end
|
||||||
|
|
||||||
barFns.next(bar) -- download
|
barFns.next(bar) -- download
|
||||||
-- task.delay(0.1, barFns.next, bar) -- download
|
|
||||||
|
|
||||||
local binaryPath: pathfs.Path
|
local binaryPath: pathfs.Path
|
||||||
if matchingAsset == nil then
|
if matchingAsset == nil then
|
||||||
|
@ -275,6 +287,8 @@ return setmetatable(
|
||||||
{
|
{
|
||||||
__call = function(lib: LibExportsImpl, tool: string, pesdeRoot: string?): number
|
__call = function(lib: LibExportsImpl, tool: string, pesdeRoot: string?): number
|
||||||
_G.interactive = true
|
_G.interactive = true
|
||||||
|
local barFns = makeConditionalBar()
|
||||||
|
|
||||||
local repo, version = string.match(tool, "([^@]+)@?(.*)")
|
local repo, version = string.match(tool, "([^@]+)@?(.*)")
|
||||||
if repo == nil or version == nil then
|
if repo == nil or version == nil then
|
||||||
stdio.ewrite(`{ERROR_PREFIX} Invalid tool provided\n`)
|
stdio.ewrite(`{ERROR_PREFIX} Invalid tool provided\n`)
|
||||||
|
@ -290,7 +304,6 @@ return setmetatable(
|
||||||
-- Use _G.PESDE_ROOT to get the install directory, then decode the
|
-- Use _G.PESDE_ROOT to get the install directory, then decode the
|
||||||
-- pesde manifest to get the version of the tool dynamically
|
-- pesde manifest to get the version of the tool dynamically
|
||||||
local manifestContents = pathfs.readFile(pathfs.Path.from(pesdeRoot :: string):join("pesde.toml"))
|
local manifestContents = pathfs.readFile(pathfs.Path.from(pesdeRoot :: string):join("pesde.toml"))
|
||||||
-- TODO: Create a pesde manifest type in toolchainlib, and use that here
|
|
||||||
local ok, manifest: manifest.PesdeManifest = pcall(serde.decode, "toml" :: "toml", manifestContents)
|
local ok, manifest: manifest.PesdeManifest = pcall(serde.decode, "toml" :: "toml", manifestContents)
|
||||||
if not ok then
|
if not ok then
|
||||||
stdio.ewrite(
|
stdio.ewrite(
|
||||||
|
@ -319,7 +332,7 @@ return setmetatable(
|
||||||
return lib.runTool(toolInstallPath)
|
return lib.runTool(toolInstallPath)
|
||||||
end
|
end
|
||||||
|
|
||||||
bar:start() -- init
|
barFns.start(bar) -- init
|
||||||
local ok, err = pcall(
|
local ok, err = pcall(
|
||||||
lib.installTool,
|
lib.installTool,
|
||||||
{
|
{
|
||||||
|
@ -332,7 +345,7 @@ return setmetatable(
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
-- Cleanup progress bar in case of error
|
-- Cleanup progress bar in case of error
|
||||||
makeConditionalBar().stop(bar)
|
barFns.stop(bar)
|
||||||
|
|
||||||
stdio.ewrite(`{ERROR_PREFIX} Failed to install {tool}\n`)
|
stdio.ewrite(`{ERROR_PREFIX} Failed to install {tool}\n`)
|
||||||
stdio.ewrite(` - {err}\n`)
|
stdio.ewrite(` - {err}\n`)
|
||||||
|
|
Loading…
Reference in a new issue