github.com/wolfd/bazel-gazelle@v0.14.0/cmd/gazelle/gazelle.go (about)

     1  /* Copyright 2016 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  // Command gazelle is a BUILD file generator for Go projects.
    17  // See "gazelle --help" for more details.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"os"
    25  )
    26  
    27  type command int
    28  
    29  const (
    30  	updateCmd command = iota
    31  	fixCmd
    32  	updateReposCmd
    33  	helpCmd
    34  )
    35  
    36  var commandFromName = map[string]command{
    37  	"fix":          fixCmd,
    38  	"help":         helpCmd,
    39  	"update":       updateCmd,
    40  	"update-repos": updateReposCmd,
    41  }
    42  
    43  var nameFromCommand = []string{
    44  	// keep in sync with definition above
    45  	"update",
    46  	"fix",
    47  	"update-repos",
    48  	"help",
    49  }
    50  
    51  func (cmd command) String() string {
    52  	return nameFromCommand[cmd]
    53  }
    54  
    55  func main() {
    56  	log.SetPrefix("gazelle: ")
    57  	log.SetFlags(0) // don't print timestamps
    58  
    59  	if err := run(os.Args[1:]); err != nil {
    60  		log.Fatal(err)
    61  	}
    62  }
    63  
    64  func run(args []string) error {
    65  	cmd := updateCmd
    66  	if len(args) == 1 && (args[0] == "-h" || args[0] == "-help" || args[0] == "--help") {
    67  		cmd = helpCmd
    68  	} else if len(args) > 0 {
    69  		c, ok := commandFromName[args[0]]
    70  		if ok {
    71  			cmd = c
    72  			args = args[1:]
    73  		}
    74  	}
    75  
    76  	switch cmd {
    77  	case fixCmd, updateCmd:
    78  		return runFixUpdate(cmd, args)
    79  	case helpCmd:
    80  		return help()
    81  	case updateReposCmd:
    82  		return updateRepos(args)
    83  	default:
    84  		log.Panicf("unknown command: %v", cmd)
    85  	}
    86  	return nil
    87  }
    88  
    89  func help() error {
    90  	fmt.Fprint(os.Stderr, `usage: gazelle <command> [args...]
    91  
    92  Gazelle is a BUILD file generator for Go projects. It can create new BUILD files
    93  for a project that follows "go build" conventions, and it can update BUILD files
    94  if they already exist. It can be invoked directly in a project workspace, or
    95  it can be run on an external dependency during the build as part of the
    96  go_repository rule.
    97  
    98  Gazelle may be run with one of the commands below. If no command is given,
    99  Gazelle defaults to "update".
   100  
   101    update - Gazelle will create new BUILD files or update existing BUILD files
   102        if needed.
   103    fix - in addition to the changes made in update, Gazelle will make potentially
   104        breaking changes. For example, it may delete obsolete rules or rename
   105        existing rules.
   106    update-repos - updates repository rules in the WORKSPACE file. Run with
   107        -h for details.
   108    help - show this message.
   109  
   110  For usage information for a specific command, run the command with the -h flag.
   111  For example:
   112  
   113    gazelle update -h
   114  
   115  Gazelle is under active delevopment, and its interface may change
   116  without notice.
   117  
   118  `)
   119  	return flag.ErrHelp
   120  }