Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Adds tribe specific commands to pulsectl
Browse files Browse the repository at this point in the history
  • Loading branch information
jcooklin authored and pittma committed Oct 29, 2015
1 parent 0581e55 commit f8a5b75
Show file tree
Hide file tree
Showing 10 changed files with 747 additions and 17 deletions.
72 changes: 72 additions & 0 deletions cmd/pulsectl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,60 @@ var (
},
},
}

tribeCommands = []cli.Command{
{
Name: "member",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "list",
Action: listMembers,
},
{
Name: "show",
Usage: "show <member_name>",
Action: showMember,
Flags: []cli.Flag{flVerbose},
},
},
},
{
Name: "agreement",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "list",
Action: listAgreements,
},
{
Name: "create",
Usage: "create <agreement_name>",
Action: createAgreement,
},
{
Name: "delete",
Usage: "delete <agreement_name>",
Action: deleteAgreement,
},
{
Name: "join",
Usage: "join <agreement_name> <member_name>",
Action: joinAgreement,
},
{
Name: "leave",
Usage: "leave <agreement_name> <member_name>",
Action: leaveAgreement,
},
{
Name: "members",
Usage: "members <agreement_name>",
Action: agreementMembers,
},
},
},
}
)

func printFields(tw *tabwriter.Writer, indent bool, width int, fields ...interface{}) {
Expand All @@ -157,3 +211,21 @@ func printFields(tw *tabwriter.Writer, indent bool, width int, fields ...interfa
}
fmt.Fprintln(tw, argArray...)
}

type ByCommand []cli.Command

func (s ByCommand) Len() int {
return len(s)
}
func (s ByCommand) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByCommand) Less(i, j int) bool {
if s[i].Name == "help" {
return false
}
if s[j].Name == "help" {
return true
}
return s[i].Name < s[j].Name
}
6 changes: 6 additions & 0 deletions cmd/pulsectl/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,10 @@ var (
Name: "metric-namespace, m",
Usage: "A metric namespace",
}

// general
flVerbose = cli.BoolFlag{
Name: "verbose, v",
Usage: "Verbose output",
}
)
6 changes: 6 additions & 0 deletions cmd/pulsectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main

import (
"os"
"sort"
"time"

"github.com/codegangsta/cli"
Expand All @@ -46,6 +47,11 @@ func main() {
if pClient == nil {
pClient = client.New(c.GlobalString("url"), c.GlobalString("api-version"), c.GlobalBool("insecure"))
}
resp := pClient.ListAgreements()
if resp.Err == nil {
app.Commands = append(app.Commands, tribeCommands...)
}
sort.Sort(ByCommand(app.Commands))
return nil
}

Expand Down
218 changes: 218 additions & 0 deletions cmd/pulsectl/tribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"text/tabwriter"

"github.com/codegangsta/cli"
"github.com/intelsdi-x/pulse/mgmt/tribe/agreement"
)

func listMembers(ctx *cli.Context) {
resp := pClient.ListMembers()
if resp.Err != nil {
fmt.Printf("Error getting members:\n%v\n", resp.Err)
os.Exit(1)
}

if len(resp.Members) > 0 {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
defer w.Flush()
printFields(w, false, 0,
"Name",
)
for _, m := range resp.Members {
printFields(w, false, 0, m)
}
} else {
fmt.Println("None")
}
}

func showMember(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.GetMember(ctx.Args().First())
if resp.Err != nil {
fmt.Printf("Error:\n%v\n", resp.Err)
os.Exit(1)
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
defer w.Flush()
fields := []interface{}{"Name", "Plugin Agreement", "Task Agreements"}
if ctx.Bool("verbose") {
fields = append(fields, "tags")
}
printFields(w, false, 0,
fields...,
)
var tasks bytes.Buffer
for idx, task := range resp.TaskAgreements {
tasks.WriteString(task)
if idx < (len(resp.TaskAgreements) - 1) {
tasks.WriteString(",")
}
}
tags, err := json.Marshal(resp.Tags)
if err != nil {
fmt.Printf("Error:\n%v\n", err)
os.Exit(1)
}

values := []interface{}{resp.Name, resp.PluginAgreement, tasks.String()}
if ctx.Bool("verbose") {
values = append(values, string(tags))
}

printFields(w, false, 0, values...)

}

func listAgreements(ctx *cli.Context) {
resp := pClient.ListAgreements()
if resp.Err != nil {
fmt.Printf("Error getting members:\n%v\n", resp.Err)
os.Exit(1)
}
printAgreements(resp.Agreements)
}

func createAgreement(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.AddAgreement(ctx.Args().First())
if resp.Err != nil {
fmt.Printf("Error creating agreement: %v\n", resp.Err)
os.Exit(1)
}
printAgreements(resp.Agreements)
}

func deleteAgreement(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.DeleteAgreement(ctx.Args().First())
if resp.Err != nil {
fmt.Printf("Error: %v\n", resp.Err)
os.Exit(1)
}
printAgreements(resp.Agreements)
}

func joinAgreement(ctx *cli.Context) {
if len(ctx.Args()) != 2 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.JoinAgreement(ctx.Args().First(), ctx.Args().Get(1))
if resp.Err != nil {
fmt.Printf("Error: %v\n", resp.Err)
os.Exit(1)
}
printAgreements(map[string]*agreement.Agreement{resp.Agreement.Name: resp.Agreement})
}

func leaveAgreement(ctx *cli.Context) {
if len(ctx.Args()) != 2 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.LeaveAgreement(ctx.Args().First(), ctx.Args().Get(1))
if resp.Err != nil {
fmt.Printf("Error: %v\n", resp.Err)
os.Exit(1)
}
printAgreements(map[string]*agreement.Agreement{resp.Agreement.Name: resp.Agreement})
}

func agreementMembers(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
fmt.Println("Incorrect usage:")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}

resp := pClient.GetAgreement(ctx.Args().First())
if resp.Err != nil {
fmt.Printf("Error: %v\n", resp.Err)
os.Exit(1)
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
defer w.Flush()
printFields(w, false, 0, "Name")
for _, v := range resp.Agreement.Members {
printFields(w, false, 0, v.Name)
}
}

func printAgreements(agreements map[string]*agreement.Agreement) {
if len(agreements) > 0 {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
defer w.Flush()
printFields(w, false, 0,
"Name", "Number of Members", "plugins", "tasks",
)

var keys []string
for k := range agreements {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
v := agreements[k]
var plugins interface{}
var tasks interface{}
if v.PluginAgreement != nil {
plugins = len(v.PluginAgreement.Plugins)
}
if v.TaskAgreement != nil {
tasks = len(v.TaskAgreement.Tasks)
}
printFields(w, false, 0, v.Name, len(v.Members), plugins, tasks)
}
} else {
fmt.Println("None")
}
}
Loading

0 comments on commit f8a5b75

Please sign in to comment.