Ai
1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
create.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
package hook
import (
"errors"
"fmt"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/flags"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// CreateCommand adds command that allows user to create new hooks
func CreateCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME]",
Short: "create new hooks",
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
if !isInteractive {
// Mark flags are required for bash-completions
_ = cmd.MarkFlagRequired("command")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
opts := newHookOpts()
if len(args) > 0 {
opts.Name = args[0]
}
opts.Namespace = cli.Config.Namespace()
if isInteractive {
if err := opts.administerQuestionnaire(false); err != nil {
return err
}
} else {
opts.withFlags(cmd.Flags())
}
// Apply given arguments to hook
hook := types.HookConfig{}
opts.Copy(&hook)
if err := hook.Validate(); err != nil {
if !isInteractive {
cmd.SilenceUsage = false
}
return err
}
//
// TODO:
//
// Current validation is a bit too laissez faire. For usability we should
// determine whether there are assets / handlers / mutators associated w/
// the hook and warn the user if they do not exist yet.
if err := cli.Client.CreateHook(&hook); err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "Created")
return nil
},
}
_ = cmd.Flags().StringP("command", "c", "", "the command the hook should run")
_ = cmd.Flags().StringP("timeout", "t", timeoutDefault, "timeout, in seconds, at which the hook has to run")
_ = cmd.Flags().BoolP("stdin", "s", false, "stdin enabled on hook")
helpers.AddInteractiveFlag(cmd.Flags())
return cmd
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助