Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 10, 2023
1 parent 0e554ca commit 23d17a5
Show file tree
Hide file tree
Showing 22 changed files with 366 additions and 427 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2022 hub
Copyright © 2023 hub

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
Expand Down
238 changes: 114 additions & 124 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func GlobalOptions(cmd *cobra.Command) *GlobalOpts {
log.Fatalln(err)
}
if proxy != "" {
// TODO: stop changing the default http client
err := requester.DefaultHttpClient.SetProxy(proxy)
if err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -122,112 +123,21 @@ var rootCmd = &cobra.Command{
mrpack-install yK0ISmKn 1.0.0-1.18 --server-dir mcserver
mrpack-install communitypack9000 --host api.labrinth.example.org
mrpack-install --version`,
Args: cobra.RangeArgs(0, 2),
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
opts := GetRootOpts(cmd)

ver, err := cmd.Flags().GetBool("version")
if err != nil {
log.Fatalln(err)
}
if ver {
if opts.Version {
fmt.Println("mrpack-install", buildinfo.Version)
return
}

if len(args) < 1 {
err = cmd.Help()
if err != nil {
fmt.Println(err)
}
os.Exit(1)
}
input := args[0]
version := ""
if len(args) > 1 {
version = args[1]
}

err = os.MkdirAll(opts.ServerDir, 0755)
if err != nil {
log.Fatalln(err)
}

archivePath := ""
if util.PathIsFile(input) {
archivePath = input

} else if util.IsValidUrl(input) {
fmt.Println("Downloading mrpack file from", args)
file, err := requester.DefaultHttpClient.DownloadFile(input, opts.ServerDir, "")
if err != nil {
log.Fatalln(err)
}
archivePath = file
defer func(name string) {
err := os.Remove(name)
if err != nil {
fmt.Println(err)
}
}(archivePath)

} else { // input is project id or slug?
versions, err := modrinth.NewClient(opts.Host).GetProjectVersions(input, nil)
if err != nil {
log.Fatalln(err)
}

// get files uploaded for specified version or latest stable if not specified
var files []modrinth.File = nil
for i := range versions {
if version != "" {
if versions[i].VersionNumber == version {
files = versions[i].Files
break
}
} else {
if versions[i].VersionType == modrinth.ReleaseVersionType {
files = versions[i].Files
break
}
}
}
if len(files) == 0 {
log.Fatalln("No files found for", input, version)
}

for i := range files {
if strings.HasSuffix(files[i].Filename, ".mrpack") {
fmt.Println("Downloading mrpack file from", files[i].Url)
file, err := requester.DefaultHttpClient.DownloadFile(files[i].Url, opts.ServerDir, "")
if err != nil {
log.Fatalln(err)
}
archivePath = file
break
}
}
if archivePath == "" {
log.Fatalln("No mrpack file found for", input, version)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
fmt.Println(err)
}
}(archivePath)
}

if archivePath == "" {
log.Fatalln("An error occured!")
}

fmt.Println("Processing mrpack file", archivePath)

index, err := mrpack.ReadIndex(archivePath)
if err != nil {
log.Fatalln(err)
}
index, archivePath := handleArgs(input, version, opts.ServerDir, opts.Host)

for _, file := range index.Files {
ok, err := util.PathIsSubpath(file.Path, opts.ServerDir)
Expand All @@ -240,36 +150,13 @@ var rootCmd = &cobra.Command{
}

fmt.Println("Installing:", index.Name)
fmt.Printf("Flavor dependencies: %+v\n", index.Dependencies)

// download server if not present
if !util.PathIsFile(path.Join(opts.ServerDir, opts.ServerFile)) {
fmt.Println("Server file not present, downloading...\n(Point --server-dir and --server-file flags to an existing server file to skip this step.)")

var provider server.Provider
if index.Dependencies.Fabric != "" {
provider, err = server.NewProvider("fabric", index.Dependencies.Minecraft, index.Dependencies.Fabric)
if err != nil {
log.Fatalln(err)
}
} else if index.Dependencies.Quilt != "" {
provider, err = server.NewProvider("quilt", index.Dependencies.Minecraft, index.Dependencies.Quilt)
if err != nil {
log.Fatalln(err)
}
} else if index.Dependencies.Forge != "" {
provider, err = server.NewProvider("forge", index.Dependencies.Minecraft, index.Dependencies.Forge)
if err != nil {
log.Fatalln(err)
}
} else {
provider, err = server.NewProvider("vanilla", index.Dependencies.Minecraft, "")
if err != nil {
log.Fatalln(err)
}
}

err = provider.Provide(opts.ServerDir, opts.ServerFile)
inst := server.InstallerFromDeps(&index.Dependencies)
err := inst.Install(opts.ServerDir, opts.ServerFile)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -285,7 +172,7 @@ var rootCmd = &cobra.Command{
if file.Env.Server == modrinth.UnsupportedEnvSupport {
continue
}
downloads = append(downloads, requester.NewDownload(file.Downloads, map[string]string{"sha1": string(file.Hashes.Sha1)}, filepath.Base(file.Path), path.Join(opts.ServerDir, filepath.Dir(file.Path))))
downloads = append(downloads, requester.NewDownload(file.Downloads, map[string]string{"sha1": file.Hashes.Sha1}, filepath.Base(file.Path), path.Join(opts.ServerDir, filepath.Dir(file.Path))))
}
downloadPools := requester.NewDownloadPools(requester.DefaultHttpClient, downloads, opts.DownloadThreads, opts.RetryTimes)
downloadPools.Do()
Expand All @@ -300,27 +187,130 @@ var rootCmd = &cobra.Command{

// overrides
fmt.Println("Extracting overrides...")
err = mrpack.ExtractOverrides(archivePath, opts.ServerDir)
err := mrpack.ExtractOverrides(archivePath, opts.ServerDir)
if err != nil {
log.Fatalln(err)
}

info, err := update.GenerateModPackInfo(index)
packInfo, err := update.GenerateModPackInfo(index)
if err != nil {
fmt.Println(err)
}
err = info.Write(path.Join(opts.ServerDir, "modpack.json"))
err = packInfo.Write(path.Join(opts.ServerDir, "modpack.json"))
if err != nil {
fmt.Println(err)
}

if modsUnclean {
fmt.Println("There have been problems downloading downloading mods, you probably have to fix some dependency problems manually!")
fmt.Println("There have been problems downloading mods, you probably have to fix some dependency problems manually!")
}
fmt.Println("Done :) Have a nice day ✌️")
},
}

func readArgs(args []string) (string, string) {
var input string
var version string

if len(args) > 0 {
input = args[0]
}

if len(args) > 1 {
version = args[1]
}

return input, version
}

func handleArgs(input string, version string, serverDir string, host string) (*mrpack.Index, string) {
err := os.MkdirAll(serverDir, 0755)
if err != nil {
log.Fatalln(err)
}

archivePath := ""
if util.PathIsFile(input) {
archivePath = input

} else if util.IsValidUrl(input) {
fmt.Println("Downloading mrpack file from", input)
file, err := requester.DefaultHttpClient.DownloadFile(input, serverDir, "")
if err != nil {
log.Fatalln(err.Error())
}
archivePath = file

defer func(name string) {
err := os.Remove(name)
if err != nil {
fmt.Println(err.Error())
}
}(archivePath)

} else {
// input is project id or slug?
versions, err := modrinth.NewClient(host).GetProjectVersions(input, nil)
if err != nil {
log.Fatalln(err)
}

var files []modrinth.File = nil
for i := range versions {
if version != "" {
if versions[i].VersionNumber == version {
files = versions[i].Files
break
}
} else {
// latest stable release if version not specified
if versions[i].VersionType == modrinth.ReleaseVersionType {
files = versions[i].Files
break
}
}
}
if len(files) == 0 {
log.Fatalln("No files found for", input, version)
}

for i := range files {
if strings.HasSuffix(files[i].Filename, ".mrpack") {
fmt.Println("Downloading mrpack file from", files[i].Url)
file, err := requester.DefaultHttpClient.DownloadFile(files[i].Url, serverDir, "")
if err != nil {
// TODO: check next file on failure
log.Fatalln(err.Error())
}
archivePath = file
break
}
}

if archivePath == "" {
log.Fatalln("No mrpack file found for", input, version)
}

defer func(name string) {
err := os.Remove(name)
if err != nil {
fmt.Println(err.Error())
}
}(archivePath)
}

if archivePath == "" {
log.Fatalln("Unable to handle input: ", input, version)
}

index, err := mrpack.ReadIndex(archivePath)
if err != nil {
log.Fatalln(err.Error())
}

return index, archivePath
}

func Execute() {
if rootCmd.Execute() != nil {
os.Exit(1)
Expand Down
Loading

0 comments on commit 23d17a5

Please sign in to comment.