I'm building a CLI app that imports some data (e.g. from a db or CSV, XML) and exports it to some other format (e.g. to a db or file). An importer and exporter is specified when the app is started as input parameters to the app. I'm looking for some inspiration on how to structure the files/packages.
At the moment, I have separated each importer and exporter in its own file. Each importer satisfies a general importer interface:
type Importer interface {
Import() ([]*data, error)
PreProcess() error
PostProcess() error
}
So, for instance, CSVImporter
would implement these 3 functions. Likewise for the exporters. Currently all importers and exporters are part of the main package together with the main file. I think this makes the most sense, as they are part of the app, and should not be used in other projects. Also, since I have a lot of config settings (some general and some for each importer and exporter), that are unmarshalled in the beginning of my main package, I believe my importers and exporters should be part of the same package, in order for me to use the settings there.
package main
type ConfigFile struct {
Environment string
Resources *Resources
Importers *Importers
Exporters *Exporters
...
}
func main() {
cfg ConfigFile
// Load config file settings
err := viper.Unmarshal(&cfg)
var i StockImporter
var e StockExporter
// Create selected importer.
switch viper.GetString("importer") {
case "csv-importer":
i = NewCSVImporter()
...
}
}
My current file structure looks like the following:
app
|- main.go
|- importer.go
|- csvimporter.go
|- xmlimporter.go
|- exporter.go
|- csvexporter.go
| - lib
| - db.go
I hope my current structure is described clearly. My question is: Are there better ways to structure my app? Specifically, I dont like having all the files in the same dir as main. Any suggestions? Thanks