github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/support-proxy-set.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  	"net/url"
    22  
    23  	"github.com/minio/cli"
    24  	"github.com/minio/mc/pkg/probe"
    25  	"github.com/minio/pkg/v2/console"
    26  )
    27  
    28  type supportProxySetMessage struct {
    29  	Status string `json:"status"`
    30  	Proxy  string `json:"proxy"`
    31  }
    32  
    33  // String colorized proxy set message
    34  func (s supportProxySetMessage) String() string {
    35  	return console.Colorize(supportSuccessMsgTag, "Proxy is now set to "+s.Proxy)
    36  }
    37  
    38  // JSON jsonified proxy set message
    39  func (s supportProxySetMessage) JSON() string {
    40  	s.Status = "success"
    41  	return toJSON(s)
    42  }
    43  
    44  var supportProxySetCmd = cli.Command{
    45  	Name:            "set",
    46  	Usage:           "configure proxy to given URL",
    47  	Action:          mainSupportProxySet,
    48  	OnUsageError:    onUsageError,
    49  	Before:          setGlobalsFromContext,
    50  	Flags:           globalFlags,
    51  	HideHelpCommand: true,
    52  	CustomHelpTemplate: `NAME:
    53    {{.HelpName}} - {{.Usage}}
    54  
    55  USAGE:
    56    {{.HelpName}} [FLAGS] TARGET PROXY_URL
    57  
    58  FLAGS:
    59    {{range .VisibleFlags}}{{.}}
    60    {{end}}
    61  EXAMPLES:
    62    1. Set the proxy to http://my.proxy for cluster with alias 'myminio'
    63       {{.Prompt}} {{.HelpName}} myminio http://my.proxy
    64  `,
    65  }
    66  
    67  func checkSupportProxySetSyntax(ctx *cli.Context) {
    68  	if len(ctx.Args()) != 2 {
    69  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    70  	}
    71  }
    72  
    73  // mainSupportProxySet is the handle for "mc support proxy set" command.
    74  func mainSupportProxySet(ctx *cli.Context) error {
    75  	// Check for command syntax
    76  	checkSupportProxySetSyntax(ctx)
    77  	setSuccessMessageColor()
    78  
    79  	// Get the alias parameter from cli
    80  	args := ctx.Args()
    81  	aliasedURL := args.Get(0)
    82  	alias, _ := url2Alias(aliasedURL)
    83  
    84  	validateClusterRegistered(alias, false)
    85  
    86  	// Create a new MinIO Admin Client
    87  	client := getClient(aliasedURL)
    88  
    89  	// Call set config API
    90  	proxy := args.Get(1)
    91  	if len(proxy) == 0 {
    92  		fatal(errDummy().Trace(), "Proxy must not be empty")
    93  	}
    94  
    95  	_, e := url.Parse(proxy)
    96  	fatalIf(probe.NewError(e), "Invalid proxy:")
    97  
    98  	// Main execution
    99  	_, e = client.SetConfigKV(globalContext, "subnet proxy="+proxy)
   100  	fatalIf(probe.NewError(e), "Unable to set proxy '%s':", proxy)
   101  
   102  	printMsg(supportProxySetMessage{Proxy: proxy})
   103  	return nil
   104  }