github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/cmd/dosa/main.go (about)

     1  // Copyright (c) 2017 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 main
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  
    27  	flags "github.com/jessevdk/go-flags"
    28  	_ "github.com/uber-go/dosa/connectors/yarpc"
    29  )
    30  
    31  // for testing, we make exit an overridable routine
    32  type exiter func(int)
    33  
    34  var exit = os.Exit
    35  
    36  // these are overridden at build-time w/ the -ldflags -X option
    37  var (
    38  	version   = "0.0.0"
    39  	githash   = "master"
    40  	timestamp = "now"
    41  )
    42  
    43  // BuildInfo reports information about the binary build environment
    44  type BuildInfo struct {
    45  	Version   string
    46  	Githash   string
    47  	Timestamp string
    48  }
    49  
    50  // String satisfies Stringer interface
    51  func (b BuildInfo) String() string {
    52  	return fmt.Sprintf("Version:\t%s\nGit Commit:\t%s\nUTC Build Time:\t%s", version, githash, timestamp)
    53  }
    54  
    55  // Execute is ran for the version subcommand
    56  func (b BuildInfo) Execute(args []string) error {
    57  	fmt.Printf("%s\n", b)
    58  	return nil
    59  }
    60  
    61  // GlobalOptions are options for all subcommands
    62  type GlobalOptions struct {
    63  	Host        string     `long:"host" default:"127.0.0.1" description:"The hostname or IP for the gateway."`
    64  	Port        string     `short:"p" long:"port" default:"21300" description:"The hostname or IP for the gateway."`
    65  	Transport   string     `long:"transport" default:"tchannel" description:"TCP Transport to use. Options: http, tchannel."`
    66  	ServiceName string     `long:"service" description:"The TChannel service name for the gateway."`
    67  	CallerName  callerFlag `long:"caller" default:"dosacli-$USER" description:"Caller will override the default caller name (which is dosacli-$USER)."`
    68  	Timeout     timeFlag   `long:"timeout" default:"60s" description:"The timeout for gateway requests. E.g., 100ms, 0.5s, 1s. If no unit is specified, milliseconds are assumed."`
    69  	Connector   string     `hidden:"true" long:"connector" default:"yarpc" description:"Name of connector to use"`
    70  	Version     bool       `long:"version" description:"Display version info"`
    71  }
    72  
    73  var (
    74  	options   GlobalOptions
    75  	buildInfo BuildInfo
    76  )
    77  
    78  // OptionsParser holds the global parser
    79  
    80  func main() {
    81  	buildInfo := &BuildInfo{}
    82  	OptionsParser := flags.NewParser(&options, flags.PassAfterNonOption|flags.HelpFlag)
    83  	OptionsParser.ShortDescription = "DOSA CLI - The command-line tool for your DOSA client"
    84  	OptionsParser.LongDescription = `
    85  dosa manages your schema both in production and development scopes`
    86  	c, _ := OptionsParser.AddCommand("version", "display build info", "display build info", &BuildInfo{})
    87  
    88  	c, _ = OptionsParser.AddCommand("scope", "commands to manage scope", "create, drop, or truncate development scopes", &ScopeOptions{})
    89  	_, _ = c.AddCommand("create", "Create scope", "creates a new scope", &ScopeCreate{})
    90  	_, _ = c.AddCommand("drop", "Drop scope", "drops a scope", &ScopeDrop{})
    91  	_, _ = c.AddCommand("truncate", "Truncate scope", "truncates a scope", &ScopeTruncate{})
    92  
    93  	c, _ = OptionsParser.AddCommand("schema", "commands to manage schemas", "check or update schemas", &SchemaOptions{})
    94  	_, _ = c.AddCommand("check", "Check schema", "check the schema", &SchemaCheck{})
    95  	_, _ = c.AddCommand("upsert", "Upsert schema", "insert or update the schema", &SchemaUpsert{})
    96  	_, _ = c.AddCommand("dump", "Dump schema", "display the schema in a given format", &SchemaDump{})
    97  	_, _ = c.AddCommand("status", "Check schema status", "Check application status of schema", &SchemaStatus{})
    98  
    99  	_, err := OptionsParser.Parse()
   100  
   101  	if options.Version {
   102  		fmt.Fprintf(os.Stdout, "%s\n", buildInfo.String())
   103  		options.Version = false // for tests, we leak state between runs
   104  		exit(0)
   105  	}
   106  
   107  	if err != nil {
   108  		fmt.Fprintf(os.Stderr, "Error: %s\n", err)
   109  		exit(1)
   110  		return
   111  	}
   112  
   113  	exit(0)
   114  }