tests working, need to get uis going now

This commit is contained in:
jms 2020-10-25 09:48:32 -05:00
parent 048940ef7a
commit 68e74f2335
5 changed files with 143 additions and 66 deletions

View File

@ -41,8 +41,8 @@ func ReadHosts() []string {
return pgee
}
// returns a value from the .pgm.yaml file
func ViperReturnKey(k string ) string {
// handles interacting with the config file.
func ViperPgmConfig(k string, a string) (bool, string) {
var h string
var s string
h, err := os.UserHomeDir()
@ -50,17 +50,47 @@ func ViperReturnKey(k string ) string {
logger.Logger("[ERROR] could not set home directory: " + err.Error())
os.Exit(0)
}
viper.SetConfigName("scr")
viper.SetConfigType("json")
viper.AddConfigPath(h + "/.pgm/scn/")
viper.SetConfigName(".pgm")
viper.SetConfigType("yaml")
viper.AddConfigPath(h)
err = viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logger.Logger("[ERROR] failed to load viper config: " + err.Error())
fmt.Println("error occurred and was logged.")
os.Exit(1)
}
// r returns values, may add an action to update or set values later if it makes sense
switch a {
case "r":
if viper.IsSet(k) {
s = viper.GetString(k)
logger.Logger("[LOG] got value from .pgm.yaml: " + s )
return s
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"`
}

View File

@ -20,12 +20,20 @@ func DbConn(c data.HostDetails) *sql.DB {
u = c.Username
pw = c.Secret
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 "+
"password=%s dbname=%s sslmode=disable",
h, 5432, u, pw, dbn)
db, err := sql.Open("postgres", psqlInfo)
testUserName := data.ViperReturnKey("test_user")
logger.Logger("[LOG] reserved username is " + testUserName)
if err != nil {
switch u {
case testUserName:

View File

@ -1,20 +1,19 @@
package loader
import (
"database/sql"
//"database/sql"
"encoding/json"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/mitchellh/mapstructure"
//"github.com/mitchellh/mapstructure"
"github.com/muesli/termenv"
"github.com/spf13/viper"
"io/ioutil"
"os"
"pgm/config"
"pgm/data"
"pgm/db"
//"pgm/db"
"pgm/logger"
"pgm/ui"
"strconv"
"strings"
"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
// and quits the ui
func setTest(s string) bool {
logger.Logger("[LOG] is test: " + s)
if s == "test" {
logger.Logger("[LOG] is test: true")
return true
}
logger.Logger("[LOG] is test: false")
return false
}
@ -235,21 +235,29 @@ func loadhost(c string, p *data.HostDetails) *data.HostDetails {
return p
}
func Screener(g data.HostDetails) error {
var scn Scr
var dbConn *sql.DB
func Screener(g data.HostDetails) {
//var scn Scr
//var dbConn *sql.DB
//var s interface{}
logger.Logger("[LOG] starting screen")
s := data.ViperReturnKey("UserSessions")
mapstructure.Decode(s, &scn)
dbConn = db.DbConn(g)
switch g.Username {
case "test":
dbConn.Close()
return nil
}
ui.GraphicUi(dbConn)
return nil
//
//found, s := data.ViperScrConfig("userSessions", "r")
//mapstructure.Decode(s, &scn)
//logger.Logger("[LOG] scn value " + scn.Title)
//if found == true {
// logger.Logger("[LOG] found screen: " + scn.Title)
//} else {
// logger.Logger("[LOG] no screen found")
//}
//
//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.

View File

@ -18,7 +18,6 @@ func Logger(s string) bool {
log.Fatal(err)
}
log.SetOutput(file)
log.Println("Hello world!")
os.Remove("/var/tmp/PGMLOGTEST")
return true
}

View File

@ -1,7 +1,6 @@
package main_test
import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
"os"
@ -80,46 +79,79 @@ func TestLoader(t *testing.T) {
}
func TestScreener(t *testing.T){
func TestViperPgmConfig(t *testing.T) {
t.Parallel()
var p data.HostDetails
var testDb string
var want error = nil
var h string
h, err := os.UserHomeDir()
testCases := []viperReturnKey{
{a: "junk", b: "r", want: false, need: ""},
{a: "test_user", b: "r", want: true, need: "test"},
}
for _, tc := range testCases{
found, got := data.ViperPgmConfig(tc.a, tc.b)
if tc.want != found{
t.Errorf("want %t, got %t", tc.want, found)
}
if tc.need != got {
t.Errorf("want %s, got %s", tc.need, got)
}
}
}
func TestViperScnConfig(t *testing.T) {
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] could not set home directory: " + err.Error())
os.Exit(0)
logger.Logger("[ERROR] scn cfg test error: " + err.Error())
}
viper.SetConfigName(".pgm") // config file name without extension
viper.SetConfigType("yaml")
viper.AddConfigPath(h)
viper.AutomaticEnv() // read value ENV variable
err = viper.ReadInConfig()
if err != nil {
logger.Logger("[ERROR] fatal error config file: " + err.Error())
os.Exit(1)
got := v.GetString(tc.t + ".title")
if tc.want != got {
t.Errorf("want %s, got %s", tc.want, got)
}
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 {
a string
}
//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
need string
}