Skip to content

Commit 069b0c3

Browse files
authored
Ask user for confirmation before deleting all users (#470)
* Ask user for confirmation before deleting all users * Add changelog entry
1 parent bb07fb6 commit 069b0c3

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.
1313

1414
### Added
1515

16+
- `src users delete` now asks for confirmation to delete all users when no user ID is provided. [#470](https://github.com/sourcegraph/src-cli/pull/470)
17+
1618
### Changed
1719

1820
### Fixed

cmd/src/users_delete.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

33
import (
4+
"bufio"
45
"context"
56
"flag"
67
"fmt"
8+
"os"
9+
"strconv"
10+
"strings"
711

812
"github.com/sourcegraph/src-cli/internal/api"
913
)
@@ -44,6 +48,37 @@ Examples:
4448

4549
client := cfg.apiClient(apiFlags, flagSet.Output())
4650

51+
if *userIDFlag == "" {
52+
query := `query UsersTotalCountCountUsers { users { totalCount } }`
53+
54+
var result struct {
55+
Users struct {
56+
TotalCount int
57+
}
58+
}
59+
ok, err := client.NewQuery(query).Do(context.Background(), &result)
60+
if err != nil || !ok {
61+
return err
62+
}
63+
64+
fmt.Printf("No user ID specified. This would delete %d users.\nType in this number to confirm and hit return: ", result.Users.TotalCount)
65+
reader := bufio.NewReader(os.Stdin)
66+
text, err := reader.ReadString('\n')
67+
if err != nil {
68+
return err
69+
}
70+
71+
count, err := strconv.Atoi(strings.TrimSpace(text))
72+
if err != nil {
73+
return err
74+
}
75+
76+
if count != result.Users.TotalCount {
77+
fmt.Println("Number does not match. Aborting.")
78+
return nil
79+
}
80+
}
81+
4782
query := `mutation DeleteUser(
4883
$user: ID!
4984
) {

0 commit comments

Comments
 (0)