storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/main.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2015-2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"sort"
    23  
    24  	"github.com/minio/cli"
    25  
    26  	"storj.io/minio/pkg/console"
    27  	"storj.io/minio/pkg/trie"
    28  	"storj.io/minio/pkg/words"
    29  )
    30  
    31  // GlobalFlags - global flags for minio.
    32  var GlobalFlags = []cli.Flag{
    33  	// Deprecated flag, so its hidden now - existing deployments will keep working.
    34  	cli.StringFlag{
    35  		Name:   "config-dir, C",
    36  		Value:  defaultConfigDir.Get(),
    37  		Usage:  "[DEPRECATED] path to legacy configuration directory",
    38  		Hidden: true,
    39  	},
    40  	cli.StringFlag{
    41  		Name:  "certs-dir, S",
    42  		Value: defaultCertsDir.Get(),
    43  		Usage: "path to certs directory",
    44  	},
    45  	cli.BoolFlag{
    46  		Name:  "quiet",
    47  		Usage: "disable startup information",
    48  	},
    49  	cli.BoolFlag{
    50  		Name:  "anonymous",
    51  		Usage: "hide sensitive information from logging",
    52  	},
    53  	cli.BoolFlag{
    54  		Name:  "json",
    55  		Usage: "output server logs and startup information in json format",
    56  	},
    57  	// Deprecated flag, so its hidden now, existing deployments will keep working.
    58  	cli.BoolFlag{
    59  		Name:   "compat",
    60  		Usage:  "enable strict S3 compatibility by turning off certain performance optimizations",
    61  		Hidden: true,
    62  	},
    63  	// This flag is hidden and to be used only during certain performance testing.
    64  	cli.BoolFlag{
    65  		Name:   "no-compat",
    66  		Usage:  "disable strict S3 compatibility by turning on certain performance optimizations",
    67  		Hidden: true,
    68  	},
    69  }
    70  
    71  // Help template for minio.
    72  var minioHelpTemplate = `NAME:
    73    {{.Name}} - {{.Usage}}
    74  
    75  DESCRIPTION:
    76    {{.Description}}
    77  
    78  USAGE:
    79    {{.HelpName}} {{if .VisibleFlags}}[FLAGS] {{end}}COMMAND{{if .VisibleFlags}}{{end}} [ARGS...]
    80  
    81  COMMANDS:
    82    {{range .VisibleCommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
    83    {{end}}{{if .VisibleFlags}}
    84  FLAGS:
    85    {{range .VisibleFlags}}{{.}}
    86    {{end}}{{end}}
    87  VERSION:
    88    {{.Version}}
    89  `
    90  
    91  func newApp(name string) *cli.App {
    92  	// Collection of minio commands currently supported are.
    93  	commands := []cli.Command{}
    94  
    95  	// Collection of minio commands currently supported in a trie tree.
    96  	commandsTree := trie.NewTrie()
    97  
    98  	// registerCommand registers a cli command.
    99  	registerCommand := func(command cli.Command) {
   100  		commands = append(commands, command)
   101  		commandsTree.Insert(command.Name)
   102  	}
   103  
   104  	findClosestCommands := func(command string) []string {
   105  		var closestCommands []string
   106  		closestCommands = append(closestCommands, commandsTree.PrefixMatch(command)...)
   107  
   108  		sort.Strings(closestCommands)
   109  		// Suggest other close commands - allow missed, wrongly added and
   110  		// even transposed characters
   111  		for _, value := range commandsTree.Walk(commandsTree.Root()) {
   112  			if sort.SearchStrings(closestCommands, value) < len(closestCommands) {
   113  				continue
   114  			}
   115  			// 2 is arbitrary and represents the max
   116  			// allowed number of typed errors
   117  			if words.DamerauLevenshteinDistance(command, value) < 2 {
   118  				closestCommands = append(closestCommands, value)
   119  			}
   120  		}
   121  
   122  		return closestCommands
   123  	}
   124  
   125  	// Register all commands.
   126  	registerCommand(serverCmd)
   127  	registerCommand(gatewayCmd)
   128  
   129  	// Set up app.
   130  	cli.HelpFlag = cli.BoolFlag{
   131  		Name:  "help, h",
   132  		Usage: "show help",
   133  	}
   134  
   135  	app := cli.NewApp()
   136  	app.Name = name
   137  	app.Author = "MinIO, Inc."
   138  	app.Version = ReleaseTag
   139  	app.Usage = "High Performance Object Storage"
   140  	app.Description = `Build high performance data infrastructure for machine learning, analytics and application data workloads with MinIO`
   141  	app.Flags = GlobalFlags
   142  	app.HideHelpCommand = true // Hide `help, h` command, we already have `minio --help`.
   143  	app.Commands = commands
   144  	app.CustomAppHelpTemplate = minioHelpTemplate
   145  	app.CommandNotFound = func(ctx *cli.Context, command string) {
   146  		console.Printf("‘%s’ is not a minio sub-command. See ‘minio --help’.\n", command)
   147  		closestCommands := findClosestCommands(command)
   148  		if len(closestCommands) > 0 {
   149  			console.Println()
   150  			console.Println("Did you mean one of these?")
   151  			for _, cmd := range closestCommands {
   152  				console.Printf("\t‘%s’\n", cmd)
   153  			}
   154  		}
   155  
   156  		os.Exit(1)
   157  	}
   158  
   159  	return app
   160  }
   161  
   162  // Main main for minio server.
   163  func Main(args []string) {
   164  	// Set the minio app name.
   165  	appName := filepath.Base(args[0])
   166  
   167  	// Run the app - exit on error.
   168  	if err := newApp(appName).Run(args); err != nil {
   169  		os.Exit(1)
   170  	}
   171  }