Skip to content

Commit 3d2274e

Browse files
committed
Add jsonschema subcmd
1 parent c5ae849 commit 3d2274e

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ require (
1111
require (
1212
github.com/alecthomas/repr v0.4.0 // indirect
1313
github.com/hexops/gotextdiff v1.0.3 // indirect
14+
github.com/swaggest/jsonschema-go v0.3.78 // indirect
15+
github.com/swaggest/refl v1.4.0 // indirect
1416
golang.org/x/sys v0.35.0 // indirect
1517
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc
66
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
77
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
88
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
9+
github.com/swaggest/jsonschema-go v0.3.78 h1:5+YFQrLxOR8z6CHvgtZc42WRy/Q9zRQQ4HoAxlinlHw=
10+
github.com/swaggest/jsonschema-go v0.3.78/go.mod h1:4nniXBuE+FIGkOGuidjOINMH7OEqZK3HCSbfDuLRI0g=
11+
github.com/swaggest/refl v1.4.0 h1:CftOSdTqRqs100xpFOT/Rifss5xBV/CT0S/FN60Xe9k=
12+
github.com/swaggest/refl v1.4.0/go.mod h1:4uUVFVfPJ0NSX9FPwMPspeHos9wPFlCMGoPRllUbpvA=
913
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
1014
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
1115
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=

pkg/cmd/cmd.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
type cli struct {
1111
ListenAddr string `help:"Address to listen on" default:":2222"`
1212
Config string `help:"Path to JSON config file"`
13+
14+
Serve serveCmd `cmd:"" default:"1" help:"Start the server"`
15+
JSONSchema jsonschemaCmd `cmd:"" name:"jsonschema" help:"Print config JSON schema"`
1316
}
1417

15-
func (c *cli) Run() error {
18+
type serveCmd struct{}
19+
20+
func (s *serveCmd) Run(c *cli) error {
1621
config, err := sshgate.Open(c.Config)
1722
if err != nil {
1823
return err
1924
}
2025

2126
server := sshgate.New(config, c.ListenAddr)
22-
23-
if err := server.ListenAndServe(context.Background()); err != nil {
24-
return err
25-
}
26-
27-
return nil
27+
return server.ListenAndServe(context.Background())
2828
}
2929

3030
func Execute() {

pkg/cmd/cmd_jsonschema.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"os"
7+
8+
"github.com/cedws/sshgate/pkg/sshgate"
9+
"github.com/swaggest/jsonschema-go"
10+
)
11+
12+
type jsonschemaCmd struct{}
13+
14+
func (j *jsonschemaCmd) Run() error {
15+
reflector := jsonschema.Reflector{}
16+
schema, err := reflector.Reflect(sshgate.Config{})
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
enc := json.NewEncoder(os.Stdout)
22+
enc.SetIndent("", " ")
23+
24+
return enc.Encode(schema)
25+
}

0 commit comments

Comments
 (0)