Skip to content

Commit

Permalink
refactor: add runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonzyt committed Dec 14, 2023
1 parent cb9cb89 commit 58f22f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
44 changes: 38 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@ const (
ColorReset = "\033[0m"
)

const (
DefaultCacheDir = "~/.cache/bdsdownloader"
)

func main() {
fmt.Println(" BDS Downloader | Distributed under the MIT License. ")
fmt.Println("=====================================================")

parser := argparse.NewParser("bdsdown", "Download and install BDS.")
usePreviewPtr := parser.Flag("p", "preview", &argparse.Options{Required: false, Help: "Use preview version"})
skipAgreePtr := parser.Flag("y", "yes", &argparse.Options{Required: false, Help: "Skip the agreement"})
clearCachePtr := parser.Flag("cc", "clear-cache", &argparse.Options{Required: false, Help: "Clear the cache directory and exit"})
noCachePtr := parser.Flag("nc", "no-cache", &argparse.Options{Required: false, Help: "Clear the default cache directory and exit"})
cacheDirPtr := parser.String("cd", "cache-dir", &argparse.Options{Required: false, Help: "The directory to store downloaded files", Default: DefaultCacheDir})
excludedFilesPtr := parser.StringList("e", "exclude", &argparse.Options{Required: false, Help: "Exclude existing files from the installation", Default: []string{"server.properties", "allowlist.json", "permissions.json"}})
targetVersionPtr := parser.String("v", "version", &argparse.Options{Required: false, Help: "The version of BDS to install. If not specified, the latest release(preview if -p specified) version will be used."})
parser.Parse(os.Args)

usePreview := *usePreviewPtr
skipAgree := *skipAgreePtr
clearCache := *clearCachePtr
excludedFiles := make([]string, 0)
targetVersion := *targetVersionPtr
cacheDir := *cacheDirPtr
useCache := !*noCachePtr

if targetVersion == "" {
for i := 0; i < len(os.Args); i++ {
if strings.Contains(os.Args[i], "\\") || strings.Contains(os.Args[i], "/") || strings.Contains(os.Args[i], "-") {
Expand All @@ -47,6 +58,26 @@ func main() {
}
}

utils.SetConfig(utils.Config{
UsePreview: usePreview,
SkipAgree: skipAgree,
ClearCache: clearCache,
UseCache: !useCache,
CacheDir: cacheDir,
ExcludedFiles: excludedFiles,
TargetVersion: targetVersion,
})

if clearCache {
err := os.RemoveAll(cacheDir)
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
return
}
fmt.Println(ColorGreen + "Cache cleared." + ColorReset)
return
}

fmt.Println("Before using this software, please read: ")
fmt.Println("- Minecraft End User License Agreement https://minecraft.net/terms")
fmt.Println("- Microsoft Privacy Policy https://go.microsoft.com/fwlink/?LinkId=521839")
Expand All @@ -72,28 +103,29 @@ func main() {
fmt.Println(ColorYellow + "Using preview version." + ColorReset)
}
if targetVersion != "" {
err := utils.Install(targetVersion, usePreview, excludedFiles)
err := utils.Install()
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
return
}
fmt.Println(ColorGreen + "Install complete." + ColorReset)
return
} else {
var ver string
var version string
var err error
if usePreview {
ver, err = utils.GetLatestPreviewVersion()
version, err = utils.GetLatestPreviewVersion()
} else {
fmt.Println("No version specified, using latest release version.")
ver, err = utils.GetLatestReleaseVersion()
version, err = utils.GetLatestReleaseVersion()
}
if err != nil {
fmt.Println(ColorRed+"ERROR:", err)
return
}
fmt.Println("Latest version: " + ColorBlue + ver + ColorReset)
err = utils.Install(ver, usePreview, excludedFiles)
fmt.Println("Latest version: " + ColorBlue + version + ColorReset)
utils.SetTargetVersion(version)
err = utils.Install()
if err != nil {
fmt.Println(ColorRed+"ERROR:", err, ColorReset)
}
Expand Down
25 changes: 25 additions & 0 deletions src/utils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

type Config struct {
UsePreview bool
SkipAgree bool
ClearCache bool
UseCache bool
CacheDir string
TargetVersion string
ExcludedFiles []string
}

var globalConfig Config

func SetConfig(config Config) {
globalConfig = config
}

func GetConfig() Config {
return globalConfig
}

func SetTargetVersion(version string) {
globalConfig.TargetVersion = version
}
13 changes: 8 additions & 5 deletions src/utils/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

// Unzip zip file to the current directory.
func Unzip(zipFile *os.File, bar *progressbar.ProgressBar, excludedFiles []string) error {
func Unzip(zipFile *os.File, bar *progressbar.ProgressBar) error {
fileStat, err := zipFile.Stat()
if err != nil {
return err
}

excludedFilesMap := make(map[string]bool)
for _, file := range excludedFiles {
for _, file := range GetConfig().ExcludedFiles {
excludedFilesMap[file] = true
}

Expand Down Expand Up @@ -64,9 +64,12 @@ func Unzip(zipFile *os.File, bar *progressbar.ProgressBar, excludedFiles []strin
}

// Install installs the given version of BDS.
func Install(version string, isPreview bool, excludedFiles []string) error {
func Install() error {
version := GetConfig().TargetVersion
usePreview := GetConfig().UsePreview

fmt.Println("Downloading BDS v" + version + "...")
path, err := DownloadVersion(version, isPreview)
path, err := DownloadVersion(version, usePreview)
if err != nil {
return err
}
Expand All @@ -92,7 +95,7 @@ func Install(version string, isPreview bool, excludedFiles []string) error {
BarEnd: "]",
}))

Unzip(file, bar, excludedFiles)
Unzip(file, bar)
fmt.Println(" Unzip complete!")

file.Close()
Expand Down

0 comments on commit 58f22f2

Please sign in to comment.