From f0e4c0e5d6a82e3437cbef596da8a2304d0859e0 Mon Sep 17 00:00:00 2001 From: welt Date: Thu, 29 Sep 2022 15:12:45 -0400 Subject: [PATCH] Add tagging support. --- const.go | 1 + main.go | 18 ++++++++++++++++++ rewrite.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ versioning.go | 6 ++++-- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/const.go b/const.go index b859983..5d20c2c 100644 --- a/const.go +++ b/const.go @@ -11,4 +11,5 @@ const ( DESC_KEY_STDIN = "Verifies the files with a key read from stdin, quarantining downloaded files in a directory under " DESC_REWRITE = "Rewrite TVN history. Proceed with caution as TVN is designed to be immutable." DESC_REWRITE_WRAP = "Converts pre-signature revision files to the post-signature wrapped version." + DESC_REWRITE_TAG = "Adds a tag to a revision file." ) diff --git a/main.go b/main.go index 94cfe86..26f18d6 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "github.com/integrii/flaggy" ) @@ -17,6 +18,8 @@ func main() { var repair bool var owGameInfo bool var verifySigs bool + var tag string + var revno string upgrade := flaggy.NewSubcommand("upgrade") upgrade.Description = DESC_UPGRADE @@ -54,7 +57,14 @@ func main() { rewriteWrap.Description = DESC_REWRITE_WRAP rewriteWrap.AddPositionalValue(&tvndir, "tvn repository", 1, true, "TVN repository to convert.") + rewriteTag := flaggy.NewSubcommand("tag") + rewriteTag.Description = DESC_REWRITE_TAG + rewriteTag.AddPositionalValue(&tvndir, "tvn repository", 1, true, "TVN repository with revision.") + rewriteTag.AddPositionalValue(&revno, "revision number", 2, true, "Number of revision file to tag.") + rewriteTag.AddPositionalValue(&tag, "tag value", 3, false, "String to tag value with, if nothing is specified it is removed.") + rewrite.AttachSubcommand(rewriteWrap, 1) + rewrite.AttachSubcommand(rewriteTag, 1) flaggy.AttachSubcommand(rewrite, 1) flaggy.SetVersion(VERSION) @@ -83,6 +93,14 @@ func main() { switch { case rewriteWrap.Used: os.Exit(rewriteWrapMain(tvndir)) + case rewriteTag.Used: + revno, err := strconv.Atoi(revno) + if err != nil || revno < 0 { + flaggy.ShowHelp("Invalid revision number supplied.") + os.Exit(1) + } + + os.Exit(rewriteTagMain(tvndir, revno, tag)) default: flaggy.ShowHelp("Please select a rewrite operation.") os.Exit(1) diff --git a/rewrite.go b/rewrite.go index f6877ba..898baa9 100644 --- a/rewrite.go +++ b/rewrite.go @@ -53,6 +53,54 @@ func rewriteWrapMain(dir string) int { file.Write(marshalled) file.Close() + } +} + +func rewriteTagMain(dir string, revno int, tag string) int { + file, err := os.OpenFile(filepath.Join(dir, "revisions", strconv.Itoa(revno)), os.O_RDWR, 0777) + if err != nil { + if os.IsNotExist(err) { + errPrintln("Revision does not exist.") + } else { + errPrintln(err) + } + + return 0 + } + + defer file.Close() + + var wrev WrappedRevision + err = json.NewDecoder(file).Decode(&wrev) + if err != nil { + errPrintln(err) + return 1 + } + + wrev.Tag = tag + marshalled, err := json.Marshal(wrev) + if err != nil { + panic(err) } + + err = file.Truncate(0) + if err != nil { + errPrintln(err) + return 1 + } + + _, err = file.Seek(0, 0) + if err != nil { + errPrintln(err) + return 1 + } + + _, err = file.Write(marshalled) + if err != nil { + errPrintln(err) + return 1 + } + + return 0 } diff --git a/versioning.go b/versioning.go index 9dd02be..c46f926 100644 --- a/versioning.go +++ b/versioning.go @@ -11,8 +11,10 @@ const ( ) type WrappedRevision struct { - No int `json:"revision"` - Changes []Change `json:"changes"` + No int `json:"revision"` + Tag string `json:"tag,omitempty"` + Changes []Change `json:"changes"` + Rest map[string]interface{} `json:"-"` // forwards compatability } type Change struct { -- 2.45.2