This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
forked from rgburke/grv
/
main.go
126 lines (103 loc) · 3.07 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
)
const (
mnRepoFilePathDefault = "."
mnWorkTreeFilePathDefault = ""
mnLogFilePathDefault = "grv.log"
// MnGenerateDocumentationEnv is set when documentation should be generated
MnGenerateDocumentationEnv = "GRV_GENERATE_DOCUMENTATION"
// MnLogLevelDefault is the default log level for grv
MnLogLevelDefault = "NONE"
)
var (
version = "Unknown"
buildDateTime = "Unknown"
)
type grvArgs struct {
repoFilePath string
workTreeFilePath string
logLevel string
logFilePath string
version bool
readOnly bool
}
func main() {
args := parseArgs()
if args.version {
printVersion()
return
}
if os.Getenv(MnGenerateDocumentationEnv) != "" {
if err := GenerateDocumentation(); err != nil {
log.Fatalf("Failed to generate documentation: %v", err)
} else {
fmt.Printf("Generated documentation\n")
}
return
}
InitialiseLogging(args.logLevel, args.logFilePath)
log.Info(getVersion())
log.Debugf("Creating GRV instance")
grv := NewGRV(args.readOnly)
if err := grv.Initialise(args.repoFilePath, args.workTreeFilePath); err != nil {
fmt.Fprintf(os.Stderr, "FATAL: Unable to initialise grv: %v\n", err)
grv.Free()
log.Fatal(err)
}
grv.Run()
grv.Free()
log.Info("Exiting normally")
}
func parseArgs() *grvArgs {
repoFilePathPtr := flag.String("repoFilePath", mnRepoFilePathDefault, "Repository file path")
workTreeFilePathPtr := flag.String("workTreeFilePath", mnWorkTreeFilePathDefault, "Work tree file path")
logLevelPtr := flag.String("logLevel", MnLogLevelDefault, "Logging level [NONE|PANIC|FATAL|ERROR|WARN|INFO|DEBUG]")
logFilePathPtr := flag.String("logFile", mnLogFilePathDefault, "Log file path")
versionPtr := flag.Bool("version", false, "Print version")
readOnlyPtr := flag.Bool("readOnly", false, "Run grv in read only mode")
flag.Parse()
return &grvArgs{
repoFilePath: *repoFilePathPtr,
workTreeFilePath: *workTreeFilePathPtr,
logLevel: *logLevelPtr,
logFilePath: *logFilePathPtr,
version: *versionPtr,
readOnly: *readOnlyPtr,
}
}
func getVersion() string {
return fmt.Sprintf("GRV - Git Repository Viewer %v (compiled with %v at %v)", version, runtime.Version(), buildDateTime)
}
func printVersion() {
fmt.Printf("%v\n", getVersion())
}
// GenerateCommandLineArgumentsHelpSections generates help documentation for command line arguments
func GenerateCommandLineArgumentsHelpSections() *HelpSection {
description := []HelpSectionText{
{text: "GRV accepts the following command line arguments:"},
{},
}
var buffer bytes.Buffer
flag.CommandLine.SetOutput(&buffer)
flag.CommandLine.PrintDefaults()
scanner := bufio.NewScanner(&buffer)
for scanner.Scan() {
description = append(description, HelpSectionText{
text: strings.TrimLeft(scanner.Text(), " "),
themeComponentID: CmpHelpViewSectionCodeBlock,
})
}
return &HelpSection{
title: HelpSectionText{text: "Command Line Arguments"},
description: description,
}
}