github.com/psrajat/prototool@v1.3.0/internal/cmd/flags.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 cmd
    22  
    23  import (
    24  	"github.com/spf13/pflag"
    25  )
    26  
    27  type flags struct {
    28  	address        string
    29  	cachePath      string
    30  	callTimeout    string
    31  	configData     string
    32  	connectTimeout string
    33  	data           string
    34  	debug          bool
    35  	diffMode       bool
    36  	disableFormat  bool
    37  	disableLint    bool
    38  	dryRun         bool
    39  	fix            bool
    40  	headers        []string
    41  	keepaliveTime  string
    42  	json           bool
    43  	listAllLinters bool
    44  	listLinters    bool
    45  	lintMode       bool
    46  	method         string
    47  	overwrite      bool
    48  	pkg            string
    49  	printFields    string
    50  	protocBinPath  string
    51  	protocWKTPath  string
    52  	protocURL      string
    53  	stdin          bool
    54  	uncomment      bool
    55  }
    56  
    57  func (f *flags) bindAddress(flagSet *pflag.FlagSet) {
    58  	flagSet.StringVar(&f.address, "address", "", "The GRPC endpoint to connect to. This is required.")
    59  }
    60  
    61  func (f *flags) bindCachePath(flagSet *pflag.FlagSet) {
    62  	flagSet.StringVar(&f.cachePath, "cache-path", "", "The path to use for the cache, otherwise uses the default behavior.")
    63  }
    64  
    65  func (f *flags) bindCallTimeout(flagSet *pflag.FlagSet) {
    66  	flagSet.StringVar(&f.callTimeout, "call-timeout", "60s", "The maximum time to for all calls to be completed.")
    67  }
    68  
    69  func (f *flags) bindConfigData(flagSet *pflag.FlagSet) {
    70  	flagSet.StringVar(&f.configData, "config-data", "", "The configuration data to use instead of reading prototool.yaml or prototool.json files.\nThis will act as if there is a configuration file with the given data in the current directory, and no other configuration files recursively.\nThis is an advanced feature and is not recommended to be generally used.")
    71  }
    72  
    73  func (f *flags) bindConnectTimeout(flagSet *pflag.FlagSet) {
    74  	flagSet.StringVar(&f.connectTimeout, "connect-timeout", "10s", "The maximum time to wait for the connection to be established.")
    75  }
    76  
    77  func (f *flags) bindData(flagSet *pflag.FlagSet) {
    78  	flagSet.StringVar(&f.data, "data", "", "The GRPC request data in JSON format. Either this or --stdin is required.")
    79  }
    80  
    81  func (f *flags) bindDebug(flagSet *pflag.FlagSet) {
    82  	flagSet.BoolVar(&f.debug, "debug", false, "Run in debug mode, which will print out debug logging.")
    83  }
    84  
    85  func (f *flags) bindDiffMode(flagSet *pflag.FlagSet) {
    86  	flagSet.BoolVarP(&f.diffMode, "diff", "d", false, "Write a diff instead of writing the formatted file to stdout.")
    87  }
    88  
    89  func (f *flags) bindDisableFormat(flagSet *pflag.FlagSet) {
    90  	flagSet.BoolVar(&f.disableFormat, "disable-format", false, "Do not run formatting.")
    91  }
    92  
    93  func (f *flags) bindDisableLint(flagSet *pflag.FlagSet) {
    94  	flagSet.BoolVar(&f.disableLint, "disable-lint", false, "Do not run linting.")
    95  }
    96  
    97  func (f *flags) bindDryRun(flagSet *pflag.FlagSet) {
    98  	flagSet.BoolVar(&f.dryRun, "dry-run", false, "Print the protoc commands that would have been run without actually running them.")
    99  }
   100  
   101  func (f *flags) bindHeaders(flagSet *pflag.FlagSet) {
   102  	flagSet.StringSliceVarP(&f.headers, "header", "H", []string{}, "Additional request headers in 'name:value' format.")
   103  }
   104  
   105  func (f *flags) bindKeepaliveTime(flagSet *pflag.FlagSet) {
   106  	flagSet.StringVar(&f.keepaliveTime, "keepalive-time", "", "The maximum idle time after which a keepalive probe is sent.")
   107  }
   108  
   109  func (f *flags) bindJSON(flagSet *pflag.FlagSet) {
   110  	flagSet.BoolVar(&f.json, "json", false, "Output as JSON.")
   111  }
   112  
   113  func (f *flags) bindLintMode(flagSet *pflag.FlagSet) {
   114  	flagSet.BoolVarP(&f.lintMode, "lint", "l", false, "Write a lint error saying that the file is not formatted instead of writing the formatted file to stdout.")
   115  }
   116  
   117  func (f *flags) bindListAllLinters(flagSet *pflag.FlagSet) {
   118  	flagSet.BoolVar(&f.listAllLinters, "list-all-linters", false, "List all available linters instead of running lint.")
   119  }
   120  
   121  func (f *flags) bindListLinters(flagSet *pflag.FlagSet) {
   122  	flagSet.BoolVar(&f.listLinters, "list-linters", false, "List the configured linters instead of running lint.")
   123  }
   124  
   125  func (f *flags) bindMethod(flagSet *pflag.FlagSet) {
   126  	flagSet.StringVar(&f.method, "method", "", "The GRPC method to call in the form package.Service/Method. This is required.")
   127  }
   128  
   129  func (f *flags) bindOverwrite(flagSet *pflag.FlagSet) {
   130  	flagSet.BoolVarP(&f.overwrite, "overwrite", "w", false, "Overwrite the existing file instead of writing the formatted file to stdout.")
   131  }
   132  
   133  func (f *flags) bindPackage(flagSet *pflag.FlagSet) {
   134  	flagSet.StringVar(&f.pkg, "package", "", "The Protobuf package to use in the created file.")
   135  }
   136  
   137  func (f *flags) bindPrintFields(flagSet *pflag.FlagSet) {
   138  	flagSet.StringVar(&f.printFields, "print-fields", "filename:line:column:message", "The colon-separated fields to print out on error.")
   139  }
   140  
   141  func (f *flags) bindProtocURL(flagSet *pflag.FlagSet) {
   142  	flagSet.StringVar(&f.protocURL, "protoc-url", "", "The url to use to download the protoc zip file, otherwise uses GitHub Releases. Setting this option will ignore the config protoc.version setting.")
   143  }
   144  
   145  func (f *flags) bindProtocBinPath(flagSet *pflag.FlagSet) {
   146  	flagSet.StringVar(&f.protocBinPath, "protoc-bin-path", "", "The path to the protoc binary. Setting this option will ignore the config protoc.version setting. This flag must be used with protoc-wkt-path and must not be used with the protoc-url flag.")
   147  }
   148  
   149  func (f *flags) bindProtocWKTPath(flagSet *pflag.FlagSet) {
   150  	flagSet.StringVar(&f.protocWKTPath, "protoc-wkt-path", "", "The path to the well-known types. Setting this option will ignore the config protoc.version setting. This flag must be used with protoc-bin-path and must not be used with the protoc-url flag.")
   151  }
   152  
   153  func (f *flags) bindStdin(flagSet *pflag.FlagSet) {
   154  	flagSet.BoolVar(&f.stdin, "stdin", false, "Read the GRPC request data from stdin in JSON format. Either this or --data is required.")
   155  }
   156  
   157  func (f *flags) bindUncomment(flagSet *pflag.FlagSet) {
   158  	flagSet.BoolVar(&f.uncomment, "uncomment", false, "Uncomment the example config settings.")
   159  }
   160  
   161  func (f *flags) bindFix(flagSet *pflag.FlagSet) {
   162  	flagSet.BoolVarP(&f.fix, "fix", "f", false, "Fix the file according to the Style Guide.")
   163  }