Fix unhandled error warnings, error messages (except import.go)

This commit is contained in:
comp500 2019-11-15 22:28:14 +00:00
parent e51537c677
commit a553c3ce08
12 changed files with 113 additions and 44 deletions

View File

@ -243,25 +243,25 @@ func init() {
initCmd.Flags().String("name", "", "The name of the modpack (omit to define interactively)")
initCmd.Flags().String("index-file", "index.toml", "The index file to use")
viper.BindPFlag("init.index-file", initCmd.Flags().Lookup("index-file"))
_ = viper.BindPFlag("init.index-file", initCmd.Flags().Lookup("index-file"))
initCmd.Flags().String("mc-version", "", "The Minecraft version to use (omit to define interactively)")
viper.BindPFlag("init.mc-version", initCmd.Flags().Lookup("mc-version"))
_ = viper.BindPFlag("init.mc-version", initCmd.Flags().Lookup("mc-version"))
initCmd.Flags().BoolP("latest", "l", false, "Automatically select the latest version of Minecraft")
viper.BindPFlag("init.latest", initCmd.Flags().Lookup("latest"))
_ = viper.BindPFlag("init.latest", initCmd.Flags().Lookup("latest"))
initCmd.Flags().BoolP("snapshot", "s", false, "Use the latest snapshot version with --latest")
viper.BindPFlag("init.snapshot", initCmd.Flags().Lookup("snapshot"))
_ = viper.BindPFlag("init.snapshot", initCmd.Flags().Lookup("snapshot"))
initCmd.Flags().BoolP("reinit", "r", false, "Recreate the pack file if it already exists, rather than exiting")
viper.BindPFlag("init.reinit", initCmd.Flags().Lookup("reinit"))
_ = viper.BindPFlag("init.reinit", initCmd.Flags().Lookup("reinit"))
initCmd.Flags().String("modloader", "", "The mod loader to use (omit to define interactively)")
viper.BindPFlag("init.modloader", initCmd.Flags().Lookup("modloader"))
_ = viper.BindPFlag("init.modloader", initCmd.Flags().Lookup("modloader"))
// ok this is epic
for _, loader := range modLoaders {
for _, component := range loader {
initCmd.Flags().String(component.Name+"-version", "", "The "+component.FriendlyName+" version to use (omit to define interactively)")
viper.BindPFlag("init."+component.Name+"-version", initCmd.Flags().Lookup(component.Name+"-version"))
_ = viper.BindPFlag("init."+component.Name+"-version", initCmd.Flags().Lookup(component.Name+"-version"))
initCmd.Flags().Bool(component.Name+"-latest", false, "Automatically select the latest version of "+component.FriendlyName)
viper.BindPFlag("init."+component.Name+"-latest", initCmd.Flags().Lookup(component.Name+"-latest"))
_ = viper.BindPFlag("init."+component.Name+"-latest", initCmd.Flags().Lookup(component.Name+"-latest"))
}
}
}

View File

@ -36,10 +36,10 @@ func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&packFile, "pack-file", "pack.toml", "The modpack metadata file to use")
viper.BindPFlag("pack-file", rootCmd.PersistentFlags().Lookup("pack-file"))
_ = viper.BindPFlag("pack-file", rootCmd.PersistentFlags().Lookup("pack-file"))
rootCmd.PersistentFlags().StringVar(&modsFolder, "mods-folder", "mods", "The default folder to store mod metadata files in")
viper.BindPFlag("mods-folder", rootCmd.PersistentFlags().Lookup("mods-folder"))
_ = viper.BindPFlag("mods-folder", rootCmd.PersistentFlags().Lookup("mods-folder"))
file, err := os.UserConfigDir()
if err != nil {

View File

@ -111,21 +111,24 @@ var serveCmd = &cobra.Command{
if err != nil {
fmt.Printf("Error reading file \"%s\": %s\n", path, err)
w.WriteHeader(404)
w.Write([]byte("File not found"))
_, _ = w.Write([]byte("File not found"))
return
}
defer f.Close()
_, err = io.Copy(w, f)
err2 := f.Close()
if err == nil {
err = err2
}
if err != nil {
fmt.Printf("Error reading file \"%s\": %s\n", path, err)
w.WriteHeader(500)
w.Write([]byte("Failed to read file"))
_, _ = w.Write([]byte("Failed to read file"))
return
}
} else {
fmt.Printf("File not found: %s\n", path)
w.WriteHeader(404)
w.Write([]byte("File not found"))
_, _ = w.Write([]byte("File not found"))
return
}
})
@ -145,9 +148,9 @@ func init() {
rootCmd.AddCommand(serveCmd)
serveCmd.Flags().IntP("port", "p", 8080, "The port to run the server on")
viper.BindPFlag("serve.port", serveCmd.Flags().Lookup("port"))
_ = viper.BindPFlag("serve.port", serveCmd.Flags().Lookup("port"))
serveCmd.Flags().BoolP("refresh", "r", true, "Automatically refresh the index file")
viper.BindPFlag("serve.refresh", serveCmd.Flags().Lookup("refresh"))
_ = viper.BindPFlag("serve.refresh", serveCmd.Flags().Lookup("refresh"))
serveCmd.Flags().Bool("basic", false, "Disable refreshing and allow all files in the directory, rather than just files listed in the index")
viper.BindPFlag("serve.basic", serveCmd.Flags().Lookup("basic"))
_ = viper.BindPFlag("serve.basic", serveCmd.Flags().Lookup("basic"))
}

View File

@ -125,16 +125,21 @@ func (in *Index) updateFile(path string) error {
if err != nil {
return err
}
defer f.Close()
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
}
if _, err := io.Copy(h, f); err != nil {
_ = f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
hashString := hex.EncodeToString(h.Sum(nil))
@ -270,12 +275,16 @@ func (in Index) Write() error {
if err != nil {
return err
}
defer f.Close()
enc := toml.NewEncoder(f)
// Disable indentation
enc.Indent = ""
return enc.Encode(in)
err = enc.Encode(in)
if err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// RefreshFileWithHash updates a file in the index, given a file hash and whether it is a mod or not

View File

@ -88,10 +88,10 @@ func (m Mod) Write() (string, string, error) {
return "sha256", "", err
}
}
defer f.Close()
h, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return "", "", err
}
w := io.MultiWriter(h, f)
@ -101,7 +101,11 @@ func (m Mod) Write() (string, string, error) {
enc.Indent = ""
err = enc.Encode(m)
hashString := hex.EncodeToString(h.Sum(nil))
if err != nil {
_ = f.Close()
return "sha256", hashString, err
}
return "sha256", hashString, f.Close()
}
// GetParsedUpdateData can be used to retrieve updater-specific information after parsing a mod file

View File

@ -59,23 +59,24 @@ func (pack *Pack) UpdateIndexHash() error {
if err != nil {
return err
}
defer f.Close()
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
}
if _, err := io.Copy(h, f); err != nil {
_ = f.Close()
return err
}
hashString := hex.EncodeToString(h.Sum(nil))
pack.Index.HashFormat = "sha256"
pack.Index.Hash = hashString
return nil
return f.Close()
}
// Write saves the pack file
@ -84,12 +85,16 @@ func (pack Pack) Write() error {
if err != nil {
return err
}
defer f.Close()
enc := toml.NewEncoder(f)
// Disable indentation
enc.Indent = ""
return enc.Encode(pack)
err = enc.Encode(pack)
if err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// GetMCVersion gets the version of Minecraft this pack uses, if it has been correctly specified

View File

@ -21,5 +21,5 @@ func getCurseDir() (string, error) {
if _, err := os.Stat(curseDir); err == nil {
return curseDir, nil
}
return "", errors.New("Curse installation directory cannot be found")
return "", errors.New("curse installation directory cannot be found")
}

View File

@ -33,6 +33,9 @@ func getFileIDsFromString(mod string) (bool, int, int, error) {
matches := v.FindStringSubmatch(mod)
if matches != nil && len(matches) == 3 {
modID, err := modIDFromSlug(matches[1])
if err != nil {
return true, 0, 0, err
}
fileID, err := strconv.Atoi(matches[2])
if err != nil {
return true, 0, 0, err

View File

@ -45,9 +45,7 @@ var exportCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
defer expFile.Close()
exp := zip.NewWriter(expFile)
defer exp.Close()
cfFileRefs := make([]packinterop.AddonFileReference, 0, len(mods))
for _, mod := range mods {
@ -82,18 +80,24 @@ var exportCmd = &cobra.Command{
manifestFile, err := exp.Create("manifest.json")
if err != nil {
_ = exp.Close()
_ = expFile.Close()
fmt.Println("Error creating manifest: " + err.Error())
os.Exit(1)
}
err = packinterop.WriteManifestFromPack(pack, cfFileRefs, manifestFile)
if err != nil {
_ = exp.Close()
_ = expFile.Close()
fmt.Println("Error creating manifest: " + err.Error())
os.Exit(1)
}
err = createModlist(exp, mods)
if err != nil {
_ = exp.Close()
_ = expFile.Close()
fmt.Println("Error creating mod list: " + err.Error())
os.Exit(1)
}
@ -124,6 +128,17 @@ var exportCmd = &cobra.Command{
}
}
err = exp.Close()
if err != nil {
fmt.Println("Error writing export file: " + err.Error())
os.Exit(1)
}
err = expFile.Close()
if err != nil {
fmt.Println("Error writing export file: " + err.Error())
os.Exit(1)
}
fmt.Println("Modpack exported to export.zip!")
},
}

View File

@ -79,22 +79,24 @@ func modIDFromSlug(slug string) (int, error) {
}
if len(response.Exception) > 0 || len(response.Message) > 0 {
return 0, fmt.Errorf("Error requesting id for slug: %s", response.Message)
return 0, fmt.Errorf("error requesting id for slug: %s", response.Message)
}
if len(response.Data.Addons) < 1 {
return 0, errors.New("Addon not found")
return 0, errors.New("addon not found")
}
return response.Data.Addons[0].ID, nil
}
//noinspection GoUnusedConst
const (
fileTypeRelease int = iota + 1
fileTypeBeta
fileTypeAlpha
)
//noinspection GoUnusedConst
const (
dependencyTypeEmbedded int = iota + 1
dependencyTypeOptional
@ -146,7 +148,7 @@ func getModInfo(modID int) (modInfo, error) {
}
if infoRes.ID != modID {
return modInfo{}, fmt.Errorf("Unexpected addon ID in CurseForge response: %d/%d", modID, infoRes.ID)
return modInfo{}, fmt.Errorf("unexpected addon ID in CurseForge response: %d/%d", modID, infoRes.ID)
}
return infoRes, nil
@ -250,7 +252,7 @@ func getFileInfo(modID int, fileID int) (modFileInfo, error) {
}
if infoRes.ID != fileID {
return modFileInfo{}, fmt.Errorf("Unexpected file ID in CurseForge response: %d/%d", modID, infoRes.ID)
return modFileInfo{}, fmt.Errorf("unexpected file ID in CurseForge response: %d/%d", modID, infoRes.ID)
}
return infoRes, nil

View File

@ -24,7 +24,11 @@ Please note that the completions may be incomplete or broken, see https://github
Run: func(cmd *cobra.Command, args []string) {
if args[0] == "bash" {
if viper.GetBool("utils.completion.source") {
cmd.Root().GenBashCompletion(os.Stdout)
err := cmd.Root().GenBashCompletion(os.Stdout)
if err != nil {
fmt.Printf("Error generating completion file: %s\n", err)
os.Exit(1)
}
} else {
file, err := getConfigPath("completion.sh")
if err != nil {
@ -59,7 +63,11 @@ Please note that the completions may be incomplete or broken, see https://github
}
if !commandFound {
// Append to bashrc
os.MkdirAll(filepath.Dir(bashrc), os.ModePerm)
err = os.MkdirAll(filepath.Dir(bashrc), os.ModePerm)
if err != nil {
fmt.Printf("Failed to make folder for bashrc: %s\n", err)
os.Exit(1)
}
f, err := os.OpenFile(bashrc, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Failed to open bashrc: %s\n", err)
@ -68,16 +76,24 @@ Please note that the completions may be incomplete or broken, see https://github
_, err = f.WriteString("\r\n" + command + "\r\n")
if err != nil {
fmt.Printf("Failed to write to bashrc: %s\n", err)
f.Close()
_ = f.Close()
os.Exit(1)
}
err = f.Close()
if err != nil {
fmt.Printf("Failed to write to bashrc: %s\n", err)
os.Exit(1)
}
f.Close()
}
fmt.Println("Completions installed! Restart your shell to load them.")
}
} else if args[0] == "powershell" {
if viper.GetBool("utils.completion.source") {
cmd.Root().GenPowerShellCompletion(os.Stdout)
err := cmd.Root().GenPowerShellCompletion(os.Stdout)
if err != nil {
fmt.Printf("Error generating completion file: %s\n", err)
os.Exit(1)
}
} else {
file, err := getConfigPath("completion.ps1")
if err != nil {
@ -111,7 +127,11 @@ Please note that the completions may be incomplete or broken, see https://github
}
if !commandFound {
// Append to profile
os.MkdirAll(filepath.Dir(profile), os.ModePerm)
err = os.MkdirAll(filepath.Dir(profile), os.ModePerm)
if err != nil {
fmt.Printf("Failed to make folder for profile: %s\n", err)
os.Exit(1)
}
f, err := os.OpenFile(profile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Failed to open profile: %s\n", err)
@ -120,16 +140,24 @@ Please note that the completions may be incomplete or broken, see https://github
_, err = f.WriteString("\r\n" + command + "\r\n")
if err != nil {
fmt.Printf("Failed to write to profile: %s\n", err)
f.Close()
_ = f.Close()
os.Exit(1)
}
err = f.Close()
if err != nil {
fmt.Printf("Failed to write to profile: %s\n", err)
os.Exit(1)
}
f.Close()
}
fmt.Println("Completions installed! Restart your shell to load them.")
}
} else if args[0] == "zsh" {
if viper.GetBool("utils.completion.source") {
cmd.Root().GenZshCompletion(os.Stdout)
err := cmd.Root().GenZshCompletion(os.Stdout)
if err != nil {
fmt.Printf("Error generating completion file: %s\n", err)
os.Exit(1)
}
} else {
file, err := getConfigPath("completion.zsh")
if err != nil {
@ -182,5 +210,5 @@ func init() {
utilsCmd.AddCommand(completionCmd)
completionCmd.Flags().Bool("source", false, "Output the source of the commands to be installed, rather than installing them")
viper.BindPFlag("utils.completion.source", completionCmd.Flags().Lookup("source"))
_ = viper.BindPFlag("utils.completion.source", completionCmd.Flags().Lookup("source"))
}

View File

@ -35,5 +35,5 @@ func init() {
utilsCmd.AddCommand(markdownCmd)
markdownCmd.Flags().String("dir", ".", "The destination directory to save docs in")
viper.BindPFlag("utils.markdown.dir", markdownCmd.Flags().Lookup("dir"))
_ = viper.BindPFlag("utils.markdown.dir", markdownCmd.Flags().Lookup("dir"))
}