Skip to content

Commit 8ca78ea

Browse files
authored
[gpctl] add users unblock command (#19320)
* [gpctl] unblock user command * [gpctl] refactor blockUser Ergonomically it is nice to block and unblock as separate commands But we can consolidate logic to a single function
1 parent 10648e1 commit 8ca78ea

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

dev/gpctl/cmd/users-block.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"context"
99

1010
"github.com/spf13/cobra"
11-
12-
"github.com/gitpod-io/gitpod/common-go/log"
13-
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
1411
)
1512

1613
// usersBlockCmd represents the describe command
@@ -21,25 +18,7 @@ var usersBlockCmd = &cobra.Command{
2118
Run: func(cmd *cobra.Command, args []string) {
2219
ctx, cancel := context.WithCancel(context.Background())
2320
defer cancel()
24-
25-
client, err := newLegacyAPIConn()
26-
if err != nil {
27-
log.WithError(err).Fatal("cannot connect")
28-
}
29-
defer client.Close()
30-
31-
for _, uid := range args {
32-
err = client.AdminBlockUser(ctx, &protocol.AdminBlockUserRequest{
33-
UserID: uid,
34-
IsBlocked: true,
35-
})
36-
if err != nil {
37-
log.WithError(err).Error("cannot block user")
38-
} else {
39-
log.WithField("uid", uid).Info("user blocked")
40-
}
41-
}
42-
21+
blockUser(ctx, args, true)
4322
},
4423
}
4524

dev/gpctl/cmd/users-unblock.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import (
8+
"context"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// usersUnblockCmd represents the describe command
14+
var usersUnblockCmd = &cobra.Command{
15+
Use: "unblock <userID> ... <userID>",
16+
Short: "unblocks a user",
17+
Args: cobra.MinimumNArgs(1),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
ctx, cancel := context.WithCancel(context.Background())
20+
defer cancel()
21+
blockUser(ctx, args, false)
22+
},
23+
}
24+
25+
func init() {
26+
usersCmd.AddCommand(usersUnblockCmd)
27+
}

dev/gpctl/cmd/users.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package cmd
66

77
import (
8+
"context"
89
"fmt"
910
"os"
1011

1112
"github.com/gitpod-io/gitpod/common-go/log"
1213
api "github.com/gitpod-io/gitpod/gitpod-protocol"
14+
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
1315
"github.com/sirupsen/logrus"
1416
"github.com/spf13/cobra"
1517
)
@@ -56,3 +58,26 @@ func newLegacyAPIConn() (*api.APIoverJSONRPC, error) {
5658

5759
return conn, nil
5860
}
61+
62+
func blockUser(ctx context.Context, args []string, block bool) {
63+
client, err := newLegacyAPIConn()
64+
if err != nil {
65+
log.WithError(err).Fatal("cannot connect")
66+
}
67+
defer client.Close()
68+
69+
for _, uid := range args {
70+
err = client.AdminBlockUser(ctx, &protocol.AdminBlockUserRequest{
71+
UserID: uid,
72+
IsBlocked: block,
73+
})
74+
if err != nil {
75+
log.WithField("uid", uid).WithField("block", block).Errorf("AdminBlockUser failed with: %v", err)
76+
return
77+
} else {
78+
log.WithField("uid", uid).WithField("block", block).Info("AdminBlockUser")
79+
return
80+
}
81+
}
82+
log.Fatal("no args")
83+
}

0 commit comments

Comments
 (0)