From 846e1a03c5e7160926f113aa9835d7e6ae81d969 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 21 Mar 2022 20:45:01 -0400 Subject: [PATCH] style: remove openapi command, replace with internal command Generating the openapi spec is a developer operation, not an end-user operation. This PR removes the sub-command from the CLI and adds a new internal/openapigen command. This matches the pattern we use for generating the user documentation (internal/docsgen). --- Makefile | 2 +- internal/cmd/cmd.go | 18 ------------------ internal/openapigen/main.go | 27 +++++++++++++++++++++++++++ internal/server/openapi.go | 2 +- internal/server/openapi_test.go | 2 +- 5 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 internal/openapigen/main.go diff --git a/Makefile b/Makefile index eccfbdaa12..d17d8bceaa 100644 --- a/Makefile +++ b/Makefile @@ -95,4 +95,4 @@ openapi-lint: docs/api/openapi3.json openapi lint $< docs/api/openapi3.json: - go run . openapi + go run ./internal/openapigen $@ diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 4064a33904..571b374a23 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -350,23 +350,6 @@ func canonicalPath(in string) (string, error) { return abs, nil } -// TODO: remove -func newOpenAPICmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "openapi", - Short: "generate the openapi spec", - Hidden: true, - - RunE: func(cmd *cobra.Command, args []string) error { - s := &server.Server{} - s.GenerateRoutes() - return nil - }, - } - - return cmd -} - func newServerCmd() *cobra.Command { cmd := &cobra.Command{ Use: "server", @@ -595,7 +578,6 @@ func NewRootCmd() (*cobra.Command, error) { rootCmd.AddCommand(newTokensCmd()) rootCmd.AddCommand(newInfoCmd()) rootCmd.AddCommand(newServerCmd()) - rootCmd.AddCommand(newOpenAPICmd()) rootCmd.AddCommand(newConnectorCmd()) rootCmd.AddCommand(newVersionCmd()) diff --git a/internal/openapigen/main.go b/internal/openapigen/main.go new file mode 100644 index 0000000000..c5de1b68f7 --- /dev/null +++ b/internal/openapigen/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "os" + + "github.com/infrahq/infra/internal/server" +) + +func main() { + if err := run(os.Args[1:]); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } +} + +func run(args []string) error { + if len(args) < 1 { + return fmt.Errorf("missing command line argument: path to openapi spec file") + } + filename := args[0] + + s := server.Server{} + s.GenerateRoutes() + + return server.WriteOpenAPISpecToFile(filename) +} diff --git a/internal/server/openapi.go b/internal/server/openapi.go index 542bf53e74..47c277be1e 100644 --- a/internal/server/openapi.go +++ b/internal/server/openapi.go @@ -236,7 +236,7 @@ func writeOpenAPISpec(version string, out io.Writer) error { return nil } -func writeOpenAPISpecToFile(filename string) error { +func WriteOpenAPISpecToFile(filename string) error { old, err := readOpenAPISpec(filename) if err != nil { return err diff --git a/internal/server/openapi_test.go b/internal/server/openapi_test.go index 3f77921868..d484079a33 100644 --- a/internal/server/openapi_test.go +++ b/internal/server/openapi_test.go @@ -15,6 +15,6 @@ func TestWriteOpenAPISpec(t *testing.T) { s.GenerateRoutes() filename := "../../docs/api/openapi3.json" - err := writeOpenAPISpecToFile(filename) + err := WriteOpenAPISpecToFile(filename) require.NoError(t, err) }