import { exists } from "https://deno.land/std@0.224.0/fs/mod.ts" import { join } from "https://deno.land/std@0.224.0/path/mod.ts" import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts" export async function fetch_repo(repo_path: string, remote_url: string) { if (!await exists(join(repo_path, ".git"))) await create_repo(repo_path, remote_url) const status = await run("git", "-C", repo_path, "pull") if (!status.success) warn("Failed to fetch : " + new TextDecoder().decode(status.stderr)) await run("git", "-C", repo_path, "push") } async function create_repo(repo_path: string, remote_url: string) { await Deno.mkdir(repo_path, { recursive: true }) const status = await run("git", "clone", remote_url, repo_path) if (!status.success) warn("Failed to clone : " + new TextDecoder().decode(status.stderr)) } async function run(cmd: string, ...args: string[]) { return await new Deno.Command(cmd, { args, stderr: "piped", stdout: "piped" }).output() } export async function link_todo(repo_path: string, todo_path: string) { const status = await run("ln", "-sf", join(repo_path, "todo"), todo_path) if (!status.success) warn("Failed to link : " + new TextDecoder().decode(status.stderr)) } function warn(text: string) { console.warn(crayon.yellow(text)) } function decode(beuffeur: Uint8Array) { return new TextDecoder().decode(beuffeur) } export async function push_repo(repo_path: string) { console.log(repo_path) const status = await run("git", "-C", repo_path, "status", "--short") if (!status.success) { console.error("Failed to stat : " + decode(status.stderr)) return } if (decode(status.stdout) === "") return await run("git", "-C", repo_path, "add", "-A") await run("git", "-C", repo_path, "commit", "-m", "update " + await hostname()) await run("git", "-C", repo_path, "push") } async function hostname() { return (await Deno.readTextFile("/etc/hostname")).trim() }