2017-03-15 03:26:14 +01:00
|
|
|
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
2016-11-17 02:19:49 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2017-03-01 19:09:27 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-11-17 02:19:49 +01:00
|
|
|
"strconv"
|
|
|
|
|
2016-11-20 01:42:27 +01:00
|
|
|
"github.com/golang/glog"
|
2016-11-17 02:19:49 +01:00
|
|
|
"github.com/spf13/cobra"
|
2017-03-01 19:09:27 +01:00
|
|
|
|
|
|
|
"github.com/pulumi/coconut/pkg/compiler/core"
|
|
|
|
"github.com/pulumi/coconut/pkg/diag"
|
2016-11-17 02:19:49 +01:00
|
|
|
)
|
|
|
|
|
2017-02-25 16:25:33 +01:00
|
|
|
func NewCoconutCmd() *cobra.Command {
|
2016-11-20 01:42:27 +01:00
|
|
|
var logToStderr bool
|
2016-11-17 02:19:49 +01:00
|
|
|
var verbose int
|
|
|
|
cmd := &cobra.Command{
|
2017-02-25 16:25:33 +01:00
|
|
|
Use: "coconut",
|
|
|
|
Short: "Coconut is a framework and toolset for reusable stacks of services",
|
2016-11-20 01:42:27 +01:00
|
|
|
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")
|
|
|
|
}
|
2016-11-17 02:19:49 +01:00
|
|
|
if verbose > 0 {
|
|
|
|
flag.Lookup("v").Value.Set(strconv.Itoa(verbose))
|
|
|
|
}
|
|
|
|
},
|
2016-11-20 01:42:27 +01:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
glog.Flush()
|
|
|
|
},
|
2016-11-17 02:19:49 +01:00
|
|
|
}
|
|
|
|
|
2016-11-20 01:42:27 +01:00
|
|
|
cmd.PersistentFlags().BoolVar(&logToStderr, "logtostderr", false, "Log to stderr instead of to files")
|
2016-11-17 02:19:49 +01:00
|
|
|
cmd.PersistentFlags().IntVarP(
|
2016-11-20 01:42:27 +01:00
|
|
|
&verbose, "verbose", "v", 0, "Enable verbose logging (e.g., v=3); anything >3 is very verbose")
|
2016-11-17 02:19:49 +01:00
|
|
|
|
Make major commands more pleasant
This change eliminates the need to constantly type in the environment
name when performing major commands like configuration, planning, and
deployment. It's probably due to my age, however, I keep fat-fingering
simple commands in front of investors and I am embarrassed!
In the new model, there is a notion of a "current environment", and
I have modeled it kinda sorta just like Git's notion of "current branch."
By default, the current environment is set when you `init` something.
Otherwise, there is the `coco env select <env>` command to change it.
(Running this command w/out a new <env> will show you the current one.)
The major commands `config`, `plan`, `deploy`, and `destroy` will prefer
to use the current environment, unless it is overridden by using the
--env flag. All of the `coco env <cmd> <env>` commands still require the
explicit passing of an environment which seems reasonable since they are,
after all, about manipulating environments.
As part of this, I've overhauled the aging workspace settings cruft,
which had fallen into disrepair since the initial prototype.
2017-03-22 03:23:32 +01:00
|
|
|
cmd.AddCommand(newConfigCmd())
|
2017-03-10 22:17:55 +01:00
|
|
|
cmd.AddCommand(newDeployCmd())
|
2017-03-15 23:40:06 +01:00
|
|
|
cmd.AddCommand(newDestroyCmd())
|
2017-03-06 15:32:39 +01:00
|
|
|
cmd.AddCommand(newEnvCmd())
|
2017-03-09 16:43:28 +01:00
|
|
|
cmd.AddCommand(newPackCmd())
|
2017-03-15 23:40:06 +01:00
|
|
|
cmd.AddCommand(newPlanCmd())
|
2016-11-17 02:19:49 +01:00
|
|
|
cmd.AddCommand(newVersionCmd())
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2017-03-01 19:09:27 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:47:42 +01:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:09:27 +01:00
|
|
|
// 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{}) {
|
2017-03-15 20:16:17 +01:00
|
|
|
sink().Errorf(diag.Message(fmt.Sprintf(msg, args...)))
|
2017-03-01 19:09:27 +01:00
|
|
|
os.Exit(code)
|
|
|
|
}
|