diff --git a/cmd/update.go b/cmd/update.go index 1bd35b1..7ff273f 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -7,8 +7,12 @@ import ( // updateCmd represents the update command var updateCmd = &cobra.Command{ - Use: "update", - Short: "Update a single site records", + Use: "update", + Short: "Update a single site records", + Long: ` +Update a single site records either by the user_name or site_name +NOTE: + When there are records with the same user_name, only the first one gets updated`, Aliases: []string{"u"}, } diff --git a/internal/model/model_handler.go b/internal/model/model_handler.go index a1537f4..260d7f5 100644 --- a/internal/model/model_handler.go +++ b/internal/model/model_handler.go @@ -3,6 +3,7 @@ package model import ( "errors" "fmt" + "reflect" "strings" "github.com/charmbracelet/huh" @@ -54,13 +55,13 @@ func UpdateRecord(recordType RecordType) { if recordType == SiteName { sitename := GetInput("Site record to Update") - err := db.Where("site = ?", sitename).First(&site) + err := db.Where("name = ?", sitename).First(&site) if err.Error != nil { del.Printf("Record not found") return } - s := UpdateRec(site) - success.Printf("\nUpdated {%s} to {%s}\n\n", sitename, s.Name) + updatedSite := UpdateRec(site) + printChanges(site, updatedSite) } else if recordType == Username { user := GetInput("User record to Update") err := db.Where("user_name = ?", user).First(&site) @@ -68,8 +69,8 @@ func UpdateRecord(recordType RecordType) { del.Printf("Record not found") return } - s := UpdateRec(site) - success.Printf("\nUpdated {%s} to {%s}\n\n", user, s.UserName) + updatesSite := UpdateRec(site) + printChanges(site, updatesSite) } } @@ -143,3 +144,18 @@ func UpdateRec(site Site) Site { db.Save(&site) return site } + +func printChanges(prev, updated Site) { + originalValue := reflect.ValueOf(prev) + updatedValue := reflect.ValueOf(updated) + for i := 0; i < originalValue.NumField(); i++ { + origField := originalValue.Field(i) + updtField := updatedValue.Field(i) + if !reflect.DeepEqual(origField.Interface(), updtField.Interface()) { + field := bold.Sprintf("%v", originalValue.Type().Field(i).Name) + prevVal := bold.Sprintf("%v", origField.Interface()) + newVal := bold.Sprintf("%v", updtField.Interface()) + success.Printf("%s changed from %s to %s\n", field, prevVal, newVal) + } + } +}