~mil/mobroute

7f610dae5bd877546983ab1a25e097894914b681 — Miles Alan 3 months ago ed1bcfd
Cleanup mobroute CLI a bit; show -h on no subcmd params
1 files changed, 76 insertions(+), 61 deletions(-)

M cli/mobroutecli.go
M cli/mobroutecli.go => cli/mobroutecli.go +76 -61
@@ 27,17 27,30 @@ func main() {
		flagBaseDebugProfile    string
		flagBaseOutputFormat    string

		flagMobsqlSet    = flag.NewFlagSet("mobsql_request", flag.ExitOnError)
		defaultJSONBufferRoute  = `{"route_params": {}}`
		defaultJSONBufferMobsql = `{"mobsql_params": {"feed_ids": [], "op": "status"}}`

		examplesSubcommandRoute = `
  - Brussels Routing Example:
    {"route_params":{"feed_ids":[1088],"from":[50.85728,4.351426],"to":[50.83214,4.350534],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}

  - Krakow Routing Example:
    {"route_params":{"feed_ids":[1270],"from":[50.012338,19.88192],"to":[50.08785,20.02387],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}

  - NYC Routing Example:
    {"route_params":{"feed_ids":[516],"from":[40.70940,-74.00537],"to":[40.72879,-73.95215],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}
		`

		flagMobsqlSet    = flag.NewFlagSet("mobsql", flag.ExitOnError)
		flagMobsqlParams = flagMobsqlSet.String(
			"mp",
			`{"mobsql_params": {"feed_ids": [], "op": "status"}}`,
			`JSON Parameters for mobsql subcommand`,
			"mp", defaultJSONBufferMobsql,
			`JSON Parameters for mobsql subcommands`,
		)

		flagRouteSet    = flag.NewFlagSet("route", flag.ExitOnError)
		flagRouteParams = flagRouteSet.String(
			"rp",
			`{"route_params": {}}`,
			defaultJSONBufferRoute,
			`JSON Parameters for oneshot route subcommand, should be in format of: {"route_params": {"feed_ids": [516],...}}

Valid route parameters:


@@ 54,33 67,16 @@ Valid route parameters:
  - transfer_categories: Array of transfers categories to load - "f" for feed, "i" for implicit, "g" for generated (default: ["f", "i"])
  - cache_dir: Directory to store sqlite db, defaults to ~/.cache/mobroute if null (default: null)
  - output_formats: Array of formats for the output may include - "legs", "geojson", "diagnostics", "mapurl", "request" (default: ["legs"])

Examples:
  - Brussels Routing Example:
    {"route_params":{"feed_ids":[1088],"from":[50.85728,4.351426],"to":[50.83214,4.350534],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}

  - Krakow Routing Example:
    {"route_params":{"feed_ids":[1270],"from":[50.012338,19.88192],"to":[50.08785,20.02387],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}

  - NYC Routing Example:
    {"route_params":{"feed_ids":[516],"from":[40.70940,-74.00537],"to":[40.72879,-73.95215],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}
	`)
	)

	_ = examplesSubcommandRoute

	// Same usage for all subcommands
	for _, f := range []*flag.FlagSet{flagRouteSet} {
		f.StringVar(
			&flagBaseDebugCategories, "d", "i",
			"Verbose debug categories: (i) Information (w) Warn (d) Debug",
		)
		f.StringVar(
			&flagBaseDebugProfile, "dprof", "",
			"Write debug pprof CPU profile to file",
		)
		f.StringVar(
			&flagBaseOutputFormat, "format", "json",
			"Output format, currently only json supported",
		)
		f.StringVar(&flagBaseDebugCategories, "d", "i", "Verbose debug categories: (i) Information (w) Warn (d) Debug")
		f.StringVar(&flagBaseDebugProfile, "dprof", "", "Write debug pprof CPU profile to file")
		f.StringVar(&flagBaseOutputFormat, "format", "json", "Output format, currently only json supported")
	}

	validSubcommands := []string{


@@ 89,13 85,13 @@ Examples:
		// -route -routeparams {}
		"route",

		// -ms{status,load,compute,purgegtfs,purgecomputed,purgeall} {}
		"msstatus",
		"msload",
		"mscompute",
		"mspurgeall",
		"mspurgegtfs",
		"mspurgecomputed",
		// -mobsql_{status,load,compute,purgegtfs,purgecomputed,purgeall} {}
		"mobsql_status",
		"mobsql_load",
		"mobsql_compute",
		"mobsql_purgeall",
		"mobsql_purgegtfs",
		"mobsql_purgecomputed",
	}

	if len(os.Args) < 2 || !utilfuncs.StringInArray(validSubcommands, os.Args[1]) {


@@ 103,31 99,52 @@ Examples:
		os.Exit(1)
	}

	var jsonBuffer string
	// Route subcommand
	if os.Args[1] == "route" {
		var jsonBuffer string
		flagRouteSet.Parse(os.Args[2:])
		globalDeferedFn := globalsSetup(flagBaseDebugCategories, flagBaseDebugProfile)
		defer globalDeferedFn()
		jsonBuffer = *flagRouteParams
		commandData, err = cmdRoute(*flagRouteParams)
	} else if utilfuncs.ItemInArray(
		os.Args[1],
		[]string{"msload", "msstatus", "mscompute", "mspurgegtfs", "mspurgecomputed", "mspurgeall"},
	) {
		flagMobsqlSet.Parse(os.Args[2:])
		jsonBuffer = *flagMobsqlParams
		commandData, err = cmdMobsql(*flagMobsqlParams)
	} else if os.Args[1] == "version" {
		commandData = "v0.5.0"
		if *flagRouteParams == defaultJSONBufferRoute {
			flagRouteSet.Usage()
			return
		} else {
			globalDeferedFn := globalsSetup(flagBaseDebugCategories, flagBaseDebugProfile)
			defer globalDeferedFn()
			jsonBuffer = *flagRouteParams
			if commandData, err = cmdRoute(*flagRouteParams); err != nil {
				errorDie(os.Args[1], jsonBuffer, err)
			} else if jsonBytes, err = json.MarshalIndent(commandData, "", "  "); err != nil {
				errorDie(os.Args[1], jsonBuffer, err)
			} else {
				fmt.Println(string(jsonBytes))
			}
		}
	}

	//_ = format
	if err != nil {
		errorDie(jsonBuffer, err)
	} else if jsonBytes, err = json.MarshalIndent(commandData, "", "  "); err != nil {
		errorDie(jsonBuffer, err)
	} else {
		fmt.Println(string(jsonBytes))
	// Mobsql_* subcommand
	if utilfuncs.ItemInArray(os.Args[1], []string{
		"mobsql_load", "mobsql_status", "mobsql_compute",
		"mobsql_purgegtfs", "mobsql_purgecomputed", "mobsql_purgeall",
	}) {
		var jsonBuffer string
		if *flagMobsqlParams == defaultJSONBufferMobsql {
			flagMobsqlSet.Usage()
			return
		} else {
			flagMobsqlSet.Parse(os.Args[2:])
			jsonBuffer = *flagMobsqlParams
			if commandData, err = cmdMobsql(*flagMobsqlParams); err != nil {
				errorDie(os.Args[1], jsonBuffer, err)
			} else if jsonBytes, err = json.MarshalIndent(commandData, "", "  "); err != nil {
				errorDie(os.Args[1], jsonBuffer, err)
			} else {
				fmt.Println(string(jsonBytes))
			}
		}
	}

	// Version subcommand
	if os.Args[1] == "version" {
		fmt.Println("v0.5.0")
	}
}



@@ 151,13 168,11 @@ func cmdMobsql(paramsJSON string) (any, error) {
	}
}

func errorDie(req string, err error) {
	//if showUsage {
	//	flag.Usage()
	//}
func errorDie(subcmd, req string, err error) {
	if err != nil {
		utillog.Infof("Request failed: %s", req)
		utillog.Fatal(err)
		log.Printf("Request failed (use 'mobroute %s -h' for proper usage)", subcmd)
		log.Printf("Failed JSON request buffer: %s", req)
		log.Fatal(err)
	}
	os.Exit(1)
}