feat: deploy command

This commit is contained in:
xuthus5 2023-01-07 14:48:56 +08:00
parent dfb4defeb9
commit d769f4115d
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
5 changed files with 39 additions and 17 deletions

View File

@ -4,13 +4,14 @@ import "time"
// Config 配置文件 // Config 配置文件
type Config struct { type Config struct {
Title string `yaml:"title"` // 网站标题 Title string `yaml:"title"` // 网站标题
Seo SEO `yaml:"seo"` // seo相关信息 SourceVersion string `yaml:"-"` // 资源版本号
Person Person `yaml:"person"` // 博主个人信息 Seo SEO `yaml:"seo"` // seo相关信息
Theme string `yaml:"theme"` // 主题 Person Person `yaml:"person"` // 博主个人信息
Site Site `yaml:"site"` // 站点配置信息 Theme string `yaml:"theme"` // 主题
Now time.Time // 当前时间 Site Site `yaml:"site"` // 站点配置信息
Deploy Deploy `yaml:"deploy"` Now time.Time `yaml:"-"` // 当前时间
Deploy Deploy `yaml:"deploy"` // 部署配置
} }
type Site struct { type Site struct {

View File

@ -20,6 +20,11 @@ func deployCmd() *cobra.Command {
} }
case GitDeploy: case GitDeploy:
} }
// generate source
if err := generateCmd().Execute(); err != nil {
sfault("generate project failed: %v", err)
}
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
config := outter.Config.Deploy config := outter.Config.Deploy
@ -31,6 +36,7 @@ func deployCmd() *cobra.Command {
} }
if err := uploadToUpyun(config.UpyunAuth); err != nil { if err := uploadToUpyun(config.UpyunAuth); err != nil {
serr("%v", err) serr("%v", err)
return
} }
sout("deploy to upyun success") sout("deploy to upyun success")
} }

View File

@ -18,15 +18,18 @@ import (
func generateCmd() *cobra.Command { func generateCmd() *cobra.Command {
var outter Outter var outter Outter
var startAt time.Time
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "generate", Use: "generate",
Aliases: []string{"g"}, Aliases: []string{"g"},
Short: "generate project to dist folder", Short: "generate project to dist folder",
Run: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
startAt := time.Now() startAt = time.Now()
// 读取配置文件 // 读取配置文件
outter.Config = readConfigFile() outter.Config = readConfigFile()
outter.Config.SourceVersion = randString(8)
},
Run: func(cmd *cobra.Command, args []string) {
// 读取资源文件 // 读取资源文件
outter.MetaData = readDataSource() outter.MetaData = readDataSource()
// 读取文章列表 // 读取文章列表
@ -37,8 +40,11 @@ func generateCmd() *cobra.Command {
outter.Theme = readTheme(outter.Config.Theme) outter.Theme = readTheme(outter.Config.Theme)
// 数据写入模板文件 // 数据写入模板文件
outter.generate() outter.generate()
},
PostRun: func(cmd *cobra.Command, args []string) {
endAt := time.Since(startAt) endAt := time.Since(startAt)
fmt.Printf("generate used: %s\n", endAt.String()) sout("generate used: %s", endAt.String())
}, },
} }
return cmd return cmd

View File

@ -81,18 +81,18 @@ func (o *Outter) sourceCopy() {
args = []string{"cp", "-r"} args = []string{"cp", "-r"}
} }
themePath := slash(fmt.Sprintf("./themes/%s/", o.Config.Theme)) themePath := slash(fmt.Sprintf("./themes/%s/", o.Config.Theme))
destPath := slash("./dist/") destPath := slash("./dist/" + o.Config.SourceVersion + "/")
cmd := exec.Command(args[0], append(args, themePath+"css", slash(destPath+"css/"))...) cmd := exec.Command(args[0], append(args[1:], themePath+"css", slash(destPath+"css/"))...)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
sout(cmd.String()) sout(cmd.String())
sfault("copy theme css source failed: %v", err) 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 { if err := cmd.Run(); err != nil {
sout(cmd.String()) sout(cmd.String())
sfault("copy theme js source failed: %v", err) 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 { if err := cmd.Run(); err != nil {
sout(cmd.String()) sout(cmd.String())
sfault("copy theme images source failed: %v", err) sfault("copy theme images source failed: %v", err)

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"crypto/sha256"
"encoding/hex"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -152,13 +154,20 @@ func goInstall(pkg string) error {
} }
func uploadToUpyun(auth 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 { if err != nil {
return fmt.Errorf("remove old data failed: %v", err) 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 { if err != nil {
return fmt.Errorf("deploy data failed: %v", err) return fmt.Errorf("deploy data failed: %v", err)
} }
return nil 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]
}