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
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"`
}

View File

@ -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

View File

@ -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)
}

BIN
main

Binary file not shown.