Refactor to use ~/.config/r2mod-go
This commit is contained in:
		
							parent
							
								
									744a32f5a5
								
							
						
					
					
						commit
						0866620ab0
					
				
							
								
								
									
										12
									
								
								api/api.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								api/api.go
									
									
									
									
									
								
							@ -47,16 +47,20 @@ type Version struct {
 | 
			
		||||
 | 
			
		||||
// InitAPI stores the API in an object when needed.
 | 
			
		||||
func InitAPI() {
 | 
			
		||||
	if CheckAPICache() != 0 {
 | 
			
		||||
		UpdateAPICache()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	PkgList = UnpackAPI()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CheckAPICache checks if the cached json is expired
 | 
			
		||||
func CheckAPICache() int {
 | 
			
		||||
	if !utils.PathExists(utils.SystemInfo.TmpDir + "/pkg.json") {
 | 
			
		||||
	if !utils.PathExists(utils.SystemInfo.ProgDir + "/pkg.json") {
 | 
			
		||||
		return 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	info, err := os.Stat(utils.SystemInfo.TmpDir + "/pkg.json")
 | 
			
		||||
	info, err := os.Stat(utils.SystemInfo.ProgDir + "/pkg.json")
 | 
			
		||||
	utils.CheckErr(err)
 | 
			
		||||
 | 
			
		||||
	lastModTime := info.ModTime()
 | 
			
		||||
@ -73,13 +77,13 @@ func CheckAPICache() int {
 | 
			
		||||
func UpdateAPICache() {
 | 
			
		||||
	apiURL := "https://thunderstore.io/api/v1/package/"
 | 
			
		||||
 | 
			
		||||
	err := utils.DownloadFile(utils.SystemInfo.TmpDir+"/pkg.json", apiURL)
 | 
			
		||||
	err := utils.DownloadFile(utils.SystemInfo.ProgDir+"/pkg.json", apiURL)
 | 
			
		||||
	utils.CheckErr(err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UnpackAPI stores the API cache as an object for later use.
 | 
			
		||||
func UnpackAPI() []Mod {
 | 
			
		||||
	pkgFile, err := os.Open(utils.SystemInfo.TmpDir + "/pkg.json")
 | 
			
		||||
	pkgFile, err := os.Open(utils.SystemInfo.ProgDir + "/pkg.json")
 | 
			
		||||
	utils.CheckErr(err)
 | 
			
		||||
 | 
			
		||||
	defer pkgFile.Close()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
									
									
									
									
								
							@ -51,7 +51,7 @@ func removeMod(pkg string) {
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	logFile, err := os.OpenFile(utils.SystemInfo.TmpDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
 | 
			
		||||
	logFile, err := os.OpenFile(utils.SystemInfo.ProgDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
 | 
			
		||||
	utils.CheckErr(err)
 | 
			
		||||
 | 
			
		||||
	defer logFile.Close()
 | 
			
		||||
 | 
			
		||||
@ -30,7 +30,7 @@ func DownloadMod(depString string) (int, string) {
 | 
			
		||||
	mod := api.GetModData(depString)
 | 
			
		||||
	downloadURL := mod.Versions[0].DownloadURL
 | 
			
		||||
 | 
			
		||||
	modName := sysinfo.TmpDir + "/dl/" + depString + ".zip"
 | 
			
		||||
	modName := sysinfo.ProgDir + "/dl/" + depString + ".zip"
 | 
			
		||||
 | 
			
		||||
	modVersion := mod.Versions[0].VersionNumber
 | 
			
		||||
	modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
 | 
			
		||||
 | 
			
		||||
@ -25,7 +25,7 @@ type SysInfo struct {
 | 
			
		||||
	BepinDir    string
 | 
			
		||||
	PluginDir   string
 | 
			
		||||
	ConfigDir   string
 | 
			
		||||
	TmpDir      string
 | 
			
		||||
	ProgDir     string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CheckErr checks for an error, prints if it happened
 | 
			
		||||
@ -81,28 +81,28 @@ func GetSysInfo() SysInfo {
 | 
			
		||||
		BepinDir:    gameDir + "/BepInEx",
 | 
			
		||||
		ConfigDir:   gameDir + "/BepInEx/config",
 | 
			
		||||
		PluginDir:   gameDir + "/BepInEx/plugins",
 | 
			
		||||
		TmpDir:      "/tmp/r2mod-go",
 | 
			
		||||
		ProgDir:     homeDir + "/.config/r2mod-go",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !PathExists(SystemInfo.TmpDir) {
 | 
			
		||||
	if !PathExists(SystemInfo.ProgDir) {
 | 
			
		||||
		log.Println("temp directory does not exist; creating...")
 | 
			
		||||
		err := os.Mkdir(SystemInfo.TmpDir, 0755)
 | 
			
		||||
		err := os.Mkdir(SystemInfo.ProgDir, 0755)
 | 
			
		||||
		CheckErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !PathExists(SystemInfo.TmpDir + "/dl") {
 | 
			
		||||
	if !PathExists(SystemInfo.ProgDir + "/dl") {
 | 
			
		||||
		log.Println("temp dl directory does not exist; creating...")
 | 
			
		||||
		err := os.Mkdir(SystemInfo.TmpDir+"/dl", 0755)
 | 
			
		||||
		err := os.Mkdir(SystemInfo.ProgDir+"/dl", 0755)
 | 
			
		||||
		CheckErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Setup log file
 | 
			
		||||
	if PathExists(SystemInfo.TmpDir + "/r2mod-go.log") {
 | 
			
		||||
		err := os.Remove(SystemInfo.TmpDir + "/r2mod-go.log")
 | 
			
		||||
	if PathExists(SystemInfo.ProgDir + "/r2mod-go.log") {
 | 
			
		||||
		err := os.Remove(SystemInfo.ProgDir + "/r2mod-go.log")
 | 
			
		||||
		CheckErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logFile, err := os.OpenFile(SystemInfo.TmpDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
 | 
			
		||||
	logFile, err := os.OpenFile(SystemInfo.ProgDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
 | 
			
		||||
	CheckErr(err)
 | 
			
		||||
 | 
			
		||||
	log.SetOutput(logFile)
 | 
			
		||||
@ -113,7 +113,7 @@ func GetSysInfo() SysInfo {
 | 
			
		||||
	log.Println("	bepinDir:", SystemInfo.BepinDir)
 | 
			
		||||
	log.Println("	configDir:", SystemInfo.ConfigDir)
 | 
			
		||||
	log.Println("	pluginDir:", SystemInfo.PluginDir)
 | 
			
		||||
	log.Println("	tmpDir:", SystemInfo.TmpDir)
 | 
			
		||||
	log.Println("	ProgDir:", SystemInfo.ProgDir)
 | 
			
		||||
 | 
			
		||||
	return SystemInfo
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user