commit 2021-12-07]11:35:00

This commit is contained in:
Godopu 2021-12-07 11:35:05 +09:00
parent 4a99764277
commit b9ce5e338d
2 changed files with 100 additions and 36 deletions

19
common/reactive_os.go Normal file
View 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
View File

@ -1,12 +1,16 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
"path" "os/exec"
"path/filepath"
"runtime"
"typorainstaller/common"
"typorainstaller/constants" "typorainstaller/constants"
) )
@ -15,10 +19,10 @@ func pull(pull_type int) error {
var fname string var fname string
switch pull_type { switch pull_type {
case PULL_INSTALLER: 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 fname = constants.Installer
case PULL_CONFIG: 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" fname = "themes.zip"
} }
@ -41,39 +45,44 @@ func pull(pull_type int) error {
return nil return nil
} }
// func install() { func installOnWindows(ctx context.Context) (context.Context, error) {
// cmd := exec.Command("powershell", "-nologo", "-noprofile") cmd := exec.Command("powershell", "-nologo", "-noprofile")
// stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()
// if err != nil { if err != nil {
// log.Fatal(err) return nil, err
// } }
// if err != nil { errorCh := make(chan error, 1)
// panic(err) defer close(errorCh)
// }
// defer stdin.Close()
// go func() { go func() {
// fmt.Fprintln(stdin, "$T = Get-Date") defer stdin.Close()
// fmt.Fprintln(stdin, "Set-Date -Date ($T).AddYears(30)") fmt.Fprintln(stdin, "$T = Get-Date")
// bout, err := exec.Command("./typora-installer/install.exe").Output() fmt.Fprintln(stdin, "Set-Date -Date ($T).AddYears(30)")
// if err != nil { bout, err := exec.Command(constants.Installer).Output()
// log.Println(err) if err != nil {
// } errorCh <- err
// fmt.Println(bout) }
// fmt.Fprintln(stdin, "Set-Date -Date ($T)") fmt.Println(bout)
// }() fmt.Fprintln(stdin, "Set-Date -Date ($T)")
errorCh <- nil
}()
// _, err = cmd.CombinedOutput() _, err = cmd.Output()
// if err != nil { if err != nil {
// log.Fatal(err) errorCh <- err
// } }
// }
// func config() { return nil, <-errorCh
// os.RemoveAll(filepath.Join(configPath, "themes")) }
// copy.Copy(filepath.Join("typora-installer", "themes"), filepath.Join(configPath, "themes"))
// } func configOnWindows() error {
err := os.RemoveAll(filepath.Join(constants.ConfigPath, "themes"))
if err != nil {
return err
}
return common.Unzip("./themes.zip", constants.ConfigPath)
}
const ( const (
PULL_INSTALLER = iota PULL_INSTALLER = iota
@ -88,11 +97,27 @@ func IsExistConfig() bool {
return false return false
} }
func IsExistInstaller() bool { func IsExistInstaller() bool {
// if _, err := os.Stat("./themes.zip"); err == nil {
// return true
// }
return false return false
} }
func main() { 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) err := os.MkdirAll(constants.WorkPath, os.ModePerm)
if err != nil { if err != nil {
panic(err) panic(err)
@ -103,16 +128,25 @@ func main() {
panic(err) panic(err)
} }
conf := flag.Bool("config", false, "Config")
inst := flag.Bool("install", false, "Install")
flag.Parse()
if *conf { if *conf {
if !IsExistConfig() { if !IsExistConfig() {
fmt.Println("Downloading config file...") fmt.Println("Downloading config file...")
pull(PULL_CONFIG) pull(PULL_CONFIG)
fmt.Println("Download Success") 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 { } else if *inst {
if !IsExistInstaller() { if !IsExistInstaller() {
fmt.Println("Downloading install program...") fmt.Println("Downloading install program...")
@ -122,7 +156,18 @@ func main() {
} }
fmt.Println("Download Success") 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 *inst {
// if _, err := os.Stat(filepath.Join(workPath, "typora-installer")); err != nil { // if _, err := os.Stat(filepath.Join(workPath, "typora-installer")); err != nil {
// fmt.Println("Start download") // fmt.Println("Start download")