Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 81ed64734823f3b3290867489c158752320f8b7e [file] [log] [blame]
Máximo Cuadros39678122016-12-12 14:50:031package examples
2
3import (
Máximo Cuadros26095382017-02-01 08:12:524 "fmt"
Máximo Cuadros39678122016-12-12 14:50:035 "os"
6 "strings"
Máximo Cuadros39678122016-12-12 14:50:037)
8
Anthony HAMON44c364f2017-12-01 15:27:529// CheckArgs should be used to ensure the right command line arguments are
marwan-at-work2b503ad2017-04-06 19:57:4810// passed before executing an example.
Máximo Cuadros39678122016-12-12 14:50:0311func CheckArgs(arg ...string) {
12 if len(os.Args) < len(arg)+1 {
13 Warning("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
14 os.Exit(1)
15 }
16}
17
marwan-at-worke0b296d2017-04-06 20:25:2918// CheckIfError should be used to naively panics if an error is not nil.
Máximo Cuadros39678122016-12-12 14:50:0319func CheckIfError(err error) {
20 if err == nil {
21 return
22 }
23
Máximo Cuadros26095382017-02-01 08:12:5224 fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
Máximo Cuadros39678122016-12-12 14:50:0325 os.Exit(1)
26}
27
marwan-at-work2b503ad2017-04-06 19:57:4828// Info should be used to describe the example commands that are about to run.
Máximo Cuadros39678122016-12-12 14:50:0329func Info(format string, args ...interface{}) {
Máximo Cuadros26095382017-02-01 08:12:5230 fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
Máximo Cuadros39678122016-12-12 14:50:0331}
32
marwan-at-work2b503ad2017-04-06 19:57:4833// Warning should be used to display a warning
Máximo Cuadros39678122016-12-12 14:50:0334func Warning(format string, args ...interface{}) {
Máximo Cuadros26095382017-02-01 08:12:5235 fmt.Printf("\x1b[36;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
Máximo Cuadros39678122016-12-12 14:50:0336}