Skip to content

Commit b90e11c

Browse files
authored
Merge pull request #5 from Peefy/feat-all-subcommands
feat: add all subcommands for kcl
2 parents a4da757 + 9ba6dd2 commit b90e11c

File tree

17 files changed

+729
-56
lines changed

17 files changed

+729
-56
lines changed

README-zh.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
您可以使用 `go install` 命令安装 `kcl`
2828

2929
```shell
30-
go install kcl-lang.io/cli@latest
30+
go install kcl-lang.io/cli@main
3131
```
3232

3333
### 从 Github Release 页面手动安装
@@ -47,6 +47,11 @@ kcl --help
4747

4848
### 从源代码构建
4949

50+
```shell
51+
git clone https://github.com/kcl-lang/cli
52+
cd cli && go build -o kcl
53+
```
54+
5055
## 快速开始
5156

5257
```shell

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
## Introduction
1717

18-
`kcl` is the KCL package manager. `KCL CLI` downloads your KCL package's dependencies, compiles your KCL packages, makes packages, and uploads them to the kcl package registry.
18+
`kcl` is a command-line interface that includes language core features, IDE features, package management tools, community integration, and other tools.
1919

2020
## Installation
2121

@@ -26,7 +26,7 @@
2626
You can download `kcl` via `go install`.
2727

2828
```shell
29-
go install kcl-lang.io/cli@latest
29+
go install kcl-lang.io/cli@main
3030
```
3131

3232
### Download from GITHUB Release Page
@@ -46,6 +46,11 @@ kcl --help
4646

4747
### Build from Source Code
4848

49+
```shell
50+
git clone https://github.com/kcl-lang/cli
51+
cd cli && go build -o kcl
52+
```
53+
4954
## Quick Start
5055

5156
```shell

cmd/clean.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright The KCL Authors. All rights reserved.
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/spf13/cobra"
11+
"kcl-lang.io/kcl-go/pkg/utils"
12+
)
13+
14+
const (
15+
cleanDesc = `
16+
This command cleans the kcl build cache.
17+
`
18+
cleanExample = ` # Clean the build cache
19+
kcl clean
20+
`
21+
)
22+
23+
// NewCleanCmd returns the clean command.
24+
func NewCleanCmd() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "clean",
27+
Short: "KCL clean tool",
28+
Long: cleanDesc,
29+
Example: cleanExample,
30+
RunE: func(_ *cobra.Command, args []string) error {
31+
if len(args) == 0 {
32+
args = append(args, ".")
33+
}
34+
pkgroot, err := utils.FindPkgRoot(args[0])
35+
if err != nil {
36+
fmt.Println("no cache found")
37+
return err
38+
}
39+
cachePaths := []string{
40+
filepath.Join(pkgroot, ".kclvm/cache"),
41+
filepath.Join(pkgroot, "__main__/.kclvm/cache"),
42+
filepath.Join(args[0], ".kclvm/cache"),
43+
filepath.Join(args[0], "__main__/.kclvm/cache"),
44+
}
45+
for _, cachePath := range cachePaths {
46+
if isDir(cachePath) {
47+
if err := os.RemoveAll(cachePath); err == nil {
48+
fmt.Printf("%s removed\n", cachePath)
49+
} else {
50+
fmt.Printf("remove %s failed\n", cachePath)
51+
return err
52+
}
53+
}
54+
}
55+
return nil
56+
},
57+
SilenceUsage: true,
58+
}
59+
60+
return cmd
61+
}
62+
63+
func isDir(path string) bool {
64+
fileInfo, err := os.Stat(path)
65+
if err != nil {
66+
return false
67+
}
68+
return fileInfo.IsDir()
69+
}

cmd/doc.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright The KCL Authors. All rights reserved.
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/spf13/cobra"
9+
"kcl-lang.io/kcl-go/pkg/tools/gen"
10+
)
11+
12+
const (
13+
docDesc = `
14+
This command shows documentation for KCL package or symbol.
15+
`
16+
docExample = ` # Generate document for current package
17+
kcl doc generate
18+
`
19+
docGenDesc = ` # Generate Markdown document for current package
20+
kcl doc generate
21+
22+
# Generate Html document for current package
23+
kcl doc generate --format html
24+
25+
# Generate Markdown document for specific package
26+
kcl doc generate --file-path <package path>
27+
28+
# Generate Markdown document for specific package to a <target directory>
29+
kcl doc generate --file-path <package path> --target <target directory>
30+
`
31+
docGenExample = ` # Generate Markdown document for current package
32+
kcl doc generate
33+
34+
# Generate Html document for current package
35+
kcl doc generate --format html
36+
37+
# Generate Markdown document for specific package
38+
kcl doc generate --file-path <package path>
39+
40+
# Generate Markdown document for specific package to a <target directory>
41+
kcl doc generate --file-path <package path> --target <target directory>
42+
`
43+
)
44+
45+
// NewDocCmd returns the doc command.
46+
func NewDocCmd() *cobra.Command {
47+
cmd := &cobra.Command{
48+
Use: "doc",
49+
Short: "KCL document tool",
50+
Long: docDesc,
51+
Example: docExample,
52+
SilenceUsage: true,
53+
}
54+
55+
cmd.AddCommand(NewDocGenerateCmd())
56+
57+
return cmd
58+
}
59+
60+
// NewDocGenerateCmd returns the doc generate command.
61+
func NewDocGenerateCmd() *cobra.Command {
62+
o := gen.GenOpts{}
63+
cmd := &cobra.Command{
64+
Use: "generate",
65+
Short: "Generates documents from code and examples",
66+
Long: docGenDesc,
67+
Example: docGenExample,
68+
RunE: func(*cobra.Command, []string) error {
69+
genContext, err := o.ValidateComplete()
70+
if err != nil {
71+
fmt.Println(fmt.Errorf("generate failed: %s", err))
72+
return err
73+
}
74+
75+
err = genContext.GenDoc()
76+
if err != nil {
77+
fmt.Println(fmt.Errorf("generate failed: %s", err))
78+
return err
79+
} else {
80+
fmt.Printf("Generate Complete! Check generated docs in %s\n", genContext.Target)
81+
return nil
82+
}
83+
},
84+
SilenceUsage: true,
85+
}
86+
87+
cmd.Flags().StringVar(&o.Path, "file-path", "",
88+
`Relative or absolute path to the KCL package root when running kcl-doc command from
89+
outside of the KCL package root directory.
90+
If not specified, the current work directory will be used as the KCL package root.`)
91+
cmd.Flags().StringVar(&o.Format, "format", string(gen.Markdown),
92+
`The document format to generate. Supported values: markdown, html, openapi.`)
93+
cmd.Flags().StringVar(&o.Target, "target", "",
94+
`If not specified, the current work directory will be used. A docs/ folder will be created under the target directory.`)
95+
cmd.Flags().StringVar(&o.TemplateDir, "template", "",
96+
`The template directory based on the KCL package root. If not specified, the built-in templates will be used.`)
97+
cmd.Flags().BoolVar(&o.IgnoreDeprecated, "ignore-deprecated", false,
98+
`Do not generate documentation for deprecated schemas.`)
99+
cmd.Flags().BoolVar(&o.EscapeHtml, "escape-html", false,
100+
`Whether to escape html symbols when the output format is markdown. Always scape when the output format is html. Default to false.`)
101+
102+
return cmd
103+
}

cmd/flags.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/pflag"
5+
"kcl-lang.io/cli/pkg/options"
6+
)
7+
8+
func appendLangFlags(o *options.RunOptions, flags *pflag.FlagSet) {
9+
flags.StringSliceVarP(&o.Arguments, "argument", "D", []string{},
10+
"Specify the top-level argument")
11+
flags.StringSliceVarP(&o.Settings, "setting", "Y", []string{},
12+
"Specify the command line setting files")
13+
flags.StringSliceVarP(&o.Overrides, "overrides", "O", []string{},
14+
"Specify the configuration override path and value")
15+
flags.StringSliceVarP(&o.PathSelectors, "path_selectors", "S", []string{},
16+
"Specify the path selectors")
17+
flags.StringSliceVarP(&o.ExternalPackages, "external", "E", []string{},
18+
" Mapping of package name and path where the package is located")
19+
flags.StringVarP(&o.Output, "output", "o", "",
20+
"Specify the YAML/JSON output file path")
21+
flags.StringVarP(&o.Tag, "tag", "t", "",
22+
"Specify the tag for the OCI or Git artifact")
23+
flags.StringVar(&o.Format, "format", "yaml",
24+
"Specify the output format")
25+
flags.BoolVarP(&o.DisableNone, "disable_none", "n", false,
26+
"Disable dumping None values")
27+
flags.BoolVarP(&o.StrictRangeCheck, "strict_range_check", "r", false,
28+
"Do perform strict numeric range checks")
29+
flags.BoolVarP(&o.Debug, "debug", "d", false,
30+
"Run in debug mode")
31+
flags.BoolVarP(&o.SortKeys, "sort_keys", "k", false,
32+
"Sort output result keys")
33+
flags.BoolVarP(&o.Vendor, "vendor", "V", false,
34+
"Sort output result keys")
35+
flags.BoolVar(&o.NoStyle, "no_style", false,
36+
"Sort output result keys")
37+
}

cmd/fmt.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright The KCL Authors. All rights reserved.
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
9+
"github.com/spf13/cobra"
10+
kcl "kcl-lang.io/kcl-go"
11+
)
12+
13+
const (
14+
fmtDesc = `
15+
This command formats all kcl files of the current crate using kcl-fmt.
16+
`
17+
fmtExample = ` # Format all files in this folder recursively
18+
kcl fmt ./...`
19+
)
20+
21+
// NewFmtCmd returns the fmt command.
22+
func NewFmtCmd() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "fmt",
25+
Short: "KCL format tool",
26+
Long: fmtDesc,
27+
Example: fmtExample,
28+
RunE: func(_ *cobra.Command, args []string) error {
29+
var changedPaths []string
30+
if len(args) == 0 {
31+
args = append(args, ".")
32+
}
33+
for _, p := range args {
34+
paths, err := kcl.FormatPath(p)
35+
if err != nil {
36+
return err
37+
}
38+
changedPaths = append(changedPaths, paths...)
39+
}
40+
fmt.Println(strings.Join(changedPaths, "\n"))
41+
return nil
42+
},
43+
SilenceUsage: true,
44+
}
45+
46+
return cmd
47+
}

cmd/import.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright The KCL Authors. All rights reserved.
2+
3+
package cmd
4+
5+
import (
6+
"github.com/spf13/cobra"
7+
"kcl-lang.io/cli/pkg/options"
8+
)
9+
10+
const (
11+
importDesc = `
12+
This command converts other formats to KCL file.
13+
14+
Supported conversion modes:
15+
- json: convert JSON data to KCL data
16+
- yaml: convert YAML data to KCL data
17+
- gostruct: convert Go struct to KCL schema
18+
- jsonschema: convert JSON schema to KCL schema
19+
- terraformschema: convert Terraform schema to KCL schema
20+
- openapi: convert OpenAPI spec to KCL schema
21+
- crd: convert Kubernetes CRD to KCL schema
22+
- auto: automatically detect the input format
23+
`
24+
importExample = ` # Generate KCL models from OpenAPI spec
25+
kcl import -m openapi swagger.json
26+
27+
# Generate KCL models from Kubernetes CRD
28+
kcl import -m crd crd.yaml
29+
30+
# Generate KCL models from JSON
31+
kcl import data.json
32+
33+
# Generate KCL models from YAML
34+
kcl import data.yaml
35+
36+
# Generate KCL models from JSON Schema
37+
kcl import -m jsonschema schema.json
38+
39+
# Generate KCL models from Terraform provider schema
40+
kcl import -m terraformschema schema.json
41+
42+
# Generate KCL models from Go structs
43+
kcl import -m gostruct schema.go
44+
`
45+
)
46+
47+
// NewImportCmd returns the import command.
48+
func NewImportCmd() *cobra.Command {
49+
o := options.NewImportOptions()
50+
cmd := &cobra.Command{
51+
Use: "import",
52+
Short: "KCL import tool",
53+
Long: importDesc,
54+
Example: importExample,
55+
RunE: func(_ *cobra.Command, args []string) error {
56+
o.Files = args
57+
return o.Run()
58+
},
59+
SilenceUsage: true,
60+
}
61+
62+
cmd.Args = cobra.MinimumNArgs(1)
63+
cmd.Flags().StringVarP(&o.Mode, "mode", "m", "auto",
64+
"Specify the import mode. Default is mode")
65+
cmd.Flags().StringVarP(&o.Output, "output", "o", "",
66+
"Specify the output file path")
67+
cmd.Flags().BoolVarP(&o.Force, "force", "f", false,
68+
"Force overwrite output file")
69+
cmd.Flags().BoolVarP(&o.SkipValidation, "skip-validation", "s", false,
70+
"Skips validation of spec prior to generation")
71+
cmd.Flags().StringVarP(&o.ModelPackage, "package", "p", "models",
72+
"The package to save the models. Default is models")
73+
74+
return cmd
75+
}

0 commit comments

Comments
 (0)