Remove old device ID from server in DeviceIDGen

This commit is contained in:
2024-08-12 15:53:40 -04:00
parent e861b2b6f3
commit 7d51d12b55
6 changed files with 48 additions and 26 deletions
+13
View File
@@ -2,6 +2,7 @@ package core
import (
"fmt"
"io/fs"
"os"
"gopkg.in/ini.v1"
@@ -49,6 +50,18 @@ func ParseConfig(valuesRequested [][2]string, missingValueError string) []string
return config
}
// GenDeviceIDList returns a pointer to a slice of all registered device IDs.
// Requires: errorOnFail (set to true to throw an error if the device ID list cannot be generated)
func GenDeviceIDList(errorOnFail bool) *[]fs.DirEntry {
// create a slice of all registered devices
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
if err != nil && errorOnFail {
fmt.Println(AnsiError + "Failed to read the devices directory: " + err.Error() + AnsiReset)
os.Exit(101)
}
return &deviceIDList
}
// WriteConfig writes the provided key-value pairs under the specified section headers in the libmutton.ini file.
// Requires: valuesToWrite (a slice of length 3 arrays each containing a section, a key name, and a value).
func WriteConfig(valuesToWrite [][3]string, append bool) {
+7 -1
View File
@@ -53,7 +53,8 @@ func GpgKeyGen() string {
}
// DirInit creates the libmutton directories.
func DirInit(preserveOldConfigDir bool) {
// Returns: oldDeviceID (before from before the directory reset).
func DirInit(preserveOldConfigDir bool) string {
// create EntryRoot
err := os.MkdirAll(EntryRoot, 0700)
if err != nil {
@@ -61,6 +62,9 @@ func DirInit(preserveOldConfigDir bool) {
os.Exit(102)
}
// get old device ID before its potential removal
oldDeviceID := (*GenDeviceIDList(false))[0].Name() // errorOnFail set to false to ignore error if device ID directory does not exist (error non-critical for this function)
// remove existing config directory (if it exists and not in append mode)
if !preserveOldConfigDir {
_, isAccessible := TargetIsFile(ConfigDir, false, 1)
@@ -79,4 +83,6 @@ func DirInit(preserveOldConfigDir bool) {
fmt.Println(AnsiError + "Failed to create \"" + ConfigDir + "\": " + err.Error() + AnsiReset)
os.Exit(102)
}
return oldDeviceID
}