-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (91 loc) · 2.53 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/alecholmes/tdiff/app"
"github.com/alecholmes/tdiff/importer"
)
var (
// Required input flags
packageFlag = flag.String("package", "", "Package to find reachable diff from")
shaFlag = flag.String("sha", "", "Git SHA after which changes will be considered (exclusive)")
// Optional flags
artifactsFlag = flag.Bool("artifacts", false, "If true, includes changed non-Go source files under the package directory, recursive")
verboseFlag = flag.Bool("verbose", false, "If set, log verbose debugging information")
// Output format flags
packagesFlag = flag.Bool("packages", false, "If set, all relevant changed packages printed")
filesFlag = flag.Bool("files", false, "If set, all relevant changed files are printed")
commitsFlag = flag.Bool("commits", false, "If set, all relevant commits are printed")
jsonFlag = flag.Bool("json", false, "If set, JSON object representing all changes is printed")
htmlFlag = flag.Bool("html", false, "If set, an HTML summary is written to a temp file")
)
func main() {
flag.Parse()
if len(*packageFlag) == 0 || len(*shaFlag) == 0 {
flag.Usage()
os.Exit(1)
}
includePaths := *jsonFlag || *htmlFlag
logger := app.NoLogging
if *verboseFlag {
logger = log.Printf
}
differ := app.NewDiffer(os.Getenv("GOPATH"), importer.DefaultRecursiveImport, *commitsFlag, includePaths, logger)
summary, err := differ.Diff(*packageFlag, *shaFlag, *artifactsFlag)
if err != nil {
log.Fatal(err)
}
if *packagesFlag {
for _, pkg := range summary.Packages {
fmt.Println(pkg.ImportPath)
}
}
if *filesFlag {
for _, file := range summary.Files {
fmt.Println(file)
}
}
if *commitsFlag {
for _, commit := range summary.Commits {
fmt.Printf("%s %s\n", commit.SHA, commit.Description)
}
}
if *jsonFlag {
body, err := json.MarshalIndent(summary, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
if *htmlFlag {
fileName, err := writeHTML(summary)
if err != nil {
log.Fatal(err)
}
fmt.Println(fileName)
}
}
func writeHTML(summary *app.Summary) (string, error) {
body, err := app.HTML(summary)
if err != nil {
return "", err
}
file, err := ioutil.TempFile("", summary.SHA)
if err != nil {
return "", err
}
defer file.Close()
if _, err := file.Write(body); err != nil {
return "", err
}
file.Close()
newName := fmt.Sprintf("%s.html", file.Name())
if err := os.Rename(file.Name(), newName); err != nil {
return "", err
}
return newName, nil
}