feat(pkg/db): postgres database

This commit is contained in:
2019-08-20 21:37:45 +02:00
parent c0aa155f36
commit 60ee044b88
53 changed files with 2269 additions and 81 deletions

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"math"
"os"
"time"
@ -34,12 +35,19 @@ var rootCmd = &cobra.Command{
return fmt.Errorf("Can not locate the hostname: %v", err)
}
// Time must be truncted for postgres
// Postgres currently does not support nanoseconds which is automatically
// include into the go time object
t := time.Now()
l, _ := time.LoadLocation("Europe/Berlin")
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l)
cnf := config.Configuration{
Device: &types.Device{
DeviceID: uuid.NewV4().String(),
DeviceName: hostname,
Logfile: "/var/log/flucky/logfile.csv",
CreationDate: time.Now(),
CreationDate: t,
},
}
@ -62,6 +70,7 @@ func Execute(version string) {
compression.InitCmd(rootCmd, &configFile)
convert.InitCmd(rootCmd, &configFile)
daemon.InitCmd(rootCmd, &configFile)
// db.InitCmd(rootCmd, &configFile)
humidity.InitCmd(rootCmd, &configFile)
pressure.InitCmd(rootCmd, &configFile)
rgbled.InitCmd(rootCmd, &configFile)

49
cmd/db/db.go Normal file
View File

@ -0,0 +1,49 @@
package db
import (
"context"
"log"
database "github.com/go-flucky/flucky/pkg/db"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra"
)
var configFile *string
var dbCmd = &cobra.Command{
Use: "db",
Short: "Operates with the configured database",
Run: func(cmd *cobra.Command, args []string) {
postgresDB, err := database.New(database.DBOTypePostgres, "localhost", "5432", "postgres", "postgres", "postgres")
if err != nil {
log.Fatalf("%v", err)
}
ctx := context.Background()
devices := []*types.Device{
&types.Device{
DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
DeviceName: "raspberr-pi",
},
&types.Device{
DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
DeviceName: "raspberr-pi",
},
}
if err := postgresDB.InsertDevices(ctx, devices); err != nil {
log.Fatalln(err)
}
},
}
// Execute a
func InitCmd(cmd *cobra.Command, cnfFile *string) {
configFile = cnfFile
cmd.AddCommand(dbCmd)
}