sharedutils/redistruable/javaentities/java.go
2023-02-03 01:53:20 +03:00

79 lines
1.6 KiB
Go

package javaentities
import (
"errors"
"os"
"path"
"runtime"
"strconv"
"strings"
)
type JavaVersion struct {
Major int
Minor int
Patch int
}
type JavaMetas struct {
JavaVersions []JavaMeta `json:"javaVersions"`
}
func (jm JavaMetas) FindOsURL(MajorVersion string) (SpecificJavaVersion, error) {
for _, v := range jm.JavaVersions {
if MajorVersion == v.Version.Major {
jv, err := v.getOsJava()
if err != nil {
continue
}
return jv, nil
}
}
return SpecificJavaVersion{}, errors.New("cant find os url")
}
type JavaMeta struct {
Version struct {
Major string `json:"major"`
Minor string `json:"minor"`
Patch string `json:"patch"`
} `json:"version"`
Specifics `json:"specifics"`
}
type SpecificJavaVersion struct {
Os string `json:"os"`
Arch string `json:"arch"`
URL string `json:"URL"`
Sha1 string `json:"SHA1"`
}
type Specifics []SpecificJavaVersion
func (s Specifics) getOsJava() (SpecificJavaVersion, error) {
for _, v := range s {
if v.Os == runtime.GOOS {
return v, nil
}
}
return SpecificJavaVersion{}, errors.New("not java for current os")
}
func (v *JavaVersion) CheckNotExists(launcherPath string) bool {
javaBinPath := path.Join(launcherPath, ".redist", "java", v.GetString())
_, err := os.Stat(javaBinPath)
return err != nil
}
func (v *JavaVersion) GetString() string {
var stringBuilder strings.Builder
stringBuilder.WriteString(strconv.Itoa(v.Major))
stringBuilder.WriteRune('.')
stringBuilder.WriteString(strconv.Itoa(v.Minor))
stringBuilder.WriteRune('.')
stringBuilder.WriteString(strconv.Itoa(v.Patch))
return stringBuilder.String()
}