Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extended last parameters #30

Merged
merged 3 commits into from Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
211 changes: 0 additions & 211 deletions gen.go

This file was deleted.

100 changes: 100 additions & 0 deletions main.go
@@ -0,0 +1,100 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"

"github.com/buger/jsonparser"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/migueleliasweb/go-github-mock/src/gen"
)

var debug bool

func init() {
flag.BoolVar(&debug, "debug", false, "output debug information")
}

func main() {
flag.Parse()

var l log.Logger

l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))

l = log.With(l, "caller", log.DefaultCaller)

if debug {
l = level.NewFilter(l, level.AllowDebug())
level.Debug(l).Log("msg", "running in debug mode")
} else {
l = level.NewFilter(l, level.AllowInfo())
}

apiDefinition := gen.FetchAPIDefinition(l)

buf := bytes.NewBuffer([]byte(gen.OUTPUT_FILE_HEADER))

jsonparser.ObjectEach(
apiDefinition,
func(key, endpointDefinition []byte, _ jsonparser.ValueType, _ int) error {
endpointPattern := string(key)

httpMethods := []string{}

jsonparser.ObjectEach(
endpointDefinition,
func(key, _ []byte, _ jsonparser.ValueType, _ int) error {
httpMethods = append(httpMethods, string(key))

return nil
},
)

for _, httpMethod := range httpMethods {
code := gen.FormatToGolangVarNameAndValue(
l,
gen.ScrapeResult{
HTTPMethod: httpMethod,
EndpointPattern: endpointPattern,
},
)

buf.WriteString(code)
}

return nil
},
"paths",
)

ioutil.WriteFile(
gen.OUTPUT_FILEPATH,
buf.Bytes(),
0755,
)

errorsFound := false

// to catch possible format errors
if err := exec.Command("gofmt", "-w", "src/mock/endpointpattern.go").Run(); err != nil {
level.Error(l).Log("msg", fmt.Sprintf("error executing gofmt: %s", err.Error()))
errorsFound = true
}

// to catch everything else (hopefully)
if err := exec.Command("go", "vet", "./...").Run(); err != nil {
level.Error(l).Log("msg", fmt.Sprintf("error executing go vet: %s", err.Error()))
errorsFound = true
}

if errorsFound {
os.Exit(1)
}
}