Skip to content

Commit

Permalink
style: remove openapi command, replace with internal command
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dnephin committed Mar 23, 2022
1 parent 3a0ba37 commit 846e1a0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ openapi-lint: docs/api/openapi3.json
openapi lint $<

docs/api/openapi3.json:
go run . openapi
go run ./internal/openapigen $@
18 changes: 0 additions & 18 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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())

Expand Down
27 changes: 27 additions & 0 deletions internal/openapigen/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion internal/server/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/server/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 846e1a0

Please sign in to comment.