Skip to content

Commit 8b8a14d

Browse files
authored
v.2.0 (clean arch) (#52)
- Refactored the entire codebase using a clean architecture approach - Added additional end-to-end tests - Upgraded the GitHub workflow
1 parent a8881c8 commit 8b8a14d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2272
-1033
lines changed

.github/workflows/go.yml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,39 @@ on:
55
branches:
66
- master
77
paths-ignore:
8-
- '**/*.md'
9-
- 'Makefile'
8+
- "**/*.md"
9+
- "Makefile"
1010
pull_request:
1111
branches:
1212
- master
1313
paths-ignore:
14-
- '**/*.md'
15-
- 'Makefile'
14+
- "**/*.md"
15+
- "Makefile"
1616

1717
jobs:
18-
1918
build:
2019
name: Build, Test, Coverage
2120
runs-on: ubuntu-latest
2221
steps:
23-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 1
2425

25-
- name: Set up Go
26-
uses: actions/setup-go@v2
27-
with:
28-
go-version: 1.17
26+
- name: Set up Go
27+
uses: actions/setup-go@v5
28+
with:
29+
go-version: 1.21
2930

30-
- name: Lint
31-
uses: golangci/golangci-lint-action@v2
31+
- name: Lint
32+
uses: golangci/golangci-lint-action@v6
3233

33-
- name: Build
34-
run: go build -v ./...
34+
- name: Build
35+
run: go build -v ./...
3536

36-
- name: Test & Coverage
37-
run: go test -v -coverprofile=coverage.out -covermode=atomic
37+
- name: Test & Coverage
38+
run: go test -v -coverprofile=coverage.out -covermode=atomic ./...
3839

39-
- name: Upload coverage to Codecov
40-
run: bash <(curl -s https://codecov.io/bash)
40+
- name: Upload coverage to Codecov
41+
uses: codecov/codecov-action@v4.2.0
42+
env:
43+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ TODO.md
55
build
66
e2e-tests/got*.md
77
dist/
8+
.cover*

Makefile

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
EXEC=gh-md-toc
22
CMD_SRC=cmd/${EXEC}/main.go
33
BUILD_DIR=build
4-
BUILD_OS="windows darwin linux"
5-
BUILD_ARCH="amd64"
64
E2E_DIR=e2e-tests
75
E2E_RUN=go run cmd/gh-md-toc/main.go ./README.md
6+
E2E_RUN_RHTML=go run cmd/gh-md-toc/main.go https://github.com/ekalinin/github-markdown-toc.go/blob/master/README.md
7+
E2E_RUN_RMD=go run cmd/gh-md-toc/main.go https://raw.githubusercontent.com/ekalinin/github-markdown-toc.go/master/README.md
8+
bold := $(shell tput bold)
9+
clear := $(shell tput sgr0)
810

911
clean:
1012
@rm -f ${EXEC}
1113
@rm -f ${BUILD_DIR}/*
1214
@go clean
1315

1416
lint:
15-
@go vet
16-
@golangci-lint run
17+
@go vet ./...
18+
@golangci-lint run ./...
1719

1820
# make run ARGS="--help"
1921
run:
@@ -23,16 +25,36 @@ build: clean lint
2325
go build -race -o ${EXEC} ${CMD_SRC}
2426

2527
test: clean lint
26-
@go test -cover -o ${EXEC}
28+
@go test -cover ./...
29+
30+
test-cover:
31+
@go test -covermode=count -coverprofile .cover.out ./internal/... | sort
32+
@go tool cover -html .cover.out -o .coverage.html
2733

2834
e2e:
29-
echo " >> Local MD & options ..."
35+
@echo "${bold}>> 1. Local MD, with options ...${clear}"
3036
${E2E_RUN} > ${E2E_DIR}/got.md
31-
diff ${E2E_DIR}/want.md ${E2E_DIR}/got.md
37+
@diff ${E2E_DIR}/want.md ${E2E_DIR}/got.md
3238
${E2E_RUN} --hide-header --hide-footer --depth=1 --no-escape > ${E2E_DIR}/got2.md
33-
diff ${E2E_DIR}/want2.md ${E2E_DIR}/got2.md
39+
@diff ${E2E_DIR}/want2.md ${E2E_DIR}/got2.md
3440
${E2E_RUN} --hide-header --hide-footer --indent=4 > ${E2E_DIR}/got3.md
35-
diff ${E2E_DIR}/want3.md ${E2E_DIR}/got3.md
41+
@diff ${E2E_DIR}/want3.md ${E2E_DIR}/got3.md
42+
43+
@echo "${bold}>> 2. Remote MD, with options ...${clear}"
44+
${E2E_RUN_RMD} > ${E2E_DIR}/got4.md
45+
@diff ${E2E_DIR}/want.md ${E2E_DIR}/got4.md
46+
${E2E_RUN_RMD} --hide-header --hide-footer --depth=1 --no-escape > ${E2E_DIR}/got5.md
47+
@diff ${E2E_DIR}/want2.md ${E2E_DIR}/got5.md
48+
${E2E_RUN_RMD} --hide-header --hide-footer --indent=4 > ${E2E_DIR}/got6.md
49+
@diff ${E2E_DIR}/want3.md ${E2E_DIR}/got6.md
50+
51+
@echo "${bold}>> 3. Remote HTML, with options ...${clear}"
52+
${E2E_RUN_RHTML} > ${E2E_DIR}/got7.md
53+
@diff ${E2E_DIR}/want.md ${E2E_DIR}/got7.md
54+
${E2E_RUN_RHTML} --hide-header --hide-footer --depth=1 --no-escape > ${E2E_DIR}/got8.md
55+
@diff ${E2E_DIR}/want2.md ${E2E_DIR}/got8.md
56+
${E2E_RUN_RHTML} --hide-header --hide-footer --indent=4 > ${E2E_DIR}/got9.md
57+
@diff ${E2E_DIR}/want3.md ${E2E_DIR}/got9.md
3658

3759
release: test
3860
@git tag v`grep "\tVersion" internal/version.go | grep -o -E '[0-9]\.[0-9]\.[0-9]{1,2}'`

cmd/gh-md-toc/main.go

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4-
"io"
4+
"log"
55
"os"
66

77
"gopkg.in/alecthomas/kingpin.v2"
88

9-
ghtoc "github.com/ekalinin/github-markdown-toc.go"
10-
"github.com/ekalinin/github-markdown-toc.go/internal"
9+
"github.com/ekalinin/github-markdown-toc.go/internal/app"
10+
"github.com/ekalinin/github-markdown-toc.go/internal/version"
1111
)
1212

1313
var (
@@ -22,67 +22,13 @@ var (
2222
token = kingpin.Flag("token", "GitHub personal token").String()
2323
indent = kingpin.Flag("indent", "Indent space of generated list").Default("2").Int()
2424
debug = kingpin.Flag("debug", "Show debug info").Bool()
25-
ghurl = kingpin.Flag("github-url", "GitHub URL, default=https://api.github.com").String()
26-
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default(internal.GH_2024_03).String()
25+
ghurl = kingpin.Flag("github-url", "GitHub URL, default=https://api.github.com").Default("https://api.github.com").String()
26+
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default(version.GH_2024_03).String()
2727
)
2828

29-
// check if there was an error (and panic if it was)
30-
func check(e error) {
31-
if e != nil {
32-
panic(e)
33-
}
34-
}
35-
36-
func processPaths() {
37-
pathsCount := len(*paths)
38-
39-
// read file paths | urls from args
40-
absPathsInToc := pathsCount > 1
41-
ch := make(chan *ghtoc.GHToc, pathsCount)
42-
43-
for _, p := range *paths {
44-
ghdoc := ghtoc.NewGHDoc(p, absPathsInToc, *startDepth, *depth, !*noEscape, *token, *indent, *debug)
45-
ghdoc.SetGHURL(*ghurl).SetReVersion(*reVersion)
46-
47-
if *serial {
48-
ch <- ghdoc.GetToc()
49-
} else {
50-
go func(path string) { ch <- ghdoc.GetToc() }(p)
51-
}
52-
}
53-
54-
if !*hideHeader && pathsCount == 1 {
55-
internal.ShowHeader(os.Stdout)
56-
}
57-
58-
for i := 1; i <= pathsCount; i++ {
59-
toc := <-ch
60-
// #14, check if there's really TOC?
61-
if toc != nil {
62-
check(toc.Print(os.Stdout))
63-
}
64-
}
65-
}
66-
67-
func processSTDIN() {
68-
bytes, err := io.ReadAll(os.Stdin)
69-
check(err)
70-
71-
file, err := os.CreateTemp(os.TempDir(), "ghtoc")
72-
check(err)
73-
defer os.Remove(file.Name())
74-
75-
check(os.WriteFile(file.Name(), bytes, 0644))
76-
check(ghtoc.NewGHDoc(file.Name(), false, *startDepth, *depth, !*noEscape, *token, *indent, *debug).
77-
SetGHURL(*ghurl).
78-
SetReVersion(*reVersion).
79-
GetToc().
80-
Print(os.Stdout))
81-
}
82-
8329
// Entry point
8430
func main() {
85-
kingpin.Version(internal.Version)
31+
kingpin.Version(version.Version)
8632
kingpin.Parse()
8733

8834
if *token == "" {
@@ -93,13 +39,22 @@ func main() {
9339
*ghurl = os.Getenv("GH_TOC_URL")
9440
}
9541

96-
if len(*paths) > 0 {
97-
processPaths()
98-
} else {
99-
processSTDIN()
42+
cfg := app.Config{
43+
Files: *paths,
44+
Serial: *serial,
45+
HideHeader: *hideHeader,
46+
HideFooter: *hideFooter,
47+
StartDepth: *startDepth,
48+
Depth: *depth,
49+
NoEscape: *noEscape,
50+
Indent: *indent,
51+
Debug: *debug,
52+
GHToken: *token,
53+
GHUrl: *ghurl,
54+
GHVersion: *reVersion,
10055
}
10156

102-
if !*hideFooter {
103-
internal.ShowFooter(os.Stdout)
57+
if err := app.New(cfg).Run(os.Stdout); err != nil {
58+
log.Fatal(err)
10459
}
10560
}

0 commit comments

Comments
 (0)