Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2242)

Unified Diff: cmd/juju/main.go

Issue 12546043: return error on no environment
Patch Set: return error on no environment Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cmd/juju/init_test.go ('k') | cmd/juju/publish.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cmd/juju/main.go
=== modified file 'cmd/juju/main.go'
--- cmd/juju/main.go 2013-07-25 01:21:16 +0000
+++ cmd/juju/main.go 2013-08-08 20:34:27 +0000
@@ -5,13 +5,13 @@
import (
"fmt"
+ "os"
+
"launchpad.net/juju-core/cmd"
+ "launchpad.net/juju-core/environs"
"launchpad.net/juju-core/juju"
- "os"
-)
-// Import the providers.
-import (
+ // Import the providers.
_ "launchpad.net/juju-core/environs/all"
)
@@ -49,56 +49,94 @@
juju.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic)
// Creation commands.
- juju.Register(&BootstrapCommand{})
- juju.Register(&AddMachineCommand{})
- juju.Register(&DeployCommand{})
- juju.Register(&AddRelationCommand{})
- juju.Register(&AddUnitCommand{})
+ juju.Register(wrap(&BootstrapCommand{}))
+ juju.Register(wrap(&AddMachineCommand{}))
+ juju.Register(wrap(&DeployCommand{}))
+ juju.Register(wrap(&AddRelationCommand{}))
+ juju.Register(wrap(&AddUnitCommand{}))
// Destruction commands.
- juju.Register(&DestroyMachineCommand{})
- juju.Register(&DestroyRelationCommand{})
- juju.Register(&DestroyServiceCommand{})
- juju.Register(&DestroyUnitCommand{})
- juju.Register(&DestroyEnvironmentCommand{})
+ juju.Register(wrap(&DestroyMachineCommand{}))
+ juju.Register(wrap(&DestroyRelationCommand{}))
+ juju.Register(wrap(&DestroyServiceCommand{}))
+ juju.Register(wrap(&DestroyUnitCommand{}))
+ juju.Register(wrap(&DestroyEnvironmentCommand{}))
// Reporting commands.
- juju.Register(&StatusCommand{})
- juju.Register(&SwitchCommand{})
+ juju.Register(wrap(&StatusCommand{}))
+ juju.Register(wrap(&SwitchCommand{}))
// Error resolution commands.
- juju.Register(&SCPCommand{})
- juju.Register(&SSHCommand{})
- juju.Register(&ResolvedCommand{})
- juju.Register(&DebugLogCommand{sshCmd: &SSHCommand{}})
+ juju.Register(wrap(&SCPCommand{}))
+ juju.Register(wrap(&SSHCommand{}))
+ juju.Register(wrap(&ResolvedCommand{}))
+ juju.Register(wrap(&DebugLogCommand{sshCmd: &SSHCommand{}}))
// Configuration commands.
- juju.Register(&InitCommand{})
- juju.Register(&ImageMetadataCommand{})
- juju.Register(&GetCommand{})
- juju.Register(&SetCommand{})
- juju.Register(&GetConstraintsCommand{})
- juju.Register(&SetConstraintsCommand{})
- juju.Register(&GetEnvironmentCommand{})
- juju.Register(&SetEnvironmentCommand{})
- juju.Register(&ExposeCommand{})
- juju.Register(&SyncToolsCommand{})
- juju.Register(&UnexposeCommand{})
- juju.Register(&UpgradeJujuCommand{})
- juju.Register(&UpgradeCharmCommand{})
+ juju.Register(wrap(&InitCommand{}))
+ juju.Register(wrap(&ImageMetadataCommand{}))
+ juju.Register(wrap(&GetCommand{}))
+ juju.Register(wrap(&SetCommand{}))
+ juju.Register(wrap(&GetConstraintsCommand{}))
+ juju.Register(wrap(&SetConstraintsCommand{}))
+ juju.Register(wrap(&GetEnvironmentCommand{}))
+ juju.Register(wrap(&SetEnvironmentCommand{}))
+ juju.Register(wrap(&ExposeCommand{}))
+ juju.Register(wrap(&SyncToolsCommand{}))
+ juju.Register(wrap(&UnexposeCommand{}))
+ juju.Register(wrap(&UpgradeJujuCommand{}))
+ juju.Register(wrap(&UpgradeCharmCommand{}))
// Charm publishing commands.
- juju.Register(&PublishCommand{})
+ juju.Register(wrap(&PublishCommand{}))
// Charm tool commands.
- juju.Register(&HelpToolCommand{})
+ juju.Register(wrap(&HelpToolCommand{}))
// Common commands.
- juju.Register(&cmd.VersionCommand{})
+ juju.Register(wrap(&cmd.VersionCommand{}))
os.Exit(cmd.Main(juju, cmd.DefaultContext(), args[1:]))
}
+// wrap encapsulates code that wraps some of the commands in a helper class
+// that handles some common errors
+func wrap(c cmd.Command) cmd.Command {
+ if ec, ok := c.(envCmd); ok {
+ return envCmdWrapper{ec}
+ }
+ return c
+}
+
+// envCmd is a Command that interacts with the juju client environment
+type envCmd interface {
+ cmd.Command
+ EnvironName() string
+}
+
+// envCmdWrapper is a struct that wraps an environment command and lets us handle
+// errors returned from Run before they're returned to the main function
+type envCmdWrapper struct {
+ envCmd
+}
+
+// Run in envCmdWrapper gives us an opportunity to handle errors after the command is
+// run. This is used to give informative messages to the user.
+func (c envCmdWrapper) Run(ctx *cmd.Context) error {
+ err := c.envCmd.Run(ctx)
+ if environs.IsNoEnv(err) && c.EnvironName() == "" {
+ fmt.Fprintln(ctx.Stderr, "No juju environment configuration file exists.")
+ fmt.Fprintln(ctx.Stderr, err)
+ fmt.Fprintln(ctx.Stderr, "Please create a configuration by running:")
+ fmt.Fprintln(ctx.Stderr, " juju init -w")
+ fmt.Fprintln(ctx.Stderr, "then edit the file to configure your juju environment.")
+ fmt.Fprintln(ctx.Stderr, "You can then re-run the command.")
+ return cmd.ErrSilent
+ }
+
+ return err
+}
+
func main() {
Main(os.Args)
}
« no previous file with comments | « cmd/juju/init_test.go ('k') | cmd/juju/publish.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b