You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
2 years ago
|
// package Database wraps some influx db methods to provide functionality.
|
||
|
package database
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
|
||
|
influx "github.com/influxdata/influxdb-client-go/v2"
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrDBConnection = errors.New("connection to database failed")
|
||
|
ErrNoURLFound = errors.New("database url not found")
|
||
|
)
|
||
|
|
||
|
var db influx.Client
|
||
|
|
||
|
// Connect takes in a config and attempts to create a client for influxdb.
|
||
|
// Will automatically write changes back to config for future attempts
|
||
|
func Connect(config *viper.Viper) error {
|
||
|
|
||
|
url := config.GetString("db.url")
|
||
|
token := config.GetString("db.token")
|
||
|
|
||
|
if url == "" {
|
||
|
return ErrNoURLFound
|
||
|
}
|
||
|
|
||
|
db = influx.NewClient(url, token)
|
||
|
|
||
|
if token == "" {
|
||
|
// try setup
|
||
|
fmt.Printf("attempting to setup database at %v\n", url)
|
||
|
|
||
|
user := config.GetString("db.username")
|
||
|
password := config.GetString("db.password")
|
||
|
org := config.GetString("db.org")
|
||
|
bucket := config.GetString("db.bucket")
|
||
|
|
||
|
Setup(user, pass, org, bucket
|
||
|
}
|
||
|
|
||
|
db = influx.NewClient(url, token)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Setup(user, pass, org, bucket string, ret int) (string, error) {
|
||
|
|
||
|
resp, err := db.Setup(context.Background(), user, pass, org, bucket, ret)
|
||
|
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
func GetBucket(id int) (string, error) {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
func GetToken(id int) (string, error) {
|
||
|
// bucket, err := client.BucketsAPI().FindBucketByName(context.Background(), id)
|
||
|
return "", nil
|
||
|
}
|