Skip to content

Commit

Permalink
Merge 1ed0fe3 into 83e9692
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixolay committed Oct 21, 2019
2 parents 83e9692 + 1ed0fe3 commit c4bf098
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var (
plotting = flag.Bool("plotting", false, "enable images in all notifications")
)

var (
userDel = flag.String("user-del", "", "Delete all contacts and subscriptions for a user")
fromUser = flag.String("from-user", "", "Transfer subscriptions and contacts from user.")
toUser = flag.String("to-user", "", "Transfer subscriptions and contacts to user.")
)

func main() {
logger, dataBase := initApp()

Expand Down Expand Up @@ -71,6 +77,17 @@ func main() {
logger.Errorf("Failed to enable images in all notifications")
}
}
if *toUser != "" {
if err := transferUserSubscriptionsAndContacts(dataBase, *fromUser, *toUser); err != nil {
logger.Error(err)
}
}

if *userDel != "" {
if err := deleteUser(dataBase, *userDel); err != nil {
logger.Error(err)
}
}
}

func initApp() (moira.Logger, moira.Database) {
Expand Down
58 changes: 58 additions & 0 deletions cmd/cli/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"github.com/moira-alert/moira"
)

func transferUserSubscriptionsAndContacts(database moira.Database, from, to string) error {
contactIDs, err := database.GetUserContactIDs(from)
if err != nil {
return err
}
contacts, err := database.GetContacts(contactIDs)
if err != nil {
return err
}
for _, contact := range contacts {
contact.User = to
if err = database.SaveContact(contact); err != nil {
return err
}
}

subscriptionIDs, err := database.GetUserSubscriptionIDs(from)
if err != nil {
return err
}
subscriptions, err := database.GetSubscriptions(subscriptionIDs)
if err != nil {
return err
}
for _, subscription := range subscriptions {
subscription.User = to
}
return database.SaveSubscriptions(subscriptions)
}

func deleteUser(database moira.Database, user string) error {
subscriptionIDs, err := database.GetUserSubscriptionIDs(user)
if err != nil {
return err
}
for _, subscriptionID := range subscriptionIDs {
if err = database.RemoveSubscription(subscriptionID); err != nil {
return err
}
}

contactIDs, err := database.GetUserContactIDs(user)
if err != nil {
return err
}
for _, contactID := range contactIDs {
if err := database.RemoveContact(contactID); err != nil {
return err
}
}
return nil
}

0 comments on commit c4bf098

Please sign in to comment.