Skip to content

mmogylenko/flexmessage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlexMessage

Go Gosec Build Status GitHub tag

FlexMessage - Notifications in a nice way :-)

gopher

Motivation

I don't know. But why not?

The idea was followed by golang declaration syntax. When the HandleFunc function from net/http is not explicitly returning something, why I should use return statement? So I went from this

func StatusHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    if r.Method != "GET" {
        w.WriteHeader(http.StatusNotImplemented)
        w.Write([]byte(`{"message": "not implemented"}`))
        return
    }
    w.Write([]byte(`{"message": "Ok"}`))
}

to this

func StatusHandler(w http.ResponseWriter, r *http.Request) {
    var notify flexmessage.FlexMessage

    w.Header().Set("Content-Type", "application/json")
    if r.Method != "GET" {
        w.WriteHeader(http.StatusNotImplemented)
        notify.Error(r.Method + " method is not implemented")
    }
    if notify.NoErrors() {
        notify.Message("Ok")
    }
    json.NewEncoder(w).Encode(notify.Compact())
}

More complicated handler can reveal the benefits of FlexMessage package.

Key Features

  • It MAY WORK or may not
  • Simplicity is a priority
  • Colors!!! I love colors!

Installation

go get -u github.com/mmogylenko/flexmessage

Usage

package main

import (
    "fmt"
    "github.com/mmogylenko/flexmessage"
)

var notify flexmessage.FlexMessage

func main() {
    if 10 > 9 {
        notify.Message("Surprise!")
    }
    if !notify.NoMessages() {
        fmt.Println(notify.Messages)
    }
}

Customization

  • Compact() - Returns single Message/Error when len(Errors) == 1 || len(Messages) == 1
package main

import (
    "encoding/json"
    "fmt"
    "github.com/mmogylenko/flexmessage"
)

// Foo function returns an error
func Foo() error {
     return fmt.Errorf("Foo error")
}

func main() {
    var notifications flexmessage.FlexMessage

    if 10 > 0 {
        // We add our 1st Message
        notifications.Message("Very important message")
    }

    err := Foo()
    if err != nil {
        // We add our 1st Error
        notifications.Error(err.Error())
    }

    // Check if need to notify
    if !notifications.Empty() {
        fmt.Println("Output without Compact()")
        s, _ := json.MarshalIndent(notifications, "", "  ")
        fmt.Println(string(s))
        fmt.Println("Output with Compact()")
        c, _ := json.MarshalIndent(notifications.Compact(), "", "  ")
        fmt.Println(string(c))
    }
}

Result:

Output without Compact()
{
  "messages": [
    "Very important message"
  ],
  "errors": [
    "Foo error"
  ]
}
Output with Compact()
{
  "error": "Foo error",
  "message": "Very important message"
}
  • Print() - Prints messages type PrintOptions used for print options:
// PrintOptions messages print options
type PrintOptions struct {
    Colors  bool
    Compact bool
    Indent  int
}

Example of Print(): flexmesssage_colors_example

Examples

Check examples directory for a use-cases of flexmessage usage

Licensing

This project is licensed under the Apache V2 License. See LICENSE for more information.