github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-group-remove.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"github.com/fatih/color"
    22  	"github.com/minio/cli"
    23  	"github.com/minio/madmin-go/v3"
    24  	"github.com/minio/mc/pkg/probe"
    25  	"github.com/minio/pkg/v2/console"
    26  )
    27  
    28  var adminGroupRemoveCmd = cli.Command{
    29  	Name:         "remove",
    30  	ShortName:    "rm",
    31  	Usage:        "remove group or members from a group",
    32  	Action:       mainAdminGroupRemove,
    33  	OnUsageError: onUsageError,
    34  	Before:       setGlobalsFromContext,
    35  	Flags:        globalFlags,
    36  	CustomHelpTemplate: `NAME:
    37    {{.HelpName}} - {{.Usage}}
    38  
    39  USAGE:
    40    {{.HelpName}} TARGET GROUPNAME [USERNAMES...]
    41  
    42  FLAGS:
    43    {{range .VisibleFlags}}{{.}}
    44    {{end}}
    45  EXAMPLES:
    46    1. Remove members 'tencent' and 'fivecent' from group 'allcents'.
    47       {{.Prompt}} {{.HelpName}} myminio allcents tencent fivecent
    48  
    49    2. Remove group 'allcents'.
    50       {{.Prompt}} {{.HelpName}} myminio allcents
    51  `,
    52  }
    53  
    54  // checkAdminGroupRemoveSyntax - validate all the passed arguments
    55  func checkAdminGroupRemoveSyntax(ctx *cli.Context) {
    56  	if len(ctx.Args()) < 2 {
    57  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    58  	}
    59  }
    60  
    61  // mainAdminGroupRemove is the handle for "mc admin group remove" command.
    62  func mainAdminGroupRemove(ctx *cli.Context) error {
    63  	checkAdminGroupRemoveSyntax(ctx)
    64  
    65  	console.SetColor("GroupMessage", color.New(color.FgGreen))
    66  
    67  	// Get the alias parameter from cli
    68  	args := ctx.Args()
    69  	aliasedURL := args.Get(0)
    70  
    71  	// Create a new MinIO Admin Client
    72  	client, err := newAdminClient(aliasedURL)
    73  	fatalIf(err, "Unable to initialize admin connection.")
    74  
    75  	members := []string{}
    76  	for i := 2; i < ctx.NArg(); i++ {
    77  		members = append(members, args.Get(i))
    78  	}
    79  	gAddRemove := madmin.GroupAddRemove{
    80  		Group:    args.Get(1),
    81  		Members:  members,
    82  		IsRemove: true,
    83  	}
    84  
    85  	e := client.UpdateGroupMembers(globalContext, gAddRemove)
    86  	fatalIf(probe.NewError(e).Trace(args...), "Could not perform remove operation")
    87  
    88  	printMsg(groupMessage{
    89  		op:        ctx.Command.Name,
    90  		GroupName: args.Get(1),
    91  		Members:   members,
    92  	})
    93  
    94  	return nil
    95  }