Linter fixes (error handling, unused constants, raw strings)

This commit is contained in:
comp500 2023-03-10 17:06:48 +00:00
parent da04eb7f13
commit aeae76a569
9 changed files with 30 additions and 19 deletions

View File

@ -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"))

View File

@ -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") {

View File

@ -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,

View File

@ -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 {

View File

@ -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)

View File

@ -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

View File

@ -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
}

View File

@ -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")

View File

@ -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