~mil/mobroute

e9cdd77f00709247ab1b20e35e975346e85e69d1 — Miles Alan 2 months ago 77774ee
Cleanup route subcommand logic in cli
2 files changed, 32 insertions(+), 57 deletions(-)

M cli/const.go
M cli/mobroutecli.go
M cli/const.go => cli/const.go +1 -1
@@ 12,7 12,7 @@ const (
    {"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"]}}
		`

		subcmdRouteDoc = `
	subcmdRouteDoc = `
JSON Parameters for oneshot route subcommand, should be in format of: {"route_params": {"feed_ids": [516],...}}

Valid route parameters:

M cli/mobroutecli.go => cli/mobroutecli.go +31 -56
@@ 20,41 20,19 @@ import (

func main() {
	var (
		jsonBytes               []byte
		commandData             any
		err                     error
		flagBaseDebugCategories, flagBaseDebugProfile, flagBaseOutputFormat string

		flagMobsqlSet           = flag.NewFlagSet("mobsql", flag.ExitOnError)
		flagMobsqlParamsDefault = `{"mobsql_params": {"feed_ids": [], "op": "status"}}`
		flagMobsqlParams        = flagMobsqlSet.String("mp", flagMobsqlParamsDefault, subcmdMobsqlDoc)

		flagRouteSet           = flag.NewFlagSet("route", flag.ExitOnError)
		flagRouteParamsDefault = `{"route_params": {}}`
		flagRouteParams        = flagRouteSet.String("rp", flagRouteParamsDefault, subcmdRouteDoc)
		jsonBytes                                     []byte
		commandData                                   any
		err                                           error
		flagBaseDebugCategories, flagBaseDebugProfile string
		flagMobsqlSet                                 = flag.NewFlagSet("mobsql", flag.ExitOnError)
		flagMobsqlParams                              = flagMobsqlSet.String("mp", "", subcmdMobsqlDoc)
		flagRouteSet                                  = flag.NewFlagSet("route", flag.ExitOnError)
		flagRouteParams                               = flagRouteSet.String("rp", "", subcmdRouteDoc)
	)

	// 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")
	}

	validSubcommands := []string{
		// -version
		"version",

		// -route -rp '{}'
		"route",

		// -mobsql_{status,load,compute,purgegtfs,purgecomputed,purgeall} -mp '{}'
		"mobsql_status",
		"mobsql_load",
		"mobsql_compute",
		"mobsql_purgeall",
		"mobsql_purgegtfs",
		"mobsql_purgecomputed",
		"version", "route", "mobsql_status", "mobsql_load", "mobsql_compute",
		"mobsql_purgeall", "mobsql_purgegtfs", "mobsql_purgecomputed",
	}

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


@@ 62,25 40,17 @@ func main() {
		os.Exit(1)
	}

	// Debug 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")
	}
	globalDeferedFn := globalsSetup(flagBaseDebugCategories, flagBaseDebugProfile)
	defer globalDeferedFn()

	// Route subcommand
	if os.Args[1] == "route" {
		var jsonBuffer string
		flagRouteSet.Parse(os.Args[2:])
		if *flagRouteParams == flagRouteParamsDefault {
			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))
			}
		}
		cmdRoute(flagRouteSet, *flagRouteParams)
	}

	// Mobsql_* subcommand


@@ 89,7 59,7 @@ func main() {
		"mobsql_purgegtfs", "mobsql_purgecomputed", "mobsql_purgeall",
	}) {
		var jsonBuffer string
		if *flagMobsqlParams == flagMobsqlParamsDefault {
		if *flagMobsqlParams == "" {
			flagMobsqlSet.Usage()
			return
		} else {


@@ 111,13 81,19 @@ func main() {
	}
}

func cmdRoute(paramsJSON string) (any, error) {
func cmdRoute(flagRouteSet *flag.FlagSet, flagRouteParams string) {
	var routeParams mobroute.OneshotRouteRequest

	if err := json.Unmarshal([]byte(paramsJSON), &routeParams); err != nil {
		return nil, fmt.Errorf("Error demarshalling YML %s: %v", paramsJSON, err)
	flagRouteSet.Parse(os.Args[2:])
	if flagRouteParams == "" {
		flagRouteSet.Usage()
	} else if err := json.Unmarshal([]byte(flagRouteParams), &routeParams); err != nil {
		errorDie(os.Args[1], flagRouteParams, err)
	} else if commandData, err := mobroute.OneshotRoute(&routeParams); err != nil {
		errorDie(os.Args[1], flagRouteParams, err)
	} else if jsonBytes, err := json.MarshalIndent(commandData, "", "  "); err != nil {
		errorDie(os.Args[1], flagRouteParams, err)
	} else {
		return mobroute.OneshotRoute(&routeParams)
		fmt.Println(string(jsonBytes))
	}
}



@@ 164,4 140,3 @@ func globalsSetup(debugCategories, debugProfile string) func() {

	return func() {}
}