pulumi/cmd/lumidl/lumidl.go
Matt Ellis 8f076b7cb3 Argument validation for CLI commands
Previously, we were inconsistent on how we handled argument validation
in the CLI. Many commands used cobra.Command's Args property to
provide a validator if they took arguments, but commands which did not
rarely used cobra.NoArgs to indicate this.

This change does two things:

1. Introduce `cmdutil.ArgsFunc` which works like `cmdutil.RunFunc`, it
wraps an existing cobra type and lets us control the behavior when an
arguments validator fails.

2. Ensure every command sets the Args property with an instance of
cmdutil.ArgsFunc. The cmdutil package defines wrapers for all the
cobra validators we are using, to prevent us from having to spell out
`cmduitl.ArgsFunc(...)` everywhere.

Fixes #588
2017-11-29 16:10:53 -08:00

76 lines
2.9 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package main
import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/tools/lumidl"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func NewIDLCCmd() *cobra.Command {
var logToStderr bool
var outPack string
var outRPC string
var pkgBaseIDL string
var pkgBaseRPC string
var quiet bool
var recursive bool
var verbose int
cmd := &cobra.Command{
Use: "lumidl pkg-name idl-path",
Short: "The Lumi IDL compiler generates Lumi metadata and RPC stubs from IDL written in Go",
Long: "The Lumi IDL compiler generates Lumi metadata and RPC stubs from IDL written in Go.\n" +
"\n" +
"The tool accepts a subset of Go types and produces packages that can be consumed by\n" +
"ordinary Lumi programs and libraries in any language. The pkg-name argument\n" +
"controls the output package name and idl-path is the path to the IDL source code.\n" +
"\n" +
"The --out-pack and --out-rpc flags indicate where generated code is to be saved,\n" +
"and pkg-base-idl and --pkg-base-rpc may be used to override the default inferred Go\n" +
"package names (which, by default, are based on your GOPATH).",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cmdutil.InitLogging(logToStderr, verbose, true)
},
Args: cmdutil.ExactArgs(2),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Now pass the arguments and compile the package.
name := args[0] // the name of the Lumi package.
path := args[1] // the path to the IDL directory that is compiled recursively.
return lumidl.Compile(lumidl.CompileOptions{
Name: tokens.PackageName(name),
PkgBaseIDL: pkgBaseIDL,
PkgBaseRPC: pkgBaseRPC,
OutPack: outPack,
OutRPC: outRPC,
Quiet: quiet,
Recursive: recursive,
}, path)
}),
PersistentPostRun: func(cmd *cobra.Command, args []string) {
glog.Flush()
},
}
cmd.PersistentFlags().BoolVar(
&logToStderr, "logtostderr", false, "Log to stderr instead of to files")
cmd.PersistentFlags().BoolVarP(
&recursive, "recursive", "r", false, "Recursively generate code for all sub-packages in the target")
cmd.PersistentFlags().StringVar(
&outPack, "out-pack", "", "Save generated package metadata to this directory")
cmd.PersistentFlags().StringVar(
&outRPC, "out-rpc", "", "Save generated RPC provider stubs to this directory")
cmd.PersistentFlags().StringVar(
&pkgBaseIDL, "pkg-base-idl", "", "Override the base URL where the IDL package is published")
cmd.PersistentFlags().StringVar(
&pkgBaseRPC, "pkg-base-rpc", "", "Override the base URL where the RPC package is published")
cmd.PersistentFlags().BoolVarP(
&quiet, "quiet", "q", false, "Suppress non-error output progress messages")
cmd.PersistentFlags().IntVarP(
&verbose, "verbose", "v", 0, "Enable verbose logging (e.g., v=3); anything >3 is very verbose")
return cmd
}