Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
✨ add upgrade action
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jun 12, 2018
1 parent 30f1031 commit bc93656
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 13 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ tale命令行帮助程序 🌟
[![Go Report Card](https://goreportcard.com/badge/github.com/otale/tale-cli)](https://goreportcard.com/report/github.com/otale/tale-cli)
[![GoDoc](https://godoc.org/github.com/otale/tale-cli?status.svg)](https://godoc.org/github.com/otale/tale-cli)

## 特性

- 支持启动、停止、升级 Tale 博客
- 支持 Linux、MacOSX 系统
- 支持旧版本迁移
- 支持博客备份

## 预览

[![tale-cli](https://i.loli.net/2017/09/10/59b5241331c47.png)](https://asciinema.org/a/137112)
Expand Down
98 changes: 96 additions & 2 deletions cmds/cmd.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package cmds

import (
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/mholt/archiver"
)

const (
statusURL = "https://otale.github.io/status/version.json"
pidFile = "tale.pid"
statusURL = "https://otale.github.io/status/version.json"
pidFile = "tale.pid"
dbFile = "resources/tale.db"
versionFile = "version.txt"
)

// Version tale版本信息
type Version struct {
LatestVersion string `json:"latest_version"`
PublishTime string `json:"publish_time"`
Hash string `json:"hash"`
ChangeLogs []string `json:"change_logs"`
DownloadURL string `json:"download_url"`
}

// StartAction 启动 tale 博客
func StartAction() error {
dat, err := ioutil.ReadFile(pidFile)
Expand Down Expand Up @@ -111,10 +126,89 @@ func LogAction() error {

// UpgradeAction 升级博客
func UpgradeAction() error {
updated := false
var ver string
if _, err := os.Stat(versionFile); err == nil {
data, _ := ioutil.ReadFile(versionFile)
ver = string(data)
} else {
if _, err := os.Stat(dbFile); err == nil {
updated = true
}
}
log.Println("ver:", ver)
if updated {
log.Println("修复 SQL")
cmd1 := "sqlite3 " + dbFile + " \"update t_comments set type = 'comment', status = 'approved';\""
cmd2 := "sqlite3 " + dbFile + " \"update t_contents set categories = '默认分类' where categories is null;\""
cmd3 := "sqlite3 " + dbFile + " \"update t_contents set allow_feed = 1 where allow_feed is null;\""
cmd4 := "sqlite3 " + dbFile + " \"insert into t_options(name,value,description) values ('allow_comment_audit', 'true', '评论需要审核');\""
log.Println(cmd1)
_, _, _, err := StartCmd(cmd1)
if err != nil {
log.Println("修复 SQL 失败")
return err
}
log.Println(cmd2)
_, _, _, err = StartCmd(cmd2)
if err != nil {
log.Println("修复 SQL 失败")
return err
}
log.Println(cmd3)
_, _, _, err = StartCmd(cmd3)
if err != nil {
log.Println("修复 SQL 失败")
return err
}
_, _, _, err = StartCmd(cmd4)
if err != nil {
log.Println("修复 SQL 失败")
return err
}
}

var version Version
body := GetRequestBody(statusURL)
if err := json.Unmarshal(body, &version); err != nil {
log.Println("解析JSON失败")
return err
}
log.Println(version)

// download leate zip

// unzip
// backup
datetime := time.Now().Format("20060102_150405") + ".zip"
err := archiver.Zip.Make(datetime, []string{"resources", "lib", "tale-least.jar"})
if err != nil {
log.Println("备份失败")
return err
}
// delete old
os.Remove("tale-least.jar")
RemoveDir("lib")
RemoveDir("resources/static")
RemoveDir("resources/templates")
// move new
err = archiver.Zip.Open("/Users/biezhi/workspace/golang/src/github.com/otale/tale-cli/tale-2.0.1.zip", "/Users/biezhi/workspace/temp/tale.biezhi.me")
if err != nil {
log.Println("解压失败")
return err
}
log.Println("升级完毕")
return nil
}

// BackupAction 备份博客,SQL和当前全部状态
func BackupAction() error {
// 备份 SQL
_, _, _, err := StartCmd("sqlite3 tale.db .dump > tale.sql")
if err != nil {
return err
}
// 备份目录

return nil
}
38 changes: 37 additions & 1 deletion cmds/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ package cmds

import (
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"syscall"
)

// GetRequestBody http get body
func GetRequestBody(reqURL string) []byte {
resp, err := http.Get(reqURL)
if err != nil {
return nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}

// KillPID kill 进程id
func KillPID(pid int) (int, error) {
// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
err := syscall.Kill(pid, syscall.SIGKILL)
return pid, err
}

// StartCmd 启动命令
func StartCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) {
var err error
c := exec.Command("/bin/sh", "-c", cmd)
Expand All @@ -30,3 +46,23 @@ func StartCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) {
}
return c, stdout, stderr, err
}

// RemoveDir 删除文件夹
func RemoveDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
10 changes: 0 additions & 10 deletions cmds/version.go

This file was deleted.

Binary file added tale-2.0.1.zip
Binary file not shown.

0 comments on commit bc93656

Please sign in to comment.