Skip to content

Commit

Permalink
adds manage.sh.
Browse files Browse the repository at this point in the history
linted
  • Loading branch information
lsh-0 committed Oct 8, 2023
1 parent 4c1b728 commit 3b0fe2f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
api-raml/
*article-json/
*article-json*/
validate-article-json
linux-amd64*
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module validate-article-json

go 1.20
go 1.21

require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIK
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.15.0 h1:5n/pM+v3r5ujuNl4YLZLsQ+UE5jlkLVm7jMzT5Mpolw=
github.com/tidwall/gjson v1.15.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand All @@ -22,3 +23,4 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
47 changes: 34 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -48,7 +50,7 @@ func configure_validator() map[string]Foo {
schema_map := map[string]Foo{}
for label, path := range schema_file_list {
rdr, err := loader(path)
panic_on_err(err, fmt.Sprintf("loading '%s' schema file: ", label, path))
panic_on_err(err, fmt.Sprintf("loading '%s' schema file: %s", label, path))
err = c.AddResource(label, rdr)
panic_on_err(err, "adding schema to compiler: "+label)
schema, err := c.Compile(label)
Expand Down Expand Up @@ -190,40 +192,59 @@ func long_validation_error(err error) {
fmt.Printf("%#v\n", err)
}

func die(b bool, msg string) {
if b {
fmt.Println(msg)
os.Exit(1)
}
}

func main() {
var err error
args := os.Args[1:]

// required first argument is path to an xml document or a directory of xml.
die(len(args) < 1, "path to article-json file or directory of files required")
input_path := args[0]
die(!path_exists(input_path), "input path does not exist")

// optional second argument is sample size
sample_size := 1000

if !path_exists(input_path) {
panic("input path does not exist")
if len(args) == 2 {
sample_size, err = strconv.Atoi(args[1])
die(err != nil, "second argument is not an integer. use -1 for 'all' articles. default is 1000.")
}

if path_is_dir(input_path) {
// validate many
path_list, err := os.ReadDir(input_path)
panic_on_err(err, "reading contents of directory: "+input_path)
sample_size := 1000
if len(args) == 2 {
// optional second argument is sample size
sample_size, err = strconv.Atoi(args[1])
panic_on_err(err, "converting sample size to an integer")
}

if sample_size == -1 || sample_size > len(path_list) {
// validate all files in dir
sample_size = len(path_list)
}

// filter directories from path listing
// filter any directories from path listing
file_list := []string{}
for _, path := range path_list[:sample_size] {
if !path.IsDir() {
file_list = append(file_list, input_path+path.Name())
file_list = append(file_list, filepath.Join(input_path, path.Name()))
}
}

slices.Sort(file_list) // sort strings
slices.Reverse(file_list) // DESC

capture_errors := false
num_workers := 10 // todo: set to num cpus
num_workers := 1
if runtime.NumCPU() > 2 {
// on local machine with 12 cores average validation time on first
// 1k articles improves from ~450ms to ~350ms at 10 cores.
// feel free to tweak/remove.
num_workers = runtime.NumCPU() - 2
}
p := pool.NewWithResults[Result]().WithMaxGoroutines(num_workers)

start_time := time.Now()
Expand Down Expand Up @@ -267,7 +288,7 @@ func main() {
}

} else {
// assume file or a link pointing to a file, validate single
// validate single
capture_errors := true
result := validate_article(input_path, capture_errors)
if !result.Success {
Expand Down
57 changes: 57 additions & 0 deletions manage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
set -e

cmd="$1"

if test ! "$cmd"; then
echo "command required."
echo
echo "available commands:"
echo " build build project"
echo " release build project for distribution"
echo " update-deps update project dependencies"
exit 1
fi

shift
rest=$*

if test "$cmd" = "build"; then
# CGO_ENABLED=0 skips CGO and linking against glibc to build static binaries.
# -v 'verbose'
CGO_ENABLED=0 go build \
-v
exit 0

elif test "$cmd" = "release"; then
# GOOS is 'Go OS' and is being explicit in which OS to build for.
# CGO_ENABLED=0 skips CGO and linking against glibc to build static binaries.
# ld -s is 'disable symbol table'
# ld -w is 'disable DWARF generation'
# -trimpath removes leading paths to source files
# -v 'verbose'
# -o 'output'
GOOS=linux CGO_ENABLED=0 go build \
-ldflags="-s -w" \
-trimpath \
-v \
-o linux-amd64
sha256sum linux-amd64 > linux-amd64.sha256
echo ---
echo "wrote linux-amd64"
echo "wrote linux-amd64.sha256"
exit 0

elif test "$cmd" = "update-deps"; then
# -u 'update modules [...] to use newer minor or patch releases when available'
go get -u
go mod tidy
./manage.sh build
exit 0

# ...

fi

echo "unknown command: $cmd"
exit 1

0 comments on commit 3b0fe2f

Please sign in to comment.