Modrinth: version number resolution (fixes #103)

This commit is contained in:
comp500 2023-01-20 05:39:36 +00:00
parent c596179c5c
commit d57140c33a
2 changed files with 56 additions and 10 deletions

View File

@ -39,9 +39,17 @@ var installCmd = &cobra.Command{
var projectID, versionID, versionFilename string var projectID, versionID, versionFilename string
if projectIDFlag != "" { if projectIDFlag != "" {
projectID = projectIDFlag projectID = projectIDFlag
if len(args) != 0 {
fmt.Println("--project-id cannot be used with a separately specified URL/slug/search term")
os.Exit(1)
}
} }
if versionIDFlag != "" { if versionIDFlag != "" {
versionID = versionIDFlag versionID = versionIDFlag
if len(args) != 0 {
fmt.Println("--version-id cannot be used with a separately specified URL/slug/search term")
os.Exit(1)
}
} }
if versionFilenameFlag != "" { if versionFilenameFlag != "" {
versionFilename = versionFilenameFlag versionFilename = versionFilenameFlag
@ -52,17 +60,15 @@ var installCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
// Try interpreting the argument as a slug/project ID, or project/version/CDN URL
var version string var version string
parsedSlug, err := parseSlugOrUrl(args[0], &projectID, &version, &versionID, &versionFilename) var parsedSlug bool
if err != nil { if projectID == "" && versionID == "" {
fmt.Printf("Failed to parse URL: %v\n", err) // Try interpreting the argument as a slug/project ID, or project/version/CDN URL
os.Exit(1) parsedSlug, err = parseSlugOrUrl(args[0], &projectID, &version, &versionID, &versionFilename)
} if err != nil {
fmt.Printf("Failed to parse URL: %v\n", err)
if version != "" && versionID == "" { os.Exit(1)
// TODO: resolve version (could be an ID, could be a version number) into ID }
versionID = version
} }
// Got version ID; install using this ID // Got version ID; install using this ID
@ -82,6 +88,22 @@ var installCmd = &cobra.Command{
project, err = mrDefaultClient.Projects.Get(projectID) project, err = mrDefaultClient.Projects.Get(projectID)
if err == nil { if err == nil {
// We found a project with that id/slug // We found a project with that id/slug
if version != "" {
// Try to look up version number
versionData, err := resolveVersion(project, version)
if err != nil {
fmt.Printf("Failed to add project: %s\n", err)
os.Exit(1)
}
err = installVersion(project, versionData, versionFilename, pack, &index)
if err != nil {
fmt.Printf("Failed to add project: %s\n", err)
os.Exit(1)
}
return
}
// No version specified; find latest
err = installProject(project, versionFilename, pack, &index) err = installProject(project, versionFilename, pack, &index)
if err != nil { if err != nil {
fmt.Printf("Failed to add project: %s\n", err) fmt.Printf("Failed to add project: %s\n", err)

View File

@ -341,3 +341,27 @@ func getInstalledProjectIDs(index *core.Index) []string {
} }
return installedProjects return installedProjects
} }
func resolveVersion(project *modrinthApi.Project, version string) (*modrinthApi.Version, error) {
// If it exists in the version list, it is already a version ID (and doesn't need querying further)
if slices.Contains(project.Versions, version) {
versionData, err := mrDefaultClient.Versions.Get(version)
if err != nil {
return nil, fmt.Errorf("failed to fetch version %s: %v", version, err)
}
return versionData, nil
}
// Look up all versions
// TODO: PR a version number filter to Modrinth?
versionsList, err := mrDefaultClient.Versions.ListVersions(*project.ID, modrinthApi.ListVersionsOptions{})
if err != nil {
return nil, fmt.Errorf("failed to fetch version list for %s: %v", *project.ID, err)
}
for _, v := range versionsList {
if *v.VersionNumber == version {
return v, nil
}
}
return nil, fmt.Errorf("unable to find version %s", version)
}