diff --git a/core/index.go b/core/index.go index 9a8bc95..4aa3fbb 100644 --- a/core/index.go +++ b/core/index.go @@ -1 +1,34 @@ package core +// 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"` + Files []struct { + File string `toml:"file"` + Hash string `toml:"hash"` + HashFormat string `toml:"hash-format,omitempty"` + Alias string `toml:"alias,omitempty"` + } `toml:"files"` +} + +// LoadIndex loads the index file +func LoadIndex(flags Flags) (Index, error) { + indexFile, err := ResolveIndex(flags) + if err != nil { + return Index{}, err + } + + _ = indexFile // TODO finish + return Index{}, nil +} + +// RemoveFile removes a file from the index. +func (in Index) RemoveFile(path string) { + newFiles := in.Files[:0] + for _, v := range in.Files { + if v.File != path { + newFiles = append(newFiles, v) + } + } + in.Files = newFiles +} + diff --git a/core/mod.go b/core/mod.go index 90e9dd5..f9e6e87 100644 --- a/core/mod.go +++ b/core/mod.go @@ -1,21 +1,21 @@ package core - import "github.com/BurntSushi/toml" // Mod stores metadata about a mod. This is written to a TOML file for each mod. type Mod struct { - metaFilename string - Name string `toml:"name"` - FileName string `toml:"filename"` - Side string `toml:"side"` - Optional bool `toml:"optional"` - Download struct { + metaFilename string // The filename for the metadata file, used as an ID + Name string `toml:"name"` + FileName string `toml:"filename"` + Side string `toml:"side,omitempty"` + Optional bool `toml:"optional,omitempty"` + Download struct { URL string `toml:"url"` HashFormat string `toml:"hash-format"` Hash string `toml:"hash"` } `toml:"download"` - Update map[string]toml.Primitive + Update map[string]toml.Primitive `toml:"update"` } + // The three possible values of Side (the side that the mod is on) are "server", "client", and "both". const ( ServerSide = "server" diff --git a/core/pack.go b/core/pack.go new file mode 100644 index 0000000..12f94a7 --- /dev/null +++ b/core/pack.go @@ -0,0 +1,27 @@ +package core +import ( + "fmt" + + "github.com/BurntSushi/toml" +) + +// Pack stores the modpack metadata, usually in pack.toml +type Pack struct { + Name string `toml:"name"` + Index struct { + File string `toml:"file"` + HashFormat string `toml:"hash-format"` + Hash string `toml:"hash"` + } `toml:"index"` + Versions map[string]string `toml:"versions"` + Client map[string]toml.Primitive `toml:"client"` + Server map[string]toml.Primitive `toml:"server"` +} + +// LoadPack loads the modpack metadata to a Pack struct +func LoadPack(flags Flags) (Pack, error) { + fmt.Println(flags.PackFile) + // TODO implement + return Pack{}, nil +} + diff --git a/core/resolve.go b/core/resolve.go index 745edf6..f2d5341 100644 --- a/core/resolve.go +++ b/core/resolve.go @@ -13,3 +13,15 @@ func ResolveMod(modName string, flags Flags) string { return filepath.Join(flags.ModsFolder, fileName) } +// ResolveIndex returns the path to the index file +func ResolveIndex(flags Flags) (string, error) { + pack, err := LoadPack(flags) + if err != nil { + return "", err + } + if filepath.IsAbs(pack.Index.File) { + return pack.Index.File, nil + } + return filepath.Join(flags.PackFile, pack.Index.File), nil +} + diff --git a/main.go b/main.go index d63be0f..0a64204 100644 --- a/main.go +++ b/main.go @@ -13,19 +13,25 @@ import ( func init() { core.Commands = append(core.Commands, cli.Command{ - Name: "delete", - Aliases: []string{"remove"}, - Usage: "Delete a mod from the modpack", + Name: "remove", + Aliases: []string{"delete", "uninstall"}, + Usage: "Remove a mod from the modpack", Action: func(c *cli.Context) error { - cmdDelete(core.FlagsFromContext(c)) + return cmdDelete(core.FlagsFromContext(c)) + }, + }, cli.Command{ + Name: "update", + Aliases: []string{"upgrade"}, + Usage: "Update a mod (or all mods) in the modpack", + Action: func(c *cli.Context) error { + // TODO: implement return nil }, }, cli.Command{ - Name: "delet", - Aliases: []string{"remov"}, - Usage: "Delete a mod from the modpack", + Name: "refresh", + Usage: "Refresh the index file", Action: func(c *cli.Context) error { - cmdDelete(core.FlagsFromContext(c)) + // TODO: implement return nil }, }) @@ -44,14 +50,14 @@ func main() { } } -func cmdDelete(flags core.Flags) { +func cmdDelete(flags core.Flags) error { mod := "demagnetize" err := os.Remove(core.ResolveMod(mod, flags)) if err != nil { - fmt.Printf("Error removing mod: %s", err) - } else { - fmt.Printf("Mod %s removed successfully!", mod) + return cli.NewExitError(err, 1) } + fmt.Printf("Mod %s removed successfully!", mod) // TODO: update index + return nil }