Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Dec 2, 2024
1 parent ec3747e commit 210c623
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 103 deletions.
2 changes: 1 addition & 1 deletion cli/command_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func DestroyCommand(args CommandArgs) int {
}

logger.Info(id, "Instance destroyed")
fmtc.Printf("{*}Done. Instance with ID %d successfully destroyed.{!}\n", id)
fmtc.Printfn("{*}Done. Instance with ID %d successfully destroyed.{!}", id)

err = SC.PropagateCommand(API.COMMAND_DESTROY, id, meta.UUID)

Expand Down
2 changes: 1 addition & 1 deletion cli/command_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func EditCommand(args CommandArgs) int {
logger.Info(id, "Instance meta updated: %s", c)
}

fmtc.Printf("{g}Done. Data for instance with ID %d successfully updated.{!}\n", id)
fmtc.Printfn("{g}Done. Data for instance with ID %d successfully updated.{!}", id)

err = SC.PropagateCommand(API.COMMAND_EDIT, meta.ID, meta.UUID)

Expand Down
2 changes: 1 addition & 1 deletion cli/command_gen_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GenTokenCommand(args CommandArgs) int {

if !useRawOutput {
fmtutil.Separator(true)
fmtc.Printf("\n {*}Token:{!} %s\n\n", token)
fmtc.Printfn("\n {*}Token:{!} %s\n", token)
fmtutil.Separator(true)
} else {
fmt.Println(token)
Expand Down
20 changes: 10 additions & 10 deletions cli/command_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ const (
func GoCommand(args CommandArgs) int {
generateSUAuthData()

fmtc.NameColor("app", "{c*}")
fmtc.NameColor("cmd", "{#110}")
fmtc.NameColor("cfg", "{#103}")
fmtc.NameColor("prop", "{#173}")
fmtc.AddColor("app", "{c*}")
fmtc.AddColor("cmd", "{#110}")
fmtc.AddColor("cfg", "{#103}")
fmtc.AddColor("prop", "{#173}")

configurationStatus, _ := CORE.GetSystemConfigurationStatus(false)

Expand Down Expand Up @@ -183,8 +183,8 @@ func GoCommand(args CommandArgs) int {
time.Sleep(5 * time.Second)
}
} else {
fmtc.Printf(
"{s}Anyway, you can find system configuration guide at\n{_}%s{!}\n",
fmtc.Printfn(
"{s}Anyway, you can find system configuration guide at\n{_}%s{!}",
_GUIDE_TUNING_URL,
)
}
Expand Down Expand Up @@ -215,10 +215,10 @@ func GoCommand(args CommandArgs) int {
guideData.String(), panel.INDENT_OUTER,
)

fmtc.Printf("{s}More information can be found at {_}%s{!}\n", _GUIDE_SYNCING_URL)
fmtc.Printfn("{s}More information can be found at {_}%s{!}", _GUIDE_SYNCING_URL)
} else {
fmtc.Printf(
"{s}Anyway, you can find syncing configuration guide at\n{_}%s{!}\n",
fmtc.Printfn(
"{s}Anyway, you can find syncing configuration guide at\n{_}%s{!}",
_GUIDE_SYNCING_URL,
)
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func generateSUAuthData() {
fmtc.Println("We have generated a password for you. With this password, you can execute {*}any{!} command on {*}any{!} instance.")

fmtutil.Separator(false)
fmtc.Printf(" {*}Your superuser password is:{!} {c@*} %s {!}\n", password)
fmtc.Printfn(" {*}Your superuser password is:{!} {c@*} %s {!}", password)
fmtutil.Separator(false)

terminal.Warn("Warning! We don't save any passwords. Please save this password in a safe place.")
Expand Down
60 changes: 30 additions & 30 deletions cli/command_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,29 +506,29 @@ func helpCommandList() {
info.renderArguments()

fmtc.Println(" Avialalbe filter values:\n")
fmtc.Printf(" {b}%-13s{!} %s\n", "my", "My (owned by current user) instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "works", "Working instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "stopped", "Stopped instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "idle", "Instances with no traffic (less than 5 commands per second)")
fmtc.Printf(" {b}%-13s{!} %s\n", "active", "Instances with traffic")
fmtc.Printf(" {b}%-13s{!} %s\n", "dead", "Dead instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "hang", "Hang instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "syncing", "Syncing instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "saving", "Saving instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "loading", "Loading instances")
fmtc.Printf(" {b}%-13s{!} %s\n", "abandoned", "Instances with no traffic for a long time (≥ week)")
fmtc.Printf(" {b}%-13s{!} %s\n", "master-up", "Instances with working syncing")
fmtc.Printf(" {b}%-13s{!} %s\n", "master-down", "Instances with broken syncing")
fmtc.Printf(" {b}%-13s{!} %s\n", "no-replica", "Instances with no replicas")
fmtc.Printf(" {b}%-13s{!} %s\n", "with-replica", "Instances with one or more replicas")
fmtc.Printf(" {b}%-13s{!} %s\n", "with-errors", "Instances with errors")
fmtc.Printf(" {b}%-13s{!} %s\n", "outdated", "Instances which require restart for update")
fmtc.Printf(" {b}%-13s{!} %s\n", "orphan", "Instances owned by deleted users")
fmtc.Printf(" {b}%-13s{!} %s\n", "standby", "Instances with standby replication")
fmtc.Printf(" {b}%-13s{!} %s\n", "replica", "Instances with real replicas")
fmtc.Printf(" {b}%-13s{!} %s\n", "secure", "Instances with enabled authentication")
fmtc.Printf(" {b}%-13s{!} %s\n", "@{tag}", "Instances tagged by given tag")
fmtc.Printf(" {b}%-13s{!} %s\n", "{username}", "Instances owned by given user")
fmtc.Printfn(" {b}%-13s{!} %s", "my", "My (owned by current user) instances")
fmtc.Printfn(" {b}%-13s{!} %s", "works", "Working instances")
fmtc.Printfn(" {b}%-13s{!} %s", "stopped", "Stopped instances")
fmtc.Printfn(" {b}%-13s{!} %s", "idle", "Instances with no traffic (less than 5 commands per second)")
fmtc.Printfn(" {b}%-13s{!} %s", "active", "Instances with traffic")
fmtc.Printfn(" {b}%-13s{!} %s", "dead", "Dead instances")
fmtc.Printfn(" {b}%-13s{!} %s", "hang", "Hang instances")
fmtc.Printfn(" {b}%-13s{!} %s", "syncing", "Syncing instances")
fmtc.Printfn(" {b}%-13s{!} %s", "saving", "Saving instances")
fmtc.Printfn(" {b}%-13s{!} %s", "loading", "Loading instances")
fmtc.Printfn(" {b}%-13s{!} %s", "abandoned", "Instances with no traffic for a long time (≥ week)")
fmtc.Printfn(" {b}%-13s{!} %s", "master-up", "Instances with working syncing")
fmtc.Printfn(" {b}%-13s{!} %s", "master-down", "Instances with broken syncing")
fmtc.Printfn(" {b}%-13s{!} %s", "no-replica", "Instances with no replicas")
fmtc.Printfn(" {b}%-13s{!} %s", "with-replica", "Instances with one or more replicas")
fmtc.Printfn(" {b}%-13s{!} %s", "with-errors", "Instances with errors")
fmtc.Printfn(" {b}%-13s{!} %s", "outdated", "Instances which require restart for update")
fmtc.Printfn(" {b}%-13s{!} %s", "orphan", "Instances owned by deleted users")
fmtc.Printfn(" {b}%-13s{!} %s", "standby", "Instances with standby replication")
fmtc.Printfn(" {b}%-13s{!} %s", "replica", "Instances with real replicas")
fmtc.Printfn(" {b}%-13s{!} %s", "secure", "Instances with enabled authentication")
fmtc.Printfn(" {b}%-13s{!} %s", "@{tag}", "Instances tagged by given tag")
fmtc.Printfn(" {b}%-13s{!} %s", "{username}", "Instances owned by given user")
fmtc.NewLine()
fmtc.Println(" If no instances are found using the property search, rds will try to find instances using")
fmtc.Println(" a full-text search of instance descriptions.")
Expand Down Expand Up @@ -1122,10 +1122,10 @@ func (i helpInfo) render() {
func (i helpInfo) renderUsage() {
fmtc.Println("{*}Usage{!}\n")

fmtc.NameColor("app", colorTagApp)
fmtc.AddColor("app", colorTagApp)

if len(i.arguments) == 0 {
fmtc.Printf(" {?app}rds{!} {y}%s{!}\n\n", i.command)
fmtc.Printfn(" {?app}rds{!} {y}%s{!}\n", i.command)
return
}

Expand All @@ -1135,7 +1135,7 @@ func (i helpInfo) renderUsage() {
arguments = append(arguments, cmdArg.name)
}

fmtc.Printf(" {?app}rds{!} {y}%s{!} {#45}%s{!}\n\n", i.command, strings.Join(arguments, " "))
fmtc.Printfn(" {?app}rds{!} {y}%s{!} {#45}%s{!}\n", i.command, strings.Join(arguments, " "))
}

// renderDescription render description
Expand Down Expand Up @@ -1180,7 +1180,7 @@ func (i helpInfo) renderOptions() {
fmtStr := getArgumentFormatting(i.options)

for _, option := range i.options {
fmtc.Printf(" {g}"+fmtStr+"{!} %s\n", option.name, option.desc)
fmtc.Printfn(" {g}"+fmtStr+"{!} %s", option.name, option.desc)
}

fmtc.NewLine()
Expand All @@ -1196,12 +1196,12 @@ func (i helpInfo) renderExamples() {

for index, example := range i.examples {
if example.command == "" {
fmtc.Printf(" rds %s\n", strings.Join([]string{i.command, example.arguments}, " "))
fmtc.Printfn(" rds %s", strings.Join([]string{i.command, example.arguments}, " "))
} else {
fmtc.Printf(" rds %s\n", strings.Join([]string{example.command, example.arguments}, " "))
fmtc.Printfn(" rds %s", strings.Join([]string{example.command, example.arguments}, " "))
}

fmtc.Printf(" {&}{s-}%s{!}\n", example.desc)
fmtc.Printfn(" {&}{s-}%s{!}", example.desc)

if index+1 != len(i.examples) {
fmtc.NewLine()
Expand Down
2 changes: 1 addition & 1 deletion cli/command_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func renderInfoData(t *table.Table, info *REDIS.Info, sections []string) {
}

t.Border()
fmtc.Printf(" ▾ {*}%s{!}\n", strings.ToUpper(section.Header))
fmtc.Printfn(" ▾ {*}%s{!}", strings.ToUpper(section.Header))
t.Border()

for _, v := range section.Fields {
Expand Down
8 changes: 4 additions & 4 deletions cli/command_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func printRDSLogLine(line string) {
}

if target != "" {
fmtc.Printf("{s-}%s{!} {s}%s{!} "+colorTag+"%s{!}\n", date, target, line)
fmtc.Printfn("{s-}%s{!} {s}%s{!} "+colorTag+"%s{!}", date, target, line)
} else {
fmtc.Printf("{s-}%s{!} "+colorTag+"%s{!}\n", date, line)
fmtc.Printfn("{s-}%s{!} "+colorTag+"%s{!}", date, line)
}
}

Expand Down Expand Up @@ -232,8 +232,8 @@ func printRedisLogLine(line string) {
return
}

fmtc.Printf(
"{s-}[ %s/%s/%s %s | %s ]{!} "+colorTag+"%s{!}\n",
fmtc.Printfn(
"{s-}[ %s/%s/%s %s | %s ]{!} "+colorTag+"%s{!}",
year, month, day, hms, role, line[sepIndex+3:],
)
}
14 changes: 7 additions & 7 deletions cli/command_sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func SentinelResetCommand(args CommandArgs) int {
// SentinelStatusCommand is "sentinel-status" command handler
func SentinelStatusCommand(args CommandArgs) int {
if CORE.IsSentinelActive() {
fmtc.Printf("Redis Sentinel is {g}works{!}\n")
fmtc.Printfn("Redis Sentinel is {g}works{!}")
} else {
fmtc.Printf("Redis Sentinel is {y}stopped{!}\n")
fmtc.Printfn("Redis Sentinel is {y}stopped{!}")
}

return EC_OK
Expand Down Expand Up @@ -172,7 +172,7 @@ func SentinelCheckCommand(args CommandArgs) int {
return EC_ERROR
}

fmtc.Printf("{g}%s{!}\n", status)
fmtc.Printfn("{g}%s{!}", status)

return EC_OK
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func SentinelInfoCommand(args CommandArgs) int {
t := table.NewTable().SetSizes(23, 96)

t.Border()
fmtc.Printf("{*} %s{!}\n", "Master")
fmtc.Printfn("{*} %s{!}", "Master")
t.Border()

for _, item := range info.Master {
Expand All @@ -225,7 +225,7 @@ func SentinelInfoCommand(args CommandArgs) int {

if replicasCount != 0 {
t.Border()
fmtc.Printf("{*} %s{!} {s}(%d){!}\n", "Replicas", replicasCount)
fmtc.Printfn("{*} %s{!} {s}(%d){!}", "Replicas", replicasCount)
t.Border()

for index, replica := range info.Replicas {
Expand All @@ -243,7 +243,7 @@ func SentinelInfoCommand(args CommandArgs) int {

if sentinelsCount != 0 {
t.Border()
fmtc.Printf("{*} %s{!} {s}(%d){!}\n", "Sentinels", sentinelsCount)
fmtc.Printfn("{*} %s{!} {s}(%d){!}", "Sentinels", sentinelsCount)
t.Border()

for index, sentinel := range info.Sentinels {
Expand Down Expand Up @@ -290,7 +290,7 @@ func SentinelMasterCommand(args CommandArgs) int {
return EC_ERROR
}

fmtc.Printf("Master IP is {*}%s{!}\n", ip)
fmtc.Printfn("Master IP is {*}%s{!}", ip)

return EC_OK
}
Expand Down
14 changes: 7 additions & 7 deletions cli/command_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func printSettingsSection(section string) {
}

fmtutil.Separator(true)
fmtc.Printf(" ▾ {*}%s{!}\n", strings.ToUpper(section))
fmtc.Printfn(" ▾ {*}%s{!}", strings.ToUpper(section))
fmtutil.Separator(true)

for _, prop := range CORE.Config.Props(section) {
Expand Down Expand Up @@ -111,16 +111,16 @@ func printSettingsProperty(name, value string, hidden bool) {

case strings.HasPrefix(value, "/"):
if fsutil.IsExist(value) {
fmtc.Printf("%s {g}✔ {!}\n", value)
fmtc.Printfn("%s {g}✔ {!}", value)
} else {
fmtc.Printf("%s {r}✖ {!}\n", value)
fmtc.Printfn("%s {r}✖ {!}", value)
}

case name == "user":
if system.IsUserExist(value) {
fmtc.Printf("%s {g}✔ {!}\n", value)
fmtc.Printfn("%s {g}✔ {!}", value)
} else {
fmtc.Printf("%s {r}✖ {!}\n", value)
fmtc.Printfn("%s {r}✖ {!}", value)
}

case name == "virtual-ip":
Expand All @@ -129,9 +129,9 @@ func printSettingsProperty(name, value string, hidden bool) {
case CORE.KEEPALIVED_STATE_UNKNOWN:
fmtc.Println(value)
case CORE.KEEPALIVED_STATE_MASTER:
fmtc.Printf("%s {g}(master){!}\n", value)
fmtc.Printfn("%s {g}(master){!}", value)
case CORE.KEEPALIVED_STATE_BACKUP:
fmtc.Printf("%s {s}(backup){!}\n", value)
fmtc.Printfn("%s {s}(backup){!}", value)
}
} else {
fmtc.Println("{s-}[empty]{!}")
Expand Down
2 changes: 1 addition & 1 deletion cli/command_slowlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func slowlogReset(id int) int {
}

logger.Info(id, "Slow log reset")
fmtc.Printf("{g}Slow log successfully cleared for instance %d{!}\n", id)
fmtc.Printfn("{g}Slow log successfully cleared for instance %d{!}", id)

return EC_OK
}
Expand Down
18 changes: 9 additions & 9 deletions cli/command_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func renderStats(stats *CORE.Stats) {
fmtutil.Separator(true)

renderStatsValue("Total System Memory", stats.Memory.TotalSystemMemory, true)
fmtc.Printf(
" %-26s {s}|{!} %s {s-}(%s ~ %s){!}\n", "System Memory",
fmtc.Printfn(
" %-26s {s}|{!} %s {s-}(%s ~ %s){!}", "System Memory",
fmtutil.PrettyNum(stats.Memory.SystemMemory),
fmtutil.PrettySize(stats.Memory.SystemMemory),
fmtutil.PrettyPerc(mathutil.Perc(stats.Memory.SystemMemory, stats.Memory.TotalSystemMemory)),
Expand All @@ -101,15 +101,15 @@ func renderStats(stats *CORE.Stats) {
switch stats.Memory.IsSwapEnabled {
case true:
renderStatsValue("Total System Swap", stats.Memory.TotalSystemSwap, true)
fmtc.Printf(
" %-26s {s}|{!} %s {s-}(%s ~ %s){!}\n", "System Swap",
fmtc.Printfn(
" %-26s {s}|{!} %s {s-}(%s ~ %s){!}", "System Swap",
fmtutil.PrettyNum(stats.Memory.SystemSwap),
fmtutil.PrettySize(stats.Memory.SystemSwap),
fmtutil.PrettyPerc(mathutil.Perc(stats.Memory.SystemSwap, stats.Memory.TotalSystemSwap)),
)
default:
fmtc.Printf(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}\n", "Total System Swap")
fmtc.Printf(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}\n", "System Swap")
fmtc.Printfn(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}", "Total System Swap")
fmtc.Printfn(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}", "System Swap")
}

renderStatsValue("Used Memory", stats.Memory.UsedMemory, true)
Expand All @@ -120,7 +120,7 @@ func renderStats(stats *CORE.Stats) {
case true:
renderStatsValue("Used Swap", stats.Memory.UsedSwap, true)
default:
fmtc.Printf(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}\n", "Used Swap")
fmtc.Printfn(" %-26s {s}|{!} {s}—{!} {s-}(disabled){!}", "Used Swap")
}

fmtutil.Separator(true)
Expand Down Expand Up @@ -259,8 +259,8 @@ func renderStatsAsXMLValue(name string, value uint64) {
// renderStatsValue print stats property for pretty output
func renderStatsValue(name string, value uint64, isSize bool) {
if isSize {
fmtc.Printf(" %-26s {s}|{!} %s {s-}(%s){!}\n", name, fmtutil.PrettyNum(value), fmtutil.PrettySize(value))
fmtc.Printfn(" %-26s {s}|{!} %s {s-}(%s){!}", name, fmtutil.PrettyNum(value), fmtutil.PrettySize(value))
} else {
fmtc.Printf(" %-26s {s}|{!} %s\n", name, fmtutil.PrettyNum(value))
fmtc.Printfn(" %-26s {s}|{!} %s", name, fmtutil.PrettyNum(value))
}
}
Loading

0 comments on commit 210c623

Please sign in to comment.