open-consul/command/intention/finder/finder.go

35 lines
682 B
Go
Raw Normal View History

package finder
import (
2018-05-12 05:19:39 +00:00
"fmt"
"github.com/hashicorp/consul/api"
)
// IDFromArgs returns the intention ID for the given CLI args. An error is returned
2018-05-12 05:19:39 +00:00
// if args is not 1 or 2 elements.
func IDFromArgs(client *api.Client, args []string) (string, error) {
2018-05-12 05:19:39 +00:00
switch len(args) {
case 1:
return args[0], nil
case 2:
ixn, _, err := client.Connect().IntentionGetExact(
args[0], args[1], nil,
)
2018-05-12 05:19:39 +00:00
if err != nil {
return "", err
}
if ixn == nil {
return "", fmt.Errorf(
"Intention with source %q and destination %q not found.",
args[0], args[1])
}
return ixn.ID, nil
default:
return "", fmt.Errorf("command requires exactly 1 or 2 arguments")
}
}