typora-installer/main.go

153 lines
3.1 KiB
Go

package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"path"
"typorainstaller/constants"
)
func pull(pull_type int) error {
var urlToDownload string
var fname string
switch pull_type {
case PULL_INSTALLER:
urlToDownload = path.Join("https://git.godopu.net/Godopu/typora-installer/src/branch/main/installer", constants.Installer)
fname = constants.Installer
case PULL_CONFIG:
urlToDownload = "https://git.godopu.net/Godopu/typora-installer/src/branch/main/themes.zip"
fname = "themes.zip"
}
resp, err := http.Get(urlToDownload)
if err != nil {
return err
}
file, err := os.Create(fname)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
// func install() {
// cmd := exec.Command("powershell", "-nologo", "-noprofile")
// stdin, err := cmd.StdinPipe()
// if err != nil {
// log.Fatal(err)
// }
// if err != nil {
// panic(err)
// }
// defer stdin.Close()
// go func() {
// fmt.Fprintln(stdin, "$T = Get-Date")
// fmt.Fprintln(stdin, "Set-Date -Date ($T).AddYears(30)")
// bout, err := exec.Command("./typora-installer/install.exe").Output()
// if err != nil {
// log.Println(err)
// }
// fmt.Println(bout)
// fmt.Fprintln(stdin, "Set-Date -Date ($T)")
// }()
// _, err = cmd.CombinedOutput()
// if err != nil {
// log.Fatal(err)
// }
// }
// func config() {
// os.RemoveAll(filepath.Join(configPath, "themes"))
// copy.Copy(filepath.Join("typora-installer", "themes"), filepath.Join(configPath, "themes"))
// }
const (
PULL_INSTALLER = iota
PULL_CONFIG
)
func IsExistConfig() bool {
if _, err := os.Stat("./themes.zip"); err == nil {
return true
}
return false
}
func IsExistInstaller() bool {
return false
}
func main() {
err := os.MkdirAll(constants.WorkPath, os.ModePerm)
if err != nil {
panic(err)
}
err = os.Chdir(constants.WorkPath)
if err != nil {
panic(err)
}
conf := flag.Bool("config", false, "Config")
inst := flag.Bool("install", false, "Install")
flag.Parse()
if *conf {
if !IsExistConfig() {
fmt.Println("Downloading config file...")
pull(PULL_CONFIG)
fmt.Println("Download Success")
}
} else if *inst {
if !IsExistInstaller() {
fmt.Println("Downloading install program...")
err := pull(PULL_INSTALLER)
if err != nil {
panic(err)
}
fmt.Println("Download Success")
}
}
// if *inst {
// if _, err := os.Stat(filepath.Join(workPath, "typora-installer")); err != nil {
// fmt.Println("Start download")
// download()
// fmt.Println("download success")
// fmt.Println("Start unzip")
// unzip("main.zip")
// fmt.Println("Unzip success")
// }
// fmt.Println("Installing...")
// install()
// fmt.Println("Install success")
// } else if *conf {
// if _, err := os.Stat(filepath.Join(workPath, "typora-installer")); err != nil {
// fmt.Println("Start download")
// download()
// fmt.Println("download success")
// fmt.Println("Start unzip")
// unzip("main.zip")
// fmt.Println("Unzip success")
// }
// fmt.Println("Set config")
// config()
// fmt.Println("Config success")
// }
}