/vendor/github.com/uber/prototool/internal/exec/exec.go

https://github.com/merklecounty/rget · Go · 143 lines · 81 code · 17 blank · 45 comment · 0 complexity · 72935d5bd20fbf57c597be80c5377ea9 MD5 · raw file

  1. // Copyright (c) 2019 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // Package exec brings all the functionality of Prototool together in a format
  21. // easily consumable by CLI libraries. It is effectively the glue between
  22. // internal/cmd and all other packages.
  23. package exec
  24. import (
  25. "io"
  26. "go.uber.org/zap"
  27. )
  28. // ExitError is an error that signals to exit with a certain code.
  29. type ExitError struct {
  30. Code int
  31. Message string
  32. }
  33. // Error implements error.
  34. func (e *ExitError) Error() string {
  35. return e.Message
  36. }
  37. // Runner runs commands.
  38. //
  39. // The args given are the args from the command line.
  40. // Each additional parameter generally refers to a command-specific flag.
  41. type Runner interface {
  42. Init(args []string, uncomment bool, document bool) error
  43. Create(args []string, pkg string) error
  44. Version() error
  45. CacheUpdate(args []string) error
  46. CacheDelete() error
  47. Files(args []string) error
  48. Compile(args []string, dryRun bool) error
  49. Gen(args []string, dryRun bool) error
  50. Lint(args []string, listAllLinters bool, listLinters bool, listAllLintGroups bool, listLintGroup string, diffLintGroups string, generateIgnores bool) error
  51. Format(args []string, overwrite, diffMode, lintMode, fix bool) error
  52. All(args []string, disableFormat, disableLint, fix bool) error
  53. GRPC(args, headers []string, address, method, data, callTimeout, connectTimeout, keepaliveTime string, stdin bool, details bool, tls bool, insecure bool, cacert string, cert string, key string, serverName string) error
  54. InspectPackages(args []string) error
  55. InspectPackageDeps(args []string, name string) error
  56. InspectPackageImporters(args []string, name string) error
  57. BreakCheck(args []string, gitBranch string, descriptorSetPath string) error
  58. BreakDescriptorSet(args []string, outputPath string) error
  59. DescriptorSet(args []string, includeImports bool, includeSourceInfo bool, outputPath string, tmp bool) error
  60. }
  61. // RunnerOption is an option for a new Runner.
  62. type RunnerOption func(*runner)
  63. // RunnerWithLogger returns a RunnerOption that uses the given logger.
  64. //
  65. // The default is to use zap.NewNop().
  66. func RunnerWithLogger(logger *zap.Logger) RunnerOption {
  67. return func(runner *runner) {
  68. runner.logger = logger
  69. }
  70. }
  71. // RunnerWithDevelMode returns a RunnerOption that allows devel-mode.
  72. func RunnerWithDevelMode() RunnerOption {
  73. return func(runner *runner) {
  74. runner.develMode = true
  75. }
  76. }
  77. // RunnerWithCachePath returns a RunnerOption that uses the given cache path.
  78. func RunnerWithCachePath(cachePath string) RunnerOption {
  79. return func(runner *runner) {
  80. runner.cachePath = cachePath
  81. }
  82. }
  83. // RunnerWithConfigData returns a RunnerOption that uses the given config path.
  84. func RunnerWithConfigData(configData string) RunnerOption {
  85. return func(runner *runner) {
  86. runner.configData = configData
  87. }
  88. }
  89. // RunnerWithJSON returns a RunnerOption that will print failures as JSON.
  90. func RunnerWithJSON() RunnerOption {
  91. return func(runner *runner) {
  92. runner.json = true
  93. }
  94. }
  95. // RunnerWithErrorFormat returns a RunnerOption that uses the given colon-separated
  96. // error format. The default is filename:line:column:message.
  97. func RunnerWithErrorFormat(errorFormat string) RunnerOption {
  98. return func(runner *runner) {
  99. runner.errorFormat = errorFormat
  100. }
  101. }
  102. // RunnerWithProtocBinPath returns a RunnerOption that uses the given protoc binary path.
  103. func RunnerWithProtocBinPath(protocBinPath string) RunnerOption {
  104. return func(runner *runner) {
  105. runner.protocBinPath = protocBinPath
  106. }
  107. }
  108. // RunnerWithProtocWKTPath returns a RunnerOption that uses the given path to include the well-known types.
  109. func RunnerWithProtocWKTPath(protocWKTPath string) RunnerOption {
  110. return func(runner *runner) {
  111. runner.protocWKTPath = protocWKTPath
  112. }
  113. }
  114. // RunnerWithProtocURL returns a RunnerOption that uses the given protoc zip file URL.
  115. func RunnerWithProtocURL(protocURL string) RunnerOption {
  116. return func(runner *runner) {
  117. runner.protocURL = protocURL
  118. }
  119. }
  120. // NewRunner returns a new Runner.
  121. //
  122. // workDirPath should generally be the current directory.
  123. // input and output generally refer to stdin and stdout.
  124. func NewRunner(workDirPath string, input io.Reader, output io.Writer, options ...RunnerOption) Runner {
  125. return newRunner(workDirPath, input, output, options...)
  126. }