-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
56 lines (51 loc) · 1.12 KB
/
util.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/shivylp/radium"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
func writeOut(cmd *cobra.Command, v interface{}) {
ugly, _ := cmd.Flags().GetBool("ugly")
asJSON, _ := cmd.Flags().GetBool("json")
if ugly {
rawDump(v, asJSON)
} else {
tryPrettyPrint(v)
}
}
func tryPrettyPrint(v interface{}) {
switch v.(type) {
case radium.Article, *radium.Article:
fmt.Println((v.(radium.Article)).Content)
case []radium.Article:
results := v.([]radium.Article)
if len(results) == 1 {
tryPrettyPrint(results[0])
} else {
for num, res := range results {
fmt.Printf("%d. %s [source=%s]\n", num+1, strings.TrimSpace(res.Title), res.Source)
fmt.Println(strings.Repeat("-", 20))
fmt.Println(res.Content)
fmt.Println(strings.Repeat("=", 20))
}
}
case error:
fmt.Printf("error: %s\n", v)
default:
rawDump(v, false)
}
}
func rawDump(v interface{}, asJSON bool) {
var data []byte
if asJSON {
data, _ = json.MarshalIndent(v, "", " ")
} else {
data, _ = yaml.Marshal(v)
}
os.Stdout.Write(data)
os.Stdout.Write([]byte("\n"))
}