-
Notifications
You must be signed in to change notification settings - Fork 64
/
teams.go
102 lines (85 loc) · 2.64 KB
/
teams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright 2020 Docker Hub Tool authors
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 org
import (
"fmt"
"io"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)
const (
teamsName = "teams"
)
var (
teamsColumns = []teamColumn{
{"TEAM", func(t hub.Team) (string, int) { return t.Name, len(t.Name) }},
{"DESCRIPTION", func(t hub.Team) (string, int) { return t.Description, len(t.Description) }},
{"MEMBERS", func(t hub.Team) (string, int) {
s := fmt.Sprintf("%v", len(t.Members))
return s, len(s)
}},
}
)
type teamColumn struct {
header string
value func(t hub.Team) (string, int)
}
type teamsOptions struct {
format.Option
}
func newTeamsCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts teamsOptions
cmd := &cobra.Command{
Use: teamsName + " ORGANIZATION",
Short: "List all the teams in an organization",
Args: cli.ExactArgs(1),
DisableFlagsInUseLine: true,
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send(parent, teamsName)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runTeams(streams, hubClient, opts, args[0])
},
}
opts.AddFormatFlag(cmd.Flags())
return cmd
}
func runTeams(streams command.Streams, hubClient *hub.Client, opts teamsOptions, organization string) error {
teams, err := hubClient.GetTeams(organization)
if err != nil {
return err
}
return opts.Print(streams.Out(), teams, printTeams)
}
func printTeams(out io.Writer, values interface{}) error {
teams := values.([]hub.Team)
tw := tabwriter.New(out, " ")
for _, column := range teamsColumns {
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
for _, team := range teams {
for _, column := range teamsColumns {
value, width := column.value(team)
tw.Column(value, width)
}
tw.Line()
}
return tw.Flush()
}