chore(cli): replace clibase with external coder/serpent (#12252)

This commit is contained in:
Ammar Bandukwala
2024-03-15 11:24:38 -05:00
committed by GitHub
parent bed2545636
commit 496232446d
185 changed files with 3770 additions and 7221 deletions
+19 -15
View File
@@ -34,8 +34,12 @@ var (
// Do not include things like "Database", as that would break the idea
// of splitting db and api types.
// Only include dirs that are client facing packages.
externalTypeDirs = [...]string{"./cli/clibase", "./coderd/healthcheck/health"}
indent = " "
externalTypePkgs = [...]string{
"./coderd/healthcheck/health",
// CLI option types:
"github.com/coder/serpent",
}
indent = " "
)
func main() {
@@ -43,7 +47,7 @@ func main() {
log := slog.Make(sloghuman.Sink(os.Stderr))
external := []*Generator{}
for _, dir := range externalTypeDirs {
for _, dir := range externalTypePkgs {
extGen, err := ParseDirectory(ctx, log, dir)
if err != nil {
log.Fatal(ctx, fmt.Sprintf("parse external directory %s: %s", dir, err.Error()))
@@ -81,7 +85,7 @@ func main() {
break
}
dir := externalTypeDirs[i]
dir := externalTypePkgs[i]
_, _ = fmt.Printf("// The code below is generated from %s.\n\n", strings.TrimPrefix(dir, "./"))
_, _ = fmt.Print(ts.String(), "\n\n")
}
@@ -829,24 +833,24 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
// We would need to add more logic to determine this, but for now
// just hard code them.
switch n.String() {
case "github.com/coder/coder/v2/cli/clibase.Regexp":
case "github.com/coder/serpent.Regexp":
return TypescriptType{ValueType: "string"}, nil
case "github.com/coder/coder/v2/cli/clibase.HostPort":
case "github.com/coder/serpent.HostPort":
// Custom marshal json to be a string
return TypescriptType{ValueType: "string"}, nil
case "github.com/coder/coder/v2/cli/clibase.StringArray":
case "github.com/coder/serpent.StringArray":
return TypescriptType{ValueType: "string[]"}, nil
case "github.com/coder/coder/v2/cli/clibase.String":
case "github.com/coder/serpent.String":
return TypescriptType{ValueType: "string"}, nil
case "github.com/coder/coder/v2/cli/clibase.YAMLConfigPath":
case "github.com/coder/serpent.YAMLConfigPath":
return TypescriptType{ValueType: "string"}, nil
case "github.com/coder/coder/v2/cli/clibase.Strings":
case "github.com/coder/serpent.Strings":
return TypescriptType{ValueType: "string[]"}, nil
case "github.com/coder/coder/v2/cli/clibase.Int64":
case "github.com/coder/serpent.Int64":
return TypescriptType{ValueType: "number"}, nil
case "github.com/coder/coder/v2/cli/clibase.Bool":
case "github.com/coder/serpent.Bool":
return TypescriptType{ValueType: "boolean"}, nil
case "github.com/coder/coder/v2/cli/clibase.Duration":
case "github.com/coder/serpent.Duration":
return TypescriptType{ValueType: "number"}, nil
case "net/url.URL":
return TypescriptType{ValueType: "string"}, nil
@@ -865,7 +869,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{ValueType: "string"}, nil
case "encoding/json.RawMessage":
return TypescriptType{ValueType: "Record<string, string>"}, nil
case "github.com/coder/coder/v2/cli/clibase.URL":
case "github.com/coder/serpent.URL":
return TypescriptType{ValueType: "string"}, nil
// XXX: For some reason, the type generator generates these as `any`
// so explicitly specifying the correct generic TS type.
@@ -885,7 +889,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
//nolint:gocritic,revive // I prefer the switch for extensibility later.
switch {
// Struct is a generic, so the type has generic constraints in the string.
case regexp.MustCompile(`github\.com/coder/coder/v2/cli/clibase.Struct\[.*\]`).MatchString(n.String()):
case regexp.MustCompile(`github\.com/coder/serpent.Struct\[.*\]`).MatchString(n.String()):
// The marshal json just marshals the underlying value.
str, ok := ty.Underlying().(*types.Struct)
if ok {
+11 -11
View File
@@ -13,8 +13,8 @@ import (
"github.com/acarl005/stripansi"
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/flog"
"github.com/coder/serpent"
)
//go:embed command.tpl
@@ -25,8 +25,8 @@ var commandTemplate *template.Template
func init() {
commandTemplate = template.Must(
template.New("command.tpl").Funcs(template.FuncMap{
"visibleSubcommands": func(cmd *clibase.Cmd) []*clibase.Cmd {
var visible []*clibase.Cmd
"visibleSubcommands": func(cmd *serpent.Cmd) []*serpent.Cmd {
var visible []*serpent.Cmd
for _, sub := range cmd.Children {
if sub.Hidden {
continue
@@ -35,8 +35,8 @@ func init() {
}
return visible
},
"visibleOptions": func(cmd *clibase.Cmd) []clibase.Option {
var visible []clibase.Option
"visibleOptions": func(cmd *serpent.Cmd) []serpent.Option {
var visible []serpent.Option
for _, opt := range cmd.Options {
if opt.Hidden {
continue
@@ -45,7 +45,7 @@ func init() {
}
return visible
},
"atRoot": func(cmd *clibase.Cmd) bool {
"atRoot": func(cmd *serpent.Cmd) bool {
return cmd.FullName() == "coder"
},
"newLinesToBr": func(s string) string {
@@ -54,7 +54,7 @@ func init() {
"wrapCode": func(s string) string {
return fmt.Sprintf("<code>%s</code>", s)
},
"commandURI": func(cmd *clibase.Cmd) string {
"commandURI": func(cmd *serpent.Cmd) string {
return fmtDocFilename(cmd)
},
"fullName": fullName,
@@ -67,14 +67,14 @@ func init() {
)
}
func fullName(cmd *clibase.Cmd) string {
func fullName(cmd *serpent.Cmd) string {
if cmd.FullName() == "coder" {
return "coder"
}
return strings.TrimPrefix(cmd.FullName(), "coder ")
}
func fmtDocFilename(cmd *clibase.Cmd) string {
func fmtDocFilename(cmd *serpent.Cmd) string {
if cmd.FullName() == "coder" {
// Special case for index.
return "../cli.md"
@@ -83,7 +83,7 @@ func fmtDocFilename(cmd *clibase.Cmd) string {
return fmt.Sprintf("%s.md", name)
}
func writeCommand(w io.Writer, cmd *clibase.Cmd) error {
func writeCommand(w io.Writer, cmd *serpent.Cmd) error {
var b strings.Builder
err := commandTemplate.Execute(&b, cmd)
if err != nil {
@@ -112,7 +112,7 @@ func writeCommand(w io.Writer, cmd *clibase.Cmd) error {
return err
}
func genTree(dir string, cmd *clibase.Cmd, wroteLog map[string]*clibase.Cmd) error {
func genTree(dir string, cmd *serpent.Cmd, wroteLog map[string]*serpent.Cmd) error {
if cmd.Hidden {
return nil
}
+2 -2
View File
@@ -7,9 +7,9 @@ import (
"sort"
"strings"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/flog"
"github.com/coder/serpent"
)
// route is an individual page object in the docs manifest.json.
@@ -83,7 +83,7 @@ func main() {
root := (&cli.RootCmd{})
// wroteMap indexes file paths to commands.
wroteMap := make(map[string]*clibase.Cmd)
wroteMap := make(map[string]*serpent.Cmd)
var (
docsDir = filepath.Join(workdir, "docs")