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

Drop cobra dependency #42

Merged
merged 6 commits into from
Aug 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/make -f

VERSION ?= "$(shell git describe --tags --match "v*" --dirty --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop")"
LOCODECLI ?= locode-db
LOCODEDB ?= pkg/locodedb/data
UNLOCODEREVISION = 3648bfa776701c329d27136bef29fb3e21853f20

.PHONY: all clean version help unlocode generate $(LOCODECLI)
.PHONY: all clean version help generate

DIRS = in tmp ${LOCODEDB}
DIRS = in ${LOCODEDB}

space := $(subst ,, )

Expand All @@ -17,14 +16,14 @@ $(DIRS):
@echo "⇒ Ensure dir: $@"
@mkdir -p $@

in/airports.dat:
wget -c https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat -O in/airports.dat
in/airports.dat: | in
wget -c https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat -O $@

in/countries.dat:
wget -c https://raw.githubusercontent.com/jpatokal/openflights/master/data/countries.dat -O in/countries.dat
in/countries.dat: | in
wget -c https://raw.githubusercontent.com/jpatokal/openflights/master/data/countries.dat -O $@

in/continents.geojson: continents.geojson.gz
gunzip -c $< > in/continents.geojson
in/continents.geojson: continents.geojson.gz | in
gunzip -c $< > $@

in/SubdivisionCodes.csv: | in
wget -c https://raw.githubusercontent.com/datasets/un-locode/${UNLOCODEREVISION}/data/subdivision-codes.csv -O $@
Expand All @@ -34,17 +33,14 @@ in/CodeList.csv: | in
wget -c https://raw.githubusercontent.com/datasets/un-locode/${UNLOCODEREVISION}/data/code-list.csv -O $@
awk 'NR>1' $@ > temp && mv temp $@

$(LOCODECLI):
go build -o $(LOCODECLI) ./internal

generate: in/airports.dat in/countries.dat in/continents.geojson in/SubdivisionCodes.csv in/CodeList.csv $(LOCODECLI) | $(LOCODEDB)
./$(LOCODECLI) generate \
generate: in/airports.dat in/countries.dat in/continents.geojson in/SubdivisionCodes.csv in/CodeList.csv | $(LOCODEDB)
go run ./internal/generate/ \
--airports in/airports.dat \
--continents in/continents.geojson \
--countries in/countries.dat \
--in in/CodeList.csv \
--subdiv in/SubdivisionCodes.csv \
--out $(LOCODEDB)
--out $(LOCODEDB);

compress_locodedb: generate
@echo "⇒ Compressing files inside $(LOCODEDB)"
Expand Down Expand Up @@ -72,6 +68,4 @@ help:
# Clean up
clean:
rm -f in/*
rm -f tmp/*
rm -f $(LOCODECLI)

3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ go 1.21

require (
github.com/paulmach/orb v0.11.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.14.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.mongodb.org/mongo-driver v1.11.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -8,8 +7,6 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
Expand All @@ -25,11 +22,6 @@ github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKf
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
90 changes: 0 additions & 90 deletions internal/generate/generate.go

This file was deleted.

109 changes: 109 additions & 0 deletions internal/generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"errors"
"flag"
"log"
"strings"

locode "github.com/nspcc-dev/locode-db/internal/parsers/db"
airportsdb "github.com/nspcc-dev/locode-db/internal/parsers/db/airports"
continentsdb "github.com/nspcc-dev/locode-db/internal/parsers/db/continents/geojson"
csvlocode "github.com/nspcc-dev/locode-db/internal/parsers/table/csv"
)

type namesDB struct {
*airportsdb.DB
*csvlocode.Table
}

const (
locodeGenerateInputFlag = "in"
locodeGenerateSubDivFlag = "subdiv"
locodeGenerateAirportsFlag = "airports"
locodeGenerateCountriesFlag = "countries"
locodeGenerateContinentsFlag = "continents"
locodeGenerateOutputFlag = "out"
)

var (
locodeGenerateInPaths []string
locodeGenerateSubDivPath string
locodeGenerateAirportsPath string
locodeGenerateCountriesPath string
locodeGenerateContinentsPath string
locodeGenerateOutPath string
)

func init() {
flag.Func(locodeGenerateInputFlag, "List of paths to UN/LOCODE tables (CSV)", func(s string) error {
splitStr := strings.Split(s, ",")
for _, path := range splitStr {
if path == "" {
return errors.New("path is empty")

Check warning on line 43 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L38-L43

Added lines #L38 - L43 were not covered by tests
}
}
locodeGenerateInPaths = append(locodeGenerateInPaths, splitStr...)
return nil

Check warning on line 47 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L46-L47

Added lines #L46 - L47 were not covered by tests
})
flag.StringVar(&locodeGenerateSubDivPath, locodeGenerateSubDivFlag, "", "Path to UN/LOCODE subdivision database (CSV)")
flag.StringVar(&locodeGenerateAirportsPath, locodeGenerateAirportsFlag, "", "Path to OpenFlights airport database (CSV)")
flag.StringVar(&locodeGenerateCountriesPath, locodeGenerateCountriesFlag, "", "Path to OpenFlights country database (CSV)")
flag.StringVar(&locodeGenerateContinentsPath, locodeGenerateContinentsFlag, "", "Path to continent polygons (GeoJSON)")
flag.StringVar(&locodeGenerateOutPath, locodeGenerateOutputFlag, "", "Target path for generated database (directory))")

Check warning on line 53 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L49-L53

Added lines #L49 - L53 were not covered by tests
}

func main() {
flag.Parse()

Check warning on line 57 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L56-L57

Added lines #L56 - L57 were not covered by tests

if err := validateFlags(); err != nil {
log.Fatal(err)

Check warning on line 60 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

locodeDB := csvlocode.New(
csvlocode.Prm{
Path: locodeGenerateInPaths[0],
SubDivPath: locodeGenerateSubDivPath,
},
csvlocode.WithExtraPaths(locodeGenerateInPaths[1:]...),
)

Check warning on line 69 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L63-L69

Added lines #L63 - L69 were not covered by tests

airportDB := airportsdb.New(airportsdb.Prm{
AirportsPath: locodeGenerateAirportsPath,
CountriesPath: locodeGenerateCountriesPath,
})

Check warning on line 74 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L71-L74

Added lines #L71 - L74 were not covered by tests

continentsDB := continentsdb.New(continentsdb.Prm{
Path: locodeGenerateContinentsPath,
})

Check warning on line 78 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L76-L78

Added lines #L76 - L78 were not covered by tests

targetDB := locode.New(locodeGenerateOutPath)

Check warning on line 80 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L80

Added line #L80 was not covered by tests

names := &namesDB{
DB: airportDB,
Table: locodeDB,

Check warning on line 84 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L82-L84

Added lines #L82 - L84 were not covered by tests
}

err := locode.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB)
if err != nil {
log.Fatal(err)

Check warning on line 89 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L87-L89

Added lines #L87 - L89 were not covered by tests
}
}

func validateFlags() error {
switch {
case len(locodeGenerateInPaths) == 0:
return errors.New("at least one UN/LOCODE table is required")
case locodeGenerateSubDivPath == "":
return errors.New("path to UN/LOCODE subdivision database is required")
case locodeGenerateAirportsPath == "":
return errors.New("path to OpenFlights airport database is required")
case locodeGenerateCountriesPath == "":
return errors.New("path to OpenFlights country database is required")
case locodeGenerateContinentsPath == "":
return errors.New("path to continent polygons is required")
case locodeGenerateOutPath == "":
return errors.New("target path for generated database is required")

Check warning on line 106 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L93-L106

Added lines #L93 - L106 were not covered by tests
}
return nil

Check warning on line 108 in internal/generate/main.go

View check run for this annotation

Codecov / codecov/patch

internal/generate/main.go#L108

Added line #L108 was not covered by tests
}
30 changes: 0 additions & 30 deletions internal/generate/root.go

This file was deleted.

9 changes: 0 additions & 9 deletions internal/main.go

This file was deleted.

Loading