-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (68 loc) · 1.51 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/shopspring/iblameyou/internal"
"gopkg.in/yaml.v2"
)
const v = "0.1.1"
var (
version = flag.Bool("version", false, "print version and exit")
config = flag.String("config",
os.Getenv("HOME")+"/.iblameyou.yaml",
"path to configuration file")
)
type Config struct {
Format internal.Format
Source internal.Source
}
func DefaultConfig() (c Config) {
c.Format.Colors = internal.DefaultPalette()
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Stderr = ioutil.Discard
if repo, err := cmd.Output(); err == nil {
c.Source.Repository = strings.TrimSpace(string(repo))
}
return c
}
func main() {
flag.Parse()
if *version {
fmt.Println(v)
os.Exit(0)
}
cfg := DefaultConfig()
if b, err := ioutil.ReadFile(*config); err == nil {
if err := yaml.Unmarshal(b, &cfg); err != nil {
log.Fatalf("Failed to unmarshal config:\n%s", err)
}
}
if cfg.Source.Repository == "" {
log.Fatal("Repository not provided and not in a Git repository.")
}
log.Println("Reading message from stdin...")
message, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
ui := internal.UI{}
err = ui.Init(&cfg.Format)
if err != nil {
log.Fatalf("Failed to initialize the UI:\n%s", err)
}
defer ui.Close()
go func() {
dump, err := cfg.Source.ParseDump(bytes.NewReader(message))
if err != nil {
log.Fatalf("Failed to parse dump:\n%s", err)
}
ui.RenderDump(dump)
}()
ui.Loop()
}