github.com/Minish144/prototool-arm64@v1.3.0/internal/exec/exec.go (about)

     1  // Copyright (c) 2018 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  
    21  // Package exec brings all the functionality of Prototool together in a format
    22  // easily consumable by CLI libraries. It is effectively the glue between
    23  // internal/cmd and all other packages.
    24  package exec
    25  
    26  import (
    27  	"io"
    28  
    29  	"go.uber.org/zap"
    30  )
    31  
    32  // ExitError is an error that signals to exit with a certain code.
    33  type ExitError struct {
    34  	Code    int
    35  	Message string
    36  }
    37  
    38  // Error implements error.
    39  func (e *ExitError) Error() string {
    40  	return e.Message
    41  }
    42  
    43  // Runner runs commands.
    44  //
    45  // The args given are the args from the command line.
    46  // Each additional parameter generally refers to a command-specific flag.
    47  type Runner interface {
    48  	Init(args []string, uncomment bool) error
    49  	Create(args []string, pkg string) error
    50  	Version() error
    51  	Download() error
    52  	Clean() error
    53  	Files(args []string) error
    54  	Compile(args []string, dryRun bool) error
    55  	Gen(args []string, dryRun bool) error
    56  	DescriptorProto(args []string) error
    57  	FieldDescriptorProto(args []string) error
    58  	ServiceDescriptorProto(args []string) error
    59  	Lint(args []string, listAllLinters bool, listLinters bool) error
    60  	ListLintGroup(group string) error
    61  	ListAllLintGroups() error
    62  	Format(args []string, overwrite, diffMode, lintMode, fix bool) error
    63  	BinaryToJSON(args []string) error
    64  	JSONToBinary(args []string) error
    65  	All(args []string, disableFormat, disableLint, fix bool) error
    66  	GRPC(args, headers []string, address, method, data, callTimeout, connectTimeout, keepaliveTime string, stdin bool) error
    67  }
    68  
    69  // RunnerOption is an option for a new Runner.
    70  type RunnerOption func(*runner)
    71  
    72  // RunnerWithLogger returns a RunnerOption that uses the given logger.
    73  //
    74  // The default is to use zap.NewNop().
    75  func RunnerWithLogger(logger *zap.Logger) RunnerOption {
    76  	return func(runner *runner) {
    77  		runner.logger = logger
    78  	}
    79  }
    80  
    81  // RunnerWithCachePath returns a RunnerOption that uses the given cache path.
    82  func RunnerWithCachePath(cachePath string) RunnerOption {
    83  	return func(runner *runner) {
    84  		runner.cachePath = cachePath
    85  	}
    86  }
    87  
    88  // RunnerWithConfigData returns a RunnerOption that uses the given config path.
    89  func RunnerWithConfigData(configData string) RunnerOption {
    90  	return func(runner *runner) {
    91  		runner.configData = configData
    92  	}
    93  }
    94  
    95  // RunnerWithJSON returns a RunnerOption that will print failures as JSON.
    96  func RunnerWithJSON() RunnerOption {
    97  	return func(runner *runner) {
    98  		runner.json = true
    99  	}
   100  }
   101  
   102  // RunnerWithPrintFields returns a RunnerOption that uses the given colon-separated
   103  // print fields. The default is filename:line:column:message.
   104  func RunnerWithPrintFields(printFields string) RunnerOption {
   105  	return func(runner *runner) {
   106  		runner.printFields = printFields
   107  	}
   108  }
   109  
   110  // RunnerWithProtocBinPath returns a RunnerOption that uses the given protoc binary path.
   111  func RunnerWithProtocBinPath(protocBinPath string) RunnerOption {
   112  	return func(runner *runner) {
   113  		runner.protocBinPath = protocBinPath
   114  	}
   115  }
   116  
   117  // RunnerWithProtocWKTPath returns a RunnerOption that uses the given path to include the well-known types.
   118  func RunnerWithProtocWKTPath(protocWKTPath string) RunnerOption {
   119  	return func(runner *runner) {
   120  		runner.protocWKTPath = protocWKTPath
   121  	}
   122  }
   123  
   124  // RunnerWithProtocURL returns a RunnerOption that uses the given protoc zip file URL.
   125  func RunnerWithProtocURL(protocURL string) RunnerOption {
   126  	return func(runner *runner) {
   127  		runner.protocURL = protocURL
   128  	}
   129  }
   130  
   131  // NewRunner returns a new Runner.
   132  //
   133  // workDirPath should generally be the current directory.
   134  // input and output generally refer to stdin and stdout.
   135  func NewRunner(workDirPath string, input io.Reader, output io.Writer, options ...RunnerOption) Runner {
   136  	return newRunner(workDirPath, input, output, options...)
   137  }