mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-31 22:36:41 -04:00
Write and append to config file using ini package
This commit is contained in:
@@ -6,16 +6,23 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// ReadConfig reads the libmutton.ini file and returns a slice of values for the specified keys
|
||||
// requires readKeys: a slice of key names (indicates requested values)
|
||||
// requires missingValueError: an error message to display if a key is missing a value, set to "" for auto-generated or "0" to exit silently with status 0
|
||||
// returns config: a slice of values for the specified keys
|
||||
func ReadConfig(readKeys []string, missingValueError string) []string {
|
||||
// loadConfig loads the libmutton.ini file and returns the configuration
|
||||
// utility function for ParseConfig and WriteConfig, do not call directly
|
||||
func loadConfig() *ini.File {
|
||||
cfg, err := ini.Load(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to load libmutton.ini: " + err.Error() + AnsiReset)
|
||||
os.Exit(1)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// ParseConfig reads the libmutton.ini file and returns a slice of values for the specified keys
|
||||
// requires readKeys: a slice of key names (indicates requested values)
|
||||
// requires missingValueError: an error message to display if a key is missing a value, set to "" for auto-generated or "0" to exit silently with status 0
|
||||
// returns config: a slice of values for the specified keys
|
||||
func ParseConfig(readKeys []string, missingValueError string) []string {
|
||||
cfg := loadConfig()
|
||||
|
||||
var config []string
|
||||
|
||||
@@ -41,7 +48,44 @@ func ReadConfig(readKeys []string, missingValueError string) []string {
|
||||
return config
|
||||
}
|
||||
|
||||
// libmuttn.ini layout
|
||||
// WriteConfig writes the provided key-value pairs to the libmutton.ini file
|
||||
func WriteConfig(configFileMap map[string]string, append bool) {
|
||||
var cfg *ini.File
|
||||
var libmuttonSection *ini.Section
|
||||
|
||||
if append {
|
||||
// load existing ini file
|
||||
cfg = loadConfig()
|
||||
|
||||
// acquire LIBMUTTON section
|
||||
libmuttonSection, _ = cfg.GetSection("LIBMUTTON")
|
||||
} else {
|
||||
// create empty ini file
|
||||
cfg = ini.Empty()
|
||||
|
||||
// create LIBMUTTON section
|
||||
libmuttonSection, _ = cfg.NewSection("LIBMUTTON")
|
||||
|
||||
// set default textEditor value
|
||||
if configFileMap["textEditor"] == "" {
|
||||
configFileMap["textEditor"] = textEditorFallback()
|
||||
}
|
||||
}
|
||||
|
||||
// write provided configFileMap key-value pairs to the LIBMUTTON section
|
||||
for key, value := range configFileMap {
|
||||
libmuttonSection.Key(key).SetValue(value)
|
||||
}
|
||||
|
||||
// save the new config file
|
||||
err := cfg.SaveTo(ConfigPath)
|
||||
if err != nil {
|
||||
fmt.Println(AnsiError + "Failed to save libmutton.ini: " + err.Error() + AnsiReset)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// libmutton.ini layout
|
||||
// [LIBMUTTON]
|
||||
// gpgID = <gpg key id>
|
||||
// textEditor = <editor command>
|
||||
@@ -51,3 +95,9 @@ func ReadConfig(readKeys []string, missingValueError string) []string {
|
||||
// sshKey = <ssh private key identity file path>
|
||||
// sshKeyProtected = <true/false>
|
||||
// netPinEnabled = <true/false>
|
||||
// sshEntryRoot = <remote entry root>
|
||||
// sshIsWindows = <true/false>
|
||||
|
||||
// Developers of alternative clients:
|
||||
// If you are adding additional settings to the config file,
|
||||
// please create a new section heading for your app-specific settings.
|
||||
|
||||
+1
-2
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
// TODO GPG support is a temporary feature - it will be replaced with a different encryption scheme in the future
|
||||
// These functions may continue to exist after that point, but consider them deprecated
|
||||
|
||||
// DecryptGPG decrypts a GPG-encrypted file and returns the contents as a slice of (trimmed) strings
|
||||
func DecryptGPG(targetLocation string) []string {
|
||||
@@ -24,7 +23,7 @@ func DecryptGPG(targetLocation string) []string {
|
||||
|
||||
// EncryptGPG encrypts a slice of strings using GPG and returns the encrypted data as a byte slice
|
||||
func EncryptGPG(input []string) []byte {
|
||||
cmd := exec.Command("gpg", "-q", "-r", ReadConfig([]string{"gpgID"}, "")[0], "-e")
|
||||
cmd := exec.Command("gpg", "-q", "-r", ParseConfig([]string{"gpgID"}, "")[0], "-e")
|
||||
writeToStdin(cmd, strings.Join(input, "\n"))
|
||||
encryptedBytes, err := cmd.Output()
|
||||
if err != nil {
|
||||
|
||||
@@ -9,25 +9,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TempInit ensures libmutton directories exist and writes the libmutton configuration file
|
||||
// TODO if run in append mode, extend old config file with new values, rather than creating from scratch
|
||||
func TempInit(configFileMap map[string]string, append bool) {
|
||||
// create EntryRoot and ConfigDir
|
||||
DirInit(append)
|
||||
|
||||
if configFileMap["textEditor"] == "" {
|
||||
configFileMap["textEditor"] = textEditorFallback()
|
||||
}
|
||||
|
||||
// create and write config file
|
||||
configFile, _ := os.OpenFile(ConfigPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||
defer configFile.Close()
|
||||
configFile.WriteString("[LIBMUTTON]\n")
|
||||
for key, value := range configFileMap {
|
||||
configFile.WriteString(key + " = " + value + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
// GpgUIDListGen generates a list of all GPG key IDs on the system and returns them as a slice of strings
|
||||
func GpgUIDListGen() []string {
|
||||
cmd := exec.Command("gpg", "-k", "--with-colons")
|
||||
|
||||
Reference in New Issue
Block a user