...
1 package owners
2
3 import (
4 "context"
5 "flag"
6 "fmt"
7
8 "github.com/peterbourgon/ff/v3/ffcli"
9
10 dsdssandboxes "edge-infra.dev/pkg/sds/lib/ci/dsds-sandboxes"
11 )
12
13 type OwnerFlagConfig struct {
14 ProjectsFilePath string
15 ProjectName string
16 }
17
18 func NewCmd() *ffcli.Command {
19 ownerFlagSet := flag.NewFlagSet("owners", flag.ExitOnError)
20 ownerFlagConfig := &OwnerFlagConfig{}
21 bindOwnerFlags(ownerFlagSet, ownerFlagConfig)
22
23 return &ffcli.Command{
24 Name: "owners",
25 ShortUsage: "billingcli owners",
26 ShortHelp: "List owners for each project",
27 Exec: func(_ context.Context, _ []string) error {
28 return ownersCmd(ownerFlagConfig)
29 },
30 FlagSet: ownerFlagSet,
31 }
32 }
33
34 func bindOwnerFlags(flags *flag.FlagSet, config *OwnerFlagConfig) {
35 flags.StringVar(
36 &config.ProjectsFilePath,
37 "project-dir",
38 dsdssandboxes.DefaultPlaypitDir,
39 "Location of the CI project YAML within edge-infra",
40 )
41 flags.StringVar(
42 &config.ProjectName,
43 "project",
44 "",
45 "Optionally restrict finding owners to a single project",
46 )
47 }
48
49 func ownersCmd(config *OwnerFlagConfig) error {
50 confs, err := dsdssandboxes.GetBillingConfs(config.ProjectsFilePath)
51 if err != nil {
52 return err
53 }
54
55 confs = filterConfsByProjectName(config.ProjectName, confs)
56
57 for project, conf := range confs {
58 report, err := ownersReport(conf)
59 if err != nil {
60 return err
61 }
62 printOwners(project, report)
63 }
64
65 return nil
66 }
67
68 func filterConfsByProjectName(name string, confs map[string]*dsdssandboxes.BillingConf) map[string]*dsdssandboxes.BillingConf {
69 if name == "" {
70 return confs
71 }
72
73 val, ok := confs[name]
74 if ok {
75 return map[string]*dsdssandboxes.BillingConf{name: val}
76 }
77
78 return map[string]*dsdssandboxes.BillingConf{}
79 }
80
81 func ownersReport(conf *dsdssandboxes.BillingConf) ([]string, error) {
82 client, err := dsdssandboxes.NewIamPolicyClient()
83 if err != nil {
84 return nil, err
85 }
86
87 return client.GetProjectOwners(conf.Metadata.Name)
88 }
89
90 func printOwners(project string, members []string) {
91 fmt.Printf("--%v--\n", project)
92 for _, member := range members {
93 fmt.Println(member)
94 }
95 fmt.Print("\n")
96 }
97
View as plain text