mirror of
				https://github.com/packwiz/packwiz.git
				synced 2025-10-31 18:44:33 +01:00 
			
		
		
		
	File writing, index sorting
This commit is contained in:
		| @@ -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) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user