Merge pull request #404 from thockin/cleanups

Fix go lint errors in util
pull/6/head
brendandburns 2014-07-10 20:45:30 -07:00
commit ad3c813198
2 changed files with 17 additions and 8 deletions

View File

@ -31,14 +31,16 @@ func init() {
flag.Set("logtostderr", "true")
}
// This serves as a bridge between the standard log package and the glog package.
// GlogWriter serves as a bridge between the standard log package and the glog package.
type GlogWriter struct{}
// Write implements the io.Writer interface.
func (writer GlogWriter) Write(data []byte) (n int, err error) {
glog.Info(string(data))
return len(data), nil
}
// InitLogs initializes logs the way we want for kubernetes.
func InitLogs() {
log.SetOutput(GlogWriter{})
log.SetFlags(0)
@ -46,10 +48,12 @@ func InitLogs() {
go Forever(glog.Flush, *logFlushFreq)
}
// FlushLogs flushes logs immediately.
func FlushLogs() {
glog.Flush()
}
// NewLogger creates a new log.Logger which sends logs to glog.Info.
func NewLogger(prefix string) *log.Logger {
return log.New(GlogWriter{}, prefix, 0)
}

View File

@ -28,7 +28,7 @@ import (
// For testing, bypass HandleCrash.
var ReallyCrash bool
// Simply catches a crash and logs an error. Meant to be called via defer.
// HandleCrash simply catches a crash and logs an error. Meant to be called via defer.
func HandleCrash() {
if ReallyCrash {
return
@ -48,7 +48,7 @@ func HandleCrash() {
}
}
// Loops forever running f every d. Catches any panics, and keeps going.
// Forever loops forever running f every d. Catches any panics, and keeps going.
func Forever(f func(), period time.Duration) {
for {
func() {
@ -59,9 +59,9 @@ func Forever(f func(), period time.Duration) {
}
}
// Returns o marshalled as a JSON string, ignoring any errors.
func MakeJSONString(o interface{}) string {
data, _ := json.Marshal(o)
// MakeJSONString returns obj marshalled as a JSON string, ignoring any errors.
func MakeJSONString(obj interface{}) string {
data, _ := json.Marshal(obj)
return string(data)
}
@ -75,13 +75,15 @@ type IntOrString struct {
StrVal string
}
// IntstrKind represents the stored type of IntOrString.
type IntstrKind int
const (
IntstrInt IntstrKind = iota
IntstrString
IntstrInt IntstrKind = iota // The IntOrString holds an int.
IntstrString // The IntOrString holds a string.
)
// SetYAML implements the yaml.Setter interface.
func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
if intVal, ok := value.(int); ok {
intstr.Kind = IntstrInt
@ -96,6 +98,7 @@ func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
return false
}
// GetYAML implements the yaml.Getter interface.
func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
switch intstr.Kind {
case IntstrInt:
@ -108,6 +111,7 @@ func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
return
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
if value[0] == '"' {
intstr.Kind = IntstrString
@ -117,6 +121,7 @@ func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
return json.Unmarshal(value, &intstr.IntVal)
}
// MarshalJSON implements the json.Marshaller interface.
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
switch intstr.Kind {
case IntstrInt: