Skip to content

Commit

Permalink
update CLI usage to be consistent about handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 16, 2017
1 parent 77978cc commit c19dad6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 78 deletions.
146 changes: 84 additions & 62 deletions cmd/kapacitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ Usage: kapacitor [options] [command] [args]
Commands:
record Record the result of a query or a snapshot of the current stream data.
define Create/update a task.
define-template Create/update a template.
define-handler Create/update an alert handler.
replay Replay a recording to a task.
replay-live Replay data against a task without recording it.
enable Enable and start running a task with live data.
disable Stop running a task.
reload Reload a running task with an updated task definition.
push Publish a task definition to another Kapacitor instance. Not implemented yet.
delete Delete tasks, templates, recordings, replays, topics or handlers.
list List information about tasks, templates, recordings, replays, topics, handlers or service-tests.
show Display detailed information about a task.
show-template Display detailed information about a template.
show-handler Display detailed information about an alert handler.
show-topic Display detailed information about an alert topic.
level Sets the logging level on the kapacitord server.
stats Display various stats about Kapacitor.
version Displays the Kapacitor version info.
vars Print debug vars in JSON format.
service-tests Test a service.
help Prints help for a command.
record Record the result of a query or a snapshot of the current stream data.
define Create/update a task.
define-template Create/update a template.
define-topic-handler Create/update an alert handler for a topic.
replay Replay a recording to a task.
replay-live Replay data against a task without recording it.
enable Enable and start running a task with live data.
disable Stop running a task.
reload Reload a running task with an updated task definition.
push Publish a task definition to another Kapacitor instance. Not implemented yet.
delete Delete tasks, templates, recordings, replays, topics or topic-handlers.
list List information about tasks, templates, recordings, replays, topics, topic-handlers or service-tests.
show Display detailed information about a task.
show-template Display detailed information about a template.
show-topic-handler Display detailed information about an alert handler for a topic.
show-topic Display detailed information about an alert topic.
level Sets the logging level on the kapacitord server.
stats Display various stats about Kapacitor.
version Displays the Kapacitor version info.
vars Print debug vars in JSON format.
service-tests Test a service.
help Prints help for a command.
Options:
`
Expand Down Expand Up @@ -138,9 +138,9 @@ func main() {
case "define-template":
commandArgs = args
commandF = doDefineTemplate
case "define-handler":
case "define-topic-handler":
commandArgs = args
commandF = doDefineHandler
commandF = doDefineTopicHandler
case "replay":
replayFlags.Parse(args)
commandArgs = replayFlags.Args()
Expand Down Expand Up @@ -174,9 +174,9 @@ func main() {
case "show-template":
commandArgs = args
commandF = doShowTemplate
case "show-handler":
case "show-topic-handler":
commandArgs = args
commandF = doShowHandler
commandF = doShowTopicHandler
case "show-topic":
commandArgs = args
commandF = doShowTopic
Expand Down Expand Up @@ -256,8 +256,8 @@ func doHelp(args []string) error {
defineFlags.Usage()
case "define-template":
defineTemplateFlags.Usage()
case "define-handler":
defineHandlerUsage()
case "define-topic-handler":
defineTopicHandlerUsage()
case "replay":
replayFlags.Usage()
case "enable":
Expand All @@ -274,8 +274,8 @@ func doHelp(args []string) error {
showUsage()
case "show-template":
showTemplateUsage()
case "show-handler":
showHandlerUsage()
case "show-topic-handler":
showTopicHandlerUsage()
case "show-topic":
showTopicUsage()
case "level":
Expand Down Expand Up @@ -815,8 +815,8 @@ func doDefineTemplate(args []string) error {
return err
}

func defineHandlerUsage() {
var u = `Usage: kapacitor define-handler <topic> <path to handler file>
func defineTopicHandlerUsage() {
var u = `Usage: kapacitor define-topic-handler <topic id> <handler id> <path to handler spec file>
Create or update a handler.
Expand All @@ -826,23 +826,25 @@ For example:
Define a handler using the slack.yaml file:
$ kapacitor define-handler system slack.yaml
$ kapacitor define-handler system my_handler slack.yaml
Options:
`
fmt.Fprintln(os.Stderr, u)
}

func doDefineHandler(args []string) error {
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "Must provide both a topic ID and a path to a handler file.")
defineHandlerUsage()
func doDefineTopicHandler(args []string) error {
if len(args) != 3 {
fmt.Fprintln(os.Stderr, "Must provide a topic ID, a handler ID and a path to a handler file.")
defineTopicHandlerUsage()
os.Exit(2)
}
topic := args[0]
p := args[1]
handlerID := args[1]
p := args[2]
f, err := os.Open(p)
if err != nil {
return errors.Wrapf(err, "failed to open handler file %q", p)
return errors.Wrapf(err, "failed to open handler spec file %q", p)
}

// Decode file into HandlerOptions
Expand All @@ -862,6 +864,7 @@ func doDefineHandler(args []string) error {
return errors.Wrapf(err, "failed to unmarshal json handler file %q", p)
}
}
ho.ID = handlerID

l := cli.TopicHandlerLink(topic, ho.ID)
handler, _ := cli.TopicHandler(l)
Expand Down Expand Up @@ -1428,18 +1431,18 @@ func doShowTemplate(args []string) error {

// Show Handler

func showHandlerUsage() {
var u = `Usage: kapacitor show-handler [topic ID] [handler ID]
func showTopicHandlerUsage() {
var u = `Usage: kapacitor show-topic-handler [topic ID] [handler ID]
Show details about a specific handler.
`
fmt.Fprintln(os.Stderr, u)
}

func doShowHandler(args []string) error {
func doShowTopicHandler(args []string) error {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "Must specify both topic and handler IDs")
showHandlerUsage()
showTopicHandlerUsage()
os.Exit(2)
}

Expand Down Expand Up @@ -1530,12 +1533,24 @@ func doShowTopic(args []string) error {
// List

func listUsage() {
var u = `Usage: kapacitor list (tasks|templates|recordings|replays|topics|handlers|service-tests) [ID or pattern]...
var u = `Usage: kapacitor list (tasks|templates|recordings|replays|topics|topic-handlers|service-tests) [ID or pattern]...
List tasks, templates, recordings, replays, topics or handlers and their current state.
If no ID or pattern is given then all items will be listed.
Listing handlers requires that the topic ID or pattern be specified before the handler patterns.
$ kapacitor list topic-handlers [topicID or pattern] [ID or pattern]
For example list all handlers in the system topic
$ kapacitor list topic-handlers system
For example list all email* handlers in the system topic
$ kapacitor list topic-handlers system email*
`
fmt.Fprintln(os.Stderr, u)
}
Expand All @@ -1554,7 +1569,7 @@ func (t TemplateList) Swap(i, j int) { t[i], t[j] = t[j], t[i] }

func doList(args []string) error {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "Must specify 'tasks', 'recordings', 'replays', 'topics', or 'handlers'")
fmt.Fprintln(os.Stderr, "Must specify 'tasks', 'recordings', 'replays', 'topics', or 'topic-handlers'")
listUsage()
os.Exit(2)
}
Expand Down Expand Up @@ -1732,19 +1747,26 @@ func doList(args []string) error {
fmt.Fprintf(os.Stdout, outFmt, s.Name)
}
}
case "handlers":
// Get all topics first
topics, err := cli.ListTopics(nil)
case "topic-handlers":
topicPattern := patterns[0]
topics, err := cli.ListTopics(&client.ListTopicsOptions{
Pattern: topicPattern,
})
if err != nil {
return err
}
maxID := 2 // len("ID")
maxTopic := 5 // len("Topic")
maxKind := 4 // len("Kind")
patterns = patterns[1:]
if len(patterns) == 0 {
// Use empty pattern to match all handlers
patterns = []string{""}
}
maxTopic := 10
maxID := 10
maxKind := 10
// The handlers are returned in sorted order already, no need to sort them here.
type info struct {
ID string
Topic string
ID string
Kind string
}
var allHandlers []info
Expand All @@ -1758,8 +1780,8 @@ func doList(args []string) error {
}
for _, h := range handlers.Handlers {
i := info{
ID: h.ID,
Topic: topic.ID,
ID: h.ID,
Kind: h.Kind,
}
if l := len(i.ID); l > maxID {
Expand All @@ -1775,10 +1797,10 @@ func doList(args []string) error {
}
}
}
outFmt := fmt.Sprintf("%%-%dv%%-%dv%%-%dv\n", maxID+1, maxTopic+1, maxKind+1)
fmt.Fprintf(os.Stdout, outFmt, "ID", "Topic", "Kind")
outFmt := fmt.Sprintf("%%-%dv%%-%dv%%-%dv\n", maxTopic+1, maxID+1, maxKind+1)
fmt.Fprintf(os.Stdout, outFmt, "Topic", "ID", "Kind")
for _, h := range allHandlers {
fmt.Fprintf(os.Stdout, outFmt, h.ID, h.Topic, h.Kind)
fmt.Fprintf(os.Stdout, outFmt, h.Topic, h.ID, h.Kind)
}
case "topics":
maxID := 2 // len("ID")
Expand Down Expand Up @@ -1808,23 +1830,23 @@ func doList(args []string) error {
fmt.Fprintf(os.Stdout, outFmt, t.ID, t.Level, t.Collected)
}
default:
return fmt.Errorf("cannot list '%s' did you mean 'tasks', 'recordings', 'replays', 'topics', 'handlers' or 'service-tests'?", kind)
return fmt.Errorf("cannot list '%s' did you mean 'tasks', 'recordings', 'replays', 'topics', 'topic-handlers' or 'service-tests'?", kind)
}
return nil

}

// Delete
func deleteUsage() {
var u = `Usage: kapacitor delete (tasks|templates|recordings|replays|topics|handlers) [ID or pattern]...
var u = `Usage: kapacitor delete (tasks|templates|recordings|replays|topics|topic-handlers) [ID or pattern]...
Delete a tasks, templates, recordings, replays, topics or handlers.
If a task is enabled it will be disabled and then deleted.
Deleting a handler requires that the topic be specified before the pattern.
$ kapacitor delete handlers [topic] [ID or pattern]
$ kapacitor delete topic-handlers [topic] [ID or pattern]
For example:
Expand All @@ -1843,7 +1865,7 @@ For example:
You can delete a handler in the topic 'system':
$ kapacitor delete handlers system slack
$ kapacitor delete topic-handlers system slack
`
fmt.Fprintln(os.Stderr, u)
}
Expand Down Expand Up @@ -1960,7 +1982,7 @@ func doDelete(args []string) error {
}
}
}
case "handlers":
case "topic-handlers":
topic := args[1]
for _, pattern := range args[2:] {
handlers, err := cli.ListTopicHandlers(cli.TopicHandlersLink(topic), &client.ListTopicHandlersOptions{
Expand All @@ -1977,7 +1999,7 @@ func doDelete(args []string) error {
}
}
default:
return fmt.Errorf("cannot delete '%s' did you mean 'tasks', 'templates', 'recordings', 'replays', 'topics' or 'handlers'?", kind)
return fmt.Errorf("cannot delete '%s' did you mean 'tasks', 'templates', 'recordings', 'replays', 'topics' or 'topic-handlers'?", kind)
}
return nil
}
Expand Down
31 changes: 15 additions & 16 deletions usr/share/bash-completion/completions/kapacitor
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,16 @@ _kapacitor()
tasks|templates|recordings|replays|topics|service-tests)
words=$(_kapacitor_list "${COMP_WORDS[2]}" "$cur")
;;
handlers)
case "${COMP_WORDS[1]}" in
list)
words=$(_kapacitor_list handlers "$cur")
;;
delete)
if [[ -z "${COMP_WORDS[3]}" || ("$cur" = "${COMP_WORDS[3]}" && -z "${COMP_WORDS[4]}") ]]
then
words=$(_kapacitor_list topics "$cur")
else
words=$(_kapacitor_list handlers "$cur")
fi
;;
esac
topic-handlers)
if [[ -z "${COMP_WORDS[3]}" || ("$cur" = "${COMP_WORDS[3]}" && -z "${COMP_WORDS[4]}") ]]
then
words=$(_kapacitor_list topics "$cur")
else
words=$(_kapacitor_list_topic_handlers "${COMP_WORDS[3]}" "$cur")
fi
;;
*)
words='tasks templates recordings replays handlers topics service-tests'
words='tasks templates recordings replays topic-handlers topics service-tests'
;;
esac
;;
Expand All @@ -187,7 +180,7 @@ _kapacitor()
then
words=$(_kapacitor_list topics "$cur")
else
words=$(_kapacitor_list handlers "$cur")
words=$(_kapacitor_list_topic_handlers "${COMP_WORDS[2]}" "$cur")
fi
;;
show-topic)
Expand Down Expand Up @@ -266,6 +259,12 @@ _kapacitor_list()
kapacitor list "$1" "$2*" 2>/dev/null | awk 'NR>1 {print $1}' 2>/dev/null
}

_kapacitor_list_topic_handlers()
{
# List a certain kind of object and return a set of IDs
kapacitor list topic-handlers "$1" "$2*" 2>/dev/null | awk 'NR>1 {print $2}' 2>/dev/null
}

_kapacitor_list_filtered()
{
# List a certain kind of object and return a filtered set of IDs ($3: AWK 'if' test)
Expand Down

0 comments on commit c19dad6

Please sign in to comment.