Skip to content

Commit

Permalink
Desktop Notifications, the sequel (#109)
Browse files Browse the repository at this point in the history
* added logo for notification window and added notification library dependency

* Added desktop notifications

* Addressing feedback on env var and commenting

* add version clarification since we use version in the notification title

* go fmt

* added --quiet flag tied to ROG_NOTIFY_QUIET
  • Loading branch information
febbraro authored Oct 30, 2017
1 parent 301bc4a commit 91abbcc
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 3 deletions.
26 changes: 25 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@
[[constraint]]
name = "gopkg.in/yaml.v2"
branch = "v2"

[[constraint]]
name = "github.com/martinlindhe/notify"
branch = "master"

6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/urfave/cli"
)

// GoReleaser will override with the latest tag on build
var version = "master"

// It all starts here
Expand All @@ -30,6 +31,11 @@ func main() {
Usage: "Show verbose output. Learning Mode!",
EnvVar: "RIG_VERBOSE",
},
cli.BoolFlag{
Name: "quiet",
Usage: "Disable all desktop notifications",
EnvVar: "RIG_NOTIFY_QUIET",
},
}

app.Before = func(c *cli.Context) error {
Expand Down
9 changes: 9 additions & 0 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type BaseCommand struct {
RigCommand
out *util.RigLogger
machine Machine
context *cli.Context
}

// Before configure the function to run before all commands to setup core services.
Expand All @@ -27,19 +28,27 @@ func (cmd *BaseCommand) Before(c *cli.Context) error {
util.LoggerInit(c.GlobalBool("verbose"))
cmd.out = util.Logger()
cmd.machine = Machine{Name: c.GlobalString("name"), out: util.Logger()}

util.NotifyInit(fmt.Sprintf("Outrigger (rig) %s", c.App.Version))

// Hold onto Context so that we can use it later without having to pass it around everywhere
cmd.context = c

return nil
}

// Success encapsulates the functionality for reporting command success
func (cmd *BaseCommand) Success(message string) error {
if message != "" {
cmd.out.Info.Println(message)
util.NotifySuccess(cmd.context, message)
}
return nil
}

// Error encapsulates the functionality for reporting command failure
func (cmd *BaseCommand) Error(message string, errorName string, exitCode int) error {
util.NotifyError(cmd.context, message)
return cli.NewExitError(fmt.Sprintf("ERROR: %s [%s] (%d)", message, errorName, exitCode), exitCode)
}

Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '2.1'

services:

# Install project dependencies.
# Run the release process.
goreleaser:
extends: base
environment:
Expand All @@ -27,7 +27,8 @@ services:
# Install project dependencies.
install:
extends: base
command: dep ensure
entrypoint: [ "dep", "ensure" ]
command: ""

# Update project dependencies.
# Will update all if no arguments are provided. To update a particular package use:
Expand Down
Binary file added util/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions util/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package util

/**
* urfave/cli Notify "Extension"
*
* This sub-package of rig is intended for potential separation into a
* standalone package or PR to the main framework. As such, it should not have
* any built-in assumptions or identifiers about Rig, nor depend on anything in
* the rig codebase.
*/

import (
"fmt"

"github.com/martinlindhe/notify"
"github.com/urfave/cli"
)

var config *NotifyConfig

// NotifyConfig holds configuration for notification support
type NotifyConfig struct {
// The label for the app to be displayed in notifications.
Label string

// Relative path to notification logo.
Icon string
}

// NotifyInit initializes notification config
func NotifyInit(label string) error {
config = &NotifyConfig{
Icon: "util/logo.png",
Label: label,
}
return nil
}

// NotifySuccess send a notification for a successful command run
func NotifySuccess(ctx *cli.Context, message string) {
if shouldNotify(ctx) {
notify.Notify(config.Label, fmt.Sprintf("Success: %s", ctx.Command.Name), message, config.Icon)
}
}

// NotifyError send a notification for a failed command run
func NotifyError(ctx *cli.Context, message string) error {
if shouldNotify(ctx) {
notify.Notify(config.Label, fmt.Sprintf("Error: %s", ctx.Command.Name), message, config.Icon)
}
return nil
}

// shouldNotify returns a boolean if notifications are enabled
func shouldNotify(ctx *cli.Context) bool {
return !ctx.GlobalBool("quiet")
}

0 comments on commit 91abbcc

Please sign in to comment.