Add service.
This commit is contained in:
parent
4094afff3d
commit
adfaa2afe2
6 changed files with 86 additions and 3 deletions
|
@ -29,7 +29,7 @@ export async function config_default() {
|
|||
"$schema": "https://git.barnulf.net/mb/feseur/raw/branch/master/asset/config_schema.json",
|
||||
file_path: join(await home_path(), "todo"),
|
||||
repo_path: join(await home_path(), ".local", "share", "feseur"),
|
||||
remote_url: "ssh://git.barnulf.net/mb/todo.git",
|
||||
remote_url: "ssh://forgejo@barnulf.net/mb/todo.git",
|
||||
} as ConfigType
|
||||
}
|
||||
|
||||
|
|
14
src/lib/debounce.ts
Normal file
14
src/lib/debounce.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
export class Debouncer {
|
||||
last = 0
|
||||
|
||||
public constructor(
|
||||
public timeout: number,
|
||||
) {}
|
||||
|
||||
public should_skip() {
|
||||
const now = Date.now()
|
||||
const delay = now - this.last
|
||||
this.last = now
|
||||
return (delay < this.timeout)
|
||||
}
|
||||
}
|
50
src/lib/repo.ts
Normal file
50
src/lib/repo.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
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()
|
||||
}
|
|
@ -1,6 +1,22 @@
|
|||
#!/usr/bin/env -S deno run
|
||||
#!/usr/bin/env -S deno run --allow-all
|
||||
|
||||
import { join } from "https://deno.land/std@0.224.0/path/mod.ts"
|
||||
import { read_conf } from "./lib/conf.ts"
|
||||
import { Debouncer } from "./lib/debounce.ts"
|
||||
import { fetch_repo, push_repo } from "./lib/repo.ts"
|
||||
import { log } from "./lib/utils.ts"
|
||||
|
||||
await main()
|
||||
async function main() {
|
||||
//
|
||||
const config = await read_conf()
|
||||
await fetch_repo(config.repo_path, config.remote_url)
|
||||
const debouncer = new Debouncer(200)
|
||||
const repo_file = join(config.repo_path, "todo")
|
||||
log("Watching file", repo_file)
|
||||
for await (const _ of Deno.watchFs(repo_file)) {
|
||||
if (debouncer.should_skip()) continue
|
||||
log("Commiting event " + Date.now())
|
||||
await fetch_repo(config.repo_path, config.remote_url)
|
||||
await push_repo(config.repo_path)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,14 @@
|
|||
|
||||
import { read_conf } from "./lib/conf.ts"
|
||||
import { print_todo } from "./lib/display.ts"
|
||||
import { fetch_repo, link_todo } from "./lib/repo.ts"
|
||||
import { read_todo } from "./lib/task.ts"
|
||||
|
||||
await main()
|
||||
async function main() {
|
||||
const config = await read_conf()
|
||||
await fetch_repo(config.repo_path, config.remote_url)
|
||||
await link_todo(config.repo_path, config.file_path)
|
||||
const todo = await read_todo(config.file_path)
|
||||
print_todo(todo)
|
||||
}
|
||||
|
|
0
todo
Normal file
0
todo
Normal file
Loading…
Add table
Add a link
Reference in a new issue