cmds

package
v0.0.0-...-f78d7e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2020 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const ListOutputFormat = "%-20s%-20s"
View Source
const PS1ChangeFormat = `` /* 167-byte string literal not displayed */
View Source
const WXWorkRobotNameEnv = "WXWORK_ROBOT_NAME"

Variables

View Source
var AddCmd = cobra.Command{
	Use:     "add",
	Short:   "Add a new robot with name, description and hookKey",
	Long:    "添加一个新的机器人或者覆盖已有机器人的信息,需要指定名称,描述和Hook URL地址中的Key",
	Example: "$ wxrobot add <name> <description> <hookKey>",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 3 {
			cmd.Help()
			os.Exit(1)
		}
		robotName := args[0]
		robotDescription := args[1]
		robotHookKey := args[2]

		if wErr := writeRobotProfile(robotName, robotDescription, robotHookKey); wErr != nil {
			fmt.Println("Err:", wErr.Error())
			os.Exit(1)
		}
	},
}
View Source
var ListCmd = cobra.Command{
	Use:   "list",
	Short: "List all the robot profiles",
	Long:  "列出当前所有的企微机器人信息",
	Run: func(cmd *cobra.Command, args []string) {
		wxworkRobotProfiles, getErr := readRobotProfiles()
		if getErr != nil {
			fmt.Println("Err:", getErr.Error())
			os.Exit(1)
		}
		fmt.Println(fmt.Sprintf(ListOutputFormat, "Name", "Description"))
		fmt.Println(fmt.Sprintf(ListOutputFormat, "----", "-----------"))
		for robotName, wxWorkRobot := range wxworkRobotProfiles {
			fmt.Println(fmt.Sprintf(ListOutputFormat, robotName, wxWorkRobot.Description))
		}
	},
}
View Source
var SendCmd = cobra.Command{
	Use:   "send",
	Short: "Send various kinds of messages supported by the robot",
	Long:  "发送企业微信机器人支持的各种类型的消息,支持文本,Markdown,图片,资讯,文件",
	Run: func(cmd *cobra.Command, args []string) {
		if sendCmdRobotName == "" {

			sendCmdRobotName = os.Getenv(WXWorkRobotNameEnv)
			if sendCmdRobotName == "" {
				fmt.Println("Err: robot name not specified")
				os.Exit(1)
			}
		}
		var err error

		if sendCmdRobotTextMessage != "" {
			err = sendMessage(sendCmdRobotName, wechat.WxMessageTypeText, sendCmdRobotTextMessage)
		} else if sendCmdRobotMarkdownFile != "" {
			err = sendMessage(sendCmdRobotName, wechat.WxMessageTypeMarkdown, sendCmdRobotMarkdownFile)
		} else if sendCmdRobotImageFile != "" {
			err = sendMessage(sendCmdRobotName, wechat.WxMessageTypeImage, sendCmdRobotImageFile)
		} else if sendCmdRobotMediaFile != "" {
			err = sendMessage(sendCmdRobotName, wechat.WxMessageTypeFile, sendCmdRobotMediaFile)
		} else if sendCmdRobotNewsFile != "" {
			err = sendMessage(sendCmdRobotName, wechat.WxMessageTypeNews, sendCmdRobotNewsFile)
		} else {
			err = fmt.Errorf("must specify a kind of message to send")
		}

		if err != nil {
			fmt.Println("Err:", err.Error())
			os.Exit(1)
		}
	},
}

SendCmd first check whether the robot name specified in the command args, if not, it will check the environment variable WXWORK_ROBOT_NAME to find the robot name. If none of the robot name found, it emits error.

View Source
var UseCmd = cobra.Command{
	Use:     "use",
	Short:   "Use an existing robot to send messages",
	Long:    "切换到一个已存在的机器人来发送消息",
	Example: "$ wxrobot use <name>",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 1 {
			cmd.Help()
			os.Exit(1)
		}

		robotName := args[0]

		wxRobotProfiles, getErr := readRobotProfiles()
		if getErr != nil {
			fmt.Println("Err:", getErr.Error())
			os.Exit(1)
		}

		var wxRobot WxWorkRobotProfile
		if v, exists := wxRobotProfiles[robotName]; !exists {
			fmt.Println("Err: robot profile not found, please add it first")
			os.Exit(1)
		} else {
			wxRobot = v
		}
		bashProfileTempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("%s_bash_profile", robotName))
		bashProfileTempFileContent := fmt.Sprintf(PS1ChangeFormat, fmt.Sprintf("%s-%s", robotName, wxRobot.Description), WXWorkRobotNameEnv, robotName)
		if writeErr := ioutil.WriteFile(bashProfileTempFilePath, []byte(bashProfileTempFileContent), 0644); writeErr != nil {
			fmt.Println("Err: write robot bash profile error,", writeErr.Error())
			os.Exit(1)
		}
		fmt.Println(fmt.Sprintf("Run command `source %s` to make the robot bash profile effective", bashProfileTempFilePath))
	},
}

Functions

This section is empty.

Types

type WxWorkRobotProfile

type WxWorkRobotProfile struct {
	Description string `json:"description"`
	HookKey     string `json:"hook_key"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL