Skip to content

Commit 9511668

Browse files
Remove go-kit/log
1 parent 429808f commit 9511668

File tree

6 files changed

+106
-664
lines changed

6 files changed

+106
-664
lines changed

go.mod

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module github.com/migueleliasweb/go-github-mock
22

3-
go 1.21
3+
go 1.23
44

55
require (
66
github.com/buger/jsonparser v1.1.1
7-
github.com/go-kit/log v0.2.1
87
github.com/google/go-github/v64 v64.0.0
98
github.com/gorilla/mux v1.8.0
9+
golang.org/x/text v0.19.0
1010
golang.org/x/time v0.3.0
1111
)
1212

13-
require (
14-
github.com/go-logfmt/logfmt v0.5.1 // indirect
15-
github.com/google/go-querystring v1.1.0 // indirect
16-
)
13+
require github.com/google/go-querystring v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
22
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
3-
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
4-
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
5-
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
6-
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
73
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
84
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
95
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -13,6 +9,8 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD
139
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
1410
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1511
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
12+
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
13+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
1614
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
1715
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
1816
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

main.go

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package main
33
import (
44
"bytes"
55
"flag"
6-
"fmt"
6+
"log/slog"
77
"os"
88
"os/exec"
99

1010
"github.com/buger/jsonparser"
1111

12-
"github.com/go-kit/log"
13-
"github.com/go-kit/log/level"
1412
"github.com/migueleliasweb/go-github-mock/src/gen"
1513
)
1614

@@ -23,37 +21,31 @@ func init() {
2321
func main() {
2422
flag.Parse()
2523

26-
var l log.Logger
27-
28-
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
29-
30-
l = log.With(l, "caller", log.DefaultCaller)
31-
32-
if debug {
33-
l = level.NewFilter(l, level.AllowDebug())
34-
level.Debug(l).Log("msg", "running in debug mode")
35-
} else {
36-
l = level.NewFilter(l, level.AllowInfo())
37-
}
38-
39-
fetchAndWriteAPIDefinition(l)
24+
fetchAndWriteAPIDefinition()
4025
}
4126

4227
// type helper just to ensure uniqueness of the generated output
4328
type uniq map[string]struct{}
4429

30+
// has will add `s` to the map whe not found
4531
func (u uniq) has(s string) bool {
46-
_, ok := u[s]
47-
return ok
48-
}
32+
_, has := u[s]
4933

50-
func fetchAndWriteAPIDefinition(l log.Logger) {
34+
if !has {
35+
u[s] = struct{}{}
36+
}
37+
38+
return has
39+
}
5140

41+
func fetchAndWriteAPIDefinition() {
5242
buf := bytes.NewBuffer([]byte(gen.OUTPUT_FILE_HEADER))
43+
5344
u := make(uniq)
45+
5446
defs := [][]byte{
55-
gen.FetchAPIDefinition(l, gen.GITHUB_OPENAPI_DEFINITION_LOCATION),
56-
gen.FetchAPIDefinition(l, gen.GITHUB_OPENAPI_ENTERPRISE_DEFINITION_LOCATION),
47+
gen.FetchAPIDefinition(gen.GITHUB_OPENAPI_DEFINITION_LOCATION),
48+
// gen.FetchAPIDefinition(gen.GITHUB_OPENAPI_ENTERPRISE_DEFINITION_LOCATION),
5749
}
5850

5951
for _, d := range defs {
@@ -75,14 +67,12 @@ func fetchAndWriteAPIDefinition(l log.Logger) {
7567

7668
for _, httpMethod := range httpMethods {
7769
code := gen.FormatToGolangVarNameAndValue(
78-
l,
7970
gen.ScrapeResult{
8071
HTTPMethod: httpMethod,
8172
EndpointPattern: endpointPattern,
8273
},
8374
)
8475
if !u.has(code) {
85-
u[code] = struct{}{}
8676
buf.WriteString(code)
8777
}
8878
}
@@ -103,13 +93,13 @@ func fetchAndWriteAPIDefinition(l log.Logger) {
10393

10494
// to catch possible format errors
10595
if err := exec.Command("gofmt", "-w", gen.OUTPUT_FILEPATH).Run(); err != nil {
106-
level.Error(l).Log("msg", fmt.Sprintf("error executing gofmt: %s", err.Error()))
96+
slog.Info("error executing gofmt", "err", err.Error())
10797
errorsFound = true
10898
}
10999

110100
// to catch everything else (hopefully)
111101
if err := exec.Command("go", "vet", "./...").Run(); err != nil {
112-
level.Error(l).Log("msg", fmt.Sprintf("error executing go vet: %s", err.Error()))
102+
slog.Info("error executing go vet", "err", err.Error())
113103
errorsFound = true
114104
}
115105

src/gen/gen.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package gen
33
import (
44
"fmt"
55
"io"
6+
"log/slog"
67
"net/http"
78
"os"
89
"regexp"
910
"strings"
1011

11-
"github.com/go-kit/log"
12-
13-
"github.com/go-kit/log/level"
12+
"golang.org/x/text/cases"
13+
"golang.org/x/text/language"
1414
)
1515

1616
const GITHUB_OPENAPI_DEFINITION_LOCATION = "https://github.com/github/rest-api-description/blob/main/descriptions/api.github.com/api.github.com.json?raw=true"
@@ -24,17 +24,21 @@ const OUTPUT_FILE_HEADER = `package mock
2424
`
2525
const OUTPUT_FILEPATH = "src/mock/endpointpattern.go"
2626

27+
// Replacing deprecated method strings.Title
28+
// requires a "Caser"
29+
var Title = cases.Title(language.English)
30+
2731
type ScrapeResult struct {
2832
HTTPMethod string
2933
EndpointPattern string
3034
}
3135

32-
func FetchAPIDefinition(l log.Logger, d string) []byte {
36+
func FetchAPIDefinition(d string) []byte {
3337
resp, err := http.Get(d)
3438

3539
if err != nil {
36-
level.Error(l).Log(
37-
"msg", "error fetching github's api definition",
40+
slog.Info(
41+
"error fetching github's api definition",
3842
"err", err.Error(),
3943
)
4044

@@ -46,8 +50,8 @@ func FetchAPIDefinition(l log.Logger, d string) []byte {
4650
bodyBytes, err := io.ReadAll(resp.Body)
4751

4852
if err != nil {
49-
level.Error(l).Log(
50-
"msg", "error fetching github's api definition",
53+
slog.Info(
54+
"error fetching github's api definition",
5155
"err", err.Error(),
5256
)
5357

@@ -59,8 +63,8 @@ func FetchAPIDefinition(l log.Logger, d string) []byte {
5963

6064
// FormatToGolangVarName generated the proper golang variable name
6165
// given a endpoint format from the API
62-
func FormatToGolangVarName(l log.Logger, sr ScrapeResult) string {
63-
result := strings.Title(strings.ToLower(sr.HTTPMethod))
66+
func FormatToGolangVarName(sr ScrapeResult) string {
67+
result := Title.String(strings.ToLower(sr.HTTPMethod))
6468

6569
if sr.EndpointPattern == "/" {
6670
return result + "Slash"
@@ -89,7 +93,7 @@ func FormatToGolangVarName(l log.Logger, sr ScrapeResult) string {
8993
splitPart := strings.Split(part, "_")
9094

9195
for _, p := range splitPart {
92-
result = result + strings.Title(p)
96+
result = result + Title.String(p)
9397
}
9498
}
9599

@@ -106,15 +110,15 @@ func FormatToGolangVarName(l log.Logger, sr ScrapeResult) string {
106110
result += "By"
107111

108112
for _, splitPart := range strings.Split(part, "_") {
109-
result += strings.Title(splitPart)
113+
result += Title.String(splitPart)
110114
}
111115
}
112116
}
113117

114118
return result
115119
}
116120

117-
func FormatToGolangVarNameAndValue(l log.Logger, sr ScrapeResult) string {
121+
func FormatToGolangVarNameAndValue(sr ScrapeResult) string {
118122
sr = applyMutation(sr)
119123

120124
return fmt.Sprintf(
@@ -123,7 +127,7 @@ func FormatToGolangVarNameAndValue(l log.Logger, sr ScrapeResult) string {
123127
Method: "%s",
124128
}
125129
`,
126-
FormatToGolangVarName(l, sr),
130+
FormatToGolangVarName(sr),
127131
sr.EndpointPattern,
128132
strings.ToUpper(sr.HTTPMethod),
129133
) + "\n"

src/gen/gen_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package gen
22

33
import (
44
"testing"
5-
6-
"github.com/go-kit/log"
75
)
86

97
func TestFormatToGolangVarName(t *testing.T) {
@@ -63,7 +61,7 @@ func TestFormatToGolangVarName(t *testing.T) {
6361
}
6462
for _, tt := range tests {
6563
t.Run(tt.name, func(t *testing.T) {
66-
if got := FormatToGolangVarName(log.NewNopLogger(), tt.sr); got != tt.want {
64+
if got := FormatToGolangVarName(tt.sr); got != tt.want {
6765
t.Errorf("formatToGolangVarName() = %v, want %v", got, tt.want)
6866
}
6967
})

0 commit comments

Comments
 (0)