tests working, need to get uis going now
This commit is contained in:
parent
048940ef7a
commit
68e74f2335
50
data/data.go
50
data/data.go
@ -41,8 +41,8 @@ func ReadHosts() []string {
|
|||||||
return pgee
|
return pgee
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns a value from the .pgm.yaml file
|
// handles interacting with the config file.
|
||||||
func ViperReturnKey(k string ) string {
|
func ViperPgmConfig(k string, a string) (bool, string) {
|
||||||
var h string
|
var h string
|
||||||
var s string
|
var s string
|
||||||
h, err := os.UserHomeDir()
|
h, err := os.UserHomeDir()
|
||||||
@ -50,17 +50,47 @@ func ViperReturnKey(k string ) string {
|
|||||||
logger.Logger("[ERROR] could not set home directory: " + err.Error())
|
logger.Logger("[ERROR] could not set home directory: " + err.Error())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
viper.SetConfigName("scr")
|
viper.SetConfigName(".pgm")
|
||||||
viper.SetConfigType("json")
|
viper.SetConfigType("yaml")
|
||||||
|
viper.AddConfigPath(h)
|
||||||
viper.AddConfigPath(h + "/.pgm/scn/")
|
|
||||||
err = viper.ReadInConfig() // Find and read the config file
|
err = viper.ReadInConfig() // Find and read the config file
|
||||||
if err != nil { // Handle errors reading the config file
|
if err != nil { // Handle errors reading the config file
|
||||||
logger.Logger("[ERROR] failed to load viper config: " + err.Error())
|
logger.Logger("[ERROR] failed to load viper config: " + err.Error())
|
||||||
fmt.Println("error occurred and was logged.")
|
fmt.Println("error occurred and was logged.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
s = viper.GetString(k)
|
// r returns values, may add an action to update or set values later if it makes sense
|
||||||
logger.Logger("[LOG] got value from .pgm.yaml: " + s )
|
switch a {
|
||||||
return s
|
case "r":
|
||||||
|
if viper.IsSet(k) {
|
||||||
|
s = viper.GetString(k)
|
||||||
|
logger.Logger("[LOG] got value from .pgm.yaml: key = " + k + ", value = " + s)
|
||||||
|
return true, s
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
default:
|
||||||
|
logger.Logger("[LOG] invalid action passed to Viper return key. use \"r\" for return")
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func ViperScrConfig(filename string, defaults map[string]interface{}) (*viper.Viper, error){
|
||||||
|
v := viper.New()
|
||||||
|
for key, value := range defaults {
|
||||||
|
v.SetDefault(key, value)
|
||||||
|
}
|
||||||
|
v.SetConfigName(filename)
|
||||||
|
v.AddConfigPath( "/home/jms/.pgm/scn")
|
||||||
|
v.AutomaticEnv()
|
||||||
|
err := v.ReadInConfig()
|
||||||
|
return v, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type ViewCfg struct {
|
||||||
|
q string `mapstructure:"query"`
|
||||||
|
c int `mapstructure:"column"`
|
||||||
|
datatypes string `mapstructure:"datatypes"`
|
||||||
|
uiT bool `mapstructure:"uiType"`
|
||||||
|
t string `mapstructure:"title"`
|
||||||
}
|
}
|
||||||
|
|||||||
12
db/db.go
12
db/db.go
@ -20,12 +20,20 @@ func DbConn(c data.HostDetails) *sql.DB {
|
|||||||
u = c.Username
|
u = c.Username
|
||||||
pw = c.Secret
|
pw = c.Secret
|
||||||
dbn = c.DatabaseName
|
dbn = c.DatabaseName
|
||||||
|
|
||||||
|
logger.Logger("[LOG] connection details: host = " + h + " username = " + u)
|
||||||
|
|
||||||
|
found, testUserName := data.ViperPgmConfig("test_user", "r")
|
||||||
|
if found == true {
|
||||||
|
logger.Logger("[LOG] test requires containerized database or custom \"test_db\" in .pgm.yaml")
|
||||||
|
} else {
|
||||||
|
logger.Logger("[LOG] no reserved username found in logfile, reset with a key \"test_user\"")
|
||||||
|
logger.Logger("[LOG] test requires containerized database or custom \"test_db\" in .pgm.yaml")
|
||||||
|
}
|
||||||
psqlInfo = fmt.Sprintf("host=%s port=%d user=%s "+
|
psqlInfo = fmt.Sprintf("host=%s port=%d user=%s "+
|
||||||
"password=%s dbname=%s sslmode=disable",
|
"password=%s dbname=%s sslmode=disable",
|
||||||
h, 5432, u, pw, dbn)
|
h, 5432, u, pw, dbn)
|
||||||
db, err := sql.Open("postgres", psqlInfo)
|
db, err := sql.Open("postgres", psqlInfo)
|
||||||
testUserName := data.ViperReturnKey("test_user")
|
|
||||||
logger.Logger("[LOG] reserved username is " + testUserName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch u {
|
switch u {
|
||||||
case testUserName:
|
case testUserName:
|
||||||
|
|||||||
@ -1,20 +1,19 @@
|
|||||||
package loader
|
package loader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
//"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/mitchellh/mapstructure"
|
//"github.com/mitchellh/mapstructure"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"pgm/config"
|
"pgm/config"
|
||||||
"pgm/data"
|
"pgm/data"
|
||||||
"pgm/db"
|
//"pgm/db"
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
"pgm/ui"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -59,10 +58,11 @@ func Loader(t []string, s string, b string) error {
|
|||||||
// loader function will test that it can load the ui to chose a database
|
// loader function will test that it can load the ui to chose a database
|
||||||
// and quits the ui
|
// and quits the ui
|
||||||
func setTest(s string) bool {
|
func setTest(s string) bool {
|
||||||
logger.Logger("[LOG] is test: " + s)
|
|
||||||
if s == "test" {
|
if s == "test" {
|
||||||
|
logger.Logger("[LOG] is test: true")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
logger.Logger("[LOG] is test: false")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,21 +235,29 @@ func loadhost(c string, p *data.HostDetails) *data.HostDetails {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func Screener(g data.HostDetails) error {
|
func Screener(g data.HostDetails) {
|
||||||
var scn Scr
|
//var scn Scr
|
||||||
var dbConn *sql.DB
|
//var dbConn *sql.DB
|
||||||
|
//var s interface{}
|
||||||
logger.Logger("[LOG] starting screen")
|
logger.Logger("[LOG] starting screen")
|
||||||
s := data.ViperReturnKey("UserSessions")
|
//
|
||||||
|
//found, s := data.ViperScrConfig("userSessions", "r")
|
||||||
mapstructure.Decode(s, &scn)
|
//mapstructure.Decode(s, &scn)
|
||||||
dbConn = db.DbConn(g)
|
//logger.Logger("[LOG] scn value " + scn.Title)
|
||||||
switch g.Username {
|
//if found == true {
|
||||||
case "test":
|
// logger.Logger("[LOG] found screen: " + scn.Title)
|
||||||
dbConn.Close()
|
//} else {
|
||||||
return nil
|
// logger.Logger("[LOG] no screen found")
|
||||||
}
|
//}
|
||||||
ui.GraphicUi(dbConn)
|
//
|
||||||
return nil
|
//dbConn = db.DbConn(g)
|
||||||
|
//switch g.Username {
|
||||||
|
//case "test":
|
||||||
|
// dbConn.Close()
|
||||||
|
// return nil
|
||||||
|
//}
|
||||||
|
////ui.GraphicUi(dbConn)
|
||||||
|
//return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple struct for a view in the UI.
|
// Simple struct for a view in the UI.
|
||||||
|
|||||||
@ -18,7 +18,6 @@ func Logger(s string) bool {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.SetOutput(file)
|
log.SetOutput(file)
|
||||||
log.Println("Hello world!")
|
|
||||||
os.Remove("/var/tmp/PGMLOGTEST")
|
os.Remove("/var/tmp/PGMLOGTEST")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
100
main_test.go
100
main_test.go
@ -1,7 +1,6 @@
|
|||||||
package main_test
|
package main_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"os"
|
"os"
|
||||||
@ -80,46 +79,79 @@ func TestLoader(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestViperPgmConfig(t *testing.T) {
|
||||||
func TestScreener(t *testing.T){
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
var p data.HostDetails
|
testCases := []viperReturnKey{
|
||||||
var testDb string
|
{a: "junk", b: "r", want: false, need: ""},
|
||||||
var want error = nil
|
{a: "test_user", b: "r", want: true, need: "test"},
|
||||||
var h string
|
|
||||||
h, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
logger.Logger("[ERROR] could not set home directory: " + err.Error())
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
viper.SetConfigName(".pgm") // config file name without extension
|
for _, tc := range testCases{
|
||||||
viper.SetConfigType("yaml")
|
found, got := data.ViperPgmConfig(tc.a, tc.b)
|
||||||
viper.AddConfigPath(h)
|
if tc.want != found{
|
||||||
viper.AutomaticEnv() // read value ENV variable
|
t.Errorf("want %t, got %t", tc.want, found)
|
||||||
|
}
|
||||||
err = viper.ReadInConfig()
|
if tc.need != got {
|
||||||
if err != nil {
|
t.Errorf("want %s, got %s", tc.need, got)
|
||||||
logger.Logger("[ERROR] fatal error config file: " + err.Error())
|
}
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
testDb = viper.GetString("test_db")
|
|
||||||
err = json.Unmarshal([]byte(testDb), &p)
|
|
||||||
if err != nil {
|
|
||||||
logger.Logger("[LOG] test screener failed: " + err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
got := loader.Screener(p)
|
|
||||||
if want != got {
|
|
||||||
t.Errorf("want %t, got %t", want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type viperHostTest struct {
|
func TestViperScnConfig(t *testing.T) {
|
||||||
a string
|
t.Parallel()
|
||||||
|
testCases := []viperScnReturn {
|
||||||
|
{t: "test", want: "test"},
|
||||||
|
{t: "userSessions", want: "active sessions"},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
var v *viper.Viper
|
||||||
|
v, err := data.ViperScrConfig("scn", map[string]interface{}{})
|
||||||
|
if err != nil {
|
||||||
|
logger.Logger("[ERROR] scn cfg test error: " + err.Error())
|
||||||
|
}
|
||||||
|
got := v.GetString(tc.t + ".title")
|
||||||
|
if tc.want != got {
|
||||||
|
t.Errorf("want %s, got %s", tc.want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//func TestScreener(t *testing.T) {
|
||||||
|
// t.Parallel()
|
||||||
|
// var p data.HostDetails
|
||||||
|
// var testDb string
|
||||||
|
// var found bool
|
||||||
|
// var want error = nil
|
||||||
|
// found, testDb = data.ViperPgmConfig("test_db", "r")
|
||||||
|
// if found != true {
|
||||||
|
// t.Error("missing test_db parameter in .pgm.yaml for screener test")
|
||||||
|
// logger.Logger("[LOG] missing test_db parameter in .pgm.yaml for TestScreener test")
|
||||||
|
// os.Exit(1)
|
||||||
|
// }
|
||||||
|
// err := json.Unmarshal([]byte(testDb), &p)
|
||||||
|
// if err != nil {
|
||||||
|
// logger.Logger("[LOG] test screener failed: " + err.Error())
|
||||||
|
// os.Exit(1)
|
||||||
|
// }
|
||||||
|
// got := loader.Screener(p)
|
||||||
|
// if want != got {
|
||||||
|
// t.Errorf("want %t, got %t", want, got)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
type viperScnReturn struct {
|
||||||
|
t string
|
||||||
|
want string
|
||||||
|
}
|
||||||
|
|
||||||
|
type viperReturnKey struct {
|
||||||
|
a, b string
|
||||||
want bool
|
want bool
|
||||||
|
need string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user