varRootCmd=&cobra.Command{Use:"graph-annotations",Short:"Graph-annotations is a very simple tool for mackerel graph annotations API.",Long:"Complete API documentation is available at https://mackerel.io/api-docs/entry/graph-annotations",Run:func(cmd*cobra.Command,args[]string){},}funcinit(){cobra.OnInitialize()}
funcinit(){postCmd.Flags().StringVarP(&title,"title","s","","required: annotation title")postCmd.Flags().StringVar(&description,"description","","[optional] annotation details")postCmd.Flags().StringVar(&service,"service",os.Getenv("MACKEREL_SERVICE_NAME"),"required: service name, when it's empty value then will use enviroment variable of 'MACKEREL_SERVICE_NAME''")RootCmd.AddCommand(postCmd)}vartitlestringvardescriptionstringvarservicestringvarpostCmd=&cobra.Command{Use:"post",Short:"Creating graph annotations",Long:"Tne post command creates graph annotations via graph-annotations API.",Example:`graph-annotations post -s 'deploy application' ExampleRole1 ExampleRole2
graph-annotations post --service ExampleService -s 'deploy application' ExampleRole1 ExampleRole2`,RunE:func(cmd*cobra.Command,args[]string)error{// flagのバリデーションを省略しています
time:=time.Now().Unix()client:=mkr.NewClient(os.Getenv("MACKEREL_API_KEY"))annotation:=&mkr.GraphAnnotation{Service:service,Roles:args,From:time,To:time,Title:title,Description:description,}err:=client.CreateGraphAnnotation(annotation)iferr!=nil{returnerrors.Wrap(err,"client error.")}fmt.Printf("completed. params title:%s, from:%d to:%d, service:%s, roles:%s",title,time,time,service,args)returnnil},}
$ graph-annotation help post
Tne post command creates graph annotations via graph-annotations API.
Usage:
graph-annotations post [flags]
Examples:
graph-annotations post -s 'deploy application' ExampleRole1 ExampleRole2
graph-annotations post --service ExampleService -s 'deploy application' ExampleRole1 ExampleRole2
Flags:
--description string [optional] annotation details
--service string required: service name, when it's empty value then will use enviroment variable of 'MACKEREL_SERVICE_NAME''(default "local")
-s, --title string required: annotation title
child_process=require'child_process'module.exports=(robot)->robot.respond/note\s+(.+)$/,(msg)->option=msg.match[1]child_process.exec"graph-annotation post #{option}",(error,stdout,stderr)->if!erroroutput=stdout+''msg.sendoutputelseoutput=stderr+''msg.sendoutput