From bfe1c41407edef485b37afaaa42fedd8bad6d716 Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Wed, 21 Aug 2024 18:52:14 +0300 Subject: [PATCH 1/6] locode-db: drop cobra dependency Use default flag package instead of cobra. Closes #34. Signed-off-by: Andrey Butusov --- go.mod | 3 - go.sum | 8 --- internal/generate/generate.go | 127 ++++++++++++++++++++-------------- internal/generate/root.go | 30 -------- 4 files changed, 76 insertions(+), 92 deletions(-) delete mode 100644 internal/generate/root.go 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 index 40dc147..ba204d4 100644 --- a/internal/generate/generate.go +++ b/internal/generate/generate.go @@ -1,11 +1,14 @@ package generate import ( + "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" - "github.com/spf13/cobra" ) type namesDB struct { @@ -13,6 +16,17 @@ type namesDB struct { *csvlocode.Table } +type stringSliceFlag []string + +func (s *stringSliceFlag) String() string { + return fmt.Sprint(*s) +} + +func (s *stringSliceFlag) Set(value string) error { + *s = append(*s, value) + return nil +} + const ( locodeGenerateInputFlag = "in" locodeGenerateSubDivFlag = "subdiv" @@ -23,68 +37,79 @@ const ( ) var ( - locodeGenerateInPaths []string + locodeGenerateInPaths stringSliceFlag 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 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))") +} + +func Execute() { + flag.Parse() + + if err := validateFlags(); err != nil { + log.Fatal(err) } -) -func initUtilLocodeGenerateCmd() { - flags := locodeGenerateCmd.Flags() + locodeDB := csvlocode.New( + csvlocode.Prm{ + Path: locodeGenerateInPaths[0], + SubDivPath: locodeGenerateSubDivPath, + }, + csvlocode.WithExtraPaths(locodeGenerateInPaths[1:]...), + ) - flags.StringSliceVar(&locodeGenerateInPaths, locodeGenerateInputFlag, nil, "List of paths to UN/LOCODE tables (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateInputFlag) + airportDB := airportsdb.New(airportsdb.Prm{ + AirportsPath: locodeGenerateAirportsPath, + CountriesPath: locodeGenerateCountriesPath, + }) - flags.StringVar(&locodeGenerateSubDivPath, locodeGenerateSubDivFlag, "", "Path to UN/LOCODE subdivision database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateSubDivFlag) + continentsDB := continentsdb.New(continentsdb.Prm{ + Path: locodeGenerateContinentsPath, + }) - flags.StringVar(&locodeGenerateAirportsPath, locodeGenerateAirportsFlag, "", "Path to OpenFlights airport database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateAirportsFlag) + targetDB := locode.New(locodeGenerateOutPath) - flags.StringVar(&locodeGenerateCountriesPath, locodeGenerateCountriesFlag, "", "Path to OpenFlights country database (CSV)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateCountriesFlag) + names := &namesDB{ + DB: airportDB, + Table: locodeDB, + } - flags.StringVar(&locodeGenerateContinentsPath, locodeGenerateContinentsFlag, "", "Path to continent polygons (GeoJSON)") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateContinentsFlag) + err := locode.FillDatabase(locodeDB, airportDB, continentsDB, names, targetDB) + if err != nil { + log.Fatal(err) + } +} - flags.StringVar(&locodeGenerateOutPath, locodeGenerateOutputFlag, "", "Target path for generated database (directory))") - _ = locodeGenerateCmd.MarkFlagRequired(locodeGenerateOutputFlag) +func validateFlags() error { + for _, inPath := range locodeGenerateInPaths { + if inPath == "" { + return fmt.Errorf("no UN/LOCODE table specified") + } + } + switch { + case len(locodeGenerateInPaths) == 0: + return fmt.Errorf("at least one UN/LOCODE table is required") + case locodeGenerateSubDivPath == "": + return fmt.Errorf("path to UN/LOCODE subdivision database is required") + case locodeGenerateAirportsPath == "": + return fmt.Errorf("path to OpenFlights airport database is required") + case locodeGenerateCountriesPath == "": + return fmt.Errorf("path to OpenFlights country database is required") + case locodeGenerateContinentsPath == "": + return fmt.Errorf("path to continent polygons is required") + case locodeGenerateOutPath == "": + return fmt.Errorf("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) - } -} From 61a066c30e738fda0f295ed2f1eb0351caf24c6b Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Wed, 21 Aug 2024 19:02:28 +0300 Subject: [PATCH 2/6] Makefile: use go run instead of binary and cleanup file Now the binary code is not created, but just runs through go run. Also removed unnecessary things and improved the code style. Signed-off-by: Andrey Butusov --- Makefile | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index d6de7dc..0b4fdec 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,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 \ @@ -72,6 +68,4 @@ help: # Clean up clean: rm -f in/* - rm -f tmp/* - rm -f $(LOCODECLI) From e4d654a4922a60ad83deb2b9fe192d219396884e Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Thu, 22 Aug 2024 13:53:29 +0300 Subject: [PATCH 3/6] locode-db: move generate to main file Move all from generate.go to main.go and make Execute main function. Signed-off-by: Andrey Butusov --- internal/generate/generate.go | 115 ---------------------------------- internal/main.go | 109 +++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 117 deletions(-) delete mode 100644 internal/generate/generate.go diff --git a/internal/generate/generate.go b/internal/generate/generate.go deleted file mode 100644 index ba204d4..0000000 --- a/internal/generate/generate.go +++ /dev/null @@ -1,115 +0,0 @@ -package generate - -import ( - "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) -} - -func (s *stringSliceFlag) Set(value string) error { - *s = append(*s, value) - return nil -} - -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))") -} - -func Execute() { - 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 { - for _, inPath := range locodeGenerateInPaths { - if inPath == "" { - return fmt.Errorf("no UN/LOCODE table specified") - } - } - switch { - case len(locodeGenerateInPaths) == 0: - return fmt.Errorf("at least one UN/LOCODE table is required") - case locodeGenerateSubDivPath == "": - return fmt.Errorf("path to UN/LOCODE subdivision database is required") - case locodeGenerateAirportsPath == "": - return fmt.Errorf("path to OpenFlights airport database is required") - case locodeGenerateCountriesPath == "": - return fmt.Errorf("path to OpenFlights country database is required") - case locodeGenerateContinentsPath == "": - return fmt.Errorf("path to continent polygons is required") - case locodeGenerateOutPath == "": - return fmt.Errorf("target path for generated database is required") - } - return nil -} diff --git a/internal/main.go b/internal/main.go index 32ccbc7..69fd503 100644 --- a/internal/main.go +++ b/internal/main.go @@ -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) +} + +func (s *stringSliceFlag) Set(value string) error { + if value == "" { + return errors.New("value is empty") + } + *s = append(*s, value) + return nil +} + +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))") +} + func main() { - generate.Execute() + 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 } From dfba02adc37d16dc14a24a88859d3c9879dab2ab Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Fri, 23 Aug 2024 12:20:32 +0300 Subject: [PATCH 4/6] locode-db: use flag.Func instead of flag.Var Use flag.Func to obtain string slice instead of flag.Var. Signed-off-by: Andrey Butusov --- internal/main.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/internal/main.go b/internal/main.go index 69fd503..4599e83 100644 --- a/internal/main.go +++ b/internal/main.go @@ -3,8 +3,8 @@ package main import ( "errors" "flag" - "fmt" "log" + "strings" locode "github.com/nspcc-dev/locode-db/internal/parsers/db" airportsdb "github.com/nspcc-dev/locode-db/internal/parsers/db/airports" @@ -17,20 +17,6 @@ type namesDB struct { *csvlocode.Table } -type stringSliceFlag []string - -func (s *stringSliceFlag) String() string { - return fmt.Sprint(*s) -} - -func (s *stringSliceFlag) Set(value string) error { - if value == "" { - return errors.New("value is empty") - } - *s = append(*s, value) - return nil -} - const ( locodeGenerateInputFlag = "in" locodeGenerateSubDivFlag = "subdiv" @@ -41,7 +27,7 @@ const ( ) var ( - locodeGenerateInPaths stringSliceFlag + locodeGenerateInPaths []string locodeGenerateSubDivPath string locodeGenerateAirportsPath string locodeGenerateCountriesPath string @@ -50,7 +36,16 @@ var ( ) func init() { - flag.Var(&locodeGenerateInPaths, locodeGenerateInputFlag, "List of paths to UN/LOCODE tables (CSV)") + 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)") From bbdac1d82f07d520b2351e7f44d18a77c351c1bf Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Fri, 23 Aug 2024 12:22:46 +0300 Subject: [PATCH 5/6] Makefile: add `;` to go run Resolve problem with specific os PATH. Signed-off-by: Andrey Butusov --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0b4fdec..18cb1b2 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ generate: in/airports.dat in/countries.dat in/continents.geojson in/SubdivisionC --countries in/countries.dat \ --in in/CodeList.csv \ --subdiv in/SubdivisionCodes.csv \ - --out $(LOCODEDB) + --out $(LOCODEDB); compress_locodedb: generate @echo "⇒ Compressing files inside $(LOCODEDB)" From 66eb9715b7ad480585e0cc1a953b5fa9dea343cd Mon Sep 17 00:00:00 2001 From: Andrey Butusov Date: Fri, 23 Aug 2024 15:16:42 +0300 Subject: [PATCH 6/6] locode-db: move main.go to internal/generate Signed-off-by: Andrey Butusov --- Makefile | 2 +- internal/{ => generate}/main.go | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename internal/{ => generate}/main.go (100%) diff --git a/Makefile b/Makefile index 18cb1b2..c9472f3 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ in/CodeList.csv: | in awk 'NR>1' $@ > temp && mv temp $@ generate: in/airports.dat in/countries.dat in/continents.geojson in/SubdivisionCodes.csv in/CodeList.csv | $(LOCODEDB) - go run ./internal \ + go run ./internal/generate/ \ --airports in/airports.dat \ --continents in/continents.geojson \ --countries in/countries.dat \ diff --git a/internal/main.go b/internal/generate/main.go similarity index 100% rename from internal/main.go rename to internal/generate/main.go