made some good progress tonight
This commit is contained in:
parent
71848481d2
commit
0f465b827f
@ -25,6 +25,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"pgm/data"
|
"pgm/data"
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
|
"pgm/look"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -80,6 +81,7 @@ to quickly create a Cobra application.`,
|
|||||||
// Print out the final choice.
|
// Print out the final choice.
|
||||||
if fn = <-result; len(fn) !=0 {
|
if fn = <-result; len(fn) !=0 {
|
||||||
logger.Logger("[LOG] loading file: " + fn)
|
logger.Logger("[LOG] loading file: " + fn)
|
||||||
|
look.Look(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|||||||
43
cmd/root.go
43
cmd/root.go
@ -18,9 +18,9 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"pgm/config"
|
"pgm/config"
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
|
|
||||||
@ -71,6 +71,21 @@ func init() {
|
|||||||
// Cobra also supports local flags, which will only run
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
customPgm := viper.GetString("pgm_home_overide")
|
||||||
|
if len(customPgm) > 0 {
|
||||||
|
logger.Logger("[LOG] custom pgm overide set at: " + customPgm)
|
||||||
|
if filepath.IsAbs(customPgm) != true {
|
||||||
|
logger.Logger("[ERROR] failed to add non absolute path to viper")
|
||||||
|
} else {
|
||||||
|
viper.AddConfigPath(customPgm)
|
||||||
|
pgmDir := viper.GetString("pgm_dir")
|
||||||
|
logsDir := viper.GetString("logs_dir")
|
||||||
|
hostsDir := viper.GetString("hosts_dir")
|
||||||
|
viper.SetDefault("logsDir", customPgm + "/" + logsDir)
|
||||||
|
viper.SetDefault("pgmDir", customPgm + pgmDir)
|
||||||
|
viper.SetDefault("hostsDir", customPgm + pgmDir + "/" + hostsDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
viper.AddConfigPath(path.Join(homeDirectory))
|
viper.AddConfigPath(path.Join(homeDirectory))
|
||||||
viper.SetConfigName(".pgm")
|
viper.SetConfigName(".pgm")
|
||||||
viper.SetDefault("homeDir", homeDirectory)
|
viper.SetDefault("homeDir", homeDirectory)
|
||||||
@ -80,12 +95,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
pgmDir := viper.GetString("pgm_dir")
|
pgmDir := viper.GetString("pgm_dir")
|
||||||
logsDir := viper.GetString("logs_dir")
|
logsDir := viper.GetString("logs_dir")
|
||||||
scnDir := viper.GetString("screen_dir")
|
|
||||||
hostsDir := viper.GetString("hosts_dir")
|
hostsDir := viper.GetString("hosts_dir")
|
||||||
viper.SetDefault("logsDir", homeDirectory + pgmDir + "/" + logsDir)
|
cfgDir := viper.GetString("cfg_dir")
|
||||||
viper.SetDefault("scnDir", homeDirectory + pgmDir + "/" + scnDir)
|
|
||||||
viper.SetDefault("pgmDir", homeDirectory + pgmDir)
|
viper.SetDefault("pgmDir", homeDirectory + pgmDir)
|
||||||
viper.SetDefault("hostsDir", homeDirectory + pgmDir + "/" + hostsDir)
|
viper.SetDefault("hostsDir", homeDirectory + pgmDir + "/" + hostsDir)
|
||||||
|
viper.SetDefault("cfgDir", homeDirectory + pgmDir + "/" + cfgDir)
|
||||||
|
viper.SetDefault("logsDir", homeDirectory + pgmDir + "/" + logsDir)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,17 +123,17 @@ func InitConfig() {
|
|||||||
viper.SetConfigName(".pgm")
|
viper.SetConfigName(".pgm")
|
||||||
|
|
||||||
}
|
}
|
||||||
h, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
var hosts string
|
var hosts string
|
||||||
var scr string
|
var cfg string
|
||||||
var logs string
|
var logs string
|
||||||
hosts = h + "/.pgm/hosts"
|
var pgm string
|
||||||
scr = h + "/.pgm/screen"
|
hosts = viper.GetString("hostsDir")
|
||||||
logs = h + "/.pgm/logs"
|
cfg = viper.GetString("cfgDir")
|
||||||
config.IsInit(hosts, scr, logs, cfgFile)
|
logs = viper.GetString("logsDir")
|
||||||
|
pgm = viper.GetString("pgmDir")
|
||||||
|
s := []string{hosts, cfg, logs, pgm}
|
||||||
|
|
||||||
|
config.IsInit(s)
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
|
|||||||
@ -10,16 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// checks to make sure things look like we want locally.
|
// checks to make sure things look like we want locally.
|
||||||
func IsInit(h string, s string, l string, cfgFile string) bool {
|
func IsInit(s []string) {
|
||||||
var isCfg []bool
|
var isCfg []bool
|
||||||
var dirs []string
|
var dirs []string
|
||||||
dirs = cfgExists(h, dirs, cfgFile)
|
|
||||||
dirs = cfgExists(s, dirs, cfgFile)
|
|
||||||
dirs = cfgExists(l, dirs, cfgFile)
|
|
||||||
homeDirectory, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// if there is no $HOME/.pgm dir
|
// if there is no $HOME/.pgm dir
|
||||||
if _, err := os.Stat(homeDirectory + "/.pgm"); os.IsNotExist(err) {
|
if _, err := os.Stat(homeDirectory + "/.pgm"); os.IsNotExist(err) {
|
||||||
err = os.Mkdir(homeDirectory+"/.pgm/", 0755)
|
err = os.Mkdir(homeDirectory+"/.pgm/", 0755)
|
||||||
@ -46,40 +40,7 @@ func IsInit(h string, s string, l string, cfgFile string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// determines if key exists in config file
|
|
||||||
func cfgExists(s string, d []string, cfgFile string) []string {
|
|
||||||
var dir []string
|
|
||||||
var cfgPath string
|
|
||||||
dir = strings.Split(s, "/")
|
|
||||||
cfgPath = dir[len(dir)-1]
|
|
||||||
|
|
||||||
configDirectoryKey := parseCfg(cfgPath)
|
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
|
||||||
viper.SetConfigFile(cfgFile)
|
|
||||||
}
|
|
||||||
if viper.GetString(configDirectoryKey) != "" {
|
|
||||||
a := viper.GetString(configDirectoryKey)
|
|
||||||
d = append(d, a)
|
|
||||||
} else {
|
|
||||||
d = append(d, "")
|
|
||||||
}
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
// neatly return a string formatted to match the config file
|
|
||||||
func parseCfg(s string) string {
|
|
||||||
var directory string
|
|
||||||
var isTest string
|
|
||||||
// if we have test directories these are configured already in .pgm.yaml
|
|
||||||
isTest = s[:3]
|
|
||||||
if isTest == "test" {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
directory = "%s_dir"
|
|
||||||
directory = fmt.Sprintf(directory, s)
|
|
||||||
return directory
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// repeated method to get config data
|
// repeated method to get config data
|
||||||
func ReadConfig() (*viper.Viper, error) {
|
func ReadConfig() (*viper.Viper, error) {
|
||||||
|
|||||||
37
look/look.go
Normal file
37
look/look.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package look
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"io/ioutil"
|
||||||
|
"pgm/data"
|
||||||
|
"pgm/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// the default view for a user once they load a database is called the look.
|
||||||
|
// the look loads
|
||||||
|
func Look(f string) {
|
||||||
|
var connectionDetails data.HostDetails
|
||||||
|
connectionDetails = loadHost(f)
|
||||||
|
fmt.Println(connectionDetails)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// gets information for host from hostDir
|
||||||
|
func loadHost(f string) data.HostDetails {
|
||||||
|
var hostDir string
|
||||||
|
hostDir = viper.GetString("hostsDir")
|
||||||
|
file, _ := ioutil.ReadFile(hostDir + "/"+ f)
|
||||||
|
|
||||||
|
data := data.HostDetails{}
|
||||||
|
|
||||||
|
err := json.Unmarshal(file, &data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Logger("[ERROR] error loading host, malformed file: " + f)
|
||||||
|
logdir := viper.GetString("logsDir")
|
||||||
|
fmt.Println("fatal error occured, see logs at " + logdir)
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
67
main_test.go
67
main_test.go
@ -2,39 +2,14 @@ package main_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"os"
|
|
||||||
"pgm/config"
|
"pgm/config"
|
||||||
"pgm/data"
|
"pgm/data"
|
||||||
"pgm/db"
|
"pgm/db"
|
||||||
"pgm/loader"
|
|
||||||
"pgm/logger"
|
"pgm/logger"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// init should always return true
|
|
||||||
// init will not write incorrect values to file
|
|
||||||
func TestInit(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var cfg string
|
|
||||||
testCases := []initTest{
|
|
||||||
{a: "junk", b: "notreal", c: "failingonpurpose", want: true},
|
|
||||||
{a: "test_host_dir", b: "test_screen_dr", c: "test_log_dir", want: true},
|
|
||||||
}
|
|
||||||
cfg, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
logger.Logger("failed to find home dir " + err.Error())
|
|
||||||
fmt.Println("an error was encountered and logged")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
for _, tc := range testCases {
|
|
||||||
got := config.IsInit(tc.a, tc.b, tc.c, cfg + "/.pgm.yml")
|
|
||||||
if tc.want != got {
|
|
||||||
t.Errorf("want %t, got %t", tc.want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogs(t *testing.T) {
|
func TestLogs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@ -61,25 +36,6 @@ func TestLoadConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// loader function will test that it can load the ui to chose a database
|
|
||||||
// and quits the ui.
|
|
||||||
// "test" is added into the array of files a user has configured and selected
|
|
||||||
func TestLoader(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var tc []string
|
|
||||||
var s string
|
|
||||||
var b string
|
|
||||||
var want error = nil
|
|
||||||
s = "test"
|
|
||||||
b = "test"
|
|
||||||
tc = data.ReadHosts()
|
|
||||||
tc = append(tc, "test")
|
|
||||||
got := loader.Loader(tc, s, b)
|
|
||||||
if want != got {
|
|
||||||
t.Errorf("want %t, got %t", want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestViperPgmConfig(t *testing.T) {
|
func TestViperPgmConfig(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@ -152,29 +108,6 @@ func TestDbConn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//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 connTest struct {
|
type connTest struct {
|
||||||
connstring string
|
connstring string
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user