github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/support-upload.go (about)

     1  // Copyright (c) 2015-2024 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  	"fmt"
    22  	"net/url"
    23  
    24  	"github.com/minio/cli"
    25  	"github.com/minio/mc/pkg/probe"
    26  	"github.com/minio/pkg/v2/console"
    27  )
    28  
    29  // profile command flags.
    30  var (
    31  	uploadFlags = append(globalFlags,
    32  		cli.IntFlag{
    33  			Name:  "issue",
    34  			Usage: "SUBNET issue number to which the file is to be uploaded",
    35  		},
    36  		cli.StringFlag{
    37  			Name:  "comment",
    38  			Usage: "comment to be posted on the issue along with the file",
    39  		},
    40  		cli.BoolFlag{
    41  			Name:   "dev",
    42  			Usage:  "Development mode",
    43  			Hidden: true,
    44  		},
    45  	)
    46  )
    47  
    48  type supportUploadMessage struct {
    49  	Status   string `json:"status"`
    50  	IssueNum int    `json:"-"`
    51  	IssueURL string `json:"issueUrl"`
    52  }
    53  
    54  // String colorized upload message
    55  func (s supportUploadMessage) String() string {
    56  	msg := fmt.Sprintf("File uploaded to SUBNET successfully. Click here to visit the issue: %s", subnetIssueURL(s.IssueNum))
    57  	return console.Colorize(supportSuccessMsgTag, msg)
    58  }
    59  
    60  // JSON jsonified upload message
    61  func (s supportUploadMessage) JSON() string {
    62  	return toJSON(s)
    63  }
    64  
    65  var supportUploadCmd = cli.Command{
    66  	Name:            "upload",
    67  	Usage:           "upload file to a SUBNET issue",
    68  	Action:          mainSupportUpload,
    69  	OnUsageError:    onUsageError,
    70  	Before:          setGlobalsFromContext,
    71  	Flags:           uploadFlags,
    72  	HideHelpCommand: true,
    73  	CustomHelpTemplate: `NAME:
    74    {{.HelpName}} - {{.Usage}}
    75  
    76  USAGE:
    77    {{.HelpName}} [FLAGS] ALIAS FILE
    78  
    79  FLAGS:
    80    {{range .VisibleFlags}}{{.}}
    81    {{end}}
    82  EXAMPLES:
    83    1. Upload file './trace.log' for cluster 'myminio' to SUBNET issue number 10
    84       {{.Prompt}} {{.HelpName}} --issue 10 myminio ./trace.log
    85  
    86    2. Upload file './trace.log' for cluster 'myminio' to SUBNET issue number 10 with comment 'here is the trace log'
    87       {{.Prompt}} {{.HelpName}} --issue 10 --comment "here is the trace log" myminio ./trace.log 
    88  `,
    89  }
    90  
    91  func checkSupportUploadSyntax(ctx *cli.Context) {
    92  	if len(ctx.Args()) != 2 {
    93  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    94  	}
    95  
    96  	if ctx.Int("issue") <= 0 {
    97  		fatal(errDummy().Trace(), "Invalid issue number")
    98  	}
    99  }
   100  
   101  // mainSupportUpload is the handle for "mc support upload" command.
   102  func mainSupportUpload(ctx *cli.Context) error {
   103  	// Check for command syntax
   104  	checkSupportUploadSyntax(ctx)
   105  	setSuccessMessageColor()
   106  
   107  	// Get the alias parameter from cli
   108  	aliasedURL := ctx.Args().Get(0)
   109  	alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true)
   110  	if len(apiKey) == 0 {
   111  		// api key not passed as flag. Check that the cluster is registered.
   112  		apiKey = validateClusterRegistered(alias, true)
   113  	}
   114  
   115  	// Main execution
   116  	execSupportUpload(ctx, alias, apiKey)
   117  	return nil
   118  }
   119  
   120  func execSupportUpload(ctx *cli.Context, alias, apiKey string) {
   121  	filePath := ctx.Args().Get(1)
   122  	issueNum := ctx.Int("issue")
   123  	msg := ctx.String("comment")
   124  
   125  	params := url.Values{}
   126  	params.Add("issueNumber", fmt.Sprintf("%d", issueNum))
   127  	if len(msg) > 0 {
   128  		params.Add("message", msg)
   129  	}
   130  
   131  	uploadURL := SubnetUploadURL("attachment")
   132  	reqURL, headers := prepareSubnetUploadURL(uploadURL, alias, apiKey)
   133  
   134  	_, e := (&SubnetFileUploader{
   135  		alias:        alias,
   136  		FilePath:     filePath,
   137  		ReqURL:       reqURL,
   138  		Headers:      headers,
   139  		AutoCompress: true,
   140  		Params:       params,
   141  	}).UploadFileToSubnet()
   142  	if e != nil {
   143  		fatalIf(probe.NewError(e), "Unable to upload file to SUBNET")
   144  	}
   145  	printMsg(supportUploadMessage{IssueNum: issueNum, Status: "success", IssueURL: subnetIssueURL(issueNum)})
   146  }