Exemplo n.º 1
0
// Write puts a value back into the manager. Example usage:
// Default Scope: Write(config.Path("currency", "option", "base"), config.Value("USD"))
// Website Scope: Write(config.Path("currency", "option", "base"), config.Value("EUR"), config.ScopeWebsite(w))
// Store   Scope: Write(config.Path("currency", "option", "base"), config.ValueReader(resp.Body), config.ScopeStore(s))
func (m *Manager) Write(o ...ArgFunc) error {
	a, err := newArg(o...)
	if err != nil {
		return log.Error("config.Manager.Write.newArg", "err", err)
	}

	if a.isBubbling() {
		if log.IsDebug() {
			log.Debug("config.Manager.Write.isBubbling", "path", a.scopePathDefault(), "bubble", a.isBubbling(), "val", a.v)
		}
		m.v.Set(a.scopePathDefault(), a.v)
		aDefault := a
		aDefault.sg = ScopeDefaultID
		aDefault.si = ScopeID(0)
		m.sendMsg(aDefault)
	}

	if log.IsDebug() {
		log.Debug("config.Manager.Write", "path", a.scopePath(), "val", a.v)
	}
	a.scopeID()
	m.v.Set(a.scopePath(), a.v)
	m.sendMsg(a)
	return nil
}
Exemplo n.º 2
0
func ToIntSliceE(i interface{}) ([]int, error) {
	if log.IsDebug() {
		log.Debug("cast=ToIntSliceE", "type", reflect.TypeOf(i), "value", i)
	}

	if i == nil {
		return []int{}, fmt.Errorf("Unable to Cast %#v to []int", i)
	}

	switch v := i.(type) {
	case []int:
		return v, nil
	}

	kind := reflect.TypeOf(i).Kind()
	switch kind {
	case reflect.Slice, reflect.Array:
		s := reflect.ValueOf(i)
		a := make([]int, s.Len())
		for j := 0; j < s.Len(); j++ {
			val, err := ToIntE(s.Index(j).Interface())
			if err != nil {
				return []int{}, fmt.Errorf("Unable to Cast %#v to []int", i)
			}
			a[j] = val
		}
		return a, nil
	default:
		return []int{}, fmt.Errorf("Unable to Cast %#v to []int", i)
	}

	return []int{}, fmt.Errorf("Unable to Cast %#v to []int", i)
}
Exemplo n.º 3
0
// SetMandrill sets the Mandrill API for sending emails. This function is not
// recursive and returns nil. @todo
func SetMandrill(opts ...MandrillOptions) DaemonOption {
	return func(da *Daemon) DaemonOption {
		// this whole func is just a quick write down. no idea if it's working
		// and refactor ... 8-)
		apiKey := da.Config.GetString(config.ScopeStore(da.ScopeID), config.Path(PathSmtpMandrillAPIKey))

		if apiKey == "" {
			da.lastErrs = append(da.lastErrs, errors.New("Mandrill API Key is empty."))
			return nil
		}

		md, err := gochimp.NewMandrill(apiKey)
		if err != nil {
			da.lastErrs = append(da.lastErrs, err)
			return nil
		}
		for _, o := range opts {
			o(md)
		}

		da.sendFunc = func(from string, to []string, msg io.WriterTo) error {

			// @todo figure out if "to" contains To, CC and BCC addresses.

			addr, err := mail.ParseAddress(from)
			if err != nil {
				return log.Error("mail.daemon.Mandrill.ParseAddress", "err", err, "from", from, "to", to)
			}

			r := gochimp.Recipient{
				Name:  addr.Name,
				Email: addr.Address,
			}

			var buf bytes.Buffer
			if _, err := msg.WriteTo(&buf); err != nil {
				return log.Error("mail.daemon.Mandrill.MessageWriteTo", "err", err, "from", from, "to", to, "msg", buf.String())
			}

			resp, err := md.MessageSendRaw(buf.String(), to, r, false)
			if err != nil {
				return log.Error("mail.daemon.Mandrill.MessageSendRaw", "err", err, "from", from, "to", to, "msg", buf.String())
			}
			if log.IsDebug() {
				log.Debug("mail.daemon.Mandrill.MessageSendRaw", "resp", resp, "from", from, "to", to, "msg", buf.String())
			}
			// The last arg in MessageSendRaw means async in the Mandrill API:
			// Async: enable a background sending mode that is optimized for bulk sending.
			// In async mode, messages/send will immediately return a status of "queued"
			// for every recipient. To handle rejections when sending in async mode, set
			// up a webhook for the 'reject' event. Defaults to false for messages with
			// no more than 10 recipients; messages with more than 10 recipients are
			// always sent asynchronously, regardless of the value of async.
			return nil
		}
		da.dialer = nil

		return nil
	}
}
Exemplo n.º 4
0
func TestNull(t *testing.T) {
	log.SetNull()
	log.SetLevel(-1000)
	if log.IsTrace() {
		t.Error("There should be no trace")
	}
	if log.IsDebug() {
		t.Error("There should be no debug")
	}
	if log.IsInfo() {
		t.Error("There should be no info")
	}
	if log.IsWarn() {
		t.Error("There should be no warn")
	}
	var args []interface{}
	args = append(args, "key1", 1, "key2", 3.14152)

	log.Trace("Hello World", args...)
	log.Debug("Hello World", args...)
	log.Info("Hello World", args...)
	log.Warn("Hello World", args...)
	log.Error("Hello World", args...)
	log.Log(1, "Hello World", args)
}
Exemplo n.º 5
0
// Write puts a value back into the manager. Example usage:
// Default Scope: Write(config.Path("currency", "option", "base"), config.Value("USD"))
// Website Scope: Write(config.Path("currency", "option", "base"), config.Value("EUR"), config.ScopeWebsite(w))
// Store   Scope: Write(config.Path("currency", "option", "base"), config.ValueReader(resp.Body), config.ScopeStore(s))
func (m *Manager) Write(o ...ArgFunc) error {
	a := newArg(o...)
	if a.isBubbling() {
		if log.IsDebug() {
			log.Debug("Manager=Write", "path", a.scopePathDefault(), "bubble", a.isBubbling(), "val", a.v)
		}
		m.v.Set(a.scopePathDefault(), a.v)
	}

	if log.IsDebug() {
		log.Debug("Manager=Write", "path", a.scopePath(), "val", a.v)
	}
	m.v.Set(a.scopePath(), a.v)

	return nil
}
Exemplo n.º 6
0
// ToStringSliceE casts an empty interface to a []string.
func ToStringSliceE(i interface{}) ([]string, error) {
	if log.IsDebug() {
		log.Debug("cast.ToStringSliceE", "type", reflect.TypeOf(i), "value", i)
	}

	var a []string

	switch v := i.(type) {
	case []interface{}:
		for _, u := range v {
			a = append(a, ToString(u))
		}
		return a, nil
	case []string:
		return v, nil
	case string:
		return strings.Fields(v), nil
	case interface{}:
		str, err := ToStringE(v)
		if err != nil {
			return a, fmt.Errorf("Unable to Cast %#v to []string", i)
		}
		return []string{str}, nil
	default:
		return a, fmt.Errorf("Unable to Cast %#v to []string", i)
	}
}
Exemplo n.º 7
0
func ToFloat64E(i interface{}) (float64, error) {
	i = indirect(i)
	if log.IsDebug() {
		log.Debug("cast=ToFloat64E", "type", reflect.TypeOf(i), "value", i)
	}

	switch s := i.(type) {
	case float64:
		return s, nil
	case float32:
		return float64(s), nil
	case int64:
		return float64(s), nil
	case int32:
		return float64(s), nil
	case int16:
		return float64(s), nil
	case int8:
		return float64(s), nil
	case int:
		return float64(s), nil
	case string:
		v, err := strconv.ParseFloat(s, 64)
		if err == nil {
			return float64(v), nil
		} else {
			return 0.0, fmt.Errorf("Unable to Cast %#v to float", i)
		}
	default:
		return 0.0, fmt.Errorf("Unable to Cast %#v to float", i)
	}
}
Exemplo n.º 8
0
func ToStringE(i interface{}) (string, error) {
	i = indirectToStringerOrError(i)
	if log.IsDebug() {
		log.Debug("cast=ToStringE", "type", reflect.TypeOf(i), "value", i)
	}

	switch s := i.(type) {
	case string:
		return s, nil
	case float64:
		return strconv.FormatFloat(i.(float64), 'f', -1, 64), nil
	case int:
		return strconv.FormatInt(int64(i.(int)), 10), nil
	case []byte:
		return string(s), nil
	case template.HTML:
		return string(s), nil
	case nil:
		return "", nil
	case fmt.Stringer:
		return s.String(), nil
	case error:
		return s.Error(), nil
	default:
		return "", fmt.Errorf("Unable to Cast %#v to string", i)
	}
}
Exemplo n.º 9
0
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
	if log.IsDebug() {
		log.Debug("cast=ToStringMapBoolE", "type", reflect.TypeOf(i), "value", i)
	}

	var m = map[string]bool{}

	switch v := i.(type) {
	case map[interface{}]interface{}:
		for k, val := range v {
			m[ToString(k)] = ToBool(val)
		}
		return m, nil
	case map[string]interface{}:
		for k, val := range v {
			m[ToString(k)] = ToBool(val)
		}
		return m, nil
	case map[string]bool:
		return v, nil
	default:
		return m, fmt.Errorf("Unable to Cast %#v to map[string]bool", i)
	}
	return m, fmt.Errorf("Unable to Cast %#v to map[string]bool", i)
}
Exemplo n.º 10
0
// ApplyDefaults reads the map and applies the keys and values to the default configuration
func (m *Manager) ApplyDefaults(ss Sectioner) *Manager {
	for k, v := range ss.Defaults() {
		if log.IsDebug() {
			log.Debug("config.Manager.ApplyDefaults", k, v)
		}
		m.v.Set(k, v)
	}
	return m
}
Exemplo n.º 11
0
// ToStringMapStringSliceE casts an empty interface to a map[string][]string.
func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
	if log.IsDebug() {
		log.Debug("cast.ToStringMapStringSliceE", "type", reflect.TypeOf(i), "value", i)
	}

	var m = map[string][]string{}

	switch v := i.(type) {
	case map[string][]string:
		return v, nil
	case map[string][]interface{}:
		for k, val := range v {
			m[ToString(k)] = ToStringSlice(val)
		}
		return m, nil
	case map[string]string:
		for k, val := range v {
			m[ToString(k)] = []string{val}
		}
	case map[string]interface{}:
		for k, val := range v {
			m[ToString(k)] = []string{ToString(val)}
		}
		return m, nil
	case map[interface{}][]string:
		for k, val := range v {
			m[ToString(k)] = ToStringSlice(val)
		}
		return m, nil
	case map[interface{}]string:
		for k, val := range v {
			m[ToString(k)] = ToStringSlice(val)
		}
		return m, nil
	case map[interface{}][]interface{}:
		for k, val := range v {
			m[ToString(k)] = ToStringSlice(val)
		}
		return m, nil
	case map[interface{}]interface{}:
		for k, val := range v {
			key, err := ToStringE(k)
			if err != nil {
				return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
			}
			value, err := ToStringSliceE(val)
			if err != nil {
				return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
			}
			m[key] = value
		}
	default:
		return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
	}
	return m, nil
}
Exemplo n.º 12
0
func defaultAdapterErrFunc(rw http.ResponseWriter, req *http.Request, err error) {
	if log.IsDebug() {
		log.Error("ctxhttp.AdapterErrorFunc", "err", err, "req", req, "url", req.URL)
	}
	code := http.StatusBadRequest
	http.Error(rw, fmt.Sprintf(
		"%s\nApp Error: %s",
		http.StatusText(code),
		err,
	), code)
}
Exemplo n.º 13
0
func TestNull(t *testing.T) {
	log.SetLevel(-1000)
	if !log.IsDebug() {
		t.Error("There should be debug logging")
	}
	if !log.IsInfo() {
		t.Error("There should be info logging")
	}
	var args []interface{}
	args = append(args, "key1", 1, "key2", 3.14152)

	log.Debug("Hello World", args...)
	log.Info("Hello World", args...)
}
Exemplo n.º 14
0
Arquivo: rest.go Projeto: postfix/csfw
// RESTErrorHandler default REST error handler ... @todo remove echo dependency
func RESTErrorHandler(err error, c *echo.Context) {
	code := http.StatusInternalServerError
	msg := http.StatusText(code)
	if he, ok := err.(*echo.HTTPError); ok {
		code = he.Code()
		msg = he.Error()
	}
	if log.IsDebug() {
		log.Error("net.RESTErrorHandler", "err", err)
	}
	msg = err.Error()

	http.Error(c.Response(), msg, code)

}
Exemplo n.º 15
0
func ToDurationE(i interface{}) (d time.Duration, err error) {
	i = indirect(i)
	if log.IsDebug() {
		log.Debug("cast=ToDurationE", "type", reflect.TypeOf(i), "value", i)
	}
	switch s := i.(type) {
	case time.Duration:
		return s, nil
	case string:
		d, err = time.ParseDuration(s)
		return
	default:
		err = fmt.Errorf("Unable to Cast %#v to Duration\n", i)
		return
	}
}
Exemplo n.º 16
0
// ApplyCoreConfigData reads the table core_config_data into the Manager and overrides
// existing values. If the column value is NULL entry will be ignored.
func (m *Manager) ApplyCoreConfigData(dbrSess dbr.SessionRunner) error {
	var ccd TableCoreConfigDataSlice
	rows, err := csdb.LoadSlice(dbrSess, TableCollection, TableIndexCoreConfigData, &ccd)
	if log.IsDebug() {
		log.Debug("config.Manager.ApplyCoreConfigData", "rows", rows)
	}
	if err != nil {
		return log.Error("config.Manager.ApplyCoreConfigData.LoadSlice", "err", err)
	}

	for _, cd := range ccd {
		if cd.Value.Valid {
			// ScopeID(cd.ScopeID) because cd.ScopeID is a struct field and cannot satisfy interface ScopeIDer
			m.Write(Path(cd.Path), Scope(GetScopeGroup(cd.Scope), ScopeID(cd.ScopeID)))
		}
	}
	return nil
}
Exemplo n.º 17
0
func ToTimeE(i interface{}) (tim time.Time, err error) {
	i = indirect(i)
	if log.IsDebug() {
		log.Debug("cast=ToTimeE", "type", reflect.TypeOf(i), "value", i)
	}

	switch s := i.(type) {
	case time.Time:
		return s, nil
	case string:
		d, e := StringToDate(s)
		if e == nil {
			return d, nil
		}
		return time.Time{}, fmt.Errorf("Could not parse Date/Time format: %v\n", e)
	default:
		return time.Time{}, fmt.Errorf("Unable to Cast %#v to Time\n", i)
	}
}
Exemplo n.º 18
0
func ToBoolE(i interface{}) (bool, error) {
	i = indirect(i)
	if log.IsDebug() {
		log.Debug("cast=ToBoolE", "type", reflect.TypeOf(i), "value", i)
	}
	switch b := i.(type) {
	case bool:
		return b, nil
	case nil:
		return false, nil
	case int:
		if i.(int) != 0 {
			return true, nil
		}
		return false, nil
	case string:
		return strconv.ParseBool(i.(string))
	default:
		return false, fmt.Errorf("Unable to Cast %#v to bool", i)
	}
}
Exemplo n.º 19
0
// ToSliceE casts an empty interface to a []interface{}.
func ToSliceE(i interface{}) ([]interface{}, error) {
	if log.IsDebug() {
		log.Debug("cast.ToSliceE", "type", reflect.TypeOf(i), "value", i)
	}

	var s []interface{}

	switch v := i.(type) {
	case []interface{}:
		for _, u := range v {
			s = append(s, u)
		}
		return s, nil
	case []map[string]interface{}:
		for _, u := range v {
			s = append(s, u)
		}
		return s, nil
	default:
		return s, fmt.Errorf("Unable to Cast %#v of type %v to []interface{}", i, reflect.TypeOf(i))
	}
}
Exemplo n.º 20
0
func ToIntE(i interface{}) (int, error) {
	i = indirect(i)
	if log.IsDebug() {
		log.Debug("cast=ToIntE", "type", reflect.TypeOf(i), "value", i)
	}

	switch s := i.(type) {
	case int:
		return s, nil
	case int64:
		return int(s), nil
	case int32:
		return int(s), nil
	case int16:
		return int(s), nil
	case int8:
		return int(s), nil
	case string:
		v, err := strconv.ParseInt(s, 0, 0)
		if err == nil {
			return int(v), nil
		} else {
			return 0, fmt.Errorf("Unable to Cast %#v to int", i)
		}
	case float64:
		return int(s), nil
	case bool:
		if bool(s) {
			return 1, nil
		} else {
			return 0, nil
		}
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("Unable to Cast %#v to int", i)
	}
}
Exemplo n.º 21
0
// GetTables returns all tables from a database. AndWhere can be optionally applied.
// Only first index (0) will be added.
func GetTables(dbrSess dbr.SessionRunner, sql ...string) ([]string, error) {

	qry := "SHOW TABLES"
	if len(sql) > 0 && sql[0] != "" {
		if false == dbr.Stmt.IsSelect(sql[0]) {
			qry = qry + " LIKE '" + sql[0] + "'"
		} else {
			qry = sql[0]
		}
	}
	if log.IsDebug() { // this if reduces 9 allocs ...
		defer log.WhenDone().Debug("Stats", "Package", "codegen", "Step", "GetTables", "query", qry)
	}

	sb := dbrSess.SelectBySql(qry)
	query, args := sb.ToSql()
	rows, err := sb.Query(query, args...)
	if err != nil {
		return nil, log.Error("codegen.GetTables.Query", "err", err)
	}
	defer rows.Close()

	var tableName string
	var tableNames = make([]string, 0, 200)
	for rows.Next() {
		if err := rows.Scan(&tableName); err != nil {
			return nil, log.Error("codegen.GetTables.Scan", "err", err)
		}
		tableNames = append(tableNames, tableName)
	}

	if err = rows.Err(); err != nil {
		return nil, log.Error("codegen.GetTables.rows", "err", err)
	}
	return tableNames, nil
}