k3s/pkg/util/util.go

155 lines
3.7 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-06-12 00:01:02 +00:00
2014-06-06 23:40:48 +00:00
package util
import (
"encoding/json"
2014-06-14 01:11:32 +00:00
"fmt"
"runtime"
2014-06-06 23:40:48 +00:00
"time"
"github.com/golang/glog"
2014-06-06 23:40:48 +00:00
)
2014-06-29 00:52:08 +00:00
// For testing, bypass HandleCrash.
var ReallyCrash bool
2014-07-11 00:32:42 +00:00
// HandleCrash simply catches a crash and logs an error. Meant to be called via defer.
2014-06-06 23:40:48 +00:00
func HandleCrash() {
2014-06-29 00:52:08 +00:00
if ReallyCrash {
return
}
2014-06-06 23:40:48 +00:00
r := recover()
if r != nil {
2014-06-14 01:11:32 +00:00
callers := ""
for i := 0; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
callers = callers + fmt.Sprintf("%v:%v\n", file, line)
}
glog.Infof("Recovered from panic: %#v (%v)\n%v", r, r, callers)
2014-06-06 23:40:48 +00:00
}
}
2014-07-11 00:32:42 +00:00
// Forever loops forever running f every d. Catches any panics, and keeps going.
2014-06-06 23:40:48 +00:00
func Forever(f func(), period time.Duration) {
for {
func() {
defer HandleCrash()
f()
}()
time.Sleep(period)
}
}
2014-07-11 00:32:42 +00:00
// MakeJSONString returns obj marshalled as a JSON string, ignoring any errors.
func MakeJSONString(obj interface{}) string {
data, _ := json.Marshal(obj)
2014-06-06 23:40:48 +00:00
return string(data)
}
// IntOrString is a type that can hold an int or a string. When used in
// JSON or YAML marshalling and unmarshalling, it produces or consumes the
// inner type. This allows you to have, for example, a JSON field that can
// accept a name or number.
type IntOrString struct {
Kind IntstrKind
IntVal int
StrVal string
}
2014-07-11 00:32:42 +00:00
// IntstrKind represents the stored type of IntOrString.
type IntstrKind int
const (
2014-07-11 00:32:42 +00:00
IntstrInt IntstrKind = iota // The IntOrString holds an int.
IntstrString // The IntOrString holds a string.
)
2014-07-11 00:32:42 +00:00
// SetYAML implements the yaml.Setter interface.
func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
2014-07-11 18:22:22 +00:00
switch v := value.(type) {
case int:
intstr.Kind = IntstrInt
2014-07-11 18:22:22 +00:00
intstr.IntVal = v
return true
2014-07-11 18:22:22 +00:00
case string:
intstr.Kind = IntstrString
2014-07-11 18:22:22 +00:00
intstr.StrVal = v
return true
}
return false
}
2014-07-11 00:32:42 +00:00
// GetYAML implements the yaml.Getter interface.
func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
switch intstr.Kind {
case IntstrInt:
value = intstr.IntVal
case IntstrString:
value = intstr.StrVal
default:
panic("impossible IntOrString.Kind")
}
return
}
2014-07-11 00:32:42 +00:00
// UnmarshalJSON implements the json.Unmarshaller interface.
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
if value[0] == '"' {
intstr.Kind = IntstrString
return json.Unmarshal(value, &intstr.StrVal)
}
intstr.Kind = IntstrInt
return json.Unmarshal(value, &intstr.IntVal)
}
2014-07-11 00:32:42 +00:00
// MarshalJSON implements the json.Marshaller interface.
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
switch intstr.Kind {
case IntstrInt:
return json.Marshal(intstr.IntVal)
case IntstrString:
return json.Marshal(intstr.StrVal)
default:
2014-07-11 18:22:22 +00:00
return []byte{}, fmt.Errorf("impossible IntOrString.Kind")
}
}
// StringDiff diffs a and b and returns a human readable diff.
func StringDiff(a, b string) string {
ba := []byte(a)
bb := []byte(b)
out := []byte{}
i := 0
for ; i < len(ba) && i < len(bb); i++ {
if ba[i] != bb[i] {
break
}
out = append(out, ba[i])
}
out = append(out, []byte("\n\nA: ")...)
out = append(out, ba[i:]...)
out = append(out, []byte("\n\nB: ")...)
out = append(out, bb[i:]...)
out = append(out, []byte("\n\n")...)
return string(out)
}