Compare commits

...

2 Commits

Author SHA1 Message Date
4cf354733c delete javaentites 2023-02-04 20:47:49 +03:00
815e624c64 Add osutils files 2023-02-04 20:47:42 +03:00
3 changed files with 111 additions and 66 deletions

View File

@ -56,5 +56,6 @@ func ListDir(path string) ([]fs.FileInfo, error) {
func GetParentDir(filePath string) (string, error) { func GetParentDir(filePath string) (string, error) {
cuttedPath := strings.Split(filePath, "/") cuttedPath := strings.Split(filePath, "/")
cuttedPath = cuttedPath[:len(cuttedPath)-1] cuttedPath = cuttedPath[:len(cuttedPath)-1]
return path.Join(cuttedPath...), nil finalPath := path.Join("/", path.Join(cuttedPath...))
return finalPath, nil
} }

47
osutils/json.go Normal file
View File

@ -0,0 +1,47 @@
package osutils
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func WriteStructAsJSON(data interface{}, versionJsonPath string) error {
file, _ := json.MarshalIndent(data, "", " ")
directory, err := GetParentDir(versionJsonPath)
if err != nil {
return err
}
err = os.MkdirAll(directory+"/", os.ModePerm)
if err != nil {
return err
}
err = ioutil.WriteFile(versionJsonPath, file, 0644)
if err != nil {
return err
}
return nil
}
func ReadJsonFromDisk(target interface{}, filePath string) error {
// Open our jsonFile
jsonFile, err := os.Open(filePath)
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
// fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &target)
return nil
}

View File

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