feat: deploy command
This commit is contained in:
parent
dfb4defeb9
commit
d769f4115d
15
config.go
15
config.go
|
@ -4,13 +4,14 @@ import "time"
|
|||
|
||||
// Config 配置文件
|
||||
type Config struct {
|
||||
Title string `yaml:"title"` // 网站标题
|
||||
Seo SEO `yaml:"seo"` // seo相关信息
|
||||
Person Person `yaml:"person"` // 博主个人信息
|
||||
Theme string `yaml:"theme"` // 主题
|
||||
Site Site `yaml:"site"` // 站点配置信息
|
||||
Now time.Time // 当前时间
|
||||
Deploy Deploy `yaml:"deploy"`
|
||||
Title string `yaml:"title"` // 网站标题
|
||||
SourceVersion string `yaml:"-"` // 资源版本号
|
||||
Seo SEO `yaml:"seo"` // seo相关信息
|
||||
Person Person `yaml:"person"` // 博主个人信息
|
||||
Theme string `yaml:"theme"` // 主题
|
||||
Site Site `yaml:"site"` // 站点配置信息
|
||||
Now time.Time `yaml:"-"` // 当前时间
|
||||
Deploy Deploy `yaml:"deploy"` // 部署配置
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
|
|
|
@ -20,6 +20,11 @@ func deployCmd() *cobra.Command {
|
|||
}
|
||||
case GitDeploy:
|
||||
}
|
||||
|
||||
// generate source
|
||||
if err := generateCmd().Execute(); err != nil {
|
||||
sfault("generate project failed: %v", err)
|
||||
}
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config := outter.Config.Deploy
|
||||
|
@ -31,6 +36,7 @@ func deployCmd() *cobra.Command {
|
|||
}
|
||||
if err := uploadToUpyun(config.UpyunAuth); err != nil {
|
||||
serr("%v", err)
|
||||
return
|
||||
}
|
||||
sout("deploy to upyun success")
|
||||
}
|
||||
|
|
14
generate.go
14
generate.go
|
@ -18,15 +18,18 @@ import (
|
|||
|
||||
func generateCmd() *cobra.Command {
|
||||
var outter Outter
|
||||
|
||||
var startAt time.Time
|
||||
cmd := &cobra.Command{
|
||||
Use: "generate",
|
||||
Aliases: []string{"g"},
|
||||
Short: "generate project to dist folder",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
startAt := time.Now()
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
startAt = time.Now()
|
||||
// 读取配置文件
|
||||
outter.Config = readConfigFile()
|
||||
outter.Config.SourceVersion = randString(8)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// 读取资源文件
|
||||
outter.MetaData = readDataSource()
|
||||
// 读取文章列表
|
||||
|
@ -37,8 +40,11 @@ func generateCmd() *cobra.Command {
|
|||
outter.Theme = readTheme(outter.Config.Theme)
|
||||
// 数据写入模板文件
|
||||
outter.generate()
|
||||
|
||||
},
|
||||
PostRun: func(cmd *cobra.Command, args []string) {
|
||||
endAt := time.Since(startAt)
|
||||
fmt.Printf("generate used: %s\n", endAt.String())
|
||||
sout("generate used: %s", endAt.String())
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
|
|
|
@ -81,18 +81,18 @@ func (o *Outter) sourceCopy() {
|
|||
args = []string{"cp", "-r"}
|
||||
}
|
||||
themePath := slash(fmt.Sprintf("./themes/%s/", o.Config.Theme))
|
||||
destPath := slash("./dist/")
|
||||
cmd := exec.Command(args[0], append(args, themePath+"css", slash(destPath+"css/"))...)
|
||||
destPath := slash("./dist/" + o.Config.SourceVersion + "/")
|
||||
cmd := exec.Command(args[0], append(args[1:], themePath+"css", slash(destPath+"css/"))...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
sout(cmd.String())
|
||||
sfault("copy theme css source failed: %v", err)
|
||||
}
|
||||
cmd = exec.Command(args[0], append(args, themePath+"js", slash(destPath+"js/"))...)
|
||||
cmd = exec.Command(args[0], append(args[1:], themePath+"js", slash(destPath+"js/"))...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
sout(cmd.String())
|
||||
sfault("copy theme js source failed: %v", err)
|
||||
}
|
||||
cmd = exec.Command(args[0], append(args, themePath+"images", slash(destPath+"images/"))...)
|
||||
cmd = exec.Command(args[0], append(args[1:], themePath+"images", slash(destPath+"images/"))...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
sout(cmd.String())
|
||||
sfault("copy theme images source failed: %v", err)
|
||||
|
|
13
utils.go
13
utils.go
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -152,13 +154,20 @@ func goInstall(pkg string) error {
|
|||
}
|
||||
|
||||
func uploadToUpyun(auth string) error {
|
||||
_, err := exec.Command("upx", "--auth", auth, "rm", "/").Output()
|
||||
_, err := exec.Command("upx", "--auth", auth, "rm", "-d", "-a", "/*").Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("remove old data failed: %v", err)
|
||||
}
|
||||
_, err = exec.Command("upx", "--auth", auth, "put", "./dist").Output()
|
||||
_, err = exec.Command("upx", "--auth", auth, "put", "./dist/.", "/").Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("deploy data failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func randString(length int) string {
|
||||
s := sha256.New()
|
||||
s.Write([]byte(time.Now().String()))
|
||||
res := s.Sum(nil)
|
||||
return hex.EncodeToString(res)[:length]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue