Skip to content

Commit

Permalink
tea URL works
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Oct 28, 2022
1 parent c068797 commit 6b6f8ca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
35 changes: 29 additions & 6 deletions src/app.exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ async function abracadabra(opts: Args): Promise<RV> {

const path = await (async () => {
if (args.length == 0) return
try {
const src = new URL(args[0])
const path = await useDownload().download({ src })
path.chmod(0x777)
args[0] = path.string
const url = urlify(args[0])
if (url) {
const logger = url.path().basename()
const path = await useDownload().download({ src: url, logger })
args[0] = path.chmod(0o777).string
return path
} catch {
} else {
return Path.cwd().join(args[0]).isFile()
}

})()

if (path && isMarkdown(path)) {
Expand Down Expand Up @@ -224,3 +225,25 @@ async function abracadabra(opts: Args): Promise<RV> {
}
}
}

function urlify(arg0: string) {
try {
const url = new URL(arg0)
// we do some magic so github URLs are immediately usable
switch (url.host) {
case "github.com":
url.host = "raw.githubusercontent.com"
url.pathname = url.pathname.replace("/blob/", "/")
break
case "gist.github.com":
url.host = "gist.githubusercontent.com"
//FIXME this is not good enough
//REF: https://gist.github.com/atenni/5604615
url.pathname += "/raw"
break
}
return url
} catch {
//noop
}
}
28 changes: 18 additions & 10 deletions src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { readerFromStreamReader, copy } from "deno/streams/conversion.ts"
import { Logger, teal } from "./useLogger.ts"
import { Logger, teal, gray } from "./useLogger.ts"
import { useFlags, usePrefix } from "hooks"
import { chuzzle, panic } from "utils"
import { Sha256 } from "deno/hash/sha256.ts"
import { isString } from "is_what"
import Path from "path"
import { gray } from "https://deno.land/[email protected]/fmt/colors.ts"

interface DownloadOptions {
src: URL
dst?: Path /// default is our own unique cache path
headers?: Record<string, string>
ephemeral?: boolean /// always download, do not rely on cache
logger?: Logger
logger?: Logger | string
}

interface RV {
Expand All @@ -25,7 +25,11 @@ interface RV {
async function internal<T>({ src, dst, headers, ephemeral, logger }: DownloadOptions,
body: (src: ReadableStream<Uint8Array>, dst: Deno.Writer, sz?: number) => Promise<T>): Promise<Path>
{
logger ??= new Logger()
if (isString(logger)) {
logger = new Logger(logger)
} else if (!logger) {
logger = new Logger()
}

console.verbose({src: src, dst})

Expand Down Expand Up @@ -96,16 +100,20 @@ async function download(opts: DownloadOptions): Promise<Path> {
return await internal(opts, (src, dst) => copy(readerFromStreamReader(src.getReader()), dst))
}

async function download_with_sha(opts: DownloadOptions): Promise<{path: Path, sha: string}> {
opts.logger ??= new Logger()
async function download_with_sha({ logger, ...opts}: DownloadOptions): Promise<{path: Path, sha: string}> {
if (isString(logger)) {
logger = new Logger(logger)
} else if (!logger) {
logger = new Logger()
}

const digest = new Sha256()
let run = false

// don’t fill CI logs with dozens of download percentage lines
const ci = Deno.env.get("CI")

const path = await internal(opts, (src, dst, sz) => {
const path = await internal({...opts, logger}, (src, dst, sz) => {
let n = 0

run = true
Expand All @@ -116,16 +124,16 @@ async function download_with_sha(opts: DownloadOptions): Promise<{path: Path, sh
digest.update(buf)
if (sz && !ci) {
n += buf.length
const pc = Math.round(n / sz * 100)
opts.logger!.replace(`${teal('downloading')} ${pc}%`)
const pc = Math.round(n / sz * 100);
(logger as Logger).replace(`${teal('downloading')} ${pc}%`)
}
return Promise.resolve(buf.length)
}})
return Promise.all([p1, p2])
})

if (!run) {
opts.logger.replace(teal('verifying'))
logger.replace(teal('verifying'))
const f = await Deno.open(path.string, { read: true })
await copy(f, { write: buf => {
//TODO in separate thread would likely be faster
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export async function run({ spin, ...opts }: RunOptions) {
try {
const exit = await proc.status()
console.verbose({ exit })
if (!exit.success) throw new RunError(exit.code)
if (!exit.success) throw new RunError(exit.code, cmd)
} catch (err) {
if (spin) {
//FIXME this doesn’t result in the output being correctly interlaced
Expand Down

0 comments on commit 6b6f8ca

Please sign in to comment.