From 0f465b827f6f5f61dc60cbb0ce01f12a41365c0f Mon Sep 17 00:00:00 2001 From: jms Date: Mon, 26 Oct 2020 19:24:09 -0500 Subject: [PATCH] made some good progress tonight --- cmd/load.go | 2 ++ cmd/root.go | 43 +++++++++++++++++++++---------- config/config.go | 43 ++----------------------------- look/look.go | 37 ++++++++++++++++++++++++++ main_test.go | 67 ------------------------------------------------ ui/ui.go | 0 6 files changed, 71 insertions(+), 121 deletions(-) create mode 100644 look/look.go delete mode 100644 ui/ui.go diff --git a/cmd/load.go b/cmd/load.go index 2f34465..0b2af4c 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -25,6 +25,7 @@ import ( "os" "pgm/data" "pgm/logger" + "pgm/look" "strconv" "strings" "time" @@ -80,6 +81,7 @@ to quickly create a Cobra application.`, // Print out the final choice. if fn = <-result; len(fn) !=0 { logger.Logger("[LOG] loading file: " + fn) + look.Look(fn) } }, diff --git a/cmd/root.go b/cmd/root.go index d616422..8f1476f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,9 +18,9 @@ package cmd import ( "fmt" "github.com/spf13/cobra" - "log" "os" "path" + "path/filepath" "pgm/config" "pgm/logger" @@ -71,6 +71,21 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. 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.SetConfigName(".pgm") viper.SetDefault("homeDir", homeDirectory) @@ -80,12 +95,14 @@ func init() { } pgmDir := viper.GetString("pgm_dir") logsDir := viper.GetString("logs_dir") - scnDir := viper.GetString("screen_dir") hostsDir := viper.GetString("hosts_dir") - viper.SetDefault("logsDir", homeDirectory + pgmDir + "/" + logsDir) - viper.SetDefault("scnDir", homeDirectory + pgmDir + "/" + scnDir) + cfgDir := viper.GetString("cfg_dir") viper.SetDefault("pgmDir", homeDirectory + pgmDir) 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") } - h, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } var hosts string - var scr string + var cfg string var logs string - hosts = h + "/.pgm/hosts" - scr = h + "/.pgm/screen" - logs = h + "/.pgm/logs" - config.IsInit(hosts, scr, logs, cfgFile) + var pgm string + hosts = viper.GetString("hostsDir") + cfg = viper.GetString("cfgDir") + 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 diff --git a/config/config.go b/config/config.go index 3f094cf..5f1c04a 100644 --- a/config/config.go +++ b/config/config.go @@ -10,16 +10,10 @@ import ( ) // 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 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 _, err := os.Stat(homeDirectory + "/.pgm"); os.IsNotExist(err) { err = os.Mkdir(homeDirectory+"/.pgm/", 0755) @@ -46,40 +40,7 @@ func IsInit(h string, s string, l string, cfgFile string) bool { 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 func ReadConfig() (*viper.Viper, error) { diff --git a/look/look.go b/look/look.go new file mode 100644 index 0000000..d77f87d --- /dev/null +++ b/look/look.go @@ -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 +} \ No newline at end of file diff --git a/main_test.go b/main_test.go index 8886538..3a07870 100644 --- a/main_test.go +++ b/main_test.go @@ -2,39 +2,14 @@ package main_test import ( "encoding/json" - "fmt" "github.com/spf13/viper" - "os" "pgm/config" "pgm/data" "pgm/db" - "pgm/loader" "pgm/logger" "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) { 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) { 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 { connstring string diff --git a/ui/ui.go b/ui/ui.go deleted file mode 100644 index e69de29..0000000