Skip to content

Commit b95b6ee

Browse files
authored
refactor: remove unused package (#8)
1 parent c70d685 commit b95b6ee

File tree

4 files changed

+60
-72
lines changed

4 files changed

+60
-72
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package main
22

3-
import kcl_openapi "kusionstack.io/kcl-openapi/pkg/cmds/kcl-openapi"
3+
import "kusionstack.io/kcl-openapi/pkg/cmds"
44

5-
func main() { kcl_openapi.Main() }
5+
func main() { cmds.Main() }

pkg/cmds/generate.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
package generate
1+
package cmds
22

33
import (
4+
"io/ioutil"
45
"log"
5-
6-
"github.com/jessevdk/go-flags"
6+
"os"
77

88
crdGen "kusionstack.io/kcl-openapi/pkg/kube_resource/generator"
99
"kusionstack.io/kcl-openapi/pkg/swagger/generator"
10+
11+
"github.com/go-openapi/loads"
12+
"github.com/go-openapi/loads/fmts"
13+
"github.com/jessevdk/go-flags"
1014
)
1115

16+
func init() {
17+
loads.AddLoader(fmts.YAMLMatcher, fmts.YAMLDoc)
18+
}
19+
20+
var opts struct {
21+
// General options applicable to all commands
22+
Quiet func() `long:"quiet" short:"q" description:"silence logs"`
23+
LogFile func(string) `long:"log-output" description:"redirect logs to file" value-name:"LOG-FILE"`
24+
}
25+
26+
// Generate command to group all generator commands together
27+
type Generate struct {
28+
Model *Model `command:"model"`
29+
}
30+
31+
// Model is the generate model file command
32+
type Model struct {
33+
Options options
34+
}
35+
1236
type options struct {
1337
Spec flags.Filename `long:"spec" short:"f" description:"the path to the OpenAPI spec file. It should be a local path in your file system" group:"shared"`
1438
Crd bool `long:"crd" description:"if the spec file is a kubernetes CRD" group:"shared"`
@@ -18,9 +42,37 @@ type options struct {
1842
DisableKeepSpecOrder bool `long:"disable-keep-spec-order" description:"disable to keep schema properties order identical to spec file"`
1943
}
2044

21-
// Model is the generate model file command
22-
type Model struct {
23-
Options options
45+
func Main() {
46+
parser := flags.NewParser(&opts, flags.Default)
47+
parser.ShortDescription = "helps you to maintain your KCL API automatically"
48+
parser.LongDescription = `kcl-openapi helps you to generate your KCL model code from OpenAPI spec or Kubernetes CRD.`
49+
50+
genpar, err := parser.AddCommand("generate", "generate KCL code", "generate kcl code from the OpenAPI spec file", &Generate{})
51+
if err != nil {
52+
log.Fatalln(err)
53+
}
54+
for _, cmd := range genpar.Commands() {
55+
switch cmd.Name {
56+
case "model":
57+
cmd.ShortDescription = "generate KCL models from OpenAPI spec"
58+
cmd.LongDescription = cmd.ShortDescription
59+
}
60+
}
61+
62+
opts.Quiet = func() {
63+
log.SetOutput(ioutil.Discard)
64+
}
65+
opts.LogFile = func(logfile string) {
66+
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
67+
if err != nil {
68+
log.Fatalf("cannot write to file %s: %v", logfile, err)
69+
}
70+
log.SetOutput(f)
71+
}
72+
73+
if _, err := parser.Parse(); err != nil {
74+
os.Exit(1)
75+
}
2476
}
2577

2678
// Execute generates a model file

pkg/cmds/kcl-openapi/main.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)