[cli] Make it optional to pass adminUser when only one account is configured

This commit is contained in:
Neeraj Gupta 2024-03-14 16:01:40 +05:30
parent 324eeed1c5
commit da3c6a78d4
2 changed files with 15 additions and 4 deletions

View file

@ -94,12 +94,12 @@ func init() {
rootCmd.AddCommand(_adminCmd)
_ = _userDetailsCmd.MarkFlagRequired("admin-user")
_ = _userDetailsCmd.MarkFlagRequired("user")
_userDetailsCmd.Flags().StringP("admin-user", "a", "", "The email of the admin user. (required)")
_userDetailsCmd.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_userDetailsCmd.Flags().StringP("user", "u", "", "The email of the user to fetch details for. (required)")
_listUsers.Flags().StringP("admin-user", "a", "", "The email of the admin user. (required)")
_disable2faCmd.Flags().StringP("admin-user", "a", "", "The email of the admin user. (required)")
_listUsers.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_disable2faCmd.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_disable2faCmd.Flags().StringP("user", "u", "", "The email of the user to disable 2FA for. (required)")
_updateFreeUserStorage.Flags().StringP("admin-user", "a", "", "The email of the admin user. (required)")
_updateFreeUserStorage.Flags().StringP("admin-user", "a", "", "The email of the admin user.")
_updateFreeUserStorage.Flags().StringP("user", "u", "", "The email of the user to update subscription for. (required)")
// add a flag with no value --no-limit
_updateFreeUserStorage.Flags().String("no-limit", "True", "When true, sets 100TB as storage limit, and expiry to current date + 100 years")

View file

@ -102,6 +102,9 @@ func (c *ClICtrl) buildAdminContext(ctx context.Context, adminEmail string) (con
if err != nil {
return nil, err
}
if len(accounts) == 0 {
return nil, fmt.Errorf("no accounts found, use `account add` to add an account")
}
var acc *model.Account
for _, a := range accounts {
if a.Email == adminEmail {
@ -109,6 +112,14 @@ func (c *ClICtrl) buildAdminContext(ctx context.Context, adminEmail string) (con
break
}
}
if (len(accounts) > 1) && (acc == nil) {
return nil, fmt.Errorf("multiple accounts found, specify the admin email using --admin-user")
}
if acc == nil && len(accounts) == 1 {
acc = &accounts[0]
fmt.Printf("Assuming %s as the Admin \n------------\n", acc.Email)
}
if acc == nil {
return nil, fmt.Errorf("account not found for %s, use `account list` to list accounts", adminEmail)
}