From aeae76a569e0801b5996a9f6882cfafb906a8672 Mon Sep 17 00:00:00 2001
From: comp500 <comp500@users.noreply.github.com>
Date: Fri, 10 Mar 2023 17:06:48 +0000
Subject: [PATCH] Linter fixes (error handling, unused constants, raw strings)

---
 cmd/root.go              |  4 ++++
 cmd/serve.go             |  5 ++++-
 core/download.go         |  2 +-
 core/index.go            |  3 +--
 core/mod.go              | 14 +++++++-------
 curseforge/curseforge.go | 12 ++++++------
 modrinth/export.go       |  2 +-
 modrinth/modrinth.go     |  4 +++-
 url/install.go           |  3 +++
 9 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/cmd/root.go b/cmd/root.go
index a0f2d6e..b7511d2 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -56,6 +56,10 @@ func init() {
 	_ = viper.BindPFlag("meta-folder-base", rootCmd.PersistentFlags().Lookup("meta-folder-base"))
 
 	defaultCacheDir, err := core.GetPackwizCache()
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
 	rootCmd.PersistentFlags().String("cache", defaultCacheDir, "The directory where packwiz will cache downloaded mods")
 	_ = viper.BindPFlag("cache.directory", rootCmd.PersistentFlags().Lookup("cache"))
 
diff --git a/cmd/serve.go b/cmd/serve.go
index d8ab233..88d6656 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -58,7 +58,10 @@ var serveCmd = &cobra.Command{
 			}
 
 			indexPageBuf := new(bytes.Buffer)
-			t.Execute(indexPageBuf, struct{ Port string }{Port: port})
+			err = t.Execute(indexPageBuf, struct{ Port string }{Port: port})
+			if err != nil {
+				panic(fmt.Errorf("failed to compile index page template: %w", err))
+			}
 
 			// Force-disable no-internal-hashes mode (equiv to --build flag in refresh) for serving over HTTP
 			if viper.GetBool("no-internal-hashes") {
diff --git a/core/download.go b/core/download.go
index 86d040e..d176719 100644
--- a/core/download.go
+++ b/core/download.go
@@ -595,7 +595,7 @@ func CreateDownloadSession(mods []*Mod, hashesToObtain []string) (DownloadSessio
 
 	// Get necessary metadata for all files
 	for _, mod := range mods {
-		if mod.Download.Mode == "url" || mod.Download.Mode == "" {
+		if mod.Download.Mode == ModeURL || mod.Download.Mode == "" {
 			downloadSession.downloadTasks = append(downloadSession.downloadTasks, downloadTask{
 				mod:        mod,
 				url:        mod.Download.URL,
diff --git a/core/index.go b/core/index.go
index ae37db3..c26e781 100644
--- a/core/index.go
+++ b/core/index.go
@@ -208,9 +208,8 @@ func (in *Index) Refresh() error {
 	pathIndex, _ := filepath.Abs(in.indexFile)
 
 	packRoot := in.GetPackRoot()
-	ignoreExists := true
 	pathIgnore, _ := filepath.Abs(filepath.Join(packRoot, ".packwizignore"))
-	ignore, ignoreExists := readGitignore(filepath.Join(packRoot, ".packwizignore"))
+	ignore, ignoreExists := readGitignore(pathIgnore)
 
 	var fileList []string
 	err := filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error {
diff --git a/core/mod.go b/core/mod.go
index 2b7a207..05a6f33 100644
--- a/core/mod.go
+++ b/core/mod.go
@@ -25,8 +25,8 @@ type Mod struct {
 }
 
 const (
-	modeURL string = "url"
-	modeCF  string = "metadata:curseforge"
+	ModeURL string = "url"
+	ModeCF  string = "metadata:curseforge"
 )
 
 // ModDownload specifies how to download the mod file
@@ -132,11 +132,11 @@ func (m Mod) GetDestFilePath() string {
 	return filepath.Join(filepath.Dir(m.metaFile), filepath.FromSlash(m.FileName))
 }
 
-var slugifyRegex1 = regexp.MustCompile("\\(.*\\)")
-var slugifyRegex2 = regexp.MustCompile(" - .+")
-var slugifyRegex3 = regexp.MustCompile("[^a-z\\d]")
-var slugifyRegex4 = regexp.MustCompile("-+")
-var slugifyRegex5 = regexp.MustCompile("^-|-$")
+var slugifyRegex1 = regexp.MustCompile(`\(.*\)`)
+var slugifyRegex2 = regexp.MustCompile(` - .+`)
+var slugifyRegex3 = regexp.MustCompile(`[^a-z\d]`)
+var slugifyRegex4 = regexp.MustCompile(`-+`)
+var slugifyRegex5 = regexp.MustCompile(`^-|-$`)
 
 func SlugifyName(name string) string {
 	lower := strings.ToLower(name)
diff --git a/curseforge/curseforge.go b/curseforge/curseforge.go
index 8630e42..89bff29 100644
--- a/curseforge/curseforge.go
+++ b/curseforge/curseforge.go
@@ -29,7 +29,7 @@ func init() {
 	core.MetaDownloaders["curseforge"] = cfDownloader{}
 }
 
-var snapshotVersionRegex = regexp.MustCompile("(?:Snapshot )?(\\d+)w0?(0|[1-9]\\d*)([a-z])")
+var snapshotVersionRegex = regexp.MustCompile(`(?:Snapshot )?(\d+)w0?(0|[1-9]\d*)([a-z])`)
 
 var snapshotNames = [...]string{"-pre", " Pre-Release ", " Pre-release ", "-rc"}
 
@@ -119,9 +119,9 @@ func getCurseforgeVersions(mcVersions []string) []string {
 }
 
 var urlRegexes = [...]*regexp.Regexp{
-	regexp.MustCompile("^https?://(?P<game>minecraft)\\.curseforge\\.com/projects/(?P<slug>[^/]+)(?:/(?:files|download)/(?P<fileID>\\d+))?"),
-	regexp.MustCompile("^https?://(?:www\\.|beta\\.)?curseforge\\.com/(?P<game>[^/]+)/(?P<category>[^/]+)/(?P<slug>[^/]+)(?:/(?:files|download)/(?P<fileID>\\d+))?"),
-	regexp.MustCompile("^(?P<slug>[a-z][\\da-z\\-_]{0,127})$"),
+	regexp.MustCompile(`^https?://(?P<game>minecraft)\.curseforge\.com/projects/(?P<slug>[^/]+)(?:/(?:files|download)/(?P<fileID>\d+))?`),
+	regexp.MustCompile(`^https?://(?:www\.|beta\.)?curseforge\.com/(?P<game>[^/]+)/(?P<category>[^/]+)/(?P<slug>[^/]+)(?:/(?:files|download)/(?P<fileID>\d+))?`),
+	regexp.MustCompile(`^(?P<slug>[a-z][\da-z\-_]{0,127})$`),
 }
 
 func parseSlugOrUrl(url string) (game string, category string, slug string, fileID uint32, err error) {
@@ -203,7 +203,7 @@ func createModFile(modInfo modInfo, fileInfo modFileInfo, index *core.Index, opt
 		Download: core.ModDownload{
 			HashFormat: hashFormat,
 			Hash:       hash,
-			Mode:       "metadata:curseforge",
+			Mode:       core.ModeCF,
 		},
 		Option: optional,
 		Update: updateMap,
@@ -456,7 +456,7 @@ func (u cfUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error {
 		v.Download = core.ModDownload{
 			HashFormat: hashFormat,
 			Hash:       hash,
-			Mode:       "metadata:curseforge",
+			Mode:       core.ModeCF,
 		}
 
 		v.Update["curseforge"]["project-id"] = modState.ID
diff --git a/modrinth/export.go b/modrinth/export.go
index c653f5c..a471756 100644
--- a/modrinth/export.go
+++ b/modrinth/export.go
@@ -280,7 +280,7 @@ var whitelistedHosts = []string{
 }
 
 func canBeIncludedDirectly(mod *core.Mod, restrictDomains bool) bool {
-	if mod.Download.Mode == "url" || mod.Download.Mode == "" {
+	if mod.Download.Mode == core.ModeURL || mod.Download.Mode == "" {
 		if !restrictDomains {
 			return true
 		}
diff --git a/modrinth/modrinth.go b/modrinth/modrinth.go
index 9371823..1d0e24a 100644
--- a/modrinth/modrinth.go
+++ b/modrinth/modrinth.go
@@ -282,7 +282,9 @@ func getLatestVersion(projectID string, name string, pack core.Pack) (*modrinthA
 		GameVersions: gameVersions,
 		Loaders:      loaders,
 	})
-
+	if err != nil {
+		return nil, fmt.Errorf("failed to fetch latest version: %w", err)
+	}
 	if len(result) == 0 {
 		// TODO: retry with datapack specified, to determine what the issue is? or just request all and filter afterwards
 		return nil, errors.New("no valid versions found\nUse the acceptable-game-versions option to accept more game versions\nTo use datapacks, add a datapack loader mod and specify the datapack-folder option with the folder this mod loads datapacks from")
diff --git a/url/install.go b/url/install.go
index 9213402..d30cc77 100644
--- a/url/install.go
+++ b/url/install.go
@@ -126,6 +126,9 @@ var installCmd = &cobra.Command{
 func getSha1(url string) (string, error) {
 	// TODO: hook up to existing cache system? might not be that useful
 	mainHasher, err := core.GetHashImpl("sha1")
+	if err != nil {
+		return "", err
+	}
 	resp, err := http.Get(url)
 	if err != nil {
 		return "", err