From 6ffbcca3dfc35eb9be89c6fcfd1ab467176b95cd Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 21 Jun 2024 19:04:42 +0200 Subject: [PATCH] `tools/data-api`: fixing an issue where the input arguments weren't being picked up/used --- .../internal/dataapi/data_api_cmd.go | 16 +++++++++++ .../repository/helpers_discovery.go | 2 +- tools/data-api/internal/commands/args.go | 10 ++++++- tools/data-api/internal/commands/serve.go | 28 ++++++++++++------- tools/data-api/main.go | 12 +------- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/tools/data-api-differ/internal/dataapi/data_api_cmd.go b/tools/data-api-differ/internal/dataapi/data_api_cmd.go index dc3233a265d..91029ddc070 100644 --- a/tools/data-api-differ/internal/dataapi/data_api_cmd.go +++ b/tools/data-api-differ/internal/dataapi/data_api_cmd.go @@ -57,7 +57,23 @@ func (p *dataApiCmd) launchAndWait(ctx context.Context, client *v1.Client) error for attempts := 0; attempts < 50; attempts++ { log.Logger.Trace(fmt.Sprintf("Checking the health of the Data API - attempt %d/50", attempts+1)) + if (p.cmd.ProcessState != nil && p.cmd.ProcessState.Exited()) || p.cmd.Process == nil { + log.Logger.Warn("The Data API doesn't appear to be running, maybe it's crashed?") + if p.cmd.Err != nil { + return fmt.Errorf("running the Data API: %+v", p.cmd.Err) + } + } + result, err := client.Health(ctx) + if err != nil { + if result != nil && result.HttpResponse != nil { + return fmt.Errorf("unexpected status code %d", result.HttpResponse.StatusCode) + } + + log.Logger.Trace(fmt.Sprintf("API not ready - waiting 1s to try again (%+v)", err)) + time.Sleep(1 * time.Second) + continue + } if result != nil && result.Available { log.Logger.Trace("API available") return nil diff --git a/tools/data-api-repository/repository/helpers_discovery.go b/tools/data-api-repository/repository/helpers_discovery.go index 7fb87cdd1f3..4c98605440a 100644 --- a/tools/data-api-repository/repository/helpers_discovery.go +++ b/tools/data-api-repository/repository/helpers_discovery.go @@ -131,7 +131,7 @@ func discoverCommonTypesWithin(workingDirectory string, sourceDataType sdkModels return nil, fmt.Errorf("parsing the Common Types within %q: %+v", commonTypesDirectory, err) } if commonTypes == nil { - logger.Debug("The directory at %q contained no Common Types - skipping") + logger.Debug(fmt.Sprintf("The directory at %q contained no Common Types - skipping", commonTypesDirectory)) continue } for apiVersion, value := range *commonTypes { diff --git a/tools/data-api/internal/commands/args.go b/tools/data-api/internal/commands/args.go index 28a6a2efef5..04875c14599 100644 --- a/tools/data-api/internal/commands/args.go +++ b/tools/data-api/internal/commands/args.go @@ -29,7 +29,7 @@ type Arguments struct { } func (a *Arguments) Parse(input []string) error { - logging.Tracef("Parsing arguments..") + logging.Tracef("Parsing arguments (%+v)..", input) var portVar int var serviceNamesRaw string @@ -40,6 +40,10 @@ func (a *Arguments) Parse(input []string) error { f.StringVar(&dataDirectoryRaw, "data-directory", a.DataDirectory, "The path to the directory the data will be read from") f.Parse(input) + if dataDirectoryRaw != "" { + a.DataDirectory = dataDirectoryRaw + } + if serviceNamesRaw != "" { a.ServiceNames = pointer.To(strings.Split(serviceNamesRaw, ",")) } @@ -63,5 +67,9 @@ func (a *Arguments) Parse(input []string) error { } a.DataDirectory = abs + logging.Log.Info(fmt.Sprintf("Using Service Names [%+v]..", a.ServiceNames)) + logging.Log.Info(fmt.Sprintf("Using the Port %d..", a.Port)) + logging.Log.Info(fmt.Sprintf("Using the Data Directory %q..", a.DataDirectory)) + return nil } diff --git a/tools/data-api/internal/commands/serve.go b/tools/data-api/internal/commands/serve.go index 817e6aba752..ab423b3e35c 100644 --- a/tools/data-api/internal/commands/serve.go +++ b/tools/data-api/internal/commands/serve.go @@ -5,6 +5,7 @@ package commands import ( "fmt" + "log" "net/http" "github.com/go-chi/chi/v5" @@ -17,14 +18,11 @@ import ( var _ cli.Command = ServeCommand{} type ServeCommand struct { - args Arguments } -func NewServeCommand(args Arguments) func() (cli.Command, error) { +func NewServeCommand() func() (cli.Command, error) { return func() (cli.Command, error) { - return ServeCommand{ - args: args, - }, nil + return ServeCommand{}, nil } } @@ -32,13 +30,23 @@ func (ServeCommand) Help() string { return "Launches the Server" } -func (c ServeCommand) Run(_ []string) int { - logging.Debugf("Launching Server on port %d", c.args.Port) +func (c ServeCommand) Run(inputArgs []string) int { + args := Arguments{ + // defaults + DataDirectory: "../../api-definitions/", + Port: 8080, + ServiceNames: nil, + } + if err := args.Parse(inputArgs); err != nil { + log.Fatalf(err.Error()) + } + + logging.Debugf("Launching Server on port %d", args.Port) r := chi.NewRouter() r.Use(middleware.Logger) - r.Route("/", endpoints.Router(c.args.DataDirectory, c.args.ServiceNames)) - logging.Infof("Data API launched at http://localhost:%d", c.args.Port) - http.ListenAndServe(fmt.Sprintf(":%d", c.args.Port), r) + r.Route("/", endpoints.Router(args.DataDirectory, args.ServiceNames)) + logging.Infof("Data API launched at http://localhost:%d", args.Port) + http.ListenAndServe(fmt.Sprintf(":%d", args.Port), r) return 0 } diff --git a/tools/data-api/main.go b/tools/data-api/main.go index c71a732e75e..8d7e142cda0 100644 --- a/tools/data-api/main.go +++ b/tools/data-api/main.go @@ -21,20 +21,10 @@ func main() { logging.Log = hclog.New(loggingOpts) logging.Infof("Data API launched..") - args := commands.Arguments{ - // defaults - DataDirectory: "../../api-definitions/", - Port: 8080, - ServiceNames: nil, - } - if err := args.Parse(os.Args[1:]); err != nil { - log.Fatalf(err.Error()) - } - c := cli.NewCLI("data-api", "1.0.0") c.Args = os.Args[1:] c.Commands = map[string]cli.CommandFactory{ - "serve": commands.NewServeCommand(args), + "serve": commands.NewServeCommand(), } exitStatus, err := c.Run()