commit
message
Allowing for aboslute and relative directories
author
Ben Vogt <[email protected]>
date
2023-04-05 17:46:12
stats
3 file(s) changed,
44 insertions(+),
20 deletions(-)
files
Makefile
README.md
main.go
1diff --git a/Makefile b/Makefile
2index 085fefb..40a9cf8 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -21,24 +21,24 @@ build: Makefile target target/output target/gshr.bin
6
7 dev: Makefile target target/output target/gshr.bin
8 ./target/gshr.bin \
9- --config=$(PWD)/gshr.toml \
10- --output=$(PWD)/target/output \
11+ -c=gshr.toml \
12+ -o=target/output \
13 && \
14 cd $(PWD)/target/output && \
15 python3 -m http.server 8000
16
17 dev-example-go-git: Makefile target target/output target/gshr.bin
18 ./target/gshr.bin \
19- --config=$(PWD)/examples/go-git.toml \
20- --output=$(PWD)/target/output \
21+ -c=$(PWD)/examples/go-git.toml \
22+ -c=$(PWD)/target/output \
23 && \
24 cd $(PWD)/target/output && \
25 python3 -m http.server 8000
26
27 dev-example-gshr: Makefile target target/output target/gshr.bin
28 ./target/gshr.bin \
29- --config=$(PWD)/examples/ghsr-simple.toml \
30- --output=$(PWD)/target/output \
31+ -c=$(PWD)/examples/ghsr-simple.toml \
32+ -o=$(PWD)/target/output \
33 && \
34 cd $(PWD)/target/output && \
35 python3 -m http.server 8000
36diff --git a/README.md b/README.md
37index 1849107..865a397 100644
38--- a/README.md
39+++ b/README.md
40@@ -32,12 +32,9 @@ See more examples in [tree/master/examples](tree/master/examples).
41
42 ```
43 Usage of gshr:
44- -config string
45- Config file.
46- -output string
47- Dir of output.
48- -silent
49- Run in silent mode.
50+ -c Config file.
51+ -o Dir of output.
52+ -s Run in silent mode.
53 ```
54
55 The toml file needs to be in the format:
56diff --git a/main.go b/main.go
57index 58493a4..4d2f8c3 100644
58--- a/main.go
59+++ b/main.go
60@@ -10,6 +10,7 @@ import (
61 "os"
62 "os/exec"
63 "path"
64+ "strings"
65
66 "github.com/alecthomas/chroma/formatters/html"
67 "github.com/alecthomas/chroma/lexers"
68@@ -56,14 +57,28 @@ func Init() {
69 log.SetOutput(new(LogWriter))
70 args = DefaultCmdArgs()
71 settings = DefaultSettings()
72- flag.StringVar(&args.ConfigFile, "config", "", "Config file.")
73- flag.StringVar(&args.OutputDir, "output", "", "Dir of output.")
74- flag.BoolVar(&args.Silent, "silent", false, "Run in silent mode.")
75+ pwd, err := os.Getwd()
76+ checkErr(err)
77+ args.Wd = pwd
78+ flag.StringVar(&args.ConfigPath, "c", "", "Config file.")
79+ flag.StringVar(&args.OutputDir, "o", "", "Dir of output.")
80+ flag.BoolVar(&args.Silent, "s", false, "Run in silent mode.")
81 flag.Parse()
82+ debug("working dir '%v'", args.Wd)
83+
84+ if !strings.HasPrefix(args.ConfigPath, "/") {
85+ args.ConfigPath = path.Join(args.Wd, args.ConfigPath)
86+ checkFile(args.ConfigPath)
87+ }
88
89- debug("config '%v'", args.ConfigFile)
90+ if !strings.HasPrefix(args.OutputDir, "/") {
91+ args.OutputDir = path.Join(args.Wd, args.OutputDir)
92+ checkDir(args.OutputDir)
93+ }
94+
95+ debug("config '%v'", args.ConfigPath)
96 debug("output '%v'", args.OutputDir)
97- configFileBytes, err := os.ReadFile(args.ConfigFile)
98+ configFileBytes, err := os.ReadFile(args.ConfigPath)
99 configString := string(configFileBytes)
100 checkErr(err)
101 config = ParseConfiguration(configString)
102@@ -129,6 +144,16 @@ func checkErr(err error) {
103 }
104 }
105
106+func checkFile(filename string) {
107+ _, err := os.Stat(filename)
108+ checkErr(err)
109+}
110+
111+func checkDir(dir string) {
112+ _, err := os.ReadDir(dir)
113+ checkErr(err)
114+}
115+
116 func debug(format string, a ...any) {
117 if !args.Silent {
118 log.Printf("DEBUG: "+format, a...)
119@@ -158,14 +183,15 @@ func highlight(pathOrExtension string, data *string) string {
120
121 type CmdArgs struct {
122 Silent bool
123- ConfigFile string
124+ Wd string
125+ ConfigPath string
126 OutputDir string
127 }
128
129 func DefaultCmdArgs() CmdArgs {
130 return CmdArgs{
131 Silent: true,
132- ConfigFile: "",
133+ ConfigPath: "",
134 OutputDir: "",
135 }
136 }