diff --git a/Makefile b/Makefile index d6de7dc..c9472f3 100644 --- a/Makefile +++ b/Makefile @@ -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 ,, ) @@ -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 $@ @@ -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)" @@ -72,6 +68,4 @@ help: # Clean up clean: rm -f in/* - rm -f tmp/* - rm -f $(LOCODECLI) diff --git a/go.mod b/go.mod index 015e7a8..5ddb81a 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 2d39b6e..82c6fa0 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/internal/generate/generate.go b/internal/generate/generate.go deleted file mode 100644 index 40dc147..0000000 --- a/internal/generate/generate.go +++ /dev/null @@ -1,90 +0,0 @@ -package generate - -import ( - 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" - "github.com/spf13/cobra" -) - -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 - - locodeGenerateCmd = &cobra.Command{ - Use: "generate", - Short: "Generate UN/LOCODE database", - Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, _ []string) { - locodeDB := csvlocode.New( - csvlocode.Prm{ - Path: locodeGenerateInPaths[0], - SubDivPath: locodeGenerateSubDivPath, - }, - csvlocode.WithExtraPaths(locodeGenerateInPaths[1:]...), - ) - - airportDB := airportsdb.New(airportsdb.Prm{ - AirportsPath: locodeGenerateAirportsPath, - CountriesPath: locodeGenerateCountriesPath, - }) - - continentsDB := continentsdb.New(continentsdb.Prm{ - Path: locodeGenerateContinentsPath, - }) - - targetDB := locode.New(locodeGenerateOutPath) - - names := &namesDB{ - DB: airportDB, - Table: locodeDB, - } - - err := locode.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB) - if err != nil { - cmd.PrintErrln(err) - } - }, - } -) - -func initUtilLocodeGenerateCmd() { - flags := locodeGenerateCmd.Flags() - - flags.StringSliceVar(&locodeGenerateInPaths, locodeGenerateInputFlag, nil, "List of paths to UN/LOCODE tables (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateInputFlag) - - flags.StringVar(&locodeGenerateSubDivPath, locodeGenerateSubDivFlag, "", "Path to UN/LOCODE subdivision database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateSubDivFlag) - - flags.StringVar(&locodeGenerateAirportsPath, locodeGenerateAirportsFlag, "", "Path to OpenFlights airport database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateAirportsFlag) - - flags.StringVar(&locodeGenerateCountriesPath, locodeGenerateCountriesFlag, "", "Path to OpenFlights country database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateCountriesFlag) - - flags.StringVar(&locodeGenerateContinentsPath, locodeGenerateContinentsFlag, "", "Path to continent polygons (GeoJSON)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateContinentsFlag) - - flags.StringVar(&locodeGenerateOutPath, locodeGenerateOutputFlag, "", "Target path for generated database (directory))") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateOutputFlag) -} diff --git a/internal/generate/main.go b/internal/generate/main.go new file mode 100644 index 0000000..4599e83 --- /dev/null +++ b/internal/generate/main.go @@ -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") + } + } + locodeGenerateInPaths = append(locodeGenerateInPaths, splitStr...) + return nil + }) + 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))") +} + +func main() { + flag.Parse() + + if err := validateFlags(); err != nil { + log.Fatal(err) + } + + locodeDB := csvlocode.New( + csvlocode.Prm{ + Path: locodeGenerateInPaths[0], + SubDivPath: locodeGenerateSubDivPath, + }, + csvlocode.WithExtraPaths(locodeGenerateInPaths[1:]...), + ) + + airportDB := airportsdb.New(airportsdb.Prm{ + AirportsPath: locodeGenerateAirportsPath, + CountriesPath: locodeGenerateCountriesPath, + }) + + continentsDB := continentsdb.New(continentsdb.Prm{ + Path: locodeGenerateContinentsPath, + }) + + targetDB := locode.New(locodeGenerateOutPath) + + names := &namesDB{ + DB: airportDB, + Table: locodeDB, + } + + err := locode.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB) + if err != nil { + log.Fatal(err) + } +} + +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") + } + return nil +} diff --git a/internal/generate/root.go b/internal/generate/root.go deleted file mode 100644 index dedc41e..0000000 --- a/internal/generate/root.go +++ /dev/null @@ -1,30 +0,0 @@ -package generate - -import ( - "github.com/spf13/cobra" -) - -// rootCmd represents the base command when called without any subcommands. -var rootCmd = &cobra.Command{ - Use: "locode-db", - Short: "UN/LOCODE database CLI", - Long: `locode db CLI is a tool for working with UN/LOCODE database.`, - Args: cobra.NoArgs, - Run: entryPoint, -} - -func init() { - rootCmd.AddCommand(locodeGenerateCmd) - initUtilLocodeGenerateCmd() -} - -func entryPoint(cmd *cobra.Command, _ []string) { - _ = cmd.Usage() -} - -func Execute() { - err := rootCmd.Execute() - if err != nil { - rootCmd.PrintErrln(err) - } -} diff --git a/internal/main.go b/internal/main.go deleted file mode 100644 index 32ccbc7..0000000 --- a/internal/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "github.com/nspcc-dev/locode-db/internal/generate" -) - -func main() { - generate.Execute() -}