Skip to content

Commit

Permalink
upgrade.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ixre committed Nov 27, 2020
1 parent 320b512 commit cbea0e1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

_注:您看到的文档有可能已经更新,请参见最新[使用文档](https://github.com/ixre/tto)_

## 安装/升级

Windows:
```
下载解压将tto.exe文件复制到C:\windows\system32下完成安装
```
下载安装包,将对应平台的二进制文件加入到系统变量中,完成安装.

**升级**
`tto`内置了升级功能,命令如下:
```
tto update -y
```

## 快速开始

1. 配置数据源
Expand Down
25 changes: 24 additions & 1 deletion bin/tto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/ixre/gof/db/orm"
"github.com/ixre/gof/shell"
"github.com/ixre/tto"
"io/ioutil"
"log"
"os"
"runtime"
"strconv"
"strings"
"time"
)
Expand All @@ -25,12 +27,28 @@ func main() {
switch cmd {
case "update":
forceUpdate := len(os.Args)> 2 && os.Args[2] =="-y"
doUpdate(forceUpdate)
_, _ = doUpdate(forceUpdate)
case "generate":
generate()
}
}

func checkEveryDay() bool {
timeFile := os.TempDir()+"/tto_check_time"
var unix int64
lastTime,err := ioutil.ReadFile(timeFile)
if err == nil{
unix1,_ := strconv.Atoi(string(lastTime))
unix = int64(unix1)
}
if dt:= time.Now().Unix();dt - unix > 3600*24{
_ = ioutil.WriteFile(timeFile, []byte(strconv.Itoa(int(dt))), os.ModePerm)
b, _ := doUpdate(false)
return b
}
return false
}

func generate() {
var genDir string //输出目录
var confPath string //设置目录
Expand All @@ -42,6 +60,7 @@ func generate() {
var printVer bool
var cleanLast bool
var compactMode bool
var keepLocal bool

flag.StringVar(&genDir, "o", "./output", "path of output directory")
flag.StringVar(&tplDir, "t", "./templates", "path of code templates directory")
Expand All @@ -52,13 +71,17 @@ func generate() {
flag.BoolVar(&cleanLast, "clean", false, "clean last generate files")
flag.BoolVar(&debug, "debug", false, "debug mode")
flag.BoolVar(&compactMode, "compact", false, "compact mode for old project")
flag.BoolVar(&keepLocal,"local",false,"don't update any new version")
flag.BoolVar(&printVer, "v", false, "print version")
flag.Parse()

if printVer {
println("tto Generator v" + tto.BuildVersion)
return
}
if !keepLocal && checkEveryDay() {
os.Exit(0)
}
re, err := tto.LoadRegistry(confPath)
if err != nil {
println("[ tto][ fatal]:", err.Error())
Expand Down
49 changes: 21 additions & 28 deletions bin/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"compress/gzip"
"errors"
"fmt"
"github.com/ixre/gof/log"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
Expand All @@ -39,19 +37,20 @@ type version struct{
distURL string
}

func doUpdate(force bool){
func doUpdate(force bool)(bool,error){
fmt.Fprint(os.Stdout,"正在检查版本...\r")
v,err := checkVersion()
if err != nil{
fmt.Println(err.Error())
os.Exit(1)
}
if v == nil{
fmt.Println("已经是最新版本")
return
fmt.Fprint(os.Stdout,"已经是最新版本\r")
return false,nil
}
printVersion(v)
//printVersion(v)
if !force {
fmt.Printf("\n是否现在立即更新?[Y/N] ")
fmt.Printf("检测到新版本v%s,是否现在更新? [Y/N] ",v.version)
input := bufio.NewScanner(os.Stdin) //初始化一个扫表对象
input.Scan()
if strings.ToLower(input.Text()) != "y"{
Expand All @@ -68,35 +67,36 @@ func doUpdate(force bool){
err = install(tmpFile)
if err != nil{
fmt.Fprint(os.Stdout,"安装失败:",err.Error())
fmt.Fprint(os.Stdout,"\n您可以重新尝试或参考http://github.com/ixre/tto手工升级")
os.Exit(1)
}
fmt.Fprint(os.Stdout,"恭喜! 安装完成!\r")
return true,nil
}

func install(file string) error {
tmpDir := filepath.Dir(file)+"/tto"
err := decompressTarFile(file,tmpDir)
dstPath := getProgramPath()
// 获取当前程序的位置
path,_:= os.Executable()
switch runtime.GOOS{
case "linux":
err = overwriteFile(tmpDir+"/tto",dstPath+"/tto")
err = overwriteFile(tmpDir+"/tto",path)
case "windows":
err = overwriteFile(tmpDir+"/tto.exe",dstPath+"/tto.exe")
err = overwriteFile(tmpDir+"/tto.exe",path)
case "darwin":
err = overwriteFile(tmpDir+"/tto-mac",dstPath+"/tto")
}
if err != nil{
log.Println(err.Error())
err = overwriteFile(tmpDir+"/tto-mac",path)
}
return err
}

// 删除原程序,替换为新程序
func overwriteFile(src string, dst string)error {
func overwriteFile(src string,dst string)error {
sf, _ := os.Open(src)
_ = os.Remove(dst)
df, err := os.Create(dst)
df.Chmod(755)
df, err := os.OpenFile(dst,os.O_CREATE|os.O_WRONLY,os.ModePerm)
if os.IsPermission(err){
return errors.New("没有文件的更改权限")
}
_, err = io.Copy(df, sf)
return err
}
Expand Down Expand Up @@ -151,9 +151,10 @@ func prepareFiles(distURL string,file string)error {
//lastLen := 0
return down(distURL,file,func(total int,reads int,time int){
prg := int(float32(reads)/float32(total)*100)
if prg > 0 {
bit := reads/time/1000
if bit > 0 {
line := "已下载 "+strconv.Itoa(prg)+"% 速度:"+
strconv.Itoa(reads/time/1000)+"kb/s \r"
strconv.Itoa(bit)+"kb/s \r"
fmt.Fprint(os.Stdout,line)
}
},-1)
Expand Down Expand Up @@ -302,11 +303,3 @@ func down(ur, target string,onProgress func(total int,reads int,seconds int), ti
}
return err
}

func getProgramPath()string{
_, filename, _, ok := runtime.Caller(1)
if ok {
return path.Join(path.Dir(filename), "") // the the main function file directory
}
return "./"
}

0 comments on commit cbea0e1

Please sign in to comment.