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

Commit

Permalink
🔨 refactor tale-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jun 10, 2018
1 parent 1ecce53 commit 4dd617c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 417 deletions.
Binary file added bin/linux_64/tale-cli
Binary file not shown.
Binary file added bin/macOSX_64/tale-cli
Binary file not shown.
31 changes: 31 additions & 0 deletions cmds/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmds

// StartAction 启动 tale 博客
func StartAction() error {
return nil
}

// StopAction 停止 tale 博客
func StopAction() error {
return nil
}

// RestartAction 重启 tale 博客
func RestartAction() error {
return nil
}

// LogAction 输出日志
func LogAction() error {
return nil
}

// UpgradeAction 升级博客
func UpgradeAction() error {
return nil
}

// BackupAction 备份博客,SQL和当前全部状态
func BackupAction() error {
return nil
}
207 changes: 26 additions & 181 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,199 +1,44 @@
package main

import (
"os"
"github.com/urfave/cli"
"fmt"
"os/exec"
"log"
"strings"
"strconv"
"time"
)
"os"

const (
taleZipName = "tale-least.zip"
taleDownloadUrl = "http://static.biezhi.me/" + taleZipName
jarFileName = "tale-least.jar"
"github.com/otale/tale-cli/cmds"
"github.com/robmerrell/comandante"
)

type Config struct {
key string
value string
}

func main() {

app := cli.NewApp()
app.Name = "tale"
app.Usage = "tale的命令行帮助程序"
app.Author = "https://github.com/biezhi"
app.Email = "[email protected]"
app.Version = "0.0.5"

app.Commands = []cli.Command{
{
Name: "start",
Usage: "启动tale",
Action: doStart,
},
{
Name: "stop",
Usage: "停止当前tale实例",
Action: doStop,
},
{
Name: "reload",
Usage: "重新启动当前tale实例",
Action: func(ctx *cli.Context) {
doStop(ctx)
doStart(ctx)
},
},
{
Name: "log",
Usage: "查看当前tale日志",
Action: func(ctx *cli.Context) {
tailLog()
},
},
{
Name: "status",
Usage: "查看当前tale状态",
Action: func(ctx *cli.Context) {
pid := findPid()
if pid < 0 {
fmt.Println("Tale 实例没有运行.")
} else {
fmt.Printf("Tale start with pid: %d\n", pid)
}
},
},
{
Name: "upgrade",
Usage: "升级当前的tale版本",
Action: doUpgrade,
},
}
app.Run(os.Args)
os.Exit(0)
}

// start tale instance
func doStart(ctx *cli.Context) {
pid := findPid()
if pid > 0 {
fmt.Println("Tale 已经启动.")
} else {
// `nohup java -Xms128m -Xmx128m -Dfile.encoding=UTF-8 -jar tale-1.3.0-alpha1.jar >/dev/null &`
cmd := exec.Command("/bin/sh", "-c", `nohup java -Xms128m -Xmx128m -Dfile.encoding=UTF-8 -jar `+jarFileName+` >/dev/null &`)
// 执行命令
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
fmt.Println("Tale 启动成功, 可以使用 ./tale-cli log 命令查看日志.")
}
}

// stop tale instance
func doStop(ctx *cli.Context) {
pid := findPid()
if pid > 0 {
proc, err := os.FindProcess(pid)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("kill pid: %d\n", pid)
proc.Kill()
os.Remove("resources/tale.pid")
}
}

// tail -f tale.log
func tailLog() {
cmd := exec.Command("tail", "-f", "logs/tale.log")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalln(err)
return
}
}

// 升级tale版本
func doUpgrade(ctx *cli.Context) {
dir, err := os.Open("./resources")
if err != nil {
log.Fatal(err)
}
var files = []*os.File{dir}
dest := "tale_backup_" + time.Now().Format("20060102150405") + ".zip"
err = Compress(files, dest)
if err != nil {
log.Fatal(err)
return
}
fmt.Println("备份成功.")
fmt.Println("开始下载最新版tale安装包, 客官请稍等...")
os.Remove(taleZipName)
//// 下载tale.zip

rand := "?t=_" + time.Now().Format("20060102150405")
DownloadFile(taleDownloadUrl+rand, "./")

Unzip(taleZipName+rand, "./")
fmt.Println("解压完成")
fmt.Println("正在升级...")
const banner = `
Tale 博客程序帮助工具
// delete 除了 resources 目录下的所有
// cd resources && delete 除了 app.properties、static、
// templates/admin、templates/install.html、templates/comm
RemoveContents("lib")
Github: https://github.com/otale/tale`

os.Rename("./tale/lib", "./lib")
os.Remove(jarFileName)

os.Rename("./tale/"+jarFileName, "./"+jarFileName)

RemoveContents("./resources/static")
os.Rename("./tale/resources/static", "./resources/static")
func main() {
bin := comandante.New("tale-cli", banner)

RemoveContents("./resources/templates/admin")
os.Rename("./tale/resources/templates/admin", "./resources/templates/admin")
// list command
startCmd := comandante.NewCommand("start", "启动 Tale 博客", cmds.StartAction)
bin.RegisterCommand(startCmd)

RemoveContents("./resources/templates/comm")
os.Rename("./tale/resources/templates/comm", "./resources/templates/comm")
// list command
stopCmd := comandante.NewCommand("stop", "停止 Tale 博客", cmds.StopAction)
bin.RegisterCommand(stopCmd)

os.Remove("./resources/templates/install.html")
os.Rename("./tale/resources/templates/install.html", "./resources/templates/install.html")
// list command
restartCmd := comandante.NewCommand("restart", "重启 Tale 博客", cmds.RestartAction)
bin.RegisterCommand(restartCmd)

RemoveContents("tale")
os.Remove("tale")
logCmd := comandante.NewCommand("log", "查看 Tale 博客日志", cmds.LogAction)
bin.RegisterCommand(logCmd)

fmt.Println("Tale 升级成功, 请手动启动.")
upgradeCmd := comandante.NewCommand("upgrade", "升级 Tale 博客", cmds.UpgradeAction)
bin.RegisterCommand(upgradeCmd)

}
backupCmd := comandante.NewCommand("backup", "备份 Tale 博客", cmds.BackupAction)
bin.RegisterCommand(backupCmd)

// find tale.jar process id
func findPid() int {
pidByte, err := exec.Command("/bin/sh", "-c", `ps -eaf|grep "`+jarFileName+`"|grep -v "grep"|awk '{print $2}'`).Output()
if err != nil {
log.Fatal(err)
return -1
// run the commands
if err := bin.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if len(pidByte) == 0 {
return -1
}
pid := string(pidByte)
pid = strings.TrimSuffix(string(pidByte), "\n")
if len(pid) == 0 {
return -1
}
intVal, _ := strconv.Atoi(pid)
return intVal
}

func getFileNmae() (string, error) {
return "", nil
}
10 changes: 10 additions & 0 deletions pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/bash

rm -rf bin
mkdir -p bin/linux_64
mkdir -p bin/macOSX_64
mkdir -p bin/windows_64

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-w -s' -o bin/linux_64/tale-cli && upx bin/linux_64/tale-cli
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags '-w -s' -o bin/macOSX_64/tale-cli && upx bin/macOSX_64/tale-cli
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags '-w -s' -o bin/windows_64/tale-cli.exe && upx bin/windows_64/tale-cli.exe
Loading

0 comments on commit 4dd617c

Please sign in to comment.