Skip to content

Commit

Permalink
Changed cli package to Kingpin
Browse files Browse the repository at this point in the history
docopt is no longer supported so changed to an active package
  • Loading branch information
joshblakeley committed Nov 6, 2017
1 parent bdae5a4 commit 0cfdcbc
Show file tree
Hide file tree
Showing 50 changed files with 9,801 additions and 3,376 deletions.
129 changes: 60 additions & 69 deletions command_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,105 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/apidef/importer"
)

var asMockStr = strconv.FormatBool(*asMock)
var createAPIStr = strconv.FormatBool(*createAPI)

var commandModeOptions = []string{
"--import-blueprint",
"--import-swagger",
"--create-api",
"--org-id",
"--upstream-target",
"--as-mock",
"--for-api",
"--as-version",
*importBlueprint,
*importSwagger,
createAPIStr,
*orgID,
*upstreamTarget,
asMockStr,
*forAPI,
*asVersion,
}

// ./tyk --import-blueprint=blueprint.json --create-api --org-id=<id> --upstream-target="http://widgets.com/api/"`
func handleCommandModeArgs(arguments map[string]interface{}) {
if arguments["--import-blueprint"] != nil {
if err := handleBluePrintMode(arguments); err != nil {
func handleCommandModeArgs() {
if *importBlueprint != "" {
if err := handleBluePrintMode(); err != nil {
log.Error(err)
}
}

if arguments["--import-swagger"] != nil {
if err := handleSwaggerMode(arguments); err != nil {
if *importSwagger != "" {
if err := handleSwaggerMode(); err != nil {
log.Error(err)
}
}
}

func handleBluePrintMode(arguments map[string]interface{}) error {
doCreate := arguments["--create-api"].(bool)
inputFile := arguments["--import-blueprint"]
if !doCreate {
func handleBluePrintMode() error {
if !*createAPI {
// Different branch, here we need an API Definition to modify
forAPIPath := arguments["--for-api"]
if forAPIPath == nil {
return fmt.Errorf("If ading to an API, the path to the defintiton must be listed")
if *forAPI == "" {
return fmt.Errorf("If adding to an API, the path to the definition must be listed")
}

versionName := arguments["--as-version"]
if versionName == nil {
if *asVersion == "" {
return fmt.Errorf("No version defined for this import operation, please set an import ID using the --as-version flag")
}

defFromFile, err := apiDefLoadFile(forAPIPath.(string))
defFromFile, err := apiDefLoadFile(*forAPI)
if err != nil {
return fmt.Errorf("failed to load and decode file data for API Definition: %v", err)
}

bp, err := bluePrintLoadFile(inputFile.(string))
bp, err := bluePrintLoadFile(*importBlueprint)
if err != nil {
return fmt.Errorf("File load error: %v", err)
}

versionData, err := bp.ConvertIntoApiVersion(arguments["--as-mock"].(bool))
versionData, err := bp.ConvertIntoApiVersion(*asMock)
if err != nil {
return fmt.Errorf("onversion into API Def failed: %v", err)
}

if err := bp.InsertIntoAPIDefinitionAsVersion(versionData, defFromFile, versionName.(string)); err != nil {
if err := bp.InsertIntoAPIDefinitionAsVersion(versionData, defFromFile, *asVersion); err != nil {
return fmt.Errorf("Insertion failed: %v", err)
}

printDef(defFromFile)

}

upstreamVal := arguments["--upstream-target"]
orgID := arguments["--org-id"]

if upstreamVal == nil && orgID == nil {
if *upstreamTarget == "" && *orgID == "" {
return fmt.Errorf("No upstream target or org ID defined, these are both required")
}

// Create the API with the blueprint
bp, err := bluePrintLoadFile(inputFile.(string))
bp, err := bluePrintLoadFile(*importBlueprint)
if err != nil {
return fmt.Errorf("File load error: %v", err)
}

def, err := bp.ToAPIDefinition(orgID.(string), upstreamVal.(string), arguments["--as-mock"].(bool))
def, err := bp.ToAPIDefinition(*orgID, *upstreamTarget, *asMock)
if err != nil {
return fmt.Errorf("Failed to create API Defintition from file")
return fmt.Errorf("Failed to create API Definition from file")
}

printDef(def)
return nil
}

func handleSwaggerMode(arguments map[string]interface{}) error {
doCreate := arguments["--create-api"]
inputFile := arguments["--import-swagger"]
if doCreate == true {
upstreamVal := arguments["--upstream-target"]
orgId := arguments["--org-id"]
if upstreamVal != nil && orgId != nil {
func handleSwaggerMode() error {
if *createAPI {
if *upstreamTarget != "" && *orgID != "" {
// Create the API with the blueprint
s, err := swaggerLoadFile(inputFile.(string))
s, err := swaggerLoadFile(*importSwagger)
if err != nil {
return fmt.Errorf("File load error: %v", err)
}

def, err := s.ToAPIDefinition(orgId.(string), upstreamVal.(string), arguments["--as-mock"].(bool))
def, err := s.ToAPIDefinition(*orgID, *upstreamTarget, *asMock)
if err != nil {
return fmt.Errorf("Failed to create API Defintition from file")
}
Expand All @@ -120,40 +113,38 @@ func handleSwaggerMode(arguments map[string]interface{}) error {

return fmt.Errorf("No upstream target or org ID defined, these are both required")

} else {
// Different branch, here we need an API Definition to modify
forApiPath := arguments["--for-api"]
if forApiPath == nil {
return fmt.Errorf("If ading to an API, the path to the defintiton must be listed")
}

versionName := arguments["--as-version"]
if versionName == nil {
return fmt.Errorf("No version defined for this import operation, please set an import ID using the --as-version flag")
}
}

defFromFile, err := apiDefLoadFile(forApiPath.(string))
if err != nil {
return fmt.Errorf("failed to load and decode file data for API Definition: %v", err)
}
// Different branch, here we need an API Definition to modify
if *forAPI == "" {
return fmt.Errorf("If adding to an API, the path to the definition must be listed")
}

s, err := swaggerLoadFile(inputFile.(string))
if err != nil {
return fmt.Errorf("File load error: %v", err)
}
if *asVersion == "" {
return fmt.Errorf("No version defined for this import operation, please set an import ID using the --as-version flag")
}

versionData, err := s.ConvertIntoApiVersion(arguments["--as-mock"].(bool))
if err != nil {
return fmt.Errorf("Conversion into API Def failed: %v", err)
}
defFromFile, err := apiDefLoadFile(*forAPI)
if err != nil {
return fmt.Errorf("failed to load and decode file data for API Definition: %v", err)
}

if err := s.InsertIntoAPIDefinitionAsVersion(versionData, defFromFile, versionName.(string)); err != nil {
return fmt.Errorf("Insertion failed: %v", err)
}
s, err := swaggerLoadFile(*importSwagger)
if err != nil {
return fmt.Errorf("File load error: %v", err)
}

printDef(defFromFile)
versionData, err := s.ConvertIntoApiVersion(*asMock)
if err != nil {
return fmt.Errorf("Conversion into API Def failed: %v", err)
}

if err := s.InsertIntoAPIDefinitionAsVersion(versionData, defFromFile, *asVersion); err != nil {
return fmt.Errorf("Insertion failed: %v", err)
}

printDef(defFromFile)

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestMain(m *testing.M) {
config.Global.Monitor.EnableTriggerMonitors = true
config.Global.AnalyticsConfig.NormaliseUrls.Enabled = true
afterConfSetup(&config.Global)
initialiseSystem(nil)
initialiseSystem()
// Small part of start()
loadAPIEndpoints(mainRouter)
if analytics.GeoIPDB == nil {
Expand Down Expand Up @@ -954,7 +954,7 @@ func TestHttpPprof(t *testing.T) {
{method: "GET", path: "/debug/pprof/", code: 404},
{method: "GET", path: "/debug/pprof/", code: 404, controlRequest: true},
}, true)
httpProfile = true
*httpProfile = true
doReload()
testHttp(t, []tykHttpTest{
{method: "GET", path: "/debug/pprof/", code: 404},
Expand Down
4 changes: 2 additions & 2 deletions instrumentation_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var applicationGCStats = debug.GCStats{}
var instrument = health.NewStream()

// setupInstrumentation handles all the intialisation of the instrumentation handler
func setupInstrumentation(arguments map[string]interface{}) {
func setupInstrumentation() {
switch {
case arguments["--log-instrumentation"] == true:
case *logInstrumentation:
case os.Getenv("TYK_INSTRUMENTATION") == "1":
default:
return
Expand Down
Loading

0 comments on commit 0cfdcbc

Please sign in to comment.