~welt/murse

f0e4c0e5d6a82e3437cbef596da8a2304d0859e0 — welt 2 months ago fb8939e master
Add tagging support.
4 files changed, 71 insertions(+), 2 deletions(-)

M const.go
M main.go
M rewrite.go
M versioning.go
M const.go => const.go +1 -0
@@ 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."
)

M main.go => main.go +18 -0
@@ 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)

M rewrite.go => rewrite.go +48 -0
@@ 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
}

M versioning.go => versioning.go +4 -2
@@ 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 {