diff --git a/cmd/add.go b/cmd/add.go index 5a1cd3d..470c2a0 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -16,19 +16,18 @@ limitations under the License. package cmd import ( - //"encoding/json" + "encoding/json" "fmt" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" + "io/ioutil" + "log" + "os" "shiny-pancake/logger" - - //"github.com/spf13/viper" - //"io/ioutil" - //"log" - //"os" ) // addCmd represents the add command @@ -84,56 +83,67 @@ func loadForm() { -//// Writes configs to .pgm/hosts directory +//// Writes configs to correct directory based on the config file func configWriter(f *tview.Form){ var ip []string + var fp string var hostFile string - // Only look - for a := 0; a < 3; a++ { + var cfg []byte + var c HostDetails + var hd string + hd = viper.GetString("hostsDirectory") + + for a := 0; a < 4; a++ { var inputValue string - if a < 3 { - inputField := f.GetFormItem(a).(*tview.InputField) - inputValue = inputField.GetText() - } else { - inputField := f.GetFormItem(a).(*tview.InputField) - inputValue = inputField.GetText() - } + inputField := f.GetFormItem(a).(*tview.InputField) + inputValue = inputField.GetText() if len(inputValue) <1 { var l logger.Log l = logger.Log{"warn", logrus.Fields{"add config": "empty form"}, "zero length input value not allowed"} logger.Lgr(&l) fmt.Println("blank value provided. unable to save config, please retry.") + os.Exit(0) } ip = append(ip, inputValue) - } - hostFile = ip[0] - fmt.Println(hostFile) - if _, err := os.Stat(hostsDirectory + "/" + ); os.IsNotExist(err) { - fp = h + "/" + cfgFileName - } else { + + } + hostFile = ip[0] + if _, err := os.Stat(hd + "/" + hostFile ); os.IsNotExist(err) { + fp = hd + "/" + hostFile + } else { + var l logger.Log + l = logger.Log{"warn", logrus.Fields{"duplicate file": hostFile}, "duplicate configuration file names not allowed"} + logger.Lgr(&l) + fmt.Println("duplicate hostname used. unable to save config, please retry.") + os.Exit(1) + } + + file, err := os.Create(fp) + c = HostDetails{ip[3], hostFile, ip[1], ip[2]} + if err != nil { + log.Fatal("unable to create file: " + fp) + } + defer file.Close() + if err != nil { + log.Fatal("error writing to file: " + err.Error()) + } + cfg, err = json.MarshalIndent(c, "", " ") + if err != nil { + log.Fatal(err) + } var l logger.Log - l = logger.Log{"warn", logrus.Fields{"duplicate file": hostFile}, "duplicate configuration file names not allowed"} + err = ioutil.WriteFile(fp, cfg, 0644) + if err !=nil{ + log.Fatal(err) + } + l = logger.Log{"info", logrus.Fields{"file": hostFile}, "created"} logger.Lgr(&l) - fmt.Println("duplicate hostname used. unable to save config, please retry.") - os.Exit(1) } - //_ = cfg - //f, err := os.Create(fp) - //if err != nil { - // log.Fatal("unable to create file: " + fp) - //} - //defer f.Close() - //if err != nil { - // log.Fatal("error writing to file: " + err.Error()) - //} - //cfg, err = json.MarshalIndent(c, "", " ") - //if err != nil { - // log.Fatal(err) - //} - //logger.Logger("[LOG] writing file to: " + fp) - //_ = ioutil.WriteFile(fp, cfg, 0644) - //lgd := logger.Logger("[LOG] configuration file written: dbname::" + c.DatabaseName + "::hostname::" + c.Hostname) - //if lgd != true { - // fmt.Println("failed to log") - //} + + +type HostDetails struct { + Secret string `json:"secret"` + Hostname string `json:"hostname"` + DatabaseName string `json:"databaseName"` + Username string `json:"username"` } \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index 130a7f2..09317b9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -88,9 +88,9 @@ func initConfig() { viper.AddConfigPath(home + "/" + configHome + "/" + configDirectory + "/") viper.SetConfigName(".config") viper.SetDefault("configHome", configHome) - viper.SetDefault("hostsDirectory", configHome + "/" + hostsDirectory) - viper.SetDefault("logsDirectory", configHome + "/" + logsDirectory) - viper.SetDefault("configDirectory", configHome + "/" + configDirectory) + viper.SetDefault("hostsDirectory", home + "/" + configHome + "/" + hostsDirectory) + viper.SetDefault("logsDirectory", home + "/" + configHome + "/" + logsDirectory) + viper.SetDefault("configDirectory", home + "/" +configHome + "/" + configDirectory) viper.SetDefault("logLevel", "debug") viper.SetDefault("logfile", "log") viper.AutomaticEnv() // read in environment variables that match diff --git a/logger/logger.go b/logger/logger.go index 5c5b48b..2550b5b 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -13,16 +13,12 @@ func Lgr(l *Log) { // The API for setting attributes is a little different than the package level // exported logger. See Godoc. var log = logrus.New() - var home string - home, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } + logLvl := viper.GetString("loglevel") setLogLeve(&logLvl, log) logDir := viper.GetString("logsdirectory") logFile := viper.GetString("logfile") - logPath := home + "/" + logDir + "/" + logFile + logPath := logDir + "/" + logFile if _, err := os.Stat(logPath); os.IsNotExist(err) { os.Create(logPath) } diff --git a/main b/main index c5b6086..af293bf 100755 Binary files a/main and b/main differ