Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 17, 2024
1 parent f2f5da3 commit e1d2ca1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 132 deletions.
1 change: 0 additions & 1 deletion .cache/Go.cache

This file was deleted.

1 change: 0 additions & 1 deletion .cache/IIU.cache

This file was deleted.

1 change: 0 additions & 1 deletion .cache/PCP.cache

This file was deleted.

1 change: 0 additions & 1 deletion .cache/PS.cache

This file was deleted.

1 change: 0 additions & 1 deletion .cache/WS.cache

This file was deleted.

232 changes: 105 additions & 127 deletions fastDeployEnvirment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,165 +4,143 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/TapXWorld/fastDeployEnvirment/base/structs"
"github.com/TapXWorld/fastDeployEnvirment/utils"
yaml "github.com/goccy/go-yaml"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
"strconv"
"strings"
"time"
)

func cacheVersionInfo() {
data, _ := os.ReadFile("meta.yml")

if err := yaml.Unmarshal(data, &meta); err != nil {
log.Println(err)
}
if utils.PathExists("./.cache/") {
return
} else {
for i := 0; i < len(meta.Software); i++ {
replaceParameter := strings.ReplaceAll(meta.Software[i].Parameters, "{timestamp}", strconv.FormatInt(time.Now().UnixMilli(), 10))

res := utils.HttpGet(meta.Url + replaceParameter)
response, _ := io.ReadAll(res.Body)
func installOptions() (string, string) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("Which product are you want Download? ")
fmt.Println(" 1.IDEA(IIU)")
fmt.Println(" 2.Goland(Go)")
fmt.Println(" 3.WebStorm(WS)")
fmt.Println(" 4.PhpStorm(PS)")
fmt.Println(" 5.PyCharm(PCP)")
fmt.Print("Input Product Name(Example: 1,2): ")
str, _, _ := reader.ReadLine()
products := strings.Replace(string(str), " ", "", -1)

os.Mkdir(".cache", os.ModePerm)
ioutil.WriteFile(".cache/"+meta.Software[i].ProductCode+".cache", response, 0644)
fmt.Println("Where to save? ")
fmt.Print("-----> (default: \".\\software\"): ")
savePath, _, _ := reader.ReadLine()
if !utils.PathExists(string(savePath)) {
os.MkdirAll(".\\software", os.ModePerm)
}

return string(savePath), products
}
}

func validateInput(u utils.User) bool {
for i := 0; i < len(u.ProductName); i++ {
inputError := true

for j := 0; j < len(meta.Software); j++ {
if strings.ToLower(u.ProductName[i]) == strings.ToLower(meta.Software[j].ProductName) {
inputError = false
}
}
if inputError {
fmt.Println("---> product name error. please input again.")
return true
}
}
return false
var codeMap = map[string]int{
"IIU": 1,
"Go": 2,
"PCP": 3,
"PS": 4,
"WS": 5,
}

func installOptions() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("Which product are you want Download? ")
fmt.Println(" 1.IDEA")
fmt.Println(" 2.Goland")
fmt.Println(" 3.WebStorm")
fmt.Println(" 4.PhpStorm")
fmt.Println(" 5.PyCharm")
fmt.Print("Input Product Name(Example: IDEA,Goland): ")

str, _, _ := reader.ReadLine()

product := strings.Replace(string(str), " ", "", -1)
func main() {
savePath, installs := installOptions()
systemType := 0

user.ProductName = strings.Split(product, ",")
if "windows" != runtime.GOOS {
systemType = 1
}

fmt.Println("Where to save? ")
fmt.Print("-----> (default: \"D:\\software\\\"): ")
for _, selectedIndex := range strings.Split(installs, ",") {
selectedIndex = strings.TrimSpace(selectedIndex)
if selectedIndex == "" {
continue
}

savePath, _, _ := reader.ReadLine()
for productCode, productIndex := range codeMap {
if selectedIndexInt, _ := strconv.Atoi(selectedIndex); selectedIndexInt == productIndex {
url := fmt.Sprintf("https://data.services.jetbrains.com/products/releases?code=%s&latest=true&type=release", productCode)
downloadURL, err := getDownloadURL(url, systemType)
if err != nil {
fmt.Printf("Get Download Link (%s): %v\n", productCode, err)
continue
}

user.DownloadPath = string(savePath)
if len(savePath) == 0 {
savePath = ".\\software"
}

if validateInput(user) {
fmt.Println("validate error, input again.")
} else {
break
err = downloadFile(downloadURL, savePath, productCode)
if err != nil {
fmt.Printf("File Download Failed (%s): %v\n", productCode, err)
} else {
fmt.Printf("File Download Success: %s\n", productCode)
}
}
}
}
}

type Meta struct {
Url string
Software []struct {
ProductName string `yaml:"product_name"`
ProductCode string `yaml:"product_code"`
Parameters string `yaml:"parameters"`
func getDownloadURL(apiURL string, systemType int) (string, error) {
resp, err := http.Get(apiURL)
if err != nil {
return "", err
}
}
defer resp.Body.Close()

var meta = Meta{}

var productCode = []string{"IIU", "Go", "PCP", "PS", "WS"}

var productMap = make(map[string]interface{})

func loadProductJson() {
for _, p := range productCode {
strByte, _ := ioutil.ReadFile(".cache/" + p + ".cache")
switch p {
case "IIU":
iiu := &structs.IIU{}
json.Unmarshal(strByte, &iiu)
productMap["IIU"] = iiu
case "Go":
goland := &structs.Goland{}
json.Unmarshal(strByte, &goland)
productMap["Go"] = goland
case "PCP":
pyCharm := &structs.PyCharm{}
json.Unmarshal(strByte, &pyCharm)
productMap["PCP"] = pyCharm
case "PS":
phpStorm := &structs.PhpStorm{}
json.Unmarshal(strByte, &phpStorm)
productMap["PS"] = phpStorm
case "WS":
webStorm := &structs.WebStorm{}
json.Unmarshal(strByte, &webStorm)
productMap["WS"] = webStorm
default:
break
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP Request failed, Status Code: %d", resp.StatusCode)
}
}

var user = utils.User{}

func main() {
loadProductJson()
cacheVersionInfo()
installOptions()

if "windows" == runtime.GOOS {
user.SystemType = 0
} else {
user.SystemType = 1
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", err
}
for _, name := range user.ProductName {
for j := 0; j < len(meta.Software); j++ {
if strings.EqualFold(name, meta.Software[j].ProductName) {
productCode := meta.Software[j].ProductCode

//install IDE
utils.Install(productCode, productMap, &user)

//create Launcher
if user.SystemType == 0 {
utils.CreateWindowsLauncher(user.DownloadPath, user, name)
} else {
utils.CreateLinuxLauncher(user.DownloadPath, user, name)
// 解析 JSON 数据
for _, product := range result {
if productArray, ok := product.([]interface{}); ok && len(productArray) > 0 {
if productDetails, ok := productArray[0].(map[string]interface{}); ok {
if downloads, ok := productDetails["downloads"].(map[string]interface{}); ok {
var fileType string
if systemType == 0 {
fileType = "windows"
} else {
fileType = "linux"
}

if binary, ok := downloads[fileType].(map[string]interface{}); ok {
if link, ok := binary["link"].(string); ok {
return link, nil
}
}
}
//create license
utils.CrackLicense()
}
}
}
fmt.Println("All Install Completed.")
return "", fmt.Errorf("not found download link")
}

// 下载文件到指定目录
func downloadFile(downloadURL, savePath, productCode string) error {
fmt.Println("Download URL: " + downloadURL)
resp, err := http.Get(downloadURL)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download Failed, Status Code: %d", resp.StatusCode)
}
filePath := fmt.Sprintf("%s/%s", savePath, path.Base(downloadURL))
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
return err
}

0 comments on commit e1d2ca1

Please sign in to comment.