Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tucats/ego
Browse files Browse the repository at this point in the history
  • Loading branch information
tucats committed Jan 25, 2025
2 parents b402493 + bf47ce9 commit bedb4fd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 98 deletions.
152 changes: 76 additions & 76 deletions app-cli/settings/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,24 @@ func Load(application string, name string) error {
return errors.New(err)
}

ui.Log(ui.AppLogger, "config.active",
"name", name)
ui.Log(ui.AppLogger, "config.active", ui.A{
"name": name})

// Do we already have that configuration loaded? If so make it current
// and we're done.
if c, ok := Configurations[name]; ok {
path := filepath.Join(home, ProfileDirectory, name+".profile")
CurrentConfiguration = c

ui.Log(ui.AppLogger, "config.base.loaded",
"path", path)
ui.Log(ui.AppLogger, "config.base.loaded", ui.A{
"path": path})

// For any keys that are stored as separate file values, get them now.
readOutboardConfigFiles(home, name, c)

ui.Log(ui.AppLogger, "config.is.active",
"name", CurrentConfiguration.Name,
"id", CurrentConfiguration.ID)
ui.Log(ui.AppLogger, "config.is.active", ui.A{
"Name": CurrentConfiguration.Name,
"id": CurrentConfiguration.ID})

return nil
}
Expand All @@ -142,8 +142,8 @@ func Load(application string, name string) error {
path := filepath.Join(home, ProfileDirectory)
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.MkdirAll(path, securePermission)
ui.Log(ui.AppLogger, "config.create.dir",
"path", path)
ui.Log(ui.AppLogger, "config.create.dir", ui.A{
"path": path})
}

// Read legacy configuration file if it exists.
Expand All @@ -157,8 +157,8 @@ func Load(application string, name string) error {
for _, file := range files {
configFile, err := os.Open(file)
if err == nil {
ui.Log(ui.AppLogger, "config.read.profile",
"path", file)
ui.Log(ui.AppLogger, "config.read.profile", ui.A{
"path": file})

defer configFile.Close()
byteValue, _ := io.ReadAll(configFile)
Expand All @@ -171,10 +171,10 @@ func Load(application string, name string) error {
profile.Name = shortProfileName
Configurations[shortProfileName] = &profile

ui.Log(ui.AppLogger, "config.loaded.config",
"name", profile.Name,
"id", profile.ID,
"count", len(profile.Items))
ui.Log(ui.AppLogger, "config.loaded.config", ui.A{
"name": profile.Name,
"id": profile.ID,
"count": len(profile.Items)})
}
}
}
Expand All @@ -184,8 +184,8 @@ func Load(application string, name string) error {
// default configuration.
cp, found := Configurations[name]
if !found {
ui.Log(ui.AppLogger, "config.not.found",
"name", name)
ui.Log(ui.AppLogger, "config.not.found", ui.A{
"name": name})

cp = &Configuration{
Description: DefaultConfiguration,
Expand All @@ -196,9 +196,9 @@ func Load(application string, name string) error {
}
Configurations[name] = cp
} else {
ui.Log(ui.AppLogger, "config.using",
"name", name,
"id", cp.ID)
ui.Log(ui.AppLogger, "config.using", ui.A{
"name": name,
"id": cp.ID})
}

ProfileName = cp.Name
Expand Down Expand Up @@ -246,24 +246,24 @@ func readOutboardConfigFiles(home string, name string, cp *Configuration) {
if err == nil {
var value string

ui.Log(ui.AppLogger, "config.external",
"name", token,
"path", fileName)
ui.Log(ui.AppLogger, "config.external", ui.A{
"name": token,
"path": fileName})

err := json.Unmarshal(bytes, &value)
if err == nil && len(value) > 0 {
// Decrypt the value using the salt as the password if it is marked as an encrypted value.
if strings.HasPrefix(value, encryptionPrefixTag) {
value, err = Decrypt(strings.TrimPrefix(value, encryptionPrefixTag), cp.Name+cp.Salt+cp.ID)
if err != nil {
ui.Log(ui.AppLogger, "config.decrypt.error",
"name", token,
"error", err)
ui.Log(ui.AppLogger, "config.decrypt.error", ui.A{
"name": token,
"error": err})

continue
} else {
ui.Log(ui.AppLogger, "config.decrypted",
"name", token)
ui.Log(ui.AppLogger, "config.decrypted", ui.A{
"name": token})
}
}

Expand All @@ -283,8 +283,8 @@ func readLegacyConfigFormat(path string, home string, name string) (error, strin
if err == nil {
// read the json config as a byte array.
defer configFile.Close()
ui.Log(ui.AppLogger, "config.legacy.read",
"path", path)
ui.Log(ui.AppLogger, "config.legacy.read", ui.A{
"path": path})

byteValue, _ := io.ReadAll(configFile)

Expand All @@ -301,10 +301,10 @@ func readLegacyConfigFormat(path string, home string, name string) (error, strin
p.Dirty = true
p.Name = profileName
Configurations[profileName] = p
ui.Log(ui.AppLogger, "config.legacy.load",
"name", profileName,
"id", p.ID,
"count", len(p.Items))
ui.Log(ui.AppLogger, "config.legacy.load", ui.A{
"name": profileName,
"id": p.ID,
"count": len(p.Items)})
}
}
} else {
Expand Down Expand Up @@ -364,18 +364,18 @@ func Save() error {
if err != nil {
err = errors.New(err)

ui.Log(ui.AppLogger, "config.save.error",
"name", name,
"error", err)
ui.Log(ui.AppLogger, "config.save.error", ui.A{
"name": name,
"error": err})

break
} else {
Configurations[name] = profile

ui.Log(ui.AppLogger, "config.save",
"name", name,
"id", profile.ID,
"path", path)
ui.Log(ui.AppLogger, "config.save", ui.A{
"name": name,
"id": profile.ID,
"path": path})
}
}

Expand All @@ -396,8 +396,8 @@ func Save() error {
// Write any keys that are intended to be stored outside the configuration into separate files.
func saveOutboardConfigItems(profile *Configuration, home string, name string, err error, savedItems map[string]string) {
for token, file := range fileMapping {
ui.Log(ui.AppLogger, "config.external.check",
"name", token)
ui.Log(ui.AppLogger, "config.external.check", ui.A{
"name": token})

// We only do this for key values that exist and are non-empty.
if value, ok := profile.Items[token]; ok && len(value) > 0 {
Expand All @@ -406,14 +406,14 @@ func saveOutboardConfigItems(profile *Configuration, home string, name string, e
// Encrypt the value using the salt as the password
value, err = Encrypt(value, profile.Name+profile.Salt+profile.ID)
if err != nil {
ui.Log(ui.AppLogger, "config.external.encrypt.error",
"name", token,
"error", err)
ui.Log(ui.AppLogger, "config.external.encrypt.error", ui.A{
"name": token,
"error": err})

continue
} else {
ui.Log(ui.AppLogger, "config.external.encxrypt",
"name", token)
ui.Log(ui.AppLogger, "config.external.encxrypt", ui.A{
"name": token})

value = encryptionPrefixTag + value
}
Expand All @@ -431,19 +431,19 @@ func saveOutboardConfigItems(profile *Configuration, home string, name string, e
if err != nil {
err = errors.New(err)

ui.Log(ui.AppLogger, "config.external.write.error",
"name", token,
"path", fileName,
"error", err)
ui.Log(ui.AppLogger, "config.external.write.error", ui.A{
"name": token,
"path": fileName,
"error": err})

break
} else {
savedItems[token] = profile.Items[token]

delete(profile.Items, token)
ui.Log(ui.AppLogger, "config.extenral.write",
"name", token,
"path", fileName)
ui.Log(ui.AppLogger, "config.extenral.write", ui.A{
"name": token,
"path": fileName})
}
}
} else {
Expand All @@ -453,15 +453,15 @@ func saveOutboardConfigItems(profile *Configuration, home string, name string, e

err := os.Remove(fileName)
if err == nil {
ui.Log(ui.AppLogger, "config.external.deleted",
"path", fileName)
ui.Log(ui.AppLogger, "config.external.deleted", ui.A{
"path": fileName})
} else if !goerr.Is(err, fs.ErrNotExist) {
ui.Log(ui.AppLogger, "config.external.delete.error",
"path", fileName,
"error", err)
ui.Log(ui.AppLogger, "config.external.delete.error", ui.A{
"path": fileName,
"error": err})
} else {
ui.Log(ui.AppLogger, "config.external.not.found",
"path", fileName)
ui.Log(ui.AppLogger, "config.external.not.found", ui.A{
"path": fileName})
}
}
}
Expand Down Expand Up @@ -509,9 +509,9 @@ func DeleteProfile(key string) error {
// profiles need to be refreshed on disk.
path := filepath.Join(home, ProfileDirectory, key+".profile")
if err = os.Remove(path); err != nil {
ui.Log(ui.AppLogger, "config.delete.error",
"path", path,
"error", err)
ui.Log(ui.AppLogger, "config.delete.error", ui.A{
"path": path,
"error": err})
} else {
err = Save()
}
Expand All @@ -523,29 +523,29 @@ func DeleteProfile(key string) error {
if _, err := os.Stat(fileName); err == nil {
err = os.Remove(fileName)
if err == nil {
ui.Log(ui.AppLogger, "config.deleted",
"name", key,
"path", fileName)
ui.Log(ui.AppLogger, "config.deleted", ui.A{
"name": key,
"path": fileName})
} else {
ui.Log(ui.AppLogger, "config.external.delete.error",
"path", fileName,
"error", err)
ui.Log(ui.AppLogger, "config.external.delete.error", ui.A{
"path": fileName,
"error": err})
}
}
}

// If the deletion was successful, log the deletion.
if err == nil {
ui.Log(ui.AppLogger, "config.deleted",
"name", key,
"path", path)
ui.Log(ui.AppLogger, "config.deleted", ui.A{
"name": key,
"path": path})
}

return err
}

ui.Log(ui.AppLogger, "config.delete.not.found",
"name", key)
ui.Log(ui.AppLogger, "config.delete.not.found", ui.A{
"name": key})

return errors.ErrNoSuchProfile.Context(key)
}
Expand Down
10 changes: 5 additions & 5 deletions app-cli/settings/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func Set(key string, value string) {
func SetDefault(key string, value string) {
explicitValues.Items[key] = value

ui.Log(ui.AppLogger, "config.set.default",
"name", key,
"value", value)
ui.Log(ui.AppLogger, "config.set.default", ui.A{
"name": key,
"value": value})
}

// ClearDefaults clears all emphemeral (default) keys from the explicit values map.
Expand Down Expand Up @@ -134,8 +134,8 @@ func Delete(key string) error {
delete(c.Items, key)
}

ui.Log(ui.AppLogger, "config.deleting",
"name", key)
ui.Log(ui.AppLogger, "config.deleting", ui.A{
"name": key})

return nil
}
Expand Down
Loading

0 comments on commit bedb4fd

Please sign in to comment.