Skip to content

Commit

Permalink
feat: enable specification of module use
Browse files Browse the repository at this point in the history
This varies based on whether the module is used as a root command, or a
subcommand in another command. Leave the decision up to the user.
  • Loading branch information
odsod committed Jul 28, 2022
1 parent e553a45 commit 564a054
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
5 changes: 0 additions & 5 deletions aipcli/compilerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type CompilerConfig struct {
DefaultHost string
Root string
GoogleCloudIdentityTokens bool
Main bool
}

func (c *CompilerConfig) DefaultAddress() (string, bool) {
Expand All @@ -25,7 +24,6 @@ func (c *CompilerConfig) AddToFlagSet(flags *pflag.FlagSet) {
flags.Var(stringStringMap{value: c.Hosts}, "hosts", "mapping from alias to host")
flags.StringVar(&c.DefaultHost, "default_host", "", "default host override")
flags.StringVar(&c.Root, "root", "", "root command")
flags.BoolVar(&c.Main, "main", false, "write default root command main.go")
flags.BoolVar(&c.GoogleCloudIdentityTokens, "gcloud_identity_tokens", false, "use gcloud to print identity tokens")
}

Expand All @@ -37,9 +35,6 @@ func (c *CompilerConfig) Validate() error {
}
return fmt.Errorf("default_host (%s) must be one of hosts (%s)", c.DefaultHost, strings.Join(hosts, ","))
}
if c.Main && c.Root == "" {
return fmt.Errorf("main option requires root option")
}
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/examplectl/gen/module.go → cmd/examplectl/gen/root.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/examplectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
if err := examplectl.NewModuleCommand().Execute(); err != nil {
if err := examplectl.NewModuleCommand("examplectl").Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down
7 changes: 3 additions & 4 deletions internal/gencli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gencli
import (
"fmt"
"path"
"strconv"
"strings"

"go.einride.tech/aip-cli/aipcli"
Expand Down Expand Up @@ -43,7 +42,7 @@ func generateRootModuleFile(gen *protogen.Plugin, config aipcli.CompilerConfig)
if !ok {
return fmt.Errorf("param root requires param module to be provided")
}
g := gen.NewGeneratedFile(path.Join(module, "module.go"), "")
g := gen.NewGeneratedFile(path.Join(module, "root.go"), "")
generateGeneratedFileHeader(g)
g.P("package ", config.Root)
cobraCommand := g.QualifiedGoIdent(protogen.GoIdent{
Expand All @@ -59,9 +58,9 @@ func generateRootModuleFile(gen *protogen.Plugin, config aipcli.CompilerConfig)
GoName: "NewModuleCommand",
})
g.P()
g.P("func NewModuleCommand() *", cobraCommand, " {")
g.P("func NewModuleCommand(use string) *", cobraCommand, " {")
g.P("return ", aipCLINewModuleCommand, "(")
g.P(strconv.Quote(config.Root), ",")
g.P("use,")
g.P("&", cliConfig, "{")
g.P("Compiler: ", fmt.Sprintf("%#v", config), ",")
g.P("},")
Expand Down

0 comments on commit 564a054

Please sign in to comment.