3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 19:22:41 +01:00
ergo/irc/utils/args.go

61 lines
1.2 KiB
Go
Raw Normal View History

2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2016-10-23 12:24:02 +02:00
// released under the MIT license
package utils
2016-10-23 12:24:02 +02:00
2019-05-20 08:56:49 +02:00
import (
"errors"
"fmt"
2019-05-20 08:56:49 +02:00
"strings"
"time"
)
const (
IRCv3TimestampFormat = "2006-01-02T15:04:05.000Z"
2019-05-20 08:56:49 +02:00
)
var (
ErrInvalidParams = errors.New("Invalid parameters")
)
func StringToBool(str string) (result bool, err error) {
switch strings.ToLower(str) {
2020-02-19 06:54:42 +01:00
case "on", "true", "t", "yes", "y", "enabled":
2019-05-20 08:56:49 +02:00
result = true
2020-02-19 06:54:42 +01:00
case "off", "false", "f", "no", "n", "disabled":
2019-05-20 08:56:49 +02:00
result = false
default:
err = ErrInvalidParams
}
return
}
// Checks that a parameter can be passed as a non-trailing, and returns "*"
// if it can't. See #697.
func SafeErrorParam(param string) string {
if param == "" || param[0] == ':' || strings.IndexByte(param, ' ') != -1 {
return "*"
}
return param
}
type IncompatibleSchemaError struct {
CurrentVersion int
RequiredVersion int
}
func (err *IncompatibleSchemaError) Error() string {
return fmt.Sprintf("Database requires update. Expected schema v%d, got v%d", err.RequiredVersion, err.CurrentVersion)
}
func NanoToTimestamp(nanotime int64) string {
return time.Unix(0, nanotime).UTC().Format(IRCv3TimestampFormat)
}
2020-03-19 22:09:52 +01:00
func BoolDefaultTrue(value *bool) bool {
if value != nil {
return *value
}
return true
}