github.com/craicoverflow/tyk@v2.9.6-rc3+incompatible/cli/cli.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 "github.com/TykTechnologies/tyk/cli/linter" 9 10 kingpin "gopkg.in/alecthomas/kingpin.v2" 11 12 "github.com/TykTechnologies/tyk/cli/bundler" 13 "github.com/TykTechnologies/tyk/cli/importer" 14 logger "github.com/TykTechnologies/tyk/log" 15 ) 16 17 const ( 18 appName = "tyk" 19 appDesc = "Tyk Gateway" 20 ) 21 22 var ( 23 // Conf specifies the configuration file path. 24 Conf *string 25 // Port specifies the listen port. 26 Port *string 27 // MemProfile enables memory profiling. 28 MemProfile *bool 29 // CPUProfile enables CPU profiling. 30 CPUProfile *bool 31 // BlockProfile enables block profiling. 32 BlockProfile *bool 33 // MutexProfile enables block profiling. 34 MutexProfile *bool 35 // HTTPProfile exposes a HTTP endpoint for accessing profiling data. 36 HTTPProfile *bool 37 // DebugMode sets the log level to debug mode. 38 DebugMode *bool 39 // LogInstrumentation outputs instrumentation data to stdout. 40 LogInstrumentation *bool 41 42 // DefaultMode is set when default command is used. 43 DefaultMode bool 44 45 app *kingpin.Application 46 47 log = logger.Get() 48 ) 49 50 // Init sets all flags and subcommands. 51 func Init(version string, confPaths []string) { 52 app = kingpin.New(appName, appDesc) 53 app.HelpFlag.Short('h') 54 app.Version(version) 55 56 // Start/default command: 57 startCmd := app.Command("start", "Starts the Tyk Gateway") 58 Conf = startCmd.Flag("conf", "load a named configuration file").PlaceHolder("FILE").String() 59 Port = startCmd.Flag("port", "listen on PORT (overrides config file)").String() 60 MemProfile = startCmd.Flag("memprofile", "generate a memory profile").Bool() 61 CPUProfile = startCmd.Flag("cpuprofile", "generate a cpu profile").Bool() 62 BlockProfile = startCmd.Flag("blockprofile", "generate a block profile").Bool() 63 MutexProfile = startCmd.Flag("mutexprofile", "generate a mutex profile").Bool() 64 HTTPProfile = startCmd.Flag("httpprofile", "expose runtime profiling data via HTTP").Bool() 65 DebugMode = startCmd.Flag("debug", "enable debug mode").Bool() 66 LogInstrumentation = startCmd.Flag("log-intrumentation", "output intrumentation output to stdout").Bool() 67 68 startCmd.Action(func(ctx *kingpin.ParseContext) error { 69 DefaultMode = true 70 return nil 71 }) 72 startCmd.Default() 73 74 // Linter: 75 lintCmd := app.Command("lint", "Runs a linter on Tyk configuration file") 76 lintCmd.Action(func(c *kingpin.ParseContext) error { 77 confSchema, err := ioutil.ReadFile("cli/linter/schema.json") 78 if err != nil { 79 return err 80 } 81 path, lines, err := linter.Run(string(confSchema), confPaths) 82 if err != nil { 83 fmt.Fprintln(os.Stderr, err) 84 os.Exit(1) 85 } 86 if len(lines) == 0 { 87 fmt.Printf("found no issues in %s\n", path) 88 os.Exit(0) 89 } 90 fmt.Printf("issues found in %s:\n", path) 91 for _, line := range lines { 92 fmt.Println(line) 93 } 94 os.Exit(1) 95 return nil 96 }) 97 98 // Add import command: 99 importer.AddTo(app) 100 101 // Add bundler commands: 102 bundler.AddTo(app) 103 } 104 105 // Parse parses the command-line arguments. 106 func Parse() { 107 kingpin.MustParse(app.Parse(os.Args[1:])) 108 }