Rearrange a little bit of the Coconut cmd scaffolding
This commit is contained in:
parent
24990062ad
commit
0af93f0989
48
cmd/coco/coco.go
Normal file
48
cmd/coco/coco.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strconv"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCocoCmd() *cobra.Command {
|
||||
var logToStderr bool
|
||||
var verbose int
|
||||
cmd := &cobra.Command{
|
||||
Use: "coco",
|
||||
Short: "Coconut is a framework and toolset for reusable stacks of services",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// Ensure the glog library has been initialized, including calling flag.Parse beforehand. Unfortunately,
|
||||
// this is the only way to control the way glog runs. That includes poking around at flags below.
|
||||
flag.Parse()
|
||||
if logToStderr {
|
||||
flag.Lookup("logtostderr").Value.Set("true")
|
||||
}
|
||||
if verbose > 0 {
|
||||
flag.Lookup("v").Value.Set(strconv.Itoa(verbose))
|
||||
}
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
glog.Flush()
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().BoolVar(&logToStderr, "logtostderr", false, "Log to stderr instead of to files")
|
||||
cmd.PersistentFlags().IntVarP(
|
||||
&verbose, "verbose", "v", 0, "Enable verbose logging (e.g., v=3); anything >3 is very verbose")
|
||||
|
||||
cmd.AddCommand(newConfigCmd())
|
||||
cmd.AddCommand(newDeployCmd())
|
||||
cmd.AddCommand(newDestroyCmd())
|
||||
cmd.AddCommand(newEnvCmd())
|
||||
cmd.AddCommand(newPackCmd())
|
||||
cmd.AddCommand(newPlanCmd())
|
||||
cmd.AddCommand(newVersionCmd())
|
||||
|
||||
return cmd
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/pulumi/coconut/pkg/compiler/core"
|
||||
"github.com/pulumi/coconut/pkg/diag"
|
||||
)
|
||||
|
||||
func NewCoconutCmd() *cobra.Command {
|
||||
var logToStderr bool
|
||||
var verbose int
|
||||
cmd := &cobra.Command{
|
||||
Use: "coco",
|
||||
Short: "Coconut is a framework and toolset for reusable stacks of services",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// Ensure the glog library has been initialized, including calling flag.Parse beforehand. Unfortunately,
|
||||
// this is the only way to control the way glog runs. That includes poking around at flags below.
|
||||
flag.Parse()
|
||||
if logToStderr {
|
||||
flag.Lookup("logtostderr").Value.Set("true")
|
||||
}
|
||||
if verbose > 0 {
|
||||
flag.Lookup("v").Value.Set(strconv.Itoa(verbose))
|
||||
}
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
glog.Flush()
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().BoolVar(&logToStderr, "logtostderr", false, "Log to stderr instead of to files")
|
||||
cmd.PersistentFlags().IntVarP(
|
||||
&verbose, "verbose", "v", 0, "Enable verbose logging (e.g., v=3); anything >3 is very verbose")
|
||||
|
||||
cmd.AddCommand(newConfigCmd())
|
||||
cmd.AddCommand(newDeployCmd())
|
||||
cmd.AddCommand(newDestroyCmd())
|
||||
cmd.AddCommand(newEnvCmd())
|
||||
cmd.AddCommand(newPackCmd())
|
||||
cmd.AddCommand(newPlanCmd())
|
||||
cmd.AddCommand(newVersionCmd())
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
var snk diag.Sink
|
||||
|
||||
// sink lazily allocates a sink to be used if we can't create a compiler.
|
||||
func sink() diag.Sink {
|
||||
if snk == nil {
|
||||
snk = core.DefaultSink("")
|
||||
}
|
||||
return snk
|
||||
}
|
||||
|
||||
// runFunc wraps an error-returning run func with standard Coconut error handling. All Coconut commands should wrap
|
||||
// themselves in this to ensure consistent and appropriate error behavior. In particular, we want to avoid any calls to
|
||||
// os.Exit in the middle of a callstack which might prohibit reaping of child processes, resources, etc. And we wish to
|
||||
// avoid the default Cobra unhandled error behavior, because it is formatted incorrectly and needlessly prints usage.
|
||||
func runFunc(run func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) {
|
||||
return func(cmd *cobra.Command, args []string) {
|
||||
if err := run(cmd, args); err != nil {
|
||||
exitError(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// exitError issues an error and exits with a standard error exit code.
|
||||
func exitError(msg string, args ...interface{}) {
|
||||
exitErrorCode(-1, msg, args...)
|
||||
}
|
||||
|
||||
// exitErrorCode issues an error and exists with the given error exit code.
|
||||
func exitErrorCode(code int, msg string, args ...interface{}) {
|
||||
sink().Errorf(diag.Message(fmt.Sprintf(msg, args...)))
|
||||
os.Exit(code)
|
||||
}
|
|
@ -5,11 +5,49 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/pulumi/coconut/pkg/compiler/core"
|
||||
"github.com/pulumi/coconut/pkg/diag"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := NewCoconutCmd().Execute(); err != nil {
|
||||
if err := NewCocoCmd().Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "An error occurred: %v\n", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
var snk diag.Sink
|
||||
|
||||
// sink lazily allocates a sink to be used if we can't create a compiler.
|
||||
func sink() diag.Sink {
|
||||
if snk == nil {
|
||||
snk = core.DefaultSink("")
|
||||
}
|
||||
return snk
|
||||
}
|
||||
|
||||
// runFunc wraps an error-returning run func with standard Coconut error handling. All Coconut commands should wrap
|
||||
// themselves in this to ensure consistent and appropriate error behavior. In particular, we want to avoid any calls to
|
||||
// os.Exit in the middle of a callstack which might prohibit reaping of child processes, resources, etc. And we wish to
|
||||
// avoid the default Cobra unhandled error behavior, because it is formatted incorrectly and needlessly prints usage.
|
||||
func runFunc(run func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) {
|
||||
return func(cmd *cobra.Command, args []string) {
|
||||
if err := run(cmd, args); err != nil {
|
||||
exitError(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// exitError issues an error and exits with a standard error exit code.
|
||||
func exitError(msg string, args ...interface{}) {
|
||||
exitErrorCode(-1, msg, args...)
|
||||
}
|
||||
|
||||
// exitErrorCode issues an error and exists with the given error exit code.
|
||||
func exitErrorCode(code int, msg string, args ...interface{}) {
|
||||
sink().Errorf(diag.Message(fmt.Sprintf(msg, args...)))
|
||||
os.Exit(code)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue