Skip to content

Commit

Permalink
Show steps and errors on console
Browse files Browse the repository at this point in the history
  • Loading branch information
krakowski committed Oct 30, 2020
1 parent b787564 commit 5db474f
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 14 deletions.
10 changes: 10 additions & 0 deletions api/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package rocket

// The last error returned from the RocketChat API
var LastError Error

type Error struct {
Status string `json:"status"`
Error string `json:"status"`
Message string `json:"status"`
}
1 change: 1 addition & 0 deletions api/rocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewClient(client *http.Client, options ClientOptions) (*Client, error) {
}

// Configure the created resty client
restyClient.SetError(&LastError)
restyClient.SetHostURL(options.ServerUrl)
restyClient.SetHeaders(map[string]string{
"Accept": "application/json",
Expand Down
37 changes: 30 additions & 7 deletions cmd/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"bytes"
"fmt"
rocket "github.com/krakowski/rocket/api"
"github.com/krakowski/rocket/util"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"log"
"os"
"path"
"strings"
Expand All @@ -32,37 +32,60 @@ var notifyCommand = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

spin := util.StartSpinner("Parsing template file")

// Parse the template file
tmpl, err := template.ParseFiles(path.Join(directory, args[0]+".yml"))
if err != nil {
log.Fatal(err)
spin.StopError(err)
os.Exit(1)
}

// Read environment variables
values, err := createTemplateValues()
if err != nil {
log.Fatal(err)
spin.StopError(err)
os.Exit(1)
}

// Execute the template
var out bytes.Buffer
if err = tmpl.Execute(&out, values); err != nil {
log.Fatal(err)
spin.StopError(err)
os.Exit(1)
}

// Decode the yaml output
var payload rocket.MessagePayload
if err = yaml.NewDecoder(bytes.NewReader(out.Bytes())).Decode(&payload); err != nil {
log.Fatal(err)
spin.StopError(err)
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)

spin = util.StartSpinner("Authenticating with RocketChat")

// Create a new RocketChat client
client := util.NewRocketClient()
client, err := util.NewRocketClient()
if err != nil {
spin.StopError(err)
fmt.Printf(" - %s\n", rocket.LastError.Message)
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)

spin = util.StartSpinner("Sending notification to channel " + payload.Channel)

// Post the message
if err = client.Message.Post(payload); err != nil {
log.Fatal(err)
spin.StopError(err)
fmt.Printf(" - %s\n", rocket.LastError.Message)
os.Exit(1)
}

spin.StopSuccess(util.NoMessage)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
version = "1.0.0"
version = "1.0.1"
)

var rootCommand = &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/krakowski/rocket
go 1.14

require (
github.com/briandowns/spinner v1.11.1
github.com/go-resty/resty/v2 v2.3.0
github.com/spf13/cobra v1.1.1
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/briandowns/spinner v1.11.1 h1:OixPqDEcX3juo5AjQZAnFPbeUA0jvkp2qzB5gOZJ/L0=
github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand All @@ -32,9 +34,11 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -108,7 +112,11 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand All @@ -126,7 +134,9 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -161,15 +171,19 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -227,6 +241,7 @@ golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/krakowski/rocket/cmd"
import (
"github.com/krakowski/rocket/cmd"
)

func main() {
cmd.Execute()
Expand Down
10 changes: 5 additions & 5 deletions util/client.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package util

import (
"fmt"
rocket "github.com/krakowski/rocket/api"
"log"
"os"
)

const (
envHost = "ROCKET_HOST"
)

func NewRocketClient() (*rocket.Client) {
func NewRocketClient() (*rocket.Client, error) {
serverUrl, present := os.LookupEnv(envHost)
if !present {
log.Fatal("Please specify the RocketChat server url in the ROCKET_HOST environment variable")
return nil, fmt.Errorf("ROCKET_HOST environment variable missing")
}

credentials := GetCredentials()
Expand All @@ -23,8 +23,8 @@ func NewRocketClient() (*rocket.Client) {
})

if err != nil {
log.Fatal(err)
return nil, err
}

return client
return client, nil
}
16 changes: 16 additions & 0 deletions util/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import "fmt"

var (
Red = Color("\033[1;31m%s\033[0m")
Green = Color("\033[1;32m%s\033[0m")
)

func Color(colorString string) func(...interface{}) string {
sprint := func(args ...interface{}) string {
return fmt.Sprintf(colorString,
fmt.Sprint(args...))
}
return sprint
}
50 changes: 50 additions & 0 deletions util/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package util

import (
"fmt"
"github.com/briandowns/spinner"
"os"
"time"
)

type Spinner struct {
spinner *spinner.Spinner
}

const (
IconSuccess = "✓"
IconError = "✘"

NoMessage = ""
)

func StartSpinner(title string) *Spinner {
spin := spinner.New(spinner.CharSets[14], 100 * time.Millisecond, spinner.WithWriter(os.Stderr))
spin.Suffix = " " + title
spin.HideCursor = true
spin.Start()
return &Spinner{spinner: spin}
}

func (spin *Spinner) StopSuccessRemove() {
spin.spinner.Stop()
}

func (spin *Spinner) StopSuccess(message string) {
spin.spinner.Stop()

if message == NoMessage {
fmt.Fprintf(os.Stderr, "%s%s\n", Green(IconSuccess), spin.spinner.Suffix)
} else {
fmt.Fprintf(os.Stderr, "%s%s - %s\n", Green(IconSuccess), spin.spinner.Suffix, message)
}
}

func (spin *Spinner) StopError(err error) {
spin.spinner.Stop()
fmt.Fprintf(os.Stderr, "%s%s - %s\n", Red(IconError), spin.spinner.Suffix, Red("(" + err.Error() + ")"))
}

func (spin *Spinner) UpdateMessage(message string) {
spin.spinner.Suffix = " " + message
}

0 comments on commit 5db474f

Please sign in to comment.