Skip to content

Commit

Permalink
Additional error case found
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan committed Jun 7, 2024
1 parent 4ea639f commit dbab8d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion host/host_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
// 07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72
// 11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01
// 08:47PM up 2 days, 20 hrs, 1 user, load average: 2.47, 2.17, 2.17
// 01:16AM up 4 days, 29 mins, 1 user, load average: 2.29, 2.31, 2.21
func UptimeWithContext(ctx context.Context) (uint64, error) {
out, err := invoke.CommandWithContext(ctx, "uptime")
if err != nil {
Expand All @@ -74,14 +75,23 @@ func parseUptime(uptime string) uint64 {
}

// day provided along with a single hour or hours
// ie: up 2 days, 20 hrs
// ie: up 2 days, 20 hrs,
if ut[5] == "hr," || ut[5] == "hrs," {
hours, err = strconv.ParseUint(ut[4], 10, 64)
if err != nil {
return 0
}
}

// mins provided along with a single min or mins
// ie: up 4 days, 29 mins,
if ut[5] == "min," || ut[5] == "mins," {
mins, err = strconv.ParseUint(ut[4], 10, 64)
if err != nil {
return 0
}
}

// alternatively day provided with hh:mm
// ie: up 83 days, 18:29
if strings.Contains(ut[4], ":") {
Expand Down
1 change: 1 addition & 0 deletions host/host_aix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestParseUptimeValidInput(t *testing.T) {
{"07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72", 300},
{"11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01", 120629},
{"08:47PM up 2 days, 20 hrs, 1 user, load average: 2.47, 2.17, 2.17", 4080},
{"01:16AM up 4 days, 29 mins, 1 user, load average: 2.29, 2.31, 2.21", 5789},
}
for _, tc := range testCases {
got := parseUptime(tc.input)
Expand Down

0 comments on commit dbab8d8

Please sign in to comment.