Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/data-api: fixing an issue where the input arguments weren't being picked up/used #4230

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tools/data-api-differ/internal/dataapi/data_api_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/data-api-repository/repository/helpers_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion tools/data-api/internal/commands/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, ","))
}
Expand All @@ -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
}
28 changes: 18 additions & 10 deletions tools/data-api/internal/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package commands

import (
"fmt"
"log"
"net/http"

"github.com/go-chi/chi/v5"
Expand All @@ -17,28 +18,35 @@ 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
}
}

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
}

Expand Down
12 changes: 1 addition & 11 deletions tools/data-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading