mirror of
https://github.com/packwiz/packwiz.git
synced 2025-05-01 18:26:31 +02:00
File writing, index sorting
This commit is contained in:
parent
d6b55b8032
commit
64a73afdf7
@ -1,4 +1,12 @@
|
|||||||
package core
|
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.
|
// Index is a representation of the index.toml file for referencing all the files in a pack.
|
||||||
type Index struct {
|
type Index struct {
|
||||||
HashFormat string `toml:"hash-format"`
|
HashFormat string `toml:"hash-format"`
|
||||||
@ -8,6 +16,8 @@ type Index struct {
|
|||||||
HashFormat string `toml:"hash-format,omitempty"`
|
HashFormat string `toml:"hash-format,omitempty"`
|
||||||
Alias string `toml:"alias,omitempty"`
|
Alias string `toml:"alias,omitempty"`
|
||||||
} `toml:"files"`
|
} `toml:"files"`
|
||||||
|
flags Flags
|
||||||
|
indexFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadIndex loads the index file
|
// LoadIndex loads the index file
|
||||||
@ -17,8 +27,17 @@ func LoadIndex(flags Flags) (Index, error) {
|
|||||||
return Index{}, err
|
return Index{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = indexFile // TODO finish
|
data, err := ioutil.ReadFile(indexFile)
|
||||||
return Index{}, nil
|
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.
|
// RemoveFile removes a file from the index.
|
||||||
@ -32,3 +51,40 @@ func (in Index) RemoveFile(path string) {
|
|||||||
in.Files = newFiles
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
18
core/pack.go
18
core/pack.go
@ -1,6 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
@ -20,8 +20,18 @@ type Pack struct {
|
|||||||
|
|
||||||
// LoadPack loads the modpack metadata to a Pack struct
|
// LoadPack loads the modpack metadata to a Pack struct
|
||||||
func LoadPack(flags Flags) (Pack, error) {
|
func LoadPack(flags Flags) (Pack, error) {
|
||||||
fmt.Println(flags.PackFile)
|
data, err := ioutil.ReadFile(flags.PackFile)
|
||||||
// TODO implement
|
if err != nil {
|
||||||
return Pack{}, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,6 @@ func ResolveIndex(flags Flags) (string, error) {
|
|||||||
if filepath.IsAbs(pack.Index.File) {
|
if filepath.IsAbs(pack.Index.File) {
|
||||||
return pack.Index.File, nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
main.go
13
main.go
@ -31,7 +31,18 @@ func init() {
|
|||||||
Name: "refresh",
|
Name: "refresh",
|
||||||
Usage: "Refresh the index file",
|
Usage: "Refresh the index file",
|
||||||
Action: func(c *cli.Context) error {
|
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
|
return nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user