commit 2021-12-07]11:35:00
This commit is contained in:
parent
4a99764277
commit
b9ce5e338d
19
common/reactive_os.go
Normal file
19
common/reactive_os.go
Normal file
@ -0,0 +1,19 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type ReactiveHandlerOnOS struct {
|
||||
HandleFunc map[string]func(ctx context.Context) (context.Context, error)
|
||||
}
|
||||
|
||||
func (h *ReactiveHandlerOnOS) Handle(ctx context.Context) (context.Context, error) {
|
||||
handle, ok := h.HandleFunc[runtime.GOOS]
|
||||
if !ok {
|
||||
return nil, errors.New("unsupported error")
|
||||
}
|
||||
return handle(ctx)
|
||||
}
|
117
main.go
117
main.go
@ -1,12 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"typorainstaller/common"
|
||||
"typorainstaller/constants"
|
||||
)
|
||||
|
||||
@ -15,10 +19,10 @@ func pull(pull_type int) error {
|
||||
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)
|
||||
urlToDownload = "https://git.godopu.net/Godopu/typora-installer/media/branch/main/installer/" + constants.Installer
|
||||
fname = constants.Installer
|
||||
case PULL_CONFIG:
|
||||
urlToDownload = "https://git.godopu.net/Godopu/typora-installer/src/branch/main/themes.zip"
|
||||
urlToDownload = "https://git.godopu.net/Godopu/typora-installer/media/branch/main/themes.zip"
|
||||
fname = "themes.zip"
|
||||
}
|
||||
|
||||
@ -41,39 +45,44 @@ func pull(pull_type int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// func install() {
|
||||
// cmd := exec.Command("powershell", "-nologo", "-noprofile")
|
||||
// stdin, err := cmd.StdinPipe()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
func installOnWindows(ctx context.Context) (context.Context, error) {
|
||||
cmd := exec.Command("powershell", "-nologo", "-noprofile")
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// defer stdin.Close()
|
||||
errorCh := make(chan error, 1)
|
||||
defer close(errorCh)
|
||||
|
||||
// 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)")
|
||||
// }()
|
||||
go func() {
|
||||
defer stdin.Close()
|
||||
fmt.Fprintln(stdin, "$T = Get-Date")
|
||||
fmt.Fprintln(stdin, "Set-Date -Date ($T).AddYears(30)")
|
||||
bout, err := exec.Command(constants.Installer).Output()
|
||||
if err != nil {
|
||||
errorCh <- err
|
||||
}
|
||||
fmt.Println(bout)
|
||||
fmt.Fprintln(stdin, "Set-Date -Date ($T)")
|
||||
errorCh <- nil
|
||||
}()
|
||||
|
||||
// _, err = cmd.CombinedOutput()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
_, err = cmd.Output()
|
||||
if err != nil {
|
||||
errorCh <- err
|
||||
}
|
||||
|
||||
// func config() {
|
||||
// os.RemoveAll(filepath.Join(configPath, "themes"))
|
||||
// copy.Copy(filepath.Join("typora-installer", "themes"), filepath.Join(configPath, "themes"))
|
||||
// }
|
||||
return nil, <-errorCh
|
||||
}
|
||||
|
||||
func configOnWindows() error {
|
||||
err := os.RemoveAll(filepath.Join(constants.ConfigPath, "themes"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return common.Unzip("./themes.zip", constants.ConfigPath)
|
||||
}
|
||||
|
||||
const (
|
||||
PULL_INSTALLER = iota
|
||||
@ -88,11 +97,27 @@ func IsExistConfig() bool {
|
||||
return false
|
||||
}
|
||||
func IsExistInstaller() bool {
|
||||
// if _, err := os.Stat("./themes.zip"); err == nil {
|
||||
// return true
|
||||
// }
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
conf := flag.Bool("config", false, "Config")
|
||||
inst := flag.Bool("install", false, "Install")
|
||||
clear := flag.Bool("clear", false, "Clear cache")
|
||||
flag.Parse()
|
||||
|
||||
if *clear {
|
||||
err := os.RemoveAll(constants.WorkPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
err := os.MkdirAll(constants.WorkPath, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -103,16 +128,25 @@ func main() {
|
||||
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")
|
||||
}
|
||||
|
||||
fmt.Println("Setting config")
|
||||
switch runtime.GOOS {
|
||||
|
||||
case "windows":
|
||||
err = configOnWindows()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
default:
|
||||
fmt.Println("Not supported operating system")
|
||||
}
|
||||
fmt.Println("Install success")
|
||||
} else if *inst {
|
||||
if !IsExistInstaller() {
|
||||
fmt.Println("Downloading install program...")
|
||||
@ -122,7 +156,18 @@ func main() {
|
||||
}
|
||||
fmt.Println("Download Success")
|
||||
}
|
||||
fmt.Println("Installing...")
|
||||
|
||||
reactiveHandler := common.ReactiveHandlerOnOS{HandleFunc: map[string]func(context.Context) (context.Context, error){}}
|
||||
reactiveHandler.HandleFunc["windows"] = installOnWindows
|
||||
_, err := reactiveHandler.Handle(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Install success")
|
||||
}
|
||||
|
||||
// if *inst {
|
||||
// if _, err := os.Stat(filepath.Join(workPath, "typora-installer")); err != nil {
|
||||
// fmt.Println("Start download")
|
||||
|
Loading…
Reference in New Issue
Block a user