This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprbox.go
131 lines (109 loc) · 3.26 KB
/
sprbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package sprbox is an agnostic config parser
// (supporting YAML, TOML, JSON and Environment vars)
// and a toolbox factory with automatic configuration
// based on your build environment.
package sprbox
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// small slant
const banner = `
__
___ ___ ____/ / ___ __ __
(_-</ _ \/ __/ _ / _ \\ \ /
/___/ .__/_/ /_.__\___/_\_\ %s
env/_/aware toolbox factory
`
const (
// struct field tag key
sftKey = "sprbox"
)
var (
debug = false
verbose = false // only settable manually
// coloredLogs enable or disable colors in console.
coloredLogs = true
// fileSearchCaseSensitive determine config files search mode.
fileSearchCaseSensitive = true
)
func init() {
//coloredLogs = isTerminal(os.Stdout.Fd())
}
// isTerminal return true if the file descriptor is terminal.
//func isTerminal(fd uintptr) bool {
// var termios syscall.Termios
// _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TIOCGETA, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
// return err == 0
//}
// SetDebug will print detailed logs in console.
func SetDebug(enabled bool) {
debug = enabled
}
func debugPrintf(format string, args ...interface{}) {
if debug {
fmt.Printf(format, args...)
}
}
// verbose mode can only be set here
func verbosePrintf(format string, args ...interface{}) {
if verbose {
fmt.Printf(format, args...)
}
}
func dump(v interface{}) string {
// To marshal directly with yaml produce a panic with unexported fields
jd, err := json.Marshal(v)
if err != nil {
//fmt.Printf("dump err on %+v: %v\n", v, err)
return fmt.Sprintf("%+v", v)
}
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err = yaml.Unmarshal(jd, &jsonObj)
if err != nil {
//fmt.Printf("dump err on %+v: %v\n", v, err)
return fmt.Sprintf("%+v", v)
}
// Marshal this object into YAML.
yd, err := yaml.Marshal(jsonObj)
if err != nil {
//fmt.Printf("dump err on %+v: %v\n", v, err)
return fmt.Sprintf("%+v", v)
}
return string(yd)
//b, _ := json.MarshalIndent(v, "", " ")
//return string(b)+"\n"
}
// PrintInfo print some useful info about the environment and git.
func PrintInfo() {
version := ""
sprboxRepo := NewRepository(filepath.Join(os.Getenv("GOPATH"), "/src/github.com/oblq/sprbox"))
if sprboxRepo.Error == nil {
version = "v" + sprboxRepo.Tag + "(" + sprboxRepo.Build + ")"
}
fmt.Printf(darkGrey(banner), version)
Env().PrintInfo()
VCS.PrintInfo()
}
// GetInfo print info in console from an http request.
func GetInfo(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "\n%s%s\n%s\n", banner, Env().Info(), VCS.Info())
}
// SetColoredLogs toggle colors in console.
func SetColoredLogs(colored bool) {
coloredLogs = colored
}
// SetFileSearchCaseSensitive toggle case sensitive cinfig files search.
func SetFileSearchCaseSensitive(caseSensitive bool) {
fileSearchCaseSensitive = caseSensitive
}