-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhr.go
74 lines (62 loc) · 1.24 KB
/
hr.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
package main
import (
"bufio"
"flag"
"fmt"
hrlib "github.com/dustin/go-humanize"
"os"
"strconv"
"strings"
)
const version = "1.0.0"
func main() {
v := flag.Bool("v", false, "Print out the version")
h := flag.Bool("h", false, "Print usage")
flag.Parse()
if *v {
printVersion()
os.Exit(1)
}
if *h {
printUsage()
os.Exit(1)
}
info, _ := os.Stdin.Stat()
if (info.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
printUsage()
os.Exit(1)
} else if info.Size() > 0 {
reader := bufio.NewReader(os.Stdin)
humanize(reader)
}
}
func humanize(r *bufio.Reader) {
input, err := r.ReadString('\n')
check(err)
input = strings.Trim(input, "\n")
val, err := strconv.ParseUint(input, 10, 64)
check(err)
// Formating
hr := hrlib.Bytes(val)
hr = strings.Replace(hr, "B", "", -1)
hr = strings.Replace(hr, " ", "", -1)
fmt.Println(hr)
}
func printUsage() {
printVersion()
fmt.Println("The command is intended to work with pipes.")
fmt.Println("Usage:")
fmt.Println(" echo 500000 | hr")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" -h show help")
fmt.Println(" -v show version")
}
func printVersion() {
fmt.Printf("hr version %s\n\n", version)
}
func check(err error) {
if err != nil {
panic(err)
}
}