github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/alias-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/pkg/v2/console"
    24  )
    25  
    26  var aliasRemoveCmd = cli.Command{
    27  	Name:      "remove",
    28  	ShortName: "rm",
    29  	Usage:     "remove an alias from configuration file",
    30  	Action: func(ctx *cli.Context) error {
    31  		return mainAliasRemove(ctx)
    32  	},
    33  	Before:          setGlobalsFromContext,
    34  	Flags:           globalFlags,
    35  	HideHelpCommand: true,
    36  	OnUsageError:    onUsageError,
    37  	CustomHelpTemplate: `NAME:
    38    {{.HelpName}} - {{.Usage}}
    39  
    40  USAGE:
    41    {{.HelpName}} ALIAS
    42  
    43  FLAGS:
    44    {{range .VisibleFlags}}{{.}}
    45    {{end}}
    46  EXAMPLES:
    47    1. Remove "goodisk" alias from the configuration.
    48       {{.Prompt}} {{.HelpName}} goodisk
    49  
    50  `,
    51  }
    52  
    53  // checkAliasRemoveSyntax - verifies input arguments to 'alias remove'.
    54  func checkAliasRemoveSyntax(ctx *cli.Context) {
    55  	args := ctx.Args()
    56  
    57  	if len(ctx.Args()) != 1 {
    58  		fatalIf(errInvalidArgument().Trace(args...),
    59  			"Incorrect number of arguments for alias remove command.")
    60  	}
    61  
    62  	alias := cleanAlias(args.Get(0))
    63  	if !isValidAlias(alias) {
    64  		fatalIf(errDummy().Trace(alias), "Invalid alias `"+alias+"`.")
    65  	}
    66  }
    67  
    68  // mainAliasRemove is the handle for "mc alias rm" command.
    69  func mainAliasRemove(ctx *cli.Context) error {
    70  	checkAliasRemoveSyntax(ctx)
    71  
    72  	console.SetColor("AliasMessage", color.New(color.FgGreen))
    73  
    74  	args := ctx.Args()
    75  	alias := args.Get(0)
    76  
    77  	aliasMsg := removeAlias(alias) // Remove an alias
    78  	aliasMsg.op = "remove"
    79  	printMsg(aliasMsg)
    80  	return nil
    81  }
    82  
    83  // aliasMustExist confirms that a given alias is present in Aliases array, returns error if not found
    84  
    85  func aliasMustExist(alias string) {
    86  	hostConfig := mustGetHostConfig(alias)
    87  	if hostConfig == nil {
    88  		fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.")
    89  	}
    90  }
    91  
    92  // removeAlias - removes an alias.
    93  func removeAlias(alias string) aliasMessage {
    94  	conf, err := loadMcConfig()
    95  	fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version `"+globalMCConfigVersion+"`.")
    96  
    97  	// check if alias is valid
    98  	aliasMustExist(alias)
    99  
   100  	// Remove the alias from the config.
   101  	delete(conf.Aliases, alias)
   102  
   103  	err = saveMcConfig(conf)
   104  	fatalIf(err.Trace(alias), "Unable to save the delete alias in config version `"+globalMCConfigVersion+"`.")
   105  
   106  	return aliasMessage{Alias: alias}
   107  }