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 3 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
26 changes: 10 additions & 16 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,11 +33,8 @@ 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 \
--airports in/airports.dat \
--continents in/continents.geojson \
--countries in/countries.dat \
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.

30 changes: 0 additions & 30 deletions internal/generate/root.go

This file was deleted.

109 changes: 107 additions & 2 deletions internal/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,114 @@
package main

import (
"github.com/nspcc-dev/locode-db/internal/generate"
"errors"
"flag"
"fmt"
"log"

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
}

type stringSliceFlag []string

func (s *stringSliceFlag) String() string {
return fmt.Sprint(*s)

Check warning on line 23 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L22-L23

Added lines #L22 - L23 were not covered by tests
}

func (s *stringSliceFlag) Set(value string) error {
if value == "" {
return errors.New("value is empty")

Check warning on line 28 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L26-L28

Added lines #L26 - L28 were not covered by tests
}
*s = append(*s, value)
return nil

Check warning on line 31 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}

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

var (
locodeGenerateInPaths stringSliceFlag
locodeGenerateSubDivPath string
locodeGenerateAirportsPath string
locodeGenerateCountriesPath string
locodeGenerateContinentsPath string
locodeGenerateOutPath string
)

func init() {
flag.Var(&locodeGenerateInPaths, locodeGenerateInputFlag, "List of paths to UN/LOCODE tables (CSV)")
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 58 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L52-L58

Added lines #L52 - L58 were not covered by tests
}

func main() {
generate.Execute()
flag.Parse()

Check warning on line 62 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L62

Added line #L62 was not covered by tests

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

Check warning on line 65 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

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

Check warning on line 74 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L68-L74

Added lines #L68 - L74 were not covered by tests

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

Check warning on line 79 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L76-L79

Added lines #L76 - L79 were not covered by tests

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

Check warning on line 83 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L81-L83

Added lines #L81 - L83 were not covered by tests

targetDB := locode.New(locodeGenerateOutPath)

Check warning on line 85 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L85

Added line #L85 was not covered by tests

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

Check warning on line 89 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L87-L89

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

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

Check warning on line 94 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L92-L94

Added lines #L92 - L94 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 111 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L98-L111

Added lines #L98 - L111 were not covered by tests
}
return nil

Check warning on line 113 in internal/main.go

View check run for this annotation

Codecov / codecov/patch

internal/main.go#L113

Added line #L113 was not covered by tests
}
Loading