gstat/commands/date.go
Augusto Dwenger J. f004c93773 refactor: implement single point for json marshal
This implementation still isn't perfect. One of the encoutered problems
is that it does not omit Memory sturcts that are technicly empty.
Resulting in JSON with empty objects. This should be a problem for other
parsers but it doesn't look good.
2021-01-02 18:10:16 +01:00

36 lines
880 B
Go

package commands
import (
"encoding/json"
"time"
"github.com/hamburghammer/gstat/args"
)
// Date is a the configuration struct to execute the date command
type Date struct {
// GetTime is the function to get the actual Time in string format.
// It should be use to replace/cusomise the time output.
GetTime func() string
}
// NewDate is a convinice constructor for the Date struct.
// It sets the GetTime function to standard formatting.
func NewDate() Date {
return Date{GetTime: getFormattedTime}
}
// Exec is the implementation of the execution interface for the Date struct.
func (d Date) Exec(args args.Arguments) ([]byte, error) {
data := struct{ Date string }{Date: d.PureExec(args)}
return json.Marshal(data)
}
func (d Date) PureExec(args.Arguments) string {
return d.GetTime()
}
func getFormattedTime() string {
return time.Now().Format(time.RFC3339)
}