-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit be63528
Showing
7 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rijdendetreinen/treinarchief-dumper/db" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var dumpCmd = &cobra.Command{ | ||
Use: "dump", | ||
Short: "Dump", | ||
} | ||
|
||
var dumpDayCmd = &cobra.Command{ | ||
Use: "day", | ||
Short: "Dump a single day", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
database := db.CreateDB() | ||
|
||
db.Dump(database, args[0], args[0]) | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(dumpCmd) | ||
dumpCmd.AddCommand(dumpDayCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
var verbose bool | ||
|
||
// Version contains the version information | ||
var Version VersionInformation | ||
|
||
// VersionInformation is a simple struct containing the version information | ||
type VersionInformation struct { | ||
Version string | ||
Commit string | ||
Date string | ||
} | ||
|
||
// VersionStringLong returns a version string | ||
func (v VersionInformation) VersionStringLong() string { | ||
return fmt.Sprintf("%v (%v; built %v)", v.Version, v.Commit, v.Date) | ||
} | ||
|
||
// VersionStringShort returns a shortened version string | ||
func (v VersionInformation) VersionStringShort() string { | ||
return fmt.Sprintf("%v (%v)", v.Version, v.Commit) | ||
} | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "treinarchief-dumper", | ||
Short: "Dump het RdT treinarchief naar een CSV bestand", | ||
Long: "Dump het RdT treinarchief naar een CSV bestand", | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := RootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") | ||
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
viper.SetConfigName("config") | ||
|
||
viper.AddConfigPath("./config") | ||
viper.AddConfigPath("/etc/alerdt/") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
log.WithField("file", viper.ConfigFileUsed()).Info("Using config file:") | ||
} | ||
|
||
log.Debug("Configuration loaded") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
!.gitignore | ||
!.example-config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// CreateDB create a database connection | ||
func CreateDB() *sql.DB { | ||
log.Info("Connecting to database") | ||
log.WithField("dsn", viper.GetString("database.dsn")).Debug("Connecting...") | ||
|
||
db, err := sql.Open("mysql", viper.GetString("database.dsn")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// See "Important settings" section. | ||
db.SetConnMaxLifetime(time.Minute * 30) | ||
db.SetMaxOpenConns(3) | ||
db.SetMaxIdleConns(3) | ||
|
||
return db | ||
} | ||
|
||
// SelectAllDevices selects all devices (without errors) | ||
func Dump(db *sql.DB, startDate, endDate string) error { | ||
serviceRows, err := db.Query("SELECT id FROM service WHERE service_date >= ? AND service_date <= ?", startDate, endDate) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer serviceRows.Close() | ||
|
||
for serviceRows.Next() { | ||
var serviceID int | ||
|
||
if err := serviceRows.Scan(&serviceID); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(serviceID) | ||
|
||
stopRows, err := db.Query("SELECT id, stop_code, stop_name FROM stop WHERE service_id = ? ORDER BY stop_index", serviceID) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer stopRows.Close() | ||
|
||
for stopRows.Next() { | ||
var stopID int | ||
var stopCode, stopName string | ||
|
||
if err := stopRows.Scan(&stopID, &stopCode, &stopName); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(" ", stopID, stopCode) | ||
|
||
} | ||
} | ||
|
||
if err := serviceRows.Err(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module github.com/rijdendetreinen/treinarchief-dumper | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/go-sql-driver/mysql v1.7.1 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.9 // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/spf13/afero v1.9.5 // indirect | ||
github.com/spf13/cast v1.5.1 // indirect | ||
github.com/spf13/cobra v1.7.0 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/spf13/viper v1.16.0 // indirect | ||
github.com/subosito/gotenv v1.6.0 // indirect | ||
golang.org/x/sys v0.11.0 // indirect | ||
golang.org/x/text v0.12.0 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.