add function simplified

This commit is contained in:
jms 2020-11-07 10:23:54 -06:00
parent 6bc6f90e70
commit 61fd5af955
4 changed files with 59 additions and 53 deletions

View File

@ -16,19 +16,18 @@ limitations under the License.
package cmd package cmd
import ( import (
//"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"os" "os"
"shiny-pancake/logger" "shiny-pancake/logger"
//"github.com/spf13/viper"
//"io/ioutil"
//"log"
//"os"
) )
// addCmd represents the add command // 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){ func configWriter(f *tview.Form){
var ip []string var ip []string
var fp string
var hostFile string var hostFile string
// Only look var cfg []byte
for a := 0; a < 3; a++ { var c HostDetails
var hd string
hd = viper.GetString("hostsDirectory")
for a := 0; a < 4; a++ {
var inputValue string var inputValue string
if a < 3 { inputField := f.GetFormItem(a).(*tview.InputField)
inputField := f.GetFormItem(a).(*tview.InputField) inputValue = inputField.GetText()
inputValue = inputField.GetText()
} else {
inputField := f.GetFormItem(a).(*tview.InputField)
inputValue = inputField.GetText()
}
if len(inputValue) <1 { if len(inputValue) <1 {
var l logger.Log var l logger.Log
l = logger.Log{"warn", logrus.Fields{"add config": "empty form"}, "zero length input value not allowed"} l = logger.Log{"warn", logrus.Fields{"add config": "empty form"}, "zero length input value not allowed"}
logger.Lgr(&l) logger.Lgr(&l)
fmt.Println("blank value provided. unable to save config, please retry.") fmt.Println("blank value provided. unable to save config, please retry.")
os.Exit(0)
} }
ip = append(ip, inputValue) ip = append(ip, inputValue)
}
hostFile = ip[0] }
fmt.Println(hostFile) hostFile = ip[0]
if _, err := os.Stat(hostsDirectory + "/" + ); os.IsNotExist(err) { if _, err := os.Stat(hd + "/" + hostFile ); os.IsNotExist(err) {
fp = h + "/" + cfgFileName fp = hd + "/" + hostFile
} else { } 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 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) 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 { type HostDetails struct {
// log.Fatal("unable to create file: " + fp) Secret string `json:"secret"`
//} Hostname string `json:"hostname"`
//defer f.Close() DatabaseName string `json:"databaseName"`
//if err != nil { Username string `json:"username"`
// 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")
//}
} }

View File

@ -88,9 +88,9 @@ func initConfig() {
viper.AddConfigPath(home + "/" + configHome + "/" + configDirectory + "/") viper.AddConfigPath(home + "/" + configHome + "/" + configDirectory + "/")
viper.SetConfigName(".config") viper.SetConfigName(".config")
viper.SetDefault("configHome", configHome) viper.SetDefault("configHome", configHome)
viper.SetDefault("hostsDirectory", configHome + "/" + hostsDirectory) viper.SetDefault("hostsDirectory", home + "/" + configHome + "/" + hostsDirectory)
viper.SetDefault("logsDirectory", configHome + "/" + logsDirectory) viper.SetDefault("logsDirectory", home + "/" + configHome + "/" + logsDirectory)
viper.SetDefault("configDirectory", configHome + "/" + configDirectory) viper.SetDefault("configDirectory", home + "/" +configHome + "/" + configDirectory)
viper.SetDefault("logLevel", "debug") viper.SetDefault("logLevel", "debug")
viper.SetDefault("logfile", "log") viper.SetDefault("logfile", "log")
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match

View File

@ -13,16 +13,12 @@ func Lgr(l *Log) {
// The API for setting attributes is a little different than the package level // The API for setting attributes is a little different than the package level
// exported logger. See Godoc. // exported logger. See Godoc.
var log = logrus.New() var log = logrus.New()
var home string
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
logLvl := viper.GetString("loglevel") logLvl := viper.GetString("loglevel")
setLogLeve(&logLvl, log) setLogLeve(&logLvl, log)
logDir := viper.GetString("logsdirectory") logDir := viper.GetString("logsdirectory")
logFile := viper.GetString("logfile") logFile := viper.GetString("logfile")
logPath := home + "/" + logDir + "/" + logFile logPath := logDir + "/" + logFile
if _, err := os.Stat(logPath); os.IsNotExist(err) { if _, err := os.Stat(logPath); os.IsNotExist(err) {
os.Create(logPath) os.Create(logPath)
} }

BIN
main

Binary file not shown.