diff --git a/main.go b/main.go index e25f852..c77cf5f 100644 --- a/main.go +++ b/main.go @@ -166,12 +166,12 @@ var commands = []Command{ IsVarargs: true, Action: func(requiredArguments []string, providedArguments []string) (string, error) { - if len(providedArguments) != len(requiredArguments) { + if len(providedArguments) < len(requiredArguments) { return "", fmt.Errorf("expected %s followed by one or more %ss", requiredArguments[0], requiredArguments[1]) } - if game, openGameError := LoadGame(providedArguments[0]); openGameError != nil { + if game, openGameError := LoadGame(providedArguments[0]); openGameError == nil { if deployError := game.Deploy(providedArguments[1:]); deployError != nil { return "", deployError } diff --git a/manager.go b/manager.go index 13e2275..0e2c371 100644 --- a/manager.go +++ b/manager.go @@ -42,6 +42,10 @@ func (game *Game) Clean() error { } func (game *Game) Deploy(names []string) error { + if cleanError := game.Clean(); cleanError != nil { + return cleanError + } + for _, name := range names { var mod, exists = game.Mods[name] @@ -83,32 +87,20 @@ func (game *Game) Deploy(names []string) error { // Backup up any pre-existing file before it is overwritten by Zip entry // extraction. - if fileInfo, statError := os.Stat(deployPath); statError == nil { + if _, statError := os.Stat(deployPath); statError == nil { var backupPath = filepath.Join( cachePath(), game.Name, "overwritten", zipFile.Name) - if dirError := os.MkdirAll(filepath.Dir(backupPath), fileInfo.Mode()); dirError != nil { - if closeError := zipReadCloser.Close(); closeError != nil { - return closeError - } - + if dirError := os.MkdirAll(filepath.Dir(backupPath), os.ModePerm); dirError != nil { return dirError } if renameError := os.Rename(deployPath, backupPath); renameError != nil { - if closeError := zipReadCloser.Close(); closeError != nil { - return closeError - } - return renameError } game.OverwrittenFilePaths = append(game.OverwrittenFilePaths, zipFile.Name) } else if !(os.IsNotExist(statError)) { - if closeError := zipReadCloser.Close(); closeError != nil { - return closeError - } - return statError } @@ -337,7 +329,7 @@ func LoadGame(name string) (Game, error) { OverwrittenFilePaths: make([]string, 0, 512), DeployedFilePaths: make([]string, 0, 512), Mods: make(map[string]Mod), - Path: entry.Key, + Path: entry.Value, } if loadError := game.Load(); loadError != nil { @@ -487,10 +479,18 @@ func fallbackPath(getFalliblePath func() (string, error), fallbackSubdir string) } func (game *Game) saveDeployment() error { - var listPath = filepath.Join(cachePath(), "deployed.txt") + var listPath = filepath.Join(cachePath(), game.Name, "deployed.txt") if len(game.DeployedFilePaths) == 0 { - return os.Truncate(listPath, 0) + var truncateError = os.Truncate(listPath, 0) + + if !(os.IsNotExist(truncateError)) { + return truncateError + } + } + + if dirError := os.MkdirAll(filepath.Dir(listPath), os.ModePerm); dirError != nil { + return dirError } if file, updateError := os.Create(listPath); updateError == nil {