Switch to cobra/viper

This commit is contained in:
comp500
2019-09-16 21:44:40 +01:00
parent 8915e16614
commit 4fea7ceebf
17 changed files with 985 additions and 719 deletions

View File

@@ -1,6 +0,0 @@
package core
import "github.com/urfave/cli"
// Commands stores all the commands that packwiz can run. Append to this slice to add your own commands.
var Commands []cli.Command

View File

@@ -1,33 +0,0 @@
package core
import (
"github.com/urfave/cli"
)
// Flags stores common information passed as flags to the program.
type Flags struct {
PackFile string
ModsFolder string
}
// FlagsFromContext converts a CLI context (from commands) into a Flags struct, for use in helper functions.
func FlagsFromContext(c *cli.Context) Flags {
return Flags{
c.GlobalString("pack-file"),
c.GlobalString("mods-folder"),
}
}
// CLIFlags is used internally to initialise the internal flags (easier to keep in one place)
var CLIFlags = [...]cli.Flag{
cli.StringFlag{
Name: "pack-file",
Value: "pack.toml",
Usage: "The modpack metadata file to use",
},
cli.StringFlag{
Name: "mods-folder",
Value: "mods",
Usage: "The mods folder to use",
},
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/denormal/go-gitignore"
"github.com/spf13/viper"
"github.com/vbauerster/mpb/v4"
"github.com/vbauerster/mpb/v4/decor"
)
@@ -20,7 +21,6 @@ import (
type Index struct {
HashFormat string `toml:"hash-format"`
Files []IndexFile `toml:"files"`
flags Flags
indexFile string
}
@@ -37,12 +37,11 @@ type IndexFile struct {
}
// LoadIndex attempts to load the index file from a path
func LoadIndex(indexFile string, flags Flags) (Index, error) {
func LoadIndex(indexFile string) (Index, error) {
var index Index
if _, err := toml.DecodeFile(indexFile, &index); err != nil {
return Index{}, err
}
index.flags = flags
index.indexFile = indexFile
if len(index.HashFormat) == 0 {
index.HashFormat = "sha256"
@@ -144,7 +143,7 @@ func (in *Index) updateFile(path string) error {
// of files, like CraftTweaker resources.
absFileDir, err := filepath.Abs(filepath.Dir(path))
if err == nil {
absModsDir, err := filepath.Abs(in.flags.ModsFolder)
absModsDir, err := filepath.Abs(viper.GetString("mods-folder"))
if err == nil {
if absFileDir == absModsDir {
mod = true
@@ -161,11 +160,11 @@ func (in *Index) Refresh() error {
// for i := 0; i < runtime.NumCPU(); i++ {}
// Is case-sensitivity a problem?
pathPF, _ := filepath.Abs(in.flags.PackFile)
pathPF, _ := filepath.Abs(viper.GetString("pack-file"))
pathIndex, _ := filepath.Abs(in.indexFile)
// TODO: A method of specifying pack root directory?
packRoot := filepath.Dir(in.flags.PackFile)
packRoot := filepath.Dir(viper.GetString("pack-file"))
ignoreExists := true
pathIgnore, _ := filepath.Abs(filepath.Join(packRoot, ".packwizignore"))
ignore, err := gitignore.NewFromFile(filepath.Join(packRoot, ".packwizignore"))

View File

@@ -67,8 +67,8 @@ func LoadMod(modFile string) (Mod, error) {
}
// SetMetaName sets the mod metadata file from a given file name (to be put in the mods folder)
func (m *Mod) SetMetaName(metaName string, flags Flags) string {
m.metaFile = ResolveMod(metaName, flags)
func (m *Mod) SetMetaName(metaName string) string {
m.metaFile = ResolveMod(metaName)
return m.metaFile
}

View File

@@ -1,4 +1,5 @@
package core
import (
"crypto/sha256"
"encoding/hex"
@@ -8,6 +9,7 @@ import (
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/spf13/viper"
)
// Pack stores the modpack metadata, usually in pack.toml
@@ -22,36 +24,34 @@ type Pack struct {
Versions map[string]string `toml:"versions"`
Client map[string]toml.Primitive `toml:"client"`
Server map[string]toml.Primitive `toml:"server"`
flags Flags
}
// LoadPack loads the modpack metadata to a Pack struct
func LoadPack(flags Flags) (Pack, error) {
func LoadPack() (Pack, error) {
var modpack Pack
if _, err := toml.DecodeFile(flags.PackFile, &modpack); err != nil {
if _, err := toml.DecodeFile(viper.GetString("pack-file"), &modpack); err != nil {
return Pack{}, err
}
if len(modpack.Index.File) == 0 {
modpack.Index.File = "index.toml"
}
modpack.flags = flags
return modpack, nil
}
// LoadIndex attempts to load the index file of this modpack
func (pack Pack) LoadIndex() (Index, error) {
if filepath.IsAbs(pack.Index.File) {
return LoadIndex(pack.Index.File, pack.flags)
return LoadIndex(pack.Index.File)
}
fileNative := filepath.FromSlash(pack.Index.File)
return LoadIndex(filepath.Join(filepath.Dir(pack.flags.PackFile), fileNative), pack.flags)
return LoadIndex(filepath.Join(filepath.Dir(viper.GetString("pack-file")), fileNative))
}
// UpdateIndexHash recalculates the hash of the index file of this modpack
func (pack *Pack) UpdateIndexHash() error {
fileNative := filepath.FromSlash(pack.Index.File)
indexFile := filepath.Join(filepath.Dir(pack.flags.PackFile), fileNative)
indexFile := filepath.Join(filepath.Dir(viper.GetString("pack-file")), fileNative)
if filepath.IsAbs(pack.Index.File) {
indexFile = pack.Index.File
}
@@ -78,7 +78,7 @@ func (pack *Pack) UpdateIndexHash() error {
// Write saves the pack file
func (pack Pack) Write() error {
f, err := os.Create(pack.flags.PackFile)
f, err := os.Create(viper.GetString("pack-file"))
if err != nil {
return err
}
@@ -98,4 +98,3 @@ func (pack Pack) GetMCVersion() (string, error) {
}
return mcVersion, nil
}

View File

@@ -3,14 +3,16 @@ package core
import (
"path/filepath"
"strings"
"github.com/spf13/viper"
)
// ModExtension is the file extension of the mod metadata files
const ModExtension = ".toml"
// ResolveMod returns the path to a mod file from it's name
func ResolveMod(modName string, flags Flags) string {
func ResolveMod(modName string) string {
// TODO: should this work for any metadata file?
fileName := strings.ToLower(strings.TrimSuffix(modName, ModExtension)) + ModExtension
return filepath.Join(flags.ModsFolder, fileName)
return filepath.Join(viper.GetString("mods-folder"), fileName)
}