danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.87k stars 138 forks source link

Error thrown when setting cli name #406

Closed ajaykarthikr closed 4 months ago

ajaykarthikr commented 4 months ago

Following the example in documentation to set cli name, an error is thrown.

cmd/api/main.go:45:2: not enough arguments in call to cmd.Run
       have ()
       want (*cobra.Command, []string)

main.go

package main

import (
    "context"
    "net/http"

    "github.com/danielgtaylor/huma/v2"
    "github.com/danielgtaylor/huma/v2/adapters/humachi"
    "github.com/danielgtaylor/huma/v2/humacli"
    "github.com/go-chi/chi/v5"
    "github.com/rs/zerolog/log"
)

func main() {
    cli := humacli.New(func(hooks humacli.Hooks, opts *Options) {
        router := chi.NewMux()
        api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))

        huma.Get(api, "/", func(ctx context.Context, input *struct {
        }) (*struct{ Body []byte }, error) {
            return &struct{ Body []byte }{Body: []byte("Hello, World!")}, nil
        })

        server := &http.Server{
            Addr:    ":8080",
            Handler: router,
        }

        hooks.OnStart(func() {
            log.Info().Msgf("Server listening on %s", server.Addr)
            err := server.ListenAndServe()
            if err != nil {
                log.Fatal().Err(err).Msg("Failed to start server")
            }

        })
    })

    cmd := cli.Root()
    cmd.Use = "appname"
    cmd.Version = "1.0.1"
    cmd.Run()
}
x-user commented 4 months ago

I think it should be cli.Run() not cmd.Run().

ajaykarthikr commented 4 months ago

Thanks this works, ig then documentation has to be updated appropriately.