Fixed #147: If an error occurs Glide exists with a non-0 exit code
diff --git a/glide.go b/glide.go
index e7900a8..eb3b61b 100644
--- a/glide.go
+++ b/glide.go
@@ -40,6 +40,7 @@
"path/filepath"
"github.com/Masterminds/glide/action"
+ "github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/repo"
@@ -111,7 +112,18 @@
app.Before = startup
app.Commands = commands()
- app.Run(os.Args)
+ // Detect errors from the Before and After calls and exit on them.
+ if err := app.Run(os.Args); err != nil {
+ msg.Error(err.Error())
+ os.Exit(1)
+ }
+
+ // If there was a Error message exit non-zero.
+ if msg.HasErrored() {
+ m := msg.Color(msg.Red, "An Error has occured")
+ msg.Msg(m)
+ os.Exit(2)
+ }
}
func commands() []cli.Command {
diff --git a/msg/msg.go b/msg/msg.go
index 880afb5..7c9f705 100644
--- a/msg/msg.go
+++ b/msg/msg.go
@@ -33,6 +33,9 @@
// The default exit code to use when dyping
ecode int
+
+ // If an error was been sent.
+ hasErrored bool
}
// NewMessanger creates a default Messanger to display output.
@@ -96,6 +99,7 @@
func (m *Messanger) Error(msg string, args ...interface{}) {
prefix := m.Color(Red, "[ERROR] ")
m.Msg(prefix+msg, args...)
+ m.hasErrored = true
}
// Error logs and error using the Default Messanger
@@ -212,3 +216,25 @@
func Print(msg string) {
Default.Print(msg)
}
+
+// HasErrored returns if Error has been called.
+//
+// This is useful if you want to known if Error was called to exit with a
+// non-zero exit code.
+func (m *Messanger) HasErrored() bool {
+ return m.hasErrored
+}
+
+// HasErrored returns if Error has been called on the Default Messanger.
+//
+// This is useful if you want to known if Error was called to exit with a
+// non-zero exit code.
+func HasErrored() bool {
+ return Default.HasErrored()
+}
+
+// Color returns a string in a certain color if colors are enabled and
+// available on that platform.
+func Color(code, msg string) string {
+ return Default.Color(code, msg)
+}