github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/alias-main.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/minio/cli"
    22  	json "github.com/minio/colorjson"
    23  	"github.com/minio/mc/pkg/probe"
    24  	"github.com/minio/pkg/v2/console"
    25  )
    26  
    27  //   Configure an alias in MinIO Client
    28  //
    29  //   ----
    30  //   NOTE: that the alias command only writes values to the config file.
    31  //   It does not use any configuration values from the environment variables.
    32  //
    33  //   One needs to edit configuration file manually, this is purposefully done
    34  //   so to avoid taking credentials over cli arguments. It is a security precaution
    35  //   ----
    36  //
    37  
    38  var aliasFlags = []cli.Flag{}
    39  
    40  var aliasSubcommands = []cli.Command{
    41  	aliasSetCmd,
    42  	aliasListCmd,
    43  	aliasRemoveCmd,
    44  	aliasImportCmd,
    45  	aliasExportCmd,
    46  }
    47  
    48  var aliasCmd = cli.Command{
    49  	Name:            "alias",
    50  	Usage:           "manage server credentials in configuration file",
    51  	Action:          mainAlias,
    52  	Before:          setGlobalsFromContext,
    53  	HideHelpCommand: true,
    54  	Flags:           append(aliasFlags, globalFlags...),
    55  	Subcommands:     aliasSubcommands,
    56  }
    57  
    58  // mainAlias is the handle for "mc alias" command. provides sub-commands which write configuration data in json format to config file.
    59  func mainAlias(ctx *cli.Context) error {
    60  	commandNotFound(ctx, aliasSubcommands)
    61  	return nil
    62  	// Sub-commands like add, list and remove have their own main.
    63  }
    64  
    65  // aliasMessage container for content message structure
    66  type aliasMessage struct {
    67  	op          string
    68  	prettyPrint bool
    69  	Status      string `json:"status"`
    70  	Alias       string `json:"alias"`
    71  	URL         string `json:"URL"`
    72  	AccessKey   string `json:"accessKey,omitempty"`
    73  	SecretKey   string `json:"secretKey,omitempty"`
    74  	API         string `json:"api,omitempty"`
    75  	Path        string `json:"path,omitempty"`
    76  	// Deprecated field, replaced by Path
    77  	Lookup string `json:"lookup,omitempty"`
    78  }
    79  
    80  // Print the config information of one alias, when prettyPrint flag
    81  // is activated, fields contents are cut and '...' will be added to
    82  // show a pretty table of all aliases configurations
    83  func (h aliasMessage) String() string {
    84  	switch h.op {
    85  	case "list":
    86  		// Create a new pretty table with cols configuration
    87  		t := newPrettyRecord(2,
    88  			Row{"Alias", "Alias"},
    89  			Row{"URL", "URL"},
    90  			Row{"AccessKey", "AccessKey"},
    91  			Row{"SecretKey", "SecretKey"},
    92  			Row{"API", "API"},
    93  			Row{"Path", "Path"},
    94  		)
    95  		// Handle deprecated lookup
    96  		path := h.Path
    97  		if path == "" {
    98  			path = h.Lookup
    99  		}
   100  		return t.buildRecord(h.Alias, h.URL, h.AccessKey, h.SecretKey, h.API, path)
   101  	case "remove":
   102  		return console.Colorize("AliasMessage", "Removed `"+h.Alias+"` successfully.")
   103  	case "add": // add is deprecated
   104  		fallthrough
   105  	case "set":
   106  		return console.Colorize("AliasMessage", "Added `"+h.Alias+"` successfully.")
   107  	case "import":
   108  		return console.Colorize("AliasMessage", "Imported `"+h.Alias+"` successfully.")
   109  	default:
   110  		return ""
   111  	}
   112  }
   113  
   114  // JSON jsonified host message
   115  func (h aliasMessage) JSON() string {
   116  	h.Status = "success"
   117  	jsonMessageBytes, e := json.MarshalIndent(h, "", " ")
   118  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
   119  
   120  	return string(jsonMessageBytes)
   121  }