给hugo加一个命令,并发布到github上

添加命令

  • 在commands下新建一个文件pull.go内容如下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package commands

import (
	"bytes"
	"fmt"
	"github.com/spf13/cobra"
	"os/exec"
	"strings"
)

var _ cmder = (*deployCmd)(nil)

type pullCmd struct {
	*baseBuilderCmd
}

func (b *commandsBuilder) newPullCmd() *pullCmd {
	cc := &pullCmd{}

	cmd := &cobra.Command{
		Use:   "pull",
		Short: "须先配置好github,调用此命令将内容推送到github",
		Long:  `须先配置好github,调用此命令将内容推送到github.`,

		RunE: func(cmd *cobra.Command, args []string) error {
			osCmdData := []string{
				"hugo",
				"git -C public add .",
				`git -C public commit -m "rebuild"`,
				"git -C public push origin master",
			}

			for _, cmd := range osCmdData {
				arr := strings.Split(cmd, " ")
				_ = runOsCmd(arr[0], arr[1:])
			}
			return nil
		},
	}

	cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
	return cc
}

func runOsCmd(command string, arg []string) error {
	osCmd := exec.Command(command, arg...)
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	osCmd.Stdout = &stdout
	osCmd.Stderr = &stderr
	_ = osCmd.Run()
	fmt.Printf("调用%s %s 返回:%v \n %v \n", command, arg, stderr.String(), stdout.String())
	return nil
}

注册此命令

编辑commands/commands.go,在方法addAll()调用b.addCommands中添加一行b.newPullCmd(),

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
	b.addCommands(
		b.newServerCmd(),
		newVersionCmd(),
		newEnvCmd(),
		b.newConfigCmd(),
		newCheckCmd(),
		b.newDeployCmd(),
		b.newConvertCmd(),
		b.newNewCmd(),
		b.newListCmd(),
		b.newPullCmd(), // 新增
		newImportCmd(),
		newGenCmd(),
		createReleaser(),
		b.newModCmd(),
	)