From 5fff1d82bc82d91150e9b131b63dbd6a65b86232 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Wed, 6 Apr 2022 09:08:27 +0900 Subject: [PATCH] rebase current main and implement revoke --- bqrole/dataset.go | 19 +++++++++++-------- bqrole/project.go | 17 ++++++++++------- cmd/revoke.go | 23 +++++++++++++++++++++-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/bqrole/dataset.go b/bqrole/dataset.go index 0d606cf..3ccf9ba 100644 --- a/bqrole/dataset.go +++ b/bqrole/dataset.go @@ -91,7 +91,7 @@ func PermitDataset(role bq.AccessRole, project string, users, datasets []string, return nil } -func RevokeDataset(role bq.AccessRole, project string, users, datasets []string) error { +func RevokeDataset(role bq.AccessRole, project string, users, datasets []string, yes bool) error { ctx := context.Background() client, err := bq.NewClient(ctx, project) if err != nil { @@ -103,14 +103,17 @@ func RevokeDataset(role bq.AccessRole, project string, users, datasets []string) fmt.Printf("role: %s\n", role) fmt.Printf("datasets: %s\n", datasets) fmt.Printf("users: %s\n", users) - fmt.Printf("Are you sure? [y/n]") - reader := bufio.NewReader(os.Stdin) - res, err := reader.ReadString('\n') + if !yes { + fmt.Printf("Are you sure? [y/n]") - if err != nil || strings.TrimSpace(res) != "y" { - fmt.Println("Abort.") - return nil + reader := bufio.NewReader(os.Stdin) + res, err := reader.ReadString('\n') + + if err != nil || strings.TrimSpace(res) != "y" { + fmt.Println("Abort.") + return nil + } } defer client.Close() @@ -207,7 +210,7 @@ func revokeDatasetPermission(ctx context.Context, client *bq.Client, role bq.Acc return nil } -func hasBQRole(p ProjectPolicy, user string, role string) bool { +func hasBQRole(p *ProjectPolicy, user string, role string) bool { for _, b := range p.Bindings { if b.Role == role { for _, m := range b.Members { diff --git a/bqrole/project.go b/bqrole/project.go index de57f36..91c0161 100644 --- a/bqrole/project.go +++ b/bqrole/project.go @@ -67,7 +67,7 @@ func PermitProject(role, project string, users []string, yes bool) error { return nil } -func RevokeProject(role, project string, users []string) error { +func RevokeProject(role, project string, users []string, yes bool) error { ctx := context.Background() client, err := bq.NewClient(ctx, project) if err != nil { @@ -78,14 +78,17 @@ func RevokeProject(role, project string, users []string) error { fmt.Printf("project_id: %s\n", project) fmt.Printf("role: %s\n", role) fmt.Printf("users: %s\n", users) - fmt.Printf("If you proceeds, PROJECT-WIDE permission will be added. Are you sure? [y/n]") - reader := bufio.NewReader(os.Stdin) - res, err := reader.ReadString('\n') + if !yes { + fmt.Printf("If you proceeds, PROJECT-WIDE permission will be added. Are you sure? [y/n]") - if err != nil || strings.TrimSpace(res) != "y" { - fmt.Println("Abort.") - return nil + reader := bufio.NewReader(os.Stdin) + res, err := reader.ReadString('\n') + + if err != nil || strings.TrimSpace(res) != "y" { + fmt.Println("Abort.") + return nil + } } defer client.Close() diff --git a/cmd/revoke.go b/cmd/revoke.go index df23d82..28c50fd 100644 --- a/cmd/revoke.go +++ b/cmd/revoke.go @@ -43,8 +43,10 @@ bqiam revoke project READER -p bq-project-id -u user1@email.com }, } + cmd.PersistentFlags().BoolP("yes", "y", false, "Automatic yes to prompts") cmd.AddCommand( newRevokeDatasetCmd(), + newRevokeProjectCmd(), ) return cmd @@ -69,6 +71,9 @@ bqiam project READER -p bq-project-id -u user1@email.com -u user2@email.com`, cmd.Flags().StringSliceP("users", "u", []string{}, "Specify user email(s)") + _ = registerProjectsCompletions(cmd) + _ = registerUsersCompletions(cmd) + return cmd } @@ -92,7 +97,12 @@ func runRevokeProjectCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to parse users flag: %s", err) } - err = bqrole.RevokeProject(role, project, users) + yes, err := cmd.Flags().GetBool("yes") + if err != nil { + return fmt.Errorf("failed to parse yes flag: %s", err) + } + + err = bqrole.RevokeProject(role, project, users, yes) if err != nil { return fmt.Errorf("failed to revoke: %s", err) } @@ -120,6 +130,10 @@ bqiam dataset READER -p bq-project-id -u user1@email.com -u user2@email.com -d d cmd.Flags().StringSliceP("users", "u", []string{}, "Specify user email(s)") cmd.Flags().StringSliceP("datasets", "d", []string{}, "Specify dataset(s)") + _ = registerProjectsCompletions(cmd) + _ = registerDatasetsCompletions(cmd) + _ = registerUsersCompletions(cmd) + return cmd } @@ -148,7 +162,12 @@ func runRevokeDatasetCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to parse datasets flag: %s", err) } - err = bqrole.RevokeDataset(role, project, users, datasets) + yes, err := cmd.Flags().GetBool("yes") + if err != nil { + return fmt.Errorf("failed to parse yes flag: %s", err) + } + + err = bqrole.RevokeDataset(role, project, users, datasets, yes) if err != nil { return fmt.Errorf("failed to revoke: %s", err) }