gazelle.go 2.9 KB

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