From 77783a3405423638100664612e7cf3957736e57b Mon Sep 17 00:00:00 2001 From: nbsp Date: Wed, 8 Jan 2025 04:21:56 +0200 Subject: [PATCH] create backup --- app/management.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ ttbp/ttbp.go | 1 + 2 files changed, 46 insertions(+) diff --git a/app/management.go b/app/management.go index 6686a8d..6af8f40 100644 --- a/app/management.go +++ b/app/management.go @@ -3,8 +3,11 @@ package app import ( "fmt" "os" + "os/exec" "os/user" + "path" "strconv" + "time" "git.sr.ht/~rockorager/vaxis" "git.tilde.town/nbsp/neofeels/ttbp" @@ -57,6 +60,10 @@ func (management *Management) Event(state *ui.State, event vaxis.Event) (process case 0: user, _ := user.Current() ui.ViewChange <- NewUserPage(user.Username, true) + // case 1: + case 2: + management.SaveBackup(state) + // case 3: case 4: ui.ViewChange <- NewBury() case 5: @@ -100,6 +107,44 @@ func wipeAccount() { close(ui.Quit) } +func (management *Management) SaveBackup(state *ui.State) { + os.MkdirAll(ttbp.PathUserBackups, 0700) + filename := time.Now().Format("feels-backup-20060102-150405.tar.gz") + + exec.Command("tar", "-C", ttbp.PathUserFeels, "-czf", filename, "entries").Run() + exec.Command("tar", "-C", ttbp.PathUserFeels, "-czf", path.Join(ttbp.PathUserBackups, filename), "entries").Run() + + management.Draw(state) + win := state.Window() + win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{ + Text: " backup saved ", + Style: vaxis.Style{ + Foreground: vaxis.IndexColor(2), + Attribute: vaxis.AttrReverse, + }, + }) + state.Render() +loop: + for ev := range state.Events() { + switch ev.(type) { + case vaxis.Resize, vaxis.Redraw: + management.Draw(state) + win.New(win.Width/2-14, win.Height/2-2+management.list.Index(), 28, 1).Print(vaxis.Segment{ + Text: " backup saved ", + Style: vaxis.Style{ + Foreground: vaxis.IndexColor(2), + Attribute: vaxis.AttrReverse, + }, + }) + state.Render() + case vaxis.Key: + if ev.(vaxis.Key).EventType == vaxis.EventPress { + break loop + } + } + } +} + // TODO: abstract this to our own List type // TODO: figure out a less janky way instead of nesting event channels func (management *Management) Confirmation(state *ui.State, action func(), message string) { diff --git a/ttbp/ttbp.go b/ttbp/ttbp.go index a60df79..acb63a0 100644 --- a/ttbp/ttbp.go +++ b/ttbp/ttbp.go @@ -30,6 +30,7 @@ var ( PathUserConfig = path.Join(PathUserFeels, "config") PathUserEntries = path.Join(PathUserFeels, "entries") PathUserBuried = path.Join(PathUserFeels, "buried") + PathUserBackups = path.Join(PathUserFeels, "backups") PathUserWWW = path.Join(PathUserFeels, "www") PathUserRc = path.Join(PathUserConfig, "ttbprc") PathUserNopub = path.Join(PathUserConfig, "nopub")