diff --git a/cmd/invitectl/README.md b/cmd/invitectl/README.md index d92acb0605b102c1db297586639e18eb73c4427c..3ad597ca4eb468c0547f6fa736d0f355e0bb977c 100644 --- a/cmd/invitectl/README.md +++ b/cmd/invitectl/README.md @@ -15,13 +15,14 @@ Usage: invitectl [command] Available Commands: - check Show bucket(s) for a supplied invite token - completion Generate the autocompletion script for the specified shell - delete Delete Invite Tokens in db by bucket(s) or by key - delete-all Delete all invite tokens in database - help Help about any command - list List all invite tokens stored in database - new Add new invite token to database + check Show bucket(s) for a supplied invite token + completion Generate the autocompletion script for the specified shell + delete Delete Invite Tokens in db by bucket(s) or by key + delete-all Delete all invite tokens in database + help Help about any command + list List all invite tokens stored in database + new Add new invite token to database + update-buckets Update invite token, with supplied buckets Flags: --db string Path to sqlite database. Creates a new db if file does not exist (required) @@ -88,6 +89,16 @@ pea@peabox: ./invitectl --db invites.db list • pYFxB4OlPvLhNhxloGqiOAfOO96eJQhKI+m1pho4hJU= bucket2 ``` + +## Update buckets for an invite token + +``` +pea@peabox: ./invitectl update-buckets -t "solitech_UjxcL3ROgF/2wBV44jFOuQ==" -b "bucket1,bucket6" --db invites.db + • Successfully deleted all invite tokens in database +``` + + + ## Delete by buckets ``` diff --git a/cmd/invitectl/cmd/db.go b/cmd/invitectl/cmd/db.go index 5085c609329bf7bd3b5d7c787927ad670edf716a..3e201b1b8719f52843b40eb2e801d72c010d5e99 100644 --- a/cmd/invitectl/cmd/db.go +++ b/cmd/invitectl/cmd/db.go @@ -47,6 +47,34 @@ func printBucketsByInviteToken(db *sqlx.DB, hashedInviteToken string) { log.Info().Msgf("%45s %s", hashedInviteToken, buckets) } +func updateBucketsByInviteToken(db *sqlx.DB, hashedInviteToken string, buckets string) { + log.Debug().Msgf("Looking in the database for hashed invite token %s", hashedInviteToken) + stmt, err := db.Prepare(`UPDATE tokens SET buckets = ? WHERE key = ?`) + failOnError("Could not create prepared statement", err) + defer stmt.Close() + + log.Debug().Msgf("Setting buckets: %s", buckets) + + _, err = stmt.Exec(buckets, hashedInviteToken) + failOnError("Could not execute statement", err) + + log.Info().Msgf("Updated buckets sucessfully") +} + +func getBucketsByInviteToken(db *sqlx.DB, hashedInviteToken string) (string, error) { + log.Debug().Msgf("Looking in the database for hashed invite token %s", hashedInviteToken) + stmt, err := db.Prepare(`SELECT buckets FROM tokens WHERE key = ?`) + failOnError("Could not create prepared statement", err) + defer stmt.Close() + + var buckets string + err = stmt.QueryRow(hashedInviteToken).Scan(&buckets) + failOnError("Could not execute statement", err) + + log.Info().Msgf("Found buckets: %s", buckets) + return buckets, nil +} + func insertInviteToken(db *sqlx.DB, token InviteToken) error { err := token.Validate() if err != nil { diff --git a/cmd/invitectl/cmd/db_test.go b/cmd/invitectl/cmd/db_test.go index bed70fe550cfcb7d1b2b7d4c593096b072544fb2..e634aef6d60276e152b28d2f1e990d736cfe86ea 100644 --- a/cmd/invitectl/cmd/db_test.go +++ b/cmd/invitectl/cmd/db_test.go @@ -80,12 +80,20 @@ func TestDBFunctions(t *testing.T) { require.Equal(t, testInviteToken2, tokens[1], "The test invite token 2 does not match with the token of the db") require.Equal(t, testInviteToken3, tokens[2], "The test invite token 3 does not match with the token of the db") + // update buckets by invite token + bucket_list = "bucket10,bucket20" + updateBucketsByInviteToken(db, "1234567890", "bucket10,bucket20") + actual, err := getBucketsByInviteToken(db, "1234567890") + require.NoError(t, err, "Could not update buckets by token") + require.Equal(t, bucket_list, actual, "Updated buckets does not match the expected buckets") + updateBucketsByInviteToken(db, "1234567890", "bucket1") + // delete two of the three buckets deletedRows, err := deleteInviteTokenByBucket(db, "bucket2") require.NoError(t, err, "Could not delete by bucket") require.Equal(t, int64(2), deletedRows, "should only delete 2 invite token") - // delete the last existing one by token/key + // delete the last existing one by token/key deletedRows, err = deleteInviteTokenByKey(db, "1234567890") require.NoError(t, err, "Could not delete by token") require.Equal(t, int64(1), deletedRows, "should only delete 2 invite token") diff --git a/cmd/invitectl/cmd/updateBuckets.go b/cmd/invitectl/cmd/updateBuckets.go new file mode 100644 index 0000000000000000000000000000000000000000..53978a60a958b1ad68965596a3842b765d909029 --- /dev/null +++ b/cmd/invitectl/cmd/updateBuckets.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "bufio" + "fmt" + "os" + "strings" + + "0xacab.org/leap/menshen/storage" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var updateBucketsCmd = &cobra.Command{ + Use: "update-buckets", + Short: "Update invite token, with supplied buckets", + Run: func(cmd *cobra.Command, args []string) { + doUpdateBucketsInviteToken(cmd) + }, +} + +var bucket_list string + +func init() { + rootCmd.AddCommand(updateBucketsCmd) + updateBucketsCmd.Flags().StringVarP(&token, "token", "t", "", "Invite token you want to updateBuckets. Use - to enter invite code interactively") + updateBucketsCmd.Flags().StringVarP(&bucket_list, "buckets", "b", "", "Comma separated list of buckets") + +} + +func doUpdateBucketsInviteToken(cmd *cobra.Command) { + + if len(token) == 0 { + log.Error().Msg("Invite token needs to be specified") + cmd.Help() // nolint: errupdateBuckets + os.Exit(1) + } + + if token == "-" { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter invite token: ") + var err error + token, err = reader.ReadString('\n') + failOnError("Could not read string from command line", err) + token = strings.TrimSpace(token) + } + updateBucketsInviteToken() +} + +func updateBucketsInviteToken() { + log.Info().Msgf("Update buckets for invite token: %s", token) + hashedToken := hashInviteToken(token) + + db, err := storage.OpenDatabase(dbPath) + failOnError("Could not open database", err) + defer db.Close() + + // to remove spaces in between. eg:( "bucket1, bucket2" -> "bucket1,bucket2") + bucket_list = strings.ReplaceAll(bucket_list, ", ", ",") + bucket_list = strings.ReplaceAll(bucket_list, " ,", ",") + updateBucketsByInviteToken(db, hashedToken, bucket_list) +} diff --git a/cmd/invitectl/invitectl b/cmd/invitectl/invitectl deleted file mode 100755 index 5c6ebc61ea3bcf105785239c1ead0679ba1335cd..0000000000000000000000000000000000000000 Binary files a/cmd/invitectl/invitectl and /dev/null differ