From 64a73afdf740225ee319037a673fa1f4096dfa85 Mon Sep 17 00:00:00 2001 From: comp500 Date: Thu, 25 Apr 2019 19:22:02 +0100 Subject: [PATCH] File writing, index sorting --- core/index.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- core/pack.go | 18 +++++++++++---- core/resolve.go | 2 +- main.go | 13 ++++++++++- 4 files changed, 85 insertions(+), 8 deletions(-) diff --git a/core/index.go b/core/index.go index 4aa3fbb..0c8e9ea 100644 --- a/core/index.go +++ b/core/index.go @@ -1,4 +1,12 @@ package core +import ( + "io/ioutil" + "os" + "sort" + + "github.com/BurntSushi/toml" +) + // Index is a representation of the index.toml file for referencing all the files in a pack. type Index struct { HashFormat string `toml:"hash-format"` @@ -8,6 +16,8 @@ type Index struct { HashFormat string `toml:"hash-format,omitempty"` Alias string `toml:"alias,omitempty"` } `toml:"files"` + flags Flags + indexFile string } // LoadIndex loads the index file @@ -17,8 +27,17 @@ func LoadIndex(flags Flags) (Index, error) { return Index{}, err } - _ = indexFile // TODO finish - return Index{}, nil + data, err := ioutil.ReadFile(indexFile) + if err != nil { + return Index{}, err + } + var index Index + if _, err := toml.Decode(string(data), &index); err != nil { + return Index{}, err + } + index.flags = flags + index.indexFile = indexFile + return index, nil } // RemoveFile removes a file from the index. @@ -32,3 +51,40 @@ func (in Index) RemoveFile(path string) { in.Files = newFiles } +// resortIndex sorts Files by file name +func (in Index) resortIndex() { + sort.SliceStable(in.Files, func(i, j int) bool { + // Compare by alias if names are equal? + // Remove duplicated entries? (compound key on file/alias?) + return in.Files[i].File < in.Files[j].File + }) +} + +// Refresh updates the hashes of all the files in the index, and adds new files to the index +func (in Index) Refresh() error { + // TODO: implement + // process: + // enumerate files, exclude index and pack.toml + // hash them + // check if they exist in list + // if exists, modify existing entry(ies) + // if not exists, add new entry + // resort + in.resortIndex() + return nil +} + +// Write saves the index file +func (in Index) Write() error { + f, err := os.Create(in.indexFile) + if err != nil { + return err + } + defer f.Close() + + enc := toml.NewEncoder(f) + // Disable indentation + enc.Indent = "" + return enc.Encode(in) +} + diff --git a/core/pack.go b/core/pack.go index 12f94a7..fadd0a4 100644 --- a/core/pack.go +++ b/core/pack.go @@ -1,6 +1,6 @@ package core import ( - "fmt" + "io/ioutil" "github.com/BurntSushi/toml" ) @@ -20,8 +20,18 @@ type Pack struct { // LoadPack loads the modpack metadata to a Pack struct func LoadPack(flags Flags) (Pack, error) { - fmt.Println(flags.PackFile) - // TODO implement - return Pack{}, nil + data, err := ioutil.ReadFile(flags.PackFile) + if err != nil { + return Pack{}, err + } + var modpack Pack + if _, err := toml.Decode(string(data), &modpack); err != nil { + return Pack{}, err + } + + if len(modpack.Index.File) == 0 { + modpack.Index.File = "index.toml" + } + return modpack, nil } diff --git a/core/resolve.go b/core/resolve.go index f2d5341..e64dbb3 100644 --- a/core/resolve.go +++ b/core/resolve.go @@ -22,6 +22,6 @@ func ResolveIndex(flags Flags) (string, error) { if filepath.IsAbs(pack.Index.File) { return pack.Index.File, nil } - return filepath.Join(flags.PackFile, pack.Index.File), nil + return filepath.Join(filepath.Dir(flags.PackFile), pack.Index.File), nil } diff --git a/main.go b/main.go index 0a64204..2d5a0f9 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,18 @@ func init() { Name: "refresh", Usage: "Refresh the index file", Action: func(c *cli.Context) error { - // TODO: implement + index, err := core.LoadIndex(core.FlagsFromContext(c)) + if err != nil { + return err + } + err = index.Refresh() + if err != nil { + return err + } + err = index.Write() + if err != nil { + return err + } return nil }, })