github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/globals.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 contains all the global variables and constants. ONLY TO BE ACCESSED VIA GET/SET FUNCTIONS.
    19  package cmd
    20  
    21  import (
    22  	"context"
    23  	"crypto/x509"
    24  	"net/url"
    25  	"os"
    26  	"strconv"
    27  	"time"
    28  
    29  	"github.com/charmbracelet/lipgloss"
    30  	"github.com/dustin/go-humanize"
    31  	"github.com/minio/cli"
    32  	"github.com/minio/madmin-go/v3"
    33  	"github.com/minio/pkg/v2/console"
    34  	"github.com/muesli/termenv"
    35  )
    36  
    37  const (
    38  	globalMCConfigVersion = "10"
    39  
    40  	globalMCConfigFile = "config.json"
    41  	globalMCCertsDir   = "certs"
    42  	globalMCCAsDir     = "CAs"
    43  
    44  	// session config and shared urls related constants
    45  	globalSessionDir           = "session"
    46  	globalSharedURLsDataDir    = "share"
    47  	globalSessionConfigVersion = "8"
    48  
    49  	// Profile directory for dumping profiler outputs.
    50  	globalProfileDir = "profile"
    51  
    52  	// Global error exit status.
    53  	globalErrorExitStatus = 1
    54  
    55  	// Global CTRL-C (SIGINT, #2) exit status.
    56  	globalCancelExitStatus = 130
    57  
    58  	// Global SIGKILL (#9) exit status.
    59  	globalKillExitStatus = 137
    60  
    61  	// Global SIGTERM (#15) exit status
    62  	globalTerminatExitStatus = 143
    63  )
    64  
    65  var (
    66  	globalQuiet        = false               // Quiet flag set via command line
    67  	globalJSON         = false               // Json flag set via command line
    68  	globalJSONLine     = false               // Print json as single line.
    69  	globalDebug        = false               // Debug flag set via command line
    70  	globalNoColor      = false               // No Color flag set via command line
    71  	globalInsecure     = false               // Insecure flag set via command line
    72  	globalAirgapped    = false               // Airgapped flag set via command line
    73  	globalSubnetConfig []madmin.SubsysConfig // Subnet config
    74  
    75  	// GlobalDevMode is set to true if the program is running in development mode
    76  	GlobalDevMode = false
    77  
    78  	// GlobalSubnetProxyURL is the proxy to be used for communication with subnet
    79  	GlobalSubnetProxyURL *url.URL
    80  
    81  	globalConnReadDeadline  time.Duration
    82  	globalConnWriteDeadline time.Duration
    83  
    84  	globalLimitUpload   uint64
    85  	globalLimitDownload uint64
    86  
    87  	globalContext, globalCancel = context.WithCancel(context.Background())
    88  )
    89  
    90  var (
    91  	// Terminal height/width, zero if not found
    92  	globalTermWidth, globalTermHeight int
    93  
    94  	globalDisablePagerEnv       = "DISABLE_PAGER"
    95  	globalDisablePagerFlag      = "--disable-pager"
    96  	globalDisablePagerFlagShort = "--dp"
    97  	globalPagerDisabled         = false
    98  	globalHelpPager             *termPager
    99  
   100  	// CA root certificates, a nil value means system certs pool will be used
   101  	globalRootCAs *x509.CertPool
   102  )
   103  
   104  func parsePagerDisableFlag(args []string) {
   105  	globalPagerDisabled, _ = strconv.ParseBool(os.Getenv(envPrefix + globalDisablePagerEnv))
   106  	for _, arg := range args {
   107  		if arg == globalDisablePagerFlag || arg == globalDisablePagerFlagShort {
   108  			globalPagerDisabled = true
   109  		}
   110  	}
   111  }
   112  
   113  // Set global states. NOTE: It is deliberately kept monolithic to ensure we dont miss out any flags.
   114  func setGlobalsFromContext(ctx *cli.Context) error {
   115  	quiet := ctx.IsSet("quiet") || ctx.GlobalIsSet("quiet")
   116  	debug := ctx.IsSet("debug") || ctx.GlobalIsSet("debug")
   117  	json := ctx.IsSet("json") || ctx.GlobalIsSet("json")
   118  	noColor := ctx.IsSet("no-color") || ctx.GlobalIsSet("no-color")
   119  	insecure := ctx.IsSet("insecure") || ctx.GlobalIsSet("insecure")
   120  	devMode := ctx.IsSet("dev") || ctx.GlobalIsSet("dev")
   121  	airgapped := ctx.IsSet("airgap") || ctx.GlobalIsSet("airgap")
   122  
   123  	globalQuiet = globalQuiet || quiet
   124  	globalDebug = globalDebug || debug
   125  	globalJSONLine = !isTerminal() && json
   126  	globalJSON = globalJSON || json
   127  	globalNoColor = globalNoColor || noColor || globalJSONLine
   128  	globalInsecure = globalInsecure || insecure
   129  	GlobalDevMode = GlobalDevMode || devMode
   130  	globalAirgapped = globalAirgapped || airgapped
   131  
   132  	// Disable colorified messages if requested.
   133  	if globalNoColor || globalQuiet {
   134  		console.SetColorOff()
   135  		lipgloss.SetColorProfile(termenv.Ascii)
   136  	}
   137  
   138  	globalConnReadDeadline = ctx.Duration("conn-read-deadline")
   139  	if globalConnReadDeadline <= 0 {
   140  		globalConnReadDeadline = ctx.GlobalDuration("conn-read-deadline")
   141  	}
   142  
   143  	globalConnWriteDeadline = ctx.Duration("conn-write-deadline")
   144  	if globalConnWriteDeadline <= 0 {
   145  		globalConnWriteDeadline = ctx.GlobalDuration("conn-write-deadline")
   146  	}
   147  
   148  	limitUploadStr := ctx.String("limit-upload")
   149  	if limitUploadStr == "" {
   150  		limitUploadStr = ctx.GlobalString("limit-upload")
   151  	}
   152  	if limitUploadStr != "" {
   153  		var e error
   154  		globalLimitUpload, e = humanize.ParseBytes(limitUploadStr)
   155  		if e != nil {
   156  			return e
   157  		}
   158  	}
   159  
   160  	limitDownloadStr := ctx.String("limit-download")
   161  	if limitDownloadStr == "" {
   162  		limitDownloadStr = ctx.GlobalString("limit-download")
   163  	}
   164  
   165  	if limitDownloadStr != "" {
   166  		var e error
   167  		globalLimitDownload, e = humanize.ParseBytes(limitDownloadStr)
   168  		if e != nil {
   169  			return e
   170  		}
   171  	}
   172  
   173  	return nil
   174  }