Skip to content

Commit

Permalink
stop using pointer in StartAt and EndAt
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Feb 10, 2019
1 parent 68b5f56 commit 22b1667
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
39 changes: 17 additions & 22 deletions horenso.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ type horenso struct {

// Report is represents the result of the command
type Report struct {
Command string `json:"command"`
CommandArgs []string `json:"commandArgs"`
Tag string `json:"tag,omitempty"`
Output string `json:"output"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode *int `json:"exitCode,omitempty"`
Signaled bool `json:"signaled"`
Result string `json:"result"`
Hostname string `json:"hostname"`
Pid *int `json:"pid,omitempty"`
StartAt *time.Time `json:"startAt,omitempty"`
EndAt *time.Time `json:"endAt,omitempty"`
SystemTime *float64 `json:"systemTime,omitempty"`
UserTime *float64 `json:"userTime,omitempty"`
Command string `json:"command"`
CommandArgs []string `json:"commandArgs"`
Tag string `json:"tag,omitempty"`
Output string `json:"output"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode *int `json:"exitCode,omitempty"`
Signaled bool `json:"signaled"`
Result string `json:"result"`
Hostname string `json:"hostname"`
Pid *int `json:"pid,omitempty"`
StartAt time.Time `json:"startAt,omitempty"`
EndAt time.Time `json:"endAt,omitempty"`
SystemTime *float64 `json:"systemTime,omitempty"`
UserTime *float64 `json:"userTime,omitempty"`
}

func (ho *horenso) openLog() (io.WriteCloser, error) {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (ho *horenso) run(args []string) (Report, error) {
stderrPipe2 := io.TeeReader(stderrPipe, io.MultiWriter(&bufStderr, wtr))

ho.logf(info, "starting executiton of the command %q", r.Command)
r.StartAt = now()
r.StartAt = time.Now()
err = cmd.Start()
if err != nil {
return ho.failReport(r, err.Error()), err
Expand Down Expand Up @@ -134,7 +134,7 @@ func (ho *horenso) run(args []string) (Report, error) {
ho.logf(warn, "something went wrong while executing the command: %s", err)
}
err = cmd.Wait()
r.EndAt = now()
r.EndAt = time.Now()
es := wrapcommander.ResolveExitStatus(err)
ecode := es.ExitCode()
r.ExitCode = &ecode
Expand All @@ -161,11 +161,6 @@ func (ho *horenso) run(args []string) (Report, error) {
return r, nil
}

func now() *time.Time {
now := time.Now()
return &now
}

func parseArgs(args []string) (*flags.Parser, *horenso, []string, error) {
ho := &horenso{}
p := flags.NewParser(ho, flags.Default)
Expand Down
36 changes: 19 additions & 17 deletions horenso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func TestRun(t *testing.T) {
if r.Stderr != "" {
t.Errorf("output should be empty but: %s", r.Stderr)
}
if r.StartAt == nil {
t.Errorf("StartAt shouldn't be nil")
if r.StartAt.IsZero() {
t.Errorf("StartAt shouldn't be zero")
}
if r.EndAt == nil {
t.Errorf("EtartAt shouldn't be nil")
if r.EndAt.IsZero() {
t.Errorf("EtartAt shouldn't be zero")
}
expectedHostname, _ := os.Hostname()
if r.Hostname != expectedHostname {
Expand All @@ -94,11 +94,11 @@ func TestRun(t *testing.T) {
if nr.Output != "" {
t.Errorf("something went wrong")
}
if nr.StartAt == nil {
t.Errorf("StartAt shouldn't be nil")
if nr.StartAt.IsZero() {
t.Errorf("StartAt shouldn't be zero")
}
if nr.EndAt != nil {
t.Errorf("EndAt should be nil")
if !nr.EndAt.IsZero() {
t.Errorf("EndAt should be zero")
}
if nr.ExitCode != nil {
t.Errorf("ExitCode should be nil")
Expand Down Expand Up @@ -148,11 +148,11 @@ func TestRunHugeOutput(t *testing.T) {
if r.Stderr != "" {
t.Errorf("output should be empty but: %s", r.Stderr)
}
if r.StartAt == nil {
t.Errorf("StartAt shouldn't be nil")
if r.StartAt.IsZero() {
t.Errorf("StartAt shouldn't be zero")
}
if r.EndAt == nil {
t.Errorf("EtartAt shouldn't be nil")
if r.EndAt.IsZero() {
t.Errorf("EtartAt shouldn't be zero")
}
expectedHostname, _ := os.Hostname()
if r.Hostname != expectedHostname {
Expand All @@ -175,11 +175,11 @@ func TestRunHugeOutput(t *testing.T) {
if nr.Output != "" {
t.Errorf("something went wrong")
}
if nr.StartAt == nil {
t.Errorf("StartAt shouldn't be nil")
if nr.StartAt.IsZero() {
t.Errorf("StartAt shouldn't be zero")
}
if nr.EndAt != nil {
t.Errorf("EndAt should be nil")
if !nr.EndAt.IsZero() {
t.Errorf("EndAt should be zero")
}
if nr.ExitCode != nil {
t.Errorf("ExitCode should be nil")
Expand All @@ -200,5 +200,7 @@ func deepEqual(r1, r2 Report) bool {
r1.Result == r2.Result &&
*r1.Pid == *r2.Pid &&
r1.Hostname == r2.Hostname &&
r1.Signaled == r2.Signaled
r1.Signaled == r2.Signaled &&
r1.StartAt.Equal(r2.StartAt) &&
r1.EndAt.Equal(r2.EndAt)
}

0 comments on commit 22b1667

Please sign in to comment.