-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
85 lines (71 loc) · 1.74 KB
/
status.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
package main
import (
"errors"
"log"
"net/http"
"os"
"github.com/fatih/color"
)
const (
version = " version 0.1"
)
var (
l = log.New(os.Stdout, "", 0)
err = errors.New("HTTP status code not found")
)
// HTTPStatusCodes all http status codes
type HTTPStatusCodes struct{}
// HTTPStatusCode Represents an HTTPStatusCode
type HTTPStatusCode struct {
statusCode int
message string
description string
color color.Attribute
err error
}
// LookUp returns an HTTPStatusCode with
// any lookup error, nil if the lookup
// found something, "unknown" if nothing
// found
func (HTTPStatusCodes) LookUp(code int, verbose bool) (*HTTPStatusCode, error) {
switch http.StatusText(code) {
case "":
return &HTTPStatusCode{color: color.FgRed}, err
default:
if verbose {
return &HTTPStatusCode{
statusCode: code,
message: statusMessage(code),
description: statusDescription(code),
color: statusColor(code),
}, nil
}
return &HTTPStatusCode{
statusCode: code,
message: statusMessage(code),
color: statusColor(code),
}, nil
}
}
// Describe gives description for some HTTPStatusCode
func (HTTPStatusCodes) Describe(code *HTTPStatusCode) {
color.Set(color.Bold, code.color)
defer color.Unset()
l.Printf(" %d - %s", code.statusCode, code.message)
if code.description != "" {
l.Println(code.description)
}
}
// ListAll all HTTPStatusCodes
func (HTTPStatusCodes) ListAll() {
for code := range StatusCodes {
c := HTTPStatusCode{statusCode: code, message: statusMessage(code)}
l.Printf("%d - %s", c.statusCode, c.message)
}
}
// DescribeErr lookup err
func (HTTPStatusCodes) DescribeErr(hsc *HTTPStatusCode) {
color.Set(color.Bold, hsc.color)
defer color.Unset()
l.Println(hsc.err)
}