2019-08-20 19:37:45 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2019-08-20 19:37:45 +00:00
|
|
|
database "github.com/go-flucky/flucky/pkg/db"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-09-01 10:27:06 +00:00
|
|
|
var (
|
|
|
|
configFile *string
|
|
|
|
|
|
|
|
version *semver.Version
|
|
|
|
)
|
2019-08-20 19:37:45 +00:00
|
|
|
|
|
|
|
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
|
2019-09-01 10:27:06 +00:00
|
|
|
func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) {
|
2019-08-20 19:37:45 +00:00
|
|
|
configFile = cnfFile
|
2019-09-01 10:27:06 +00:00
|
|
|
version = sversion
|
2019-08-20 19:37:45 +00:00
|
|
|
|
|
|
|
cmd.AddCommand(dbCmd)
|
|
|
|
|
|
|
|
}
|