Add support for date placeholder in process config

Because it doesn't make sense to replace the date placeholder at
process creation, it has to be replaced at every start of the process.

On process creation only the static placeholders (such as process ID)
are replaced. Dynamic placeholders (so far only "date") are not
replaced. On process start, a callback has been introduced that gives
the chance to change the command line. This is the point where
the restreamer replaces the date placeholders.
This commit is contained in:
Ingo Oppermann
2023-02-28 17:46:08 +01:00
parent f345707c63
commit be718eac0a
32 changed files with 2510 additions and 237 deletions

View File

@@ -42,6 +42,7 @@ import (
"github.com/datarhei/core/v16/update"
"github.com/caddyserver/certmagic"
"github.com/lestrrat-go/strftime"
"go.uber.org/zap"
)
@@ -508,27 +509,46 @@ func (a *api) start() error {
a.replacer = replace.New()
{
a.replacer.RegisterTemplateFunc("diskfs", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("date", func(params map[string]string, config *restreamapp.Config, section string) string {
t, err := time.Parse(time.RFC3339, params["timestamp"])
if err != nil {
return ""
}
s, err := strftime.Format(params["format"], t)
if err != nil {
return ""
}
return s
}, map[string]string{
"format": "%Y-%m-%d_%H-%M-%S",
"timestamp": "$timestamp",
})
a.replacer.RegisterReplaceFunc("diskfs", func(params map[string]string, config *restreamapp.Config, section string) string {
return a.diskfs.Metadata("base")
}, nil)
a.replacer.RegisterTemplateFunc("fs:disk", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("fs:disk", func(params map[string]string, config *restreamapp.Config, section string) string {
return a.diskfs.Metadata("base")
}, nil)
a.replacer.RegisterTemplateFunc("memfs", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("memfs", func(params map[string]string, config *restreamapp.Config, section string) string {
return a.memfs.Metadata("base")
}, nil)
a.replacer.RegisterTemplateFunc("fs:mem", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("fs:mem", func(params map[string]string, config *restreamapp.Config, section string) string {
return a.memfs.Metadata("base")
}, nil)
for name, s3 := range a.s3fs {
a.replacer.RegisterTemplate("fs:"+name, s3.Metadata("base"), nil)
a.replacer.RegisterReplaceFunc("fs:"+name, func(params map[string]string, config *restreamapp.Config, section string) string {
return s3.Metadata("base")
}, nil)
}
a.replacer.RegisterTemplateFunc("rtmp", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("rtmp", func(params map[string]string, bla *restreamapp.Config, section string) string {
host, port, _ := gonet.SplitHostPort(cfg.RTMP.Address)
if len(host) == 0 {
host = "localhost"
@@ -538,22 +558,24 @@ func (a *api) start() error {
if cfg.RTMP.App != "/" {
template += cfg.RTMP.App
}
template += "/{name}"
template += "/" + params["name"]
if len(cfg.RTMP.Token) != 0 {
template += "?token=" + cfg.RTMP.Token
}
return template
}, nil)
}, map[string]string{
"name": "",
})
a.replacer.RegisterTemplateFunc("srt", func(config *restreamapp.Config, section string) string {
a.replacer.RegisterReplaceFunc("srt", func(params map[string]string, bla *restreamapp.Config, section string) string {
host, port, _ = gonet.SplitHostPort(cfg.SRT.Address)
if len(host) == 0 {
host = "localhost"
}
template := "srt://" + host + ":" + port + "?mode=caller&transtype=live&latency={latency}&streamid={name}"
template := "srt://" + host + ":" + port + "?mode=caller&transtype=live&latency=" + params["latency"] + "&streamid=" + params["name"]
if section == "output" {
template += ",mode:publish"
} else {
@@ -568,6 +590,7 @@ func (a *api) start() error {
return template
}, map[string]string{
"name": "",
"latency": "20000", // 20 milliseconds, FFmpeg requires microseconds
})
}

View File

@@ -32,9 +32,10 @@ type ProcessConfig struct {
Reconnect bool
ReconnectDelay time.Duration
StaleTimeout time.Duration
Command []string
Args []string
Parser process.Parser
Logger log.Logger
OnArgs func([]string) []string
OnExit func()
OnStart func()
OnStateChange func(from, to string)
@@ -113,12 +114,13 @@ func New(config Config) (FFmpeg, error) {
func (f *ffmpeg) New(config ProcessConfig) (process.Process, error) {
ffmpeg, err := process.New(process.Config{
Binary: f.binary,
Args: config.Command,
Args: config.Args,
Reconnect: config.Reconnect,
ReconnectDelay: config.ReconnectDelay,
StaleTimeout: config.StaleTimeout,
Parser: config.Parser,
Logger: config.Logger,
OnArgs: config.OnArgs,
OnStart: config.OnStart,
OnExit: config.OnExit,
OnStateChange: func(from, to string) {

1
go.mod
View File

@@ -16,6 +16,7 @@ require (
github.com/invopop/jsonschema v0.4.0
github.com/joho/godotenv v1.4.0
github.com/labstack/echo/v4 v4.9.1
github.com/lestrrat-go/strftime v1.0.6
github.com/lithammer/shortuuid/v4 v4.0.0
github.com/mattn/go-isatty v0.0.17
github.com/minio/minio-go/v7 v7.0.47

4
go.sum
View File

@@ -125,6 +125,10 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/lithammer/shortuuid/v4 v4.0.0 h1:QRbbVkfgNippHOS8PXDkti4NaWeyYfcBTHtw7k08o4c=

View File

@@ -55,6 +55,7 @@ type Config struct {
LimitMemory uint64 // Kill the process if the memory consumption in bytes is above this value
LimitDuration time.Duration // Kill the process if the limits are exceeded for this duration
Parser Parser // A parser for the output of the process
OnArgs func(args []string) []string // A callback which is called right before the process will start with the command args
OnStart func() // A callback which is called after the process started
OnExit func() // A callback which is called after the process exited
OnStateChange func(from, to string) // A callback which is called after a state changed
@@ -189,6 +190,7 @@ type process struct {
logger log.Logger
debuglogger log.Logger
callbacks struct {
onArgs func(args []string) []string
onStart func()
onExit func()
onStateChange func(from, to string)
@@ -239,6 +241,7 @@ func New(config Config) (Process, error) {
p.stale.last = time.Now()
p.stale.timeout = config.StaleTimeout
p.callbacks.onArgs = config.OnArgs
p.callbacks.onStart = config.OnStart
p.callbacks.onExit = config.OnExit
p.callbacks.onStateChange = config.OnStateChange
@@ -465,7 +468,15 @@ func (p *process) start() error {
p.setState(stateStarting)
p.cmd = exec.Command(p.binary, p.args...)
args := p.args
if p.callbacks.onArgs != nil {
args = make([]string, len(p.args))
copy(args, p.args)
args = p.callbacks.onArgs(args)
}
p.cmd = exec.Command(p.binary, args...)
p.cmd.Env = []string{}
p.stdout, err = p.cmd.StderrPipe()

View File

@@ -9,18 +9,18 @@ import (
"github.com/datarhei/core/v16/restream/app"
)
type TemplateFn func(config *app.Config, section string) string
type ReplaceFunc func(params map[string]string, config *app.Config, section string) string
type Replacer interface {
// RegisterTemplate registers a template for a specific placeholder. Template
// may contain placeholders as well of the form {name}. They will be replaced
// by the parameters of the placeholder (see Replace). If a parameter is not of
// a template is not present, default values can be provided.
RegisterTemplate(placeholder, template string, defaults map[string]string)
// RegisterTemplateFunc does the same as RegisterTemplate, but the template
// is returned by the template function.
RegisterTemplateFunc(placeholder string, template TemplateFn, defaults map[string]string)
RegisterReplaceFunc(placeholder string, replacer ReplaceFunc, defaults map[string]string)
// Replace replaces all occurences of placeholder in str with value. The placeholder is of the
// form {placeholder}. It is possible to escape a characters in value with \\ by appending a ^
@@ -35,13 +35,13 @@ type Replacer interface {
Replace(str, placeholder, value string, vars map[string]string, config *app.Config, section string) string
}
type template struct {
fn TemplateFn
type replace struct {
fn ReplaceFunc
defaults map[string]string
}
type replacer struct {
templates map[string]template
replacers map[string]replace
re *regexp.Regexp
templateRe *regexp.Regexp
@@ -50,7 +50,7 @@ type replacer struct {
// New returns a Replacer
func New() Replacer {
r := &replacer{
templates: make(map[string]template),
replacers: make(map[string]replace),
re: regexp.MustCompile(`{([a-z:]+)(?:\^(.))?(?:,(.*?))?}`),
templateRe: regexp.MustCompile(`{([a-z:]+)}`),
}
@@ -58,13 +58,9 @@ func New() Replacer {
return r
}
func (r *replacer) RegisterTemplate(placeholder, tmpl string, defaults map[string]string) {
r.RegisterTemplateFunc(placeholder, func(*app.Config, string) string { return tmpl }, defaults)
}
func (r *replacer) RegisterTemplateFunc(placeholder string, templateFn TemplateFn, defaults map[string]string) {
r.templates[placeholder] = template{
fn: templateFn,
func (r *replacer) RegisterReplaceFunc(placeholder string, replaceFn ReplaceFunc, defaults map[string]string) {
r.replacers[placeholder] = replace{
fn: replaceFn,
defaults: defaults,
}
}
@@ -81,20 +77,21 @@ func (r *replacer) Replace(str, placeholder, value string, vars map[string]strin
// We need a copy from the value
v := value
var tmpl template = template{
fn: func(*app.Config, string) string { return v },
var repl replace = replace{
fn: func(map[string]string, *app.Config, string) string { return v },
}
// Check for a registered template
if len(v) == 0 {
t, ok := r.templates[placeholder]
// Check for a registered template
t, ok := r.replacers[placeholder]
if ok {
tmpl = t
repl = t
}
}
v = tmpl.fn(config, section)
v = r.compileTemplate(v, matches[3], vars, tmpl.defaults)
params := r.parseParametes(matches[3], vars, repl.defaults)
v = repl.fn(params, config, section)
if len(matches[2]) != 0 {
// If there's a character to escape, we also have to escape the
@@ -112,22 +109,19 @@ func (r *replacer) Replace(str, placeholder, value string, vars map[string]strin
return str
}
// compileTemplate fills in the placeholder in the template with the values from the params
// string. The placeholders in the template are delimited by {} and their name may only
// contain the letters a-z. The params string is a comma-separated string of key=value pairs.
// Example: the template is "Hello {who}!", the params string is "who=World". The key is the
// placeholder name and will be replaced with the value. The resulting string is "Hello World!".
// If a placeholder name is not present in the params string, it will not be replaced. The key
// and values can be escaped as in net/url.QueryEscape.
func (r *replacer) compileTemplate(str, params string, vars map[string]string, defaults map[string]string) string {
if len(params) == 0 && len(defaults) == 0 {
return str
}
func (r *replacer) parseParametes(params string, vars map[string]string, defaults map[string]string) map[string]string {
p := make(map[string]string)
if len(params) == 0 && len(defaults) == 0 {
return p
}
// Copy the defaults
for key, value := range defaults {
for name, v := range vars {
value = strings.ReplaceAll(value, "$"+name, v)
}
p[key] = value
}
@@ -157,16 +151,13 @@ func (r *replacer) compileTemplate(str, params string, vars map[string]string, d
p[key] = value
}
str = r.templateRe.ReplaceAllStringFunc(str, func(match string) string {
matches := r.templateRe.FindStringSubmatch(match)
value, ok := p[matches[1]]
if !ok {
return match
return p
}
return strings.Replace(match, matches[0], value, 1)
})
return str
}
// compileTemplate fills in the placeholder in the template with the values from the params
// string. The placeholders in the template are delimited by {} and their name may only
// contain the letters a-z. The params string is a comma-separated string of key=value pairs.
// Example: the template is "Hello {who}!", the params string is "who=World". The key is the
// placeholder name and will be replaced with the value. The resulting string is "Hello World!".
// If a placeholder name is not present in the params string, it will not be replaced. The key
// and values can be escaped as in net/url.QueryEscape.

View File

@@ -24,21 +24,58 @@ func TestReplace(t *testing.T) {
r := New()
r.RegisterReplaceFunc(
"foobar",
func(params map[string]string, config *app.Config, section string) string {
return ";:.,-_$\\£!^"
},
nil,
)
for _, e := range samples {
replaced := r.Replace(e[0], "foobar", foobar, nil, nil, "")
replaced := r.Replace(e[0], "foobar", "", nil, nil, "")
require.Equal(t, e[1], replaced, e[0])
}
r.RegisterReplaceFunc(
"foobar",
func(params map[string]string, config *app.Config, section string) string {
return ""
},
nil,
)
replaced := r.Replace("{foobar}", "foobar", "", nil, nil, "")
require.Equal(t, "", replaced)
replaced = r.Replace("{foobar}", "barfoo", "", nil, nil, "")
require.Equal(t, "{foobar}", replaced)
replaced = r.Replace("{barfoo}", "barfoo", "barfoo", nil, nil, "")
require.Equal(t, "barfoo", replaced)
replaced = r.Replace("{barfoo}", "barfoo", "", nil, nil, "")
require.Equal(t, "", replaced)
}
func TestReplaceTemplate(t *testing.T) {
func TestReplacerFunc(t *testing.T) {
r := New()
r.RegisterTemplate("foo:bar", "Hello {who}! {what}?", nil)
r.RegisterReplaceFunc(
"foo:bar",
func(params map[string]string, config *app.Config, section string) string {
return "Hello " + params["who"] + "! " + params["what"] + "?"
},
map[string]string{
"who": "defaultWho",
"what": "defaultWhat",
},
)
replaced := r.Replace("{foo:bar,who=World}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! {what}?", replaced)
replaced := r.Replace("{foo:bar}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello defaultWho! defaultWhat?", replaced)
replaced = r.Replace("{foo:bar,who=World}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! defaultWhat?", replaced)
replaced = r.Replace("{foo:bar,who=World,what=E%3dmc^2}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! E=mc^2?", replaced)
@@ -47,97 +84,56 @@ func TestReplaceTemplate(t *testing.T) {
require.Equal(t, "Hello World! E=mc\\\\:2?", replaced)
}
func TestReplaceTemplateFunc(t *testing.T) {
func TestReplacerFuncWithVars(t *testing.T) {
r := New()
r.RegisterTemplateFunc("foo:bar", func(config *app.Config, kind string) string { return "Hello {who}! {what}?" }, nil)
replaced := r.Replace("{foo:bar,who=World}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! {what}?", replaced)
replaced = r.Replace("{foo:bar,who=World,what=E%3dmc^2}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! E=mc^2?", replaced)
replaced = r.Replace("{foo:bar^:,who=World,what=E%3dmc:2}", "foo:bar", "", nil, nil, "")
require.Equal(t, "Hello World! E=mc\\\\:2?", replaced)
}
func TestReplaceTemplateDefaults(t *testing.T) {
r := New()
r.RegisterTemplate("foobar", "Hello {who}! {what}?", map[string]string{
"who": "someone",
"what": "something",
})
replaced := r.Replace("{foobar}", "foobar", "", nil, nil, "")
require.Equal(t, "Hello someone! something?", replaced)
replaced = r.Replace("{foobar,who=World}", "foobar", "", nil, nil, "")
require.Equal(t, "Hello World! something?", replaced)
}
func TestReplaceCompileTemplate(t *testing.T) {
samples := [][3]string{
{"Hello {who}!", "who=World", "Hello World!"},
{"Hello {who}! {what}?", "who=World", "Hello World! {what}?"},
{"Hello {who}! {what}?", "who=World,what=Yeah", "Hello World! Yeah?"},
{"Hello {who}! {what}?", "who=World,what=", "Hello World! ?"},
{"Hello {who}!", "who=E%3dmc^2", "Hello E=mc^2!"},
}
r := New().(*replacer)
for _, e := range samples {
replaced := r.compileTemplate(e[0], e[1], nil, nil)
require.Equal(t, e[2], replaced, e[0])
}
}
func TestReplaceCompileTemplateDefaults(t *testing.T) {
samples := [][3]string{
{"Hello {who}!", "", "Hello someone!"},
{"Hello {who}!", "who=World", "Hello World!"},
{"Hello {who}! {what}?", "who=World", "Hello World! something?"},
{"Hello {who}! {what}?", "who=World,what=Yeah", "Hello World! Yeah?"},
{"Hello {who}! {what}?", "who=World,what=", "Hello World! ?"},
}
r := New().(*replacer)
for _, e := range samples {
replaced := r.compileTemplate(e[0], e[1], nil, map[string]string{
"who": "someone",
"what": "something",
})
require.Equal(t, e[2], replaced, e[0])
}
}
func TestReplaceCompileTemplateWithVars(t *testing.T) {
samples := [][3]string{
{"Hello {who}!", "who=$processid", "Hello 123456789!"},
{"Hello {who}! {what}?", "who=$location", "Hello World! {what}?"},
{"Hello {who}! {what}?", "who=$location,what=Yeah", "Hello World! Yeah?"},
{"Hello {who}! {what}?", "who=$location,what=$processid", "Hello World! 123456789?"},
{"Hello {who}!", "who=$processidxxx", "Hello 123456789xxx!"},
}
r.RegisterReplaceFunc(
"foo:bar",
func(params map[string]string, config *app.Config, section string) string {
return "Hello " + params["who"] + "! " + params["what"] + "?"
},
map[string]string{
"who": "$processid_$location",
"what": "$location",
},
)
vars := map[string]string{
"processid": "123456789",
"location": "World",
}
r := New().(*replacer)
replaced := r.Replace("{foo:bar}", "foo:bar", "", vars, nil, "")
require.Equal(t, "Hello 123456789_World! World?", replaced)
for _, e := range samples {
replaced := r.compileTemplate(e[0], e[1], vars, nil)
require.Equal(t, e[2], replaced, e[0])
}
replaced = r.Replace("{foo:bar,who=World}", "foo:bar", "", vars, nil, "")
require.Equal(t, "Hello World! World?", replaced)
replaced = r.Replace("{foo:bar,who=World,what=E%3dmc^2}", "foo:bar", "", vars, nil, "")
require.Equal(t, "Hello World! E=mc^2?", replaced)
replaced = r.Replace("{foo:bar^:,who=World,what=E%3dmc:2}", "foo:bar", "", vars, nil, "")
require.Equal(t, "Hello World! E=mc\\\\:2?", replaced)
replaced = r.Replace("{foo:bar,who=$location,what=$processid}", "foo:bar", "", vars, nil, "")
require.Equal(t, "Hello World! 123456789?", replaced)
}
func TestReplaceGlob(t *testing.T) {
r := New()
r.RegisterTemplate("foo:bar", "Hello foobar", nil)
r.RegisterTemplate("foo:baz", "Hello foobaz", nil)
r.RegisterReplaceFunc(
"foo:bar",
func(params map[string]string, config *app.Config, section string) string {
return "Hello foobar"
},
nil,
)
r.RegisterReplaceFunc(
"foo:baz",
func(params map[string]string, config *app.Config, section string) string {
return "Hello foobaz"
},
nil,
)
replaced := r.Replace("{foo:baz}, {foo:bar}", "foo:*", "", nil, nil, "")
require.Equal(t, "Hello foobaz, Hello foobar", replaced)

View File

@@ -295,7 +295,7 @@ func (r *restream) load() error {
}
// Replace all placeholders in the config
resolvePlaceholders(t.config, r.replace)
resolveStaticPlaceholders(t.config, r.replace)
tasks[id] = t
}
@@ -336,7 +336,12 @@ func (r *restream) load() error {
continue
}
t.usesDisk, err = r.validateConfig(t.config)
// Validate config with all placeholders replaced. However, we need to take care
// that the config with the task keeps its dynamic placeholders for process starts.
config := t.config.Clone()
resolveDynamicPlaceholder(config, r.replace)
t.usesDisk, err = validateConfig(config, r.fs.diskfs, r.ffmpeg)
if err != nil {
r.logger.Warn().WithField("id", t.id).WithError(err).Log("Ignoring")
continue
@@ -355,9 +360,10 @@ func (r *restream) load() error {
Reconnect: t.config.Reconnect,
ReconnectDelay: time.Duration(t.config.ReconnectDelay) * time.Second,
StaleTimeout: time.Duration(t.config.StaleTimeout) * time.Second,
Command: t.command,
Args: t.command,
Parser: t.parser,
Logger: t.logger,
OnArgs: r.onArgs(t.config.Clone()),
})
if err != nil {
return err
@@ -468,17 +474,24 @@ func (r *restream) createTask(config *app.Config) (*task, error) {
logger: r.logger.WithField("id", process.ID),
}
resolvePlaceholders(t.config, r.replace)
resolveStaticPlaceholders(t.config, r.replace)
err := r.resolveAddresses(r.tasks, t.config)
if err != nil {
return nil, err
}
t.usesDisk, err = r.validateConfig(t.config)
{
// Validate config with all placeholders replaced. However, we need to take care
// that the config with the task keeps its dynamic placeholders for process starts.
config := t.config.Clone()
resolveDynamicPlaceholder(config, r.replace)
t.usesDisk, err = validateConfig(config, r.fs.diskfs, r.ffmpeg)
if err != nil {
return nil, err
}
}
err = r.setPlayoutPorts(t)
if err != nil {
@@ -492,20 +505,37 @@ func (r *restream) createTask(config *app.Config) (*task, error) {
Reconnect: t.config.Reconnect,
ReconnectDelay: time.Duration(t.config.ReconnectDelay) * time.Second,
StaleTimeout: time.Duration(t.config.StaleTimeout) * time.Second,
Command: t.command,
Args: t.command,
Parser: t.parser,
Logger: t.logger,
OnArgs: r.onArgs(t.config.Clone()),
})
if err != nil {
return nil, err
}
t.ffmpeg = ffmpeg
t.valid = true
return t, nil
}
func (r *restream) onArgs(cfg *app.Config) func([]string) []string {
return func(args []string) []string {
config := cfg.Clone()
resolveDynamicPlaceholder(config, r.replace)
_, err := validateConfig(config, r.fs.diskfs, r.ffmpeg)
if err != nil {
return []string{}
}
return config.CreateCommand()
}
}
func (r *restream) setCleanup(id string, config *app.Config) {
rePrefix := regexp.MustCompile(`^([a-z]+):`)
@@ -611,7 +641,7 @@ func (r *restream) unsetPlayoutPorts(t *task) {
t.playout = nil
}
func (r *restream) validateConfig(config *app.Config) (bool, error) {
func validateConfig(config *app.Config, fss []rfs.Filesystem, ffmpeg ffmpeg.FFmpeg) (bool, error) {
if len(config.Input) == 0 {
return false, fmt.Errorf("at least one input must be defined for the process '%s'", config.ID)
}
@@ -639,20 +669,20 @@ func (r *restream) validateConfig(config *app.Config) (bool, error) {
return false, fmt.Errorf("the address for input '#%s:%s' must not be empty", config.ID, io.ID)
}
if len(r.fs.diskfs) != 0 {
if len(fss) != 0 {
maxFails := 0
for _, fs := range r.fs.diskfs {
io.Address, err = r.validateInputAddress(io.Address, fs.Metadata("base"))
for _, fs := range fss {
io.Address, err = validateInputAddress(io.Address, fs.Metadata("base"), ffmpeg)
if err != nil {
maxFails++
}
}
if maxFails == len(r.fs.diskfs) {
if maxFails == len(fss) {
return false, fmt.Errorf("the address for input '#%s:%s' (%s) is invalid: %w", config.ID, io.ID, io.Address, err)
}
} else {
io.Address, err = r.validateInputAddress(io.Address, "/")
io.Address, err = validateInputAddress(io.Address, "/", ffmpeg)
if err != nil {
return false, fmt.Errorf("the address for input '#%s:%s' (%s) is invalid: %w", config.ID, io.ID, io.Address, err)
}
@@ -685,11 +715,11 @@ func (r *restream) validateConfig(config *app.Config) (bool, error) {
return false, fmt.Errorf("the address for output '#%s:%s' must not be empty", config.ID, io.ID)
}
if len(r.fs.diskfs) != 0 {
if len(fss) != 0 {
maxFails := 0
for _, fs := range r.fs.diskfs {
for _, fs := range fss {
isFile := false
io.Address, isFile, err = r.validateOutputAddress(io.Address, fs.Metadata("base"))
io.Address, isFile, err = validateOutputAddress(io.Address, fs.Metadata("base"), ffmpeg)
if err != nil {
maxFails++
}
@@ -699,12 +729,12 @@ func (r *restream) validateConfig(config *app.Config) (bool, error) {
}
}
if maxFails == len(r.fs.diskfs) {
if maxFails == len(fss) {
return false, fmt.Errorf("the address for output '#%s:%s' is invalid: %w", config.ID, io.ID, err)
}
} else {
isFile := false
io.Address, isFile, err = r.validateOutputAddress(io.Address, "/")
io.Address, isFile, err = validateOutputAddress(io.Address, "/", ffmpeg)
if err != nil {
return false, fmt.Errorf("the address for output '#%s:%s' is invalid: %w", config.ID, io.ID, err)
}
@@ -718,21 +748,21 @@ func (r *restream) validateConfig(config *app.Config) (bool, error) {
return hasFiles, nil
}
func (r *restream) validateInputAddress(address, basedir string) (string, error) {
func validateInputAddress(address, basedir string, ffmpeg ffmpeg.FFmpeg) (string, error) {
if ok := url.HasScheme(address); ok {
if err := url.Validate(address); err != nil {
return address, err
}
}
if !r.ffmpeg.ValidateInputAddress(address) {
if !ffmpeg.ValidateInputAddress(address) {
return address, fmt.Errorf("address is not allowed")
}
return address, nil
}
func (r *restream) validateOutputAddress(address, basedir string) (string, bool, error) {
func validateOutputAddress(address, basedir string, ffmpeg ffmpeg.FFmpeg) (string, bool, error) {
// If the address contains a "|" or it starts with a "[", then assume that it
// is an address for the tee muxer.
if strings.Contains(address, "|") || strings.HasPrefix(address, "[") {
@@ -746,7 +776,7 @@ func (r *restream) validateOutputAddress(address, basedir string) (string, bool,
options := teeOptions.FindString(a)
a = teeOptions.ReplaceAllString(a, "")
va, file, err := r.validateOutputAddress(a, basedir)
va, file, err := validateOutputAddress(a, basedir, ffmpeg)
if err != nil {
return address, false, err
}
@@ -768,7 +798,7 @@ func (r *restream) validateOutputAddress(address, basedir string) (string, bool,
return address, false, err
}
if !r.ffmpeg.ValidateOutputAddress(address) {
if !ffmpeg.ValidateOutputAddress(address) {
return address, false, fmt.Errorf("address is not allowed")
}
@@ -779,13 +809,14 @@ func (r *restream) validateOutputAddress(address, basedir string) (string, bool,
return "pipe:", false, nil
}
address, err := filepath.Abs(address)
if err != nil {
return address, false, fmt.Errorf("not a valid path (%w)", err)
address = filepath.Clean(address)
if !filepath.IsAbs(address) {
address = filepath.Join(basedir, address)
}
if strings.HasPrefix(address, "/dev/") {
if !r.ffmpeg.ValidateOutputAddress("file:" + address) {
if !ffmpeg.ValidateOutputAddress("file:" + address) {
return address, false, fmt.Errorf("address is not allowed")
}
@@ -796,7 +827,7 @@ func (r *restream) validateOutputAddress(address, basedir string) (string, bool,
return address, false, fmt.Errorf("%s is not inside of %s", address, basedir)
}
if !r.ffmpeg.ValidateOutputAddress("file:" + address) {
if !ffmpeg.ValidateOutputAddress("file:" + address) {
return address, false, fmt.Errorf("address is not allowed")
}
@@ -1034,11 +1065,13 @@ func (r *restream) startProcess(id string) error {
return fmt.Errorf("invalid process definition")
}
if task.ffmpeg != nil {
status := task.ffmpeg.Status()
if task.process.Order == "start" && status.Order == "start" {
return nil
}
}
if r.maxProc > 0 && r.nProc >= r.maxProc {
return fmt.Errorf("max. number of running processes (%d) reached", r.maxProc)
@@ -1113,7 +1146,9 @@ func (r *restream) restartProcess(id string) error {
return nil
}
if task.ffmpeg != nil {
task.ffmpeg.Kill(true)
}
return nil
}
@@ -1142,14 +1177,19 @@ func (r *restream) reloadProcess(id string) error {
t.config = t.process.Config.Clone()
resolvePlaceholders(t.config, r.replace)
resolveStaticPlaceholders(t.config, r.replace)
err := r.resolveAddresses(r.tasks, t.config)
if err != nil {
return err
}
t.usesDisk, err = r.validateConfig(t.config)
// Validate config with all placeholders replaced. However, we need to take care
// that the config with the task keeps its dynamic placeholders for process starts.
config := t.config.Clone()
resolveDynamicPlaceholder(config, r.replace)
t.usesDisk, err = validateConfig(config, r.fs.diskfs, r.ffmpeg)
if err != nil {
return err
}
@@ -1173,9 +1213,10 @@ func (r *restream) reloadProcess(id string) error {
Reconnect: t.config.Reconnect,
ReconnectDelay: time.Duration(t.config.ReconnectDelay) * time.Second,
StaleTimeout: time.Duration(t.config.StaleTimeout) * time.Second,
Command: t.command,
Args: t.command,
Parser: t.parser,
Logger: t.logger,
OnArgs: r.onArgs(t.config.Clone()),
})
if err != nil {
return err
@@ -1346,7 +1387,7 @@ func (r *restream) ProbeWithTimeout(id string, timeout time.Duration) app.Probe
Reconnect: false,
ReconnectDelay: 0,
StaleTimeout: timeout,
Command: command,
Args: command,
Parser: prober,
Logger: task.logger,
OnExit: func() {
@@ -1495,9 +1536,8 @@ func (r *restream) GetMetadata(key string) (interface{}, error) {
return data, nil
}
// resolvePlaceholders replaces all placeholders in the config. The config
// will be modified in place.
func resolvePlaceholders(config *app.Config, r replace.Replacer) {
// resolveStaticPlaceholders replaces all placeholders in the config. The config will be modified in place.
func resolveStaticPlaceholders(config *app.Config, r replace.Replacer) {
vars := map[string]string{
"processid": config.ID,
"reference": config.Reference,
@@ -1514,14 +1554,14 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
// Resolving the given inputs
for i, input := range config.Input {
// Replace any known placeholders
input.ID = r.Replace(input.ID, "processid", config.ID, nil, nil, "input")
input.ID = r.Replace(input.ID, "reference", config.Reference, nil, nil, "input")
input.ID = r.Replace(input.ID, "processid", config.ID, vars, config, "input")
input.ID = r.Replace(input.ID, "reference", config.Reference, vars, config, "input")
vars["inputid"] = input.ID
input.Address = r.Replace(input.Address, "inputid", input.ID, nil, nil, "input")
input.Address = r.Replace(input.Address, "processid", config.ID, nil, nil, "input")
input.Address = r.Replace(input.Address, "reference", config.Reference, nil, nil, "input")
input.Address = r.Replace(input.Address, "inputid", input.ID, vars, config, "input")
input.Address = r.Replace(input.Address, "processid", config.ID, vars, config, "input")
input.Address = r.Replace(input.Address, "reference", config.Reference, vars, config, "input")
input.Address = r.Replace(input.Address, "diskfs", "", vars, config, "input")
input.Address = r.Replace(input.Address, "memfs", "", vars, config, "input")
input.Address = r.Replace(input.Address, "fs:*", "", vars, config, "input")
@@ -1530,9 +1570,9 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
for j, option := range input.Options {
// Replace any known placeholders
option = r.Replace(option, "inputid", input.ID, nil, nil, "input")
option = r.Replace(option, "processid", config.ID, nil, nil, "input")
option = r.Replace(option, "reference", config.Reference, nil, nil, "input")
option = r.Replace(option, "inputid", input.ID, vars, config, "input")
option = r.Replace(option, "processid", config.ID, vars, config, "input")
option = r.Replace(option, "reference", config.Reference, vars, config, "input")
option = r.Replace(option, "diskfs", "", vars, config, "input")
option = r.Replace(option, "memfs", "", vars, config, "input")
option = r.Replace(option, "fs:*", "", vars, config, "input")
@@ -1548,14 +1588,14 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
// Resolving the given outputs
for i, output := range config.Output {
// Replace any known placeholders
output.ID = r.Replace(output.ID, "processid", config.ID, nil, nil, "output")
output.ID = r.Replace(output.ID, "reference", config.Reference, nil, nil, "output")
output.ID = r.Replace(output.ID, "processid", config.ID, vars, config, "output")
output.ID = r.Replace(output.ID, "reference", config.Reference, vars, config, "output")
vars["outputid"] = output.ID
output.Address = r.Replace(output.Address, "outputid", output.ID, nil, nil, "output")
output.Address = r.Replace(output.Address, "processid", config.ID, nil, nil, "output")
output.Address = r.Replace(output.Address, "reference", config.Reference, nil, nil, "output")
output.Address = r.Replace(output.Address, "outputid", output.ID, vars, config, "output")
output.Address = r.Replace(output.Address, "processid", config.ID, vars, config, "output")
output.Address = r.Replace(output.Address, "reference", config.Reference, vars, config, "output")
output.Address = r.Replace(output.Address, "diskfs", "", vars, config, "output")
output.Address = r.Replace(output.Address, "memfs", "", vars, config, "output")
output.Address = r.Replace(output.Address, "fs:*", "", vars, config, "output")
@@ -1564,9 +1604,9 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
for j, option := range output.Options {
// Replace any known placeholders
option = r.Replace(option, "outputid", output.ID, nil, nil, "output")
option = r.Replace(option, "processid", config.ID, nil, nil, "output")
option = r.Replace(option, "reference", config.Reference, nil, nil, "output")
option = r.Replace(option, "outputid", output.ID, vars, config, "output")
option = r.Replace(option, "processid", config.ID, vars, config, "output")
option = r.Replace(option, "reference", config.Reference, vars, config, "output")
option = r.Replace(option, "diskfs", "", vars, config, "output")
option = r.Replace(option, "memfs", "", vars, config, "output")
option = r.Replace(option, "fs:*", "", vars, config, "output")
@@ -1576,9 +1616,9 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
for j, cleanup := range output.Cleanup {
// Replace any known placeholders
cleanup.Pattern = r.Replace(cleanup.Pattern, "outputid", output.ID, nil, nil, "output")
cleanup.Pattern = r.Replace(cleanup.Pattern, "processid", config.ID, nil, nil, "output")
cleanup.Pattern = r.Replace(cleanup.Pattern, "reference", config.Reference, nil, nil, "output")
cleanup.Pattern = r.Replace(cleanup.Pattern, "outputid", output.ID, vars, config, "output")
cleanup.Pattern = r.Replace(cleanup.Pattern, "processid", config.ID, vars, config, "output")
cleanup.Pattern = r.Replace(cleanup.Pattern, "reference", config.Reference, vars, config, "output")
output.Cleanup[j] = cleanup
}
@@ -1588,3 +1628,47 @@ func resolvePlaceholders(config *app.Config, r replace.Replacer) {
config.Output[i] = output
}
}
// resolveDynamicPlaceholder replaces placeholders in the config that should be replaced at process start.
// The config will be modified in place.
func resolveDynamicPlaceholder(config *app.Config, r replace.Replacer) {
vars := map[string]string{
"timestamp": time.Now().UTC().Format(time.RFC3339),
}
for i, option := range config.Options {
option = r.Replace(option, "date", "", vars, config, "global")
config.Options[i] = option
}
for i, input := range config.Input {
input.Address = r.Replace(input.Address, "date", "", vars, config, "input")
for j, option := range input.Options {
option = r.Replace(option, "date", "", vars, config, "input")
input.Options[j] = option
}
config.Input[i] = input
}
for i, output := range config.Output {
output.Address = r.Replace(output.Address, "date", "", vars, config, "output")
for j, option := range output.Options {
option = r.Replace(option, "date", "", vars, config, "output")
output.Options[j] = option
}
for j, cleanup := range output.Cleanup {
cleanup.Pattern = r.Replace(cleanup.Pattern, "date", "", vars, config, "output")
output.Cleanup[j] = cleanup
}
config.Output[i] = output
}
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/datarhei/core/v16/net"
"github.com/datarhei/core/v16/restream/app"
"github.com/datarhei/core/v16/restream/replace"
"github.com/lestrrat-go/strftime"
"github.com/stretchr/testify/require"
)
@@ -500,7 +501,8 @@ func TestPlayoutRange(t *testing.T) {
_, err = rs.GetPlayout(process.ID, "foobar")
require.NotEqual(t, nil, err, "playout of non-existing input should error")
addr, _ := rs.GetPlayout(process.ID, process.Input[0].ID)
addr, err := rs.GetPlayout(process.ID, process.Input[0].ID)
require.NoError(t, err)
require.NotEqual(t, 0, len(addr), "the playout address should not be empty if a port range is given")
require.Equal(t, "127.0.0.1:3000", addr, "the playout address should be 127.0.0.1:3000")
}
@@ -545,36 +547,36 @@ func TestConfigValidation(t *testing.T) {
config := getDummyProcess()
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.NoError(t, err)
config.Input = []app.ConfigIO{}
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config = getDummyProcess()
config.Input[0].ID = ""
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config = getDummyProcess()
config.Input[0].Address = ""
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config = getDummyProcess()
config.Output = []app.ConfigIO{}
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config = getDummyProcess()
config.Output[0].ID = ""
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config = getDummyProcess()
config.Output[0].Address = ""
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
}
@@ -592,21 +594,21 @@ func TestConfigValidationFFmpeg(t *testing.T) {
config := getDummyProcess()
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.Error(t, err)
config.Input[0].Address = "http://stream.example.com/master.m3u8"
config.Output[0].Address = "http://stream.example.com/master2.m3u8"
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.NoError(t, err)
config.Output[0].Address = "[f=flv]http://stream.example.com/master2.m3u8"
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.NoError(t, err)
config.Output[0].Address = "[f=hls]http://stream.example.com/master2.m3u8|[f=flv]rtmp://stream.example.com/stream"
_, err = rs.validateConfig(config)
_, err = validateConfig(config, rs.fs.diskfs, rs.ffmpeg)
require.NoError(t, err)
}
@@ -639,10 +641,10 @@ func TestOutputAddressValidation(t *testing.T) {
}
for path, r := range paths {
path, _, err := rs.validateOutputAddress(path, "/core/data")
path, _, err := validateOutputAddress(path, "/core/data", rs.ffmpeg)
if r.err {
require.Error(t, err)
require.Error(t, err, path)
} else {
require.NoError(t, err)
}
@@ -673,28 +675,45 @@ func TestMetadata(t *testing.T) {
func TestReplacer(t *testing.T) {
replacer := replace.New()
replacer.RegisterTemplateFunc("diskfs", func(config *app.Config, section string) string {
replacer.RegisterReplaceFunc("date", func(params map[string]string, config *app.Config, section string) string {
t, err := time.Parse(time.RFC3339, params["timestamp"])
if err != nil {
return ""
}
s, err := strftime.Format(params["format"], t)
if err != nil {
return ""
}
return s
}, map[string]string{
"format": "%Y-%m-%d_%H-%M-%S",
"timestamp": "2019-10-12T07:20:50.52Z",
})
replacer.RegisterReplaceFunc("diskfs", func(params map[string]string, config *app.Config, section string) string {
return "/mnt/diskfs"
}, nil)
replacer.RegisterTemplateFunc("fs:disk", func(config *app.Config, section string) string {
replacer.RegisterReplaceFunc("fs:disk", func(params map[string]string, config *app.Config, section string) string {
return "/mnt/diskfs"
}, nil)
replacer.RegisterTemplateFunc("memfs", func(config *app.Config, section string) string {
replacer.RegisterReplaceFunc("memfs", func(params map[string]string, config *app.Config, section string) string {
return "http://localhost/mnt/memfs"
}, nil)
replacer.RegisterTemplateFunc("fs:mem", func(config *app.Config, section string) string {
replacer.RegisterReplaceFunc("fs:mem", func(params map[string]string, config *app.Config, section string) string {
return "http://localhost/mnt/memfs"
}, nil)
replacer.RegisterTemplateFunc("rtmp", func(config *app.Config, section string) string {
return "rtmp://localhost/app/{name}?token=foobar"
replacer.RegisterReplaceFunc("rtmp", func(params map[string]string, config *app.Config, section string) string {
return "rtmp://localhost/app/" + params["name"] + "?token=foobar"
}, nil)
replacer.RegisterTemplateFunc("srt", func(config *app.Config, section string) string {
template := "srt://localhost:6000?mode=caller&transtype=live&latency={latency}&streamid={name}"
replacer.RegisterReplaceFunc("srt", func(params map[string]string, config *app.Config, section string) string {
template := "srt://localhost:6000?mode=caller&transtype=live&latency=" + params["latency"] + "&streamid=" + params["name"]
if section == "output" {
template += ",mode:publish"
} else {
@@ -704,6 +723,216 @@ func TestReplacer(t *testing.T) {
return template
}, map[string]string{
"name": "",
"latency": "20000", // 20 milliseconds, FFmpeg requires microseconds
})
process := &app.Config{
ID: "314159265359",
Reference: "refref",
FFVersion: "^4.0.2",
Input: []app.ConfigIO{
{
ID: "in_{processid}_{reference}",
Address: "input:{inputid}_process:{processid}_reference:{reference}_diskfs:{diskfs}/disk.txt_memfs:{memfs}/mem.txt_fsdisk:{fs:disk}/fsdisk.txt_fsmem:{fs:mem}/fsmem.txt_rtmp:{rtmp,name=pmtr}_srt:{srt,name=trs}_rtmp:{rtmp,name=$inputid}",
Options: []string{
"-f",
"lavfi",
"-re",
"input:{inputid}",
"process:{processid}",
"reference:{reference}",
"diskfs:{diskfs}/disk.txt",
"memfs:{memfs}/mem.txt",
"fsdisk:{fs:disk}/fsdisk_{date}.txt",
"fsmem:{fs:mem}/$inputid.txt",
},
},
},
Output: []app.ConfigIO{
{
ID: "out_{processid}_{reference}",
Address: "output:{outputid}_process:{processid}_reference:{reference}_diskfs:{diskfs}/disk.txt_memfs:{memfs}/mem.txt_fsdisk:{fs:disk}/fsdisk.txt_fsmem:{fs:mem}/fsmem.txt_rtmp:{rtmp,name=$processid}_srt:{srt,name=$reference,latency=42}_rtmp:{rtmp,name=$outputid}",
Options: []string{
"-codec",
"copy",
"-f",
"null",
"output:{outputid}",
"process:{processid}",
"reference:{reference}",
"diskfs:{diskfs}/disk.txt",
"memfs:{memfs}/mem.txt",
"fsdisk:{fs:disk}/fsdisk.txt",
"fsmem:{fs:mem}/$outputid.txt",
},
Cleanup: []app.ConfigIOCleanup{
{
Pattern: "pattern_{outputid}_{processid}_{reference}_{rtmp,name=$outputid}",
MaxFiles: 0,
MaxFileAge: 0,
PurgeOnDelete: false,
},
},
},
},
Options: []string{
"-loglevel",
"info",
"{diskfs}/foobar_on_disk.txt",
"{memfs}/foobar_in_mem.txt",
"{fs:disk}/foobar_on_disk_aswell.txt",
"{fs:mem}/foobar_in_mem_aswell.txt",
},
Reconnect: true,
ReconnectDelay: 10,
Autostart: false,
StaleTimeout: 0,
}
resolveStaticPlaceholders(process, replacer)
wantprocess := &app.Config{
ID: "314159265359",
Reference: "refref",
FFVersion: "^4.0.2",
Input: []app.ConfigIO{
{
ID: "in_314159265359_refref",
Address: "input:in_314159265359_refref_process:314159265359_reference:refref_diskfs:/mnt/diskfs/disk.txt_memfs:http://localhost/mnt/memfs/mem.txt_fsdisk:/mnt/diskfs/fsdisk.txt_fsmem:http://localhost/mnt/memfs/fsmem.txt_rtmp:rtmp://localhost/app/pmtr?token=foobar_srt:srt://localhost:6000?mode=caller&transtype=live&latency=20000&streamid=trs,mode:request,token:abcfoobar&passphrase=secret_rtmp:rtmp://localhost/app/in_314159265359_refref?token=foobar",
Options: []string{
"-f",
"lavfi",
"-re",
"input:in_314159265359_refref",
"process:314159265359",
"reference:refref",
"diskfs:/mnt/diskfs/disk.txt",
"memfs:http://localhost/mnt/memfs/mem.txt",
"fsdisk:/mnt/diskfs/fsdisk_{date}.txt",
"fsmem:http://localhost/mnt/memfs/$inputid.txt",
},
},
},
Output: []app.ConfigIO{
{
ID: "out_314159265359_refref",
Address: "output:out_314159265359_refref_process:314159265359_reference:refref_diskfs:/mnt/diskfs/disk.txt_memfs:http://localhost/mnt/memfs/mem.txt_fsdisk:/mnt/diskfs/fsdisk.txt_fsmem:http://localhost/mnt/memfs/fsmem.txt_rtmp:rtmp://localhost/app/314159265359?token=foobar_srt:srt://localhost:6000?mode=caller&transtype=live&latency=42&streamid=refref,mode:publish,token:abcfoobar&passphrase=secret_rtmp:rtmp://localhost/app/out_314159265359_refref?token=foobar",
Options: []string{
"-codec",
"copy",
"-f",
"null",
"output:out_314159265359_refref",
"process:314159265359",
"reference:refref",
"diskfs:/mnt/diskfs/disk.txt",
"memfs:http://localhost/mnt/memfs/mem.txt",
"fsdisk:/mnt/diskfs/fsdisk.txt",
"fsmem:http://localhost/mnt/memfs/$outputid.txt",
},
Cleanup: []app.ConfigIOCleanup{
{
Pattern: "pattern_out_314159265359_refref_314159265359_refref_{rtmp,name=$outputid}",
MaxFiles: 0,
MaxFileAge: 0,
PurgeOnDelete: false,
},
},
},
},
Options: []string{
"-loglevel",
"info",
"/mnt/diskfs/foobar_on_disk.txt",
"{memfs}/foobar_in_mem.txt",
"/mnt/diskfs/foobar_on_disk_aswell.txt",
"http://localhost/mnt/memfs/foobar_in_mem_aswell.txt",
},
Reconnect: true,
ReconnectDelay: 10,
Autostart: false,
StaleTimeout: 0,
}
require.Equal(t, wantprocess, process)
resolveDynamicPlaceholder(process, replacer)
wantprocess.Input = []app.ConfigIO{
{
ID: "in_314159265359_refref",
Address: "input:in_314159265359_refref_process:314159265359_reference:refref_diskfs:/mnt/diskfs/disk.txt_memfs:http://localhost/mnt/memfs/mem.txt_fsdisk:/mnt/diskfs/fsdisk.txt_fsmem:http://localhost/mnt/memfs/fsmem.txt_rtmp:rtmp://localhost/app/pmtr?token=foobar_srt:srt://localhost:6000?mode=caller&transtype=live&latency=20000&streamid=trs,mode:request,token:abcfoobar&passphrase=secret_rtmp:rtmp://localhost/app/in_314159265359_refref?token=foobar",
Options: []string{
"-f",
"lavfi",
"-re",
"input:in_314159265359_refref",
"process:314159265359",
"reference:refref",
"diskfs:/mnt/diskfs/disk.txt",
"memfs:http://localhost/mnt/memfs/mem.txt",
"fsdisk:/mnt/diskfs/fsdisk_2019-10-12_07-20-50.txt",
"fsmem:http://localhost/mnt/memfs/$inputid.txt",
},
},
}
require.Equal(t, wantprocess, process)
}
func TestProcessReplacer(t *testing.T) {
replacer := replace.New()
replacer.RegisterReplaceFunc("date", func(params map[string]string, config *app.Config, section string) string {
t, err := time.Parse(time.RFC3339, params["timestamp"])
if err != nil {
return ""
}
s, err := strftime.Format(params["format"], t)
if err != nil {
return ""
}
return s
}, map[string]string{
"format": "%Y-%m-%d_%H-%M-%S",
"timestamp": "2019-10-12T07:20:50.52Z",
})
replacer.RegisterReplaceFunc("diskfs", func(params map[string]string, config *app.Config, section string) string {
return "/mnt/diskfs"
}, nil)
replacer.RegisterReplaceFunc("fs:disk", func(params map[string]string, config *app.Config, section string) string {
return "/mnt/diskfs"
}, nil)
replacer.RegisterReplaceFunc("memfs", func(params map[string]string, config *app.Config, section string) string {
return "http://localhost/mnt/memfs"
}, nil)
replacer.RegisterReplaceFunc("fs:mem", func(params map[string]string, config *app.Config, section string) string {
return "http://localhost/mnt/memfs"
}, nil)
replacer.RegisterReplaceFunc("rtmp", func(params map[string]string, config *app.Config, section string) string {
return "rtmp://localhost/app/" + params["name"] + "?token=foobar"
}, nil)
replacer.RegisterReplaceFunc("srt", func(params map[string]string, config *app.Config, section string) string {
template := "srt://localhost:6000?mode=caller&transtype=live&latency=" + params["latency"] + "&streamid=" + params["name"]
if section == "output" {
template += ",mode:publish"
} else {
template += ",mode:request"
}
template += ",token:abcfoobar&passphrase=secret"
return template
}, map[string]string{
"name": "",
"latency": "20000", // 20 milliseconds, FFmpeg requires microseconds
})
@@ -726,7 +955,7 @@ func TestReplacer(t *testing.T) {
"reference:{reference}",
"diskfs:{diskfs}/disk.txt",
"memfs:{memfs}/mem.txt",
"fsdisk:{fs:disk}/fsdisk.txt",
"fsdisk:{fs:disk}/fsdisk_{date}.txt",
"fsmem:{fs:mem}/$inputid.txt",
},
},
@@ -794,7 +1023,7 @@ func TestReplacer(t *testing.T) {
"reference:refref",
"diskfs:/mnt/diskfs/disk.txt",
"memfs:http://localhost/mnt/memfs/mem.txt",
"fsdisk:/mnt/diskfs/fsdisk.txt",
"fsdisk:/mnt/diskfs/fsdisk_{date}.txt",
"fsmem:http://localhost/mnt/memfs/$inputid.txt",
},
Cleanup: []app.ConfigIOCleanup{},

24
vendor/github.com/lestrrat-go/strftime/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

5
vendor/github.com/lestrrat-go/strftime/.golangci.yml generated vendored Normal file
View File

@@ -0,0 +1,5 @@
issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck

28
vendor/github.com/lestrrat-go/strftime/Changes generated vendored Normal file
View File

@@ -0,0 +1,28 @@
Changes
=======
v1.0.6 - 20 Apr 2022
[Miscellaneous]
* Minimum go version is now go 1.13
* github.com/pkg/errors is going to be phased out in steps. In this release,
users may opt-in to using native errors using `fmt.Errorf("%w")` by
specifying the tag `strftime_native_errors`. In the next release, the default
will be to use native errors, but users will be able to opt-in to using
github.com/pkg/errors using a tag. The version after will remove github.com/pkg/errors.
This is something that we normally would do over a major version upgrade
but since we do not expect this library to receive API breaking changes in the
near future and thus no v2 is expected, we have decided to do this over few
non-major releases.
v1.0.5
[New features]
* `(strftime.Strftime).FormatBuffer([]byte, time.Time) []byte` has been added.
This allows the user to provide the same underlying `[]byte` buffer for each
call to `FormatBuffer`, which avoid allocation per call.
* `%I` formatted midnight as `00`, where it should have been using `01`
before v1.0.4
Apparently we have failed to provide Changes prior to v1.0.5 :(

21
vendor/github.com/lestrrat-go/strftime/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 lestrrat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

29
vendor/github.com/lestrrat-go/strftime/Makefile generated vendored Normal file
View File

@@ -0,0 +1,29 @@
.PHONY: bench realclean cover viewcover test lint
bench:
go test -tags bench -benchmem -bench .
@git checkout go.mod
@rm go.sum
realclean:
rm coverage.out
test:
go test -v -race ./...
cover:
ifeq ($(strip $(STRFTIME_TAGS)),)
go test -v -race -coverpkg=./... -coverprofile=coverage.out ./...
else
go test -v -tags $(STRFTIME_TAGS) -race -coverpkg=./... -coverprofile=coverage.out ./...
endif
viewcover:
go tool cover -html=coverage.out
lint:
golangci-lint run ./...
imports:
goimports -w ./

227
vendor/github.com/lestrrat-go/strftime/README.md generated vendored Normal file
View File

@@ -0,0 +1,227 @@
# strftime
Fast strftime for Go
[![Build Status](https://travis-ci.org/lestrrat-go/strftime.png?branch=master)](https://travis-ci.org/lestrrat-go/strftime)
[![GoDoc](https://godoc.org/github.com/lestrrat-go/strftime?status.svg)](https://godoc.org/github.com/lestrrat-go/strftime)
# SYNOPSIS
```go
f, err := strftime.New(`.... pattern ...`)
if err := f.Format(buf, time.Now()); err != nil {
log.Println(err.Error())
}
```
# DESCRIPTION
The goals for this library are
* Optimized for the same pattern being called repeatedly
* Be flexible about destination to write the results out
* Be as complete as possible in terms of conversion specifications
# API
## Format(string, time.Time) (string, error)
Takes the pattern and the time, and formats it. This function is a utility function that recompiles the pattern every time the function is called. If you know beforehand that you will be formatting the same pattern multiple times, consider using `New` to create a `Strftime` object and reuse it.
## New(string) (\*Strftime, error)
Takes the pattern and creates a new `Strftime` object.
## obj.Pattern() string
Returns the pattern string used to create this `Strftime` object
## obj.Format(io.Writer, time.Time) error
Formats the time according to the pre-compiled pattern, and writes the result to the specified `io.Writer`
## obj.FormatString(time.Time) string
Formats the time according to the pre-compiled pattern, and returns the result string.
# SUPPORTED CONVERSION SPECIFICATIONS
| pattern | description |
|:--------|:------------|
| %A | national representation of the full weekday name |
| %a | national representation of the abbreviated weekday |
| %B | national representation of the full month name |
| %b | national representation of the abbreviated month name |
| %C | (year / 100) as decimal number; single digits are preceded by a zero |
| %c | national representation of time and date |
| %D | equivalent to %m/%d/%y |
| %d | day of the month as a decimal number (01-31) |
| %e | the day of the month as a decimal number (1-31); single digits are preceded by a blank |
| %F | equivalent to %Y-%m-%d |
| %H | the hour (24-hour clock) as a decimal number (00-23) |
| %h | same as %b |
| %I | the hour (12-hour clock) as a decimal number (01-12) |
| %j | the day of the year as a decimal number (001-366) |
| %k | the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank |
| %l | the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank |
| %M | the minute as a decimal number (00-59) |
| %m | the month as a decimal number (01-12) |
| %n | a newline |
| %p | national representation of either "ante meridiem" (a.m.) or "post meridiem" (p.m.) as appropriate. |
| %R | equivalent to %H:%M |
| %r | equivalent to %I:%M:%S %p |
| %S | the second as a decimal number (00-60) |
| %T | equivalent to %H:%M:%S |
| %t | a tab |
| %U | the week number of the year (Sunday as the first day of the week) as a decimal number (00-53) |
| %u | the weekday (Monday as the first day of the week) as a decimal number (1-7) |
| %V | the week number of the year (Monday as the first day of the week) as a decimal number (01-53) |
| %v | equivalent to %e-%b-%Y |
| %W | the week number of the year (Monday as the first day of the week) as a decimal number (00-53) |
| %w | the weekday (Sunday as the first day of the week) as a decimal number (0-6) |
| %X | national representation of the time |
| %x | national representation of the date |
| %Y | the year with century as a decimal number |
| %y | the year without century as a decimal number (00-99) |
| %Z | the time zone name |
| %z | the time zone offset from UTC |
| %% | a '%' |
# EXTENSIONS / CUSTOM SPECIFICATIONS
This library in general tries to be POSIX compliant, but sometimes you just need that
extra specification or two that is relatively widely used but is not included in the
POSIX specification.
For example, POSIX does not specify how to print out milliseconds,
but popular implementations allow `%f` or `%L` to achieve this.
For those instances, `strftime.Strftime` can be configured to use a custom set of
specifications:
```
ss := strftime.NewSpecificationSet()
ss.Set('L', ...) // provide implementation for `%L`
// pass this new specification set to the strftime instance
p, err := strftime.New(`%L`, strftime.WithSpecificationSet(ss))
p.Format(..., time.Now())
```
The implementation must implement the `Appender` interface, which is
```
type Appender interface {
Append([]byte, time.Time) []byte
}
```
For commonly used extensions such as the millisecond example and Unix timestamp, we provide a default
implementation so the user can do one of the following:
```
// (1) Pass a specification byte and the Appender
// This allows you to pass arbitrary Appenders
p, err := strftime.New(
`%L`,
strftime.WithSpecification('L', strftime.Milliseconds),
)
// (2) Pass an option that knows to use strftime.Milliseconds
p, err := strftime.New(
`%L`,
strftime.WithMilliseconds('L'),
)
```
Similarly for Unix Timestamp:
```
// (1) Pass a specification byte and the Appender
// This allows you to pass arbitrary Appenders
p, err := strftime.New(
`%s`,
strftime.WithSpecification('s', strftime.UnixSeconds),
)
// (2) Pass an option that knows to use strftime.UnixSeconds
p, err := strftime.New(
`%s`,
strftime.WithUnixSeconds('s'),
)
```
If a common specification is missing, please feel free to submit a PR
(but please be sure to be able to defend how "common" it is)
## List of available extensions
- [`Milliseconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#Milliseconds) (related option: [`WithMilliseconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#WithMilliseconds));
- [`Microseconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#Microseconds) (related option: [`WithMicroseconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#WithMicroseconds));
- [`UnixSeconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#UnixSeconds) (related option: [`WithUnixSeconds`](https://pkg.go.dev/github.com/lestrrat-go/strftime?tab=doc#WithUnixSeconds)).
# PERFORMANCE / OTHER LIBRARIES
The following benchmarks were run separately because some libraries were using cgo on specific platforms (notabley, the fastly version)
```
// On my OS X 10.14.6, 2.3 GHz Intel Core i5, 16GB memory.
// go version go1.13.4 darwin/amd64
hummingbird% go test -tags bench -benchmem -bench .
<snip>
BenchmarkTebeka-4 297471 3905 ns/op 257 B/op 20 allocs/op
BenchmarkJehiah-4 818444 1773 ns/op 256 B/op 17 allocs/op
BenchmarkFastly-4 2330794 550 ns/op 80 B/op 5 allocs/op
BenchmarkLestrrat-4 916365 1458 ns/op 80 B/op 2 allocs/op
BenchmarkLestrratCachedString-4 2527428 546 ns/op 128 B/op 2 allocs/op
BenchmarkLestrratCachedWriter-4 537422 2155 ns/op 192 B/op 3 allocs/op
PASS
ok github.com/lestrrat-go/strftime 25.618s
```
```
// On a host on Google Cloud Platform, machine-type: f1-micro (vCPU x 1, memory: 0.6GB)
// (Yes, I was being skimpy)
// Linux <snip> 4.9.0-11-amd64 #1 SMP Debian 4.9.189-3+deb9u1 (2019-09-20) x86_64 GNU/Linux
// go version go1.13.4 linux/amd64
hummingbird% go test -tags bench -benchmem -bench .
<snip>
BenchmarkTebeka 254997 4726 ns/op 256 B/op 20 allocs/op
BenchmarkJehiah 659289 1882 ns/op 256 B/op 17 allocs/op
BenchmarkFastly 389150 3044 ns/op 224 B/op 13 allocs/op
BenchmarkLestrrat 699069 1780 ns/op 80 B/op 2 allocs/op
BenchmarkLestrratCachedString 2081594 589 ns/op 128 B/op 2 allocs/op
BenchmarkLestrratCachedWriter 825763 1480 ns/op 192 B/op 3 allocs/op
PASS
ok github.com/lestrrat-go/strftime 11.355s
```
This library is much faster than other libraries *IF* you can reuse the format pattern.
Here's the annotated list from the benchmark results. You can clearly see that (re)using a `Strftime` object
and producing a string is the fastest. Writing to an `io.Writer` seems a bit sluggish, but since
the one producing the string is doing almost exactly the same thing, we believe this is purely the overhead of
writing to an `io.Writer`
| Import Path | Score | Note |
|:------------------------------------|--------:|:--------------------------------|
| github.com/lestrrat-go/strftime | 3000000 | Using `FormatString()` (cached) |
| github.com/fastly/go-utils/strftime | 2000000 | Pure go version on OS X |
| github.com/lestrrat-go/strftime | 1000000 | Using `Format()` (NOT cached) |
| github.com/jehiah/go-strftime | 1000000 | |
| github.com/fastly/go-utils/strftime | 1000000 | cgo version on Linux |
| github.com/lestrrat-go/strftime | 500000 | Using `Format()` (cached) |
| github.com/tebeka/strftime | 300000 | |
However, depending on your pattern, this speed may vary. If you find a particular pattern that seems sluggish,
please send in patches or tests.
Please also note that this benchmark only uses the subset of conversion specifications that are supported by *ALL* of the libraries compared.
Somethings to consider when making performance comparisons in the future:
* Can it write to io.Writer?
* Which `%specification` does it handle?

348
vendor/github.com/lestrrat-go/strftime/appenders.go generated vendored Normal file
View File

@@ -0,0 +1,348 @@
package strftime
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"time"
)
// These are all of the standard, POSIX compliant specifications.
// Extensions should be in extensions.go
var (
fullWeekDayName = StdlibFormat("Monday")
abbrvWeekDayName = StdlibFormat("Mon")
fullMonthName = StdlibFormat("January")
abbrvMonthName = StdlibFormat("Jan")
centuryDecimal = AppendFunc(appendCentury)
timeAndDate = StdlibFormat("Mon Jan _2 15:04:05 2006")
mdy = StdlibFormat("01/02/06")
dayOfMonthZeroPad = StdlibFormat("02")
dayOfMonthSpacePad = StdlibFormat("_2")
ymd = StdlibFormat("2006-01-02")
twentyFourHourClockZeroPad = &hourPadded{twelveHour: false, pad: '0'}
twelveHourClockZeroPad = &hourPadded{twelveHour: true, pad: '0'}
dayOfYear = AppendFunc(appendDayOfYear)
twentyFourHourClockSpacePad = &hourPadded{twelveHour: false, pad: ' '}
twelveHourClockSpacePad = &hourPadded{twelveHour: true, pad: ' '}
minutesZeroPad = StdlibFormat("04")
monthNumberZeroPad = StdlibFormat("01")
newline = Verbatim("\n")
ampm = StdlibFormat("PM")
hm = StdlibFormat("15:04")
imsp = hmsWAMPM{}
secondsNumberZeroPad = StdlibFormat("05")
hms = StdlibFormat("15:04:05")
tab = Verbatim("\t")
weekNumberSundayOrigin = weeknumberOffset(0) // week number of the year, Sunday first
weekdayMondayOrigin = weekday(1)
// monday as the first day, and 01 as the first value
weekNumberMondayOriginOneOrigin = AppendFunc(appendWeekNumber)
eby = StdlibFormat("_2-Jan-2006")
// monday as the first day, and 00 as the first value
weekNumberMondayOrigin = weeknumberOffset(1) // week number of the year, Monday first
weekdaySundayOrigin = weekday(0)
natReprTime = StdlibFormat("15:04:05") // national representation of the time XXX is this correct?
natReprDate = StdlibFormat("01/02/06") // national representation of the date XXX is this correct?
year = StdlibFormat("2006") // year with century
yearNoCentury = StdlibFormat("06") // year w/o century
timezone = StdlibFormat("MST") // time zone name
timezoneOffset = StdlibFormat("-0700") // time zone ofset from UTC
percent = Verbatim("%")
)
// Appender is the interface that must be fulfilled by components that
// implement the translation of specifications to actual time value.
//
// The Append method takes the accumulated byte buffer, and the time to
// use to generate the textual representation. The resulting byte
// sequence must be returned by this method, normally by using the
// append() builtin function.
type Appender interface {
Append([]byte, time.Time) []byte
}
// AppendFunc is an utility type to allow users to create a
// function-only version of an Appender
type AppendFunc func([]byte, time.Time) []byte
func (af AppendFunc) Append(b []byte, t time.Time) []byte {
return af(b, t)
}
type appenderList []Appender
type dumper interface {
dump(io.Writer)
}
func (l appenderList) dump(out io.Writer) {
var buf bytes.Buffer
ll := len(l)
for i, a := range l {
if dumper, ok := a.(dumper); ok {
dumper.dump(&buf)
} else {
fmt.Fprintf(&buf, "%#v", a)
}
if i < ll-1 {
fmt.Fprintf(&buf, ",\n")
}
}
if _, err := buf.WriteTo(out); err != nil {
panic(err)
}
}
// does the time.Format thing
type stdlibFormat struct {
s string
}
// StdlibFormat returns an Appender that simply goes through `time.Format()`
// For example, if you know you want to display the abbreviated month name for %b,
// you can create a StdlibFormat with the pattern `Jan` and register that
// for specification `b`:
//
// a := StdlibFormat(`Jan`)
// ss := NewSpecificationSet()
// ss.Set('b', a) // does %b -> abbreviated month name
func StdlibFormat(s string) Appender {
return &stdlibFormat{s: s}
}
func (v stdlibFormat) Append(b []byte, t time.Time) []byte {
return t.AppendFormat(b, v.s)
}
func (v stdlibFormat) str() string {
return v.s
}
func (v stdlibFormat) canCombine() bool {
return true
}
func (v stdlibFormat) combine(w combiner) Appender {
return StdlibFormat(v.s + w.str())
}
func (v stdlibFormat) dump(out io.Writer) {
fmt.Fprintf(out, "stdlib: %s", v.s)
}
type verbatimw struct {
s string
}
// Verbatim returns an Appender suitable for generating static text.
// For static text, this method is slightly favorable than creating
// your own appender, as adjacent verbatim blocks will be combined
// at compile time to produce more efficient Appenders
func Verbatim(s string) Appender {
return &verbatimw{s: s}
}
func (v verbatimw) Append(b []byte, _ time.Time) []byte {
return append(b, v.s...)
}
func (v verbatimw) canCombine() bool {
return canCombine(v.s)
}
func (v verbatimw) combine(w combiner) Appender {
if _, ok := w.(*stdlibFormat); ok {
return StdlibFormat(v.s + w.str())
}
return Verbatim(v.s + w.str())
}
func (v verbatimw) str() string {
return v.s
}
func (v verbatimw) dump(out io.Writer) {
fmt.Fprintf(out, "verbatim: %s", v.s)
}
// These words below, as well as any decimal character
var combineExclusion = []string{
"Mon",
"Monday",
"Jan",
"January",
"MST",
"PM",
"pm",
}
func canCombine(s string) bool {
if strings.ContainsAny(s, "0123456789") {
return false
}
for _, word := range combineExclusion {
if strings.Contains(s, word) {
return false
}
}
return true
}
type combiner interface {
canCombine() bool
combine(combiner) Appender
str() string
}
// this is container for the compiler to keep track of appenders,
// and combine them as we parse and compile the pattern
type combiningAppend struct {
list appenderList
prev Appender
prevCanCombine bool
}
func (ca *combiningAppend) Append(w Appender) {
if ca.prevCanCombine {
if wc, ok := w.(combiner); ok && wc.canCombine() {
ca.prev = ca.prev.(combiner).combine(wc)
ca.list[len(ca.list)-1] = ca.prev
return
}
}
ca.list = append(ca.list, w)
ca.prev = w
ca.prevCanCombine = false
if comb, ok := w.(combiner); ok {
if comb.canCombine() {
ca.prevCanCombine = true
}
}
}
func appendCentury(b []byte, t time.Time) []byte {
n := t.Year() / 100
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
type weekday int
func (v weekday) Append(b []byte, t time.Time) []byte {
n := int(t.Weekday())
if n < int(v) {
n += 7
}
return append(b, byte(n+48))
}
type weeknumberOffset int
func (v weeknumberOffset) Append(b []byte, t time.Time) []byte {
yd := t.YearDay()
offset := int(t.Weekday()) - int(v)
if offset < 0 {
offset += 7
}
if yd < offset {
return append(b, '0', '0')
}
n := ((yd - offset) / 7) + 1
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
func appendWeekNumber(b []byte, t time.Time) []byte {
_, n := t.ISOWeek()
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
func appendDayOfYear(b []byte, t time.Time) []byte {
n := t.YearDay()
if n < 10 {
b = append(b, '0', '0')
} else if n < 100 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
type hourPadded struct {
pad byte
twelveHour bool
}
func (v hourPadded) Append(b []byte, t time.Time) []byte {
h := t.Hour()
if v.twelveHour && h > 12 {
h = h - 12
}
if v.twelveHour && h == 0 {
h = 12
}
if h < 10 {
b = append(b, v.pad)
b = append(b, byte(h+48))
} else {
b = unrollTwoDigits(b, h)
}
return b
}
func unrollTwoDigits(b []byte, v int) []byte {
b = append(b, byte((v/10)+48))
b = append(b, byte((v%10)+48))
return b
}
type hmsWAMPM struct{}
func (v hmsWAMPM) Append(b []byte, t time.Time) []byte {
h := t.Hour()
var am bool
if h == 0 {
b = append(b, '1')
b = append(b, '2')
am = true
} else {
switch {
case h == 12:
// no op
case h > 12:
h = h - 12
default:
am = true
}
b = unrollTwoDigits(b, h)
}
b = append(b, ':')
b = unrollTwoDigits(b, t.Minute())
b = append(b, ':')
b = unrollTwoDigits(b, t.Second())
b = append(b, ' ')
if am {
b = append(b, 'A')
} else {
b = append(b, 'P')
}
b = append(b, 'M')
return b
}

67
vendor/github.com/lestrrat-go/strftime/extension.go generated vendored Normal file
View File

@@ -0,0 +1,67 @@
package strftime
import (
"strconv"
"time"
)
// NOTE: declare private variable and iniitalize once in init(),
// and leave the Milliseconds() function as returning static content.
// This way, `go doc -all` does not show the contents of the
// milliseconds function
var milliseconds Appender
var microseconds Appender
var unixseconds Appender
func init() {
milliseconds = AppendFunc(func(b []byte, t time.Time) []byte {
millisecond := int(t.Nanosecond()) / int(time.Millisecond)
if millisecond < 100 {
b = append(b, '0')
}
if millisecond < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(millisecond)...)
})
microseconds = AppendFunc(func(b []byte, t time.Time) []byte {
microsecond := int(t.Nanosecond()) / int(time.Microsecond)
if microsecond < 100000 {
b = append(b, '0')
}
if microsecond < 10000 {
b = append(b, '0')
}
if microsecond < 1000 {
b = append(b, '0')
}
if microsecond < 100 {
b = append(b, '0')
}
if microsecond < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(microsecond)...)
})
unixseconds = AppendFunc(func(b []byte, t time.Time) []byte {
return append(b, strconv.FormatInt(t.Unix(), 10)...)
})
}
// Milliseconds returns the Appender suitable for creating a zero-padded,
// 3-digit millisecond textual representation.
func Milliseconds() Appender {
return milliseconds
}
// Microsecond returns the Appender suitable for creating a zero-padded,
// 6-digit microsecond textual representation.
func Microseconds() Appender {
return microseconds
}
// UnixSeconds returns the Appender suitable for creating
// unix timestamp textual representation.
func UnixSeconds() Appender {
return unixseconds
}

View File

@@ -0,0 +1,18 @@
//go:build strftime_native_errors
// +build strftime_native_errors
package errors
import "fmt"
func New(s string) error {
return fmt.Errorf(s)
}
func Errorf(s string, args ...interface{}) error {
return fmt.Errorf(s, args...)
}
func Wrap(err error, s string) error {
return fmt.Errorf(s+`: %w`, err)
}

View File

@@ -0,0 +1,18 @@
//go:build !strftime_native_errors
// +build !strftime_native_errors
package errors
import "github.com/pkg/errors"
func New(s string) error {
return errors.New(s)
}
func Errorf(s string, args ...interface{}) error {
return errors.Errorf(s, args...)
}
func Wrap(err error, s string) error {
return errors.Wrap(err, s)
}

67
vendor/github.com/lestrrat-go/strftime/options.go generated vendored Normal file
View File

@@ -0,0 +1,67 @@
package strftime
type Option interface {
Name() string
Value() interface{}
}
type option struct {
name string
value interface{}
}
func (o *option) Name() string { return o.name }
func (o *option) Value() interface{} { return o.value }
const optSpecificationSet = `opt-specification-set`
// WithSpecification allows you to specify a custom specification set
func WithSpecificationSet(ds SpecificationSet) Option {
return &option{
name: optSpecificationSet,
value: ds,
}
}
type optSpecificationPair struct {
name byte
appender Appender
}
const optSpecification = `opt-specification`
// WithSpecification allows you to create a new specification set on the fly,
// to be used only for that invocation.
func WithSpecification(b byte, a Appender) Option {
return &option{
name: optSpecification,
value: &optSpecificationPair{
name: b,
appender: a,
},
}
}
// WithMilliseconds is similar to WithSpecification, and specifies that
// the Strftime object should interpret the pattern `%b` (where b
// is the byte that you specify as the argument)
// as the zero-padded, 3 letter milliseconds of the time.
func WithMilliseconds(b byte) Option {
return WithSpecification(b, Milliseconds())
}
// WithMicroseconds is similar to WithSpecification, and specifies that
// the Strftime object should interpret the pattern `%b` (where b
// is the byte that you specify as the argument)
// as the zero-padded, 3 letter microseconds of the time.
func WithMicroseconds(b byte) Option {
return WithSpecification(b, Microseconds())
}
// WithUnixSeconds is similar to WithSpecification, and specifies that
// the Strftime object should interpret the pattern `%b` (where b
// is the byte that you specify as the argument)
// as the unix timestamp in seconds
func WithUnixSeconds(b byte) Option {
return WithSpecification(b, UnixSeconds())
}

View File

@@ -0,0 +1,152 @@
package strftime
import (
"fmt"
"sync"
"github.com/lestrrat-go/strftime/internal/errors"
)
// because there is no such thing was a sync.RWLocker
type rwLocker interface {
RLock()
RUnlock()
sync.Locker
}
// SpecificationSet is a container for patterns that Strftime uses.
// If you want a custom strftime, you can copy the default
// SpecificationSet and tweak it
type SpecificationSet interface {
Lookup(byte) (Appender, error)
Delete(byte) error
Set(byte, Appender) error
}
type specificationSet struct {
mutable bool
lock rwLocker
store map[byte]Appender
}
// The default specification set does not need any locking as it is never
// accessed from the outside, and is never mutated.
var defaultSpecificationSet SpecificationSet
func init() {
defaultSpecificationSet = newImmutableSpecificationSet()
}
func newImmutableSpecificationSet() SpecificationSet {
// Create a mutable one so that populateDefaultSpecifications work through
// its magic, then copy the associated map
// (NOTE: this is done this way because there used to be
// two struct types for specification set, united under an interface.
// it can now be removed, but we would need to change the entire
// populateDefaultSpecifications method, and I'm currently too lazy
// PRs welcome)
tmp := NewSpecificationSet()
ss := &specificationSet{
mutable: false,
lock: nil, // never used, so intentionally not initialized
store: tmp.(*specificationSet).store,
}
return ss
}
// NewSpecificationSet creates a specification set with the default specifications.
func NewSpecificationSet() SpecificationSet {
ds := &specificationSet{
mutable: true,
lock: &sync.RWMutex{},
store: make(map[byte]Appender),
}
populateDefaultSpecifications(ds)
return ds
}
var defaultSpecifications = map[byte]Appender{
'A': fullWeekDayName,
'a': abbrvWeekDayName,
'B': fullMonthName,
'b': abbrvMonthName,
'C': centuryDecimal,
'c': timeAndDate,
'D': mdy,
'd': dayOfMonthZeroPad,
'e': dayOfMonthSpacePad,
'F': ymd,
'H': twentyFourHourClockZeroPad,
'h': abbrvMonthName,
'I': twelveHourClockZeroPad,
'j': dayOfYear,
'k': twentyFourHourClockSpacePad,
'l': twelveHourClockSpacePad,
'M': minutesZeroPad,
'm': monthNumberZeroPad,
'n': newline,
'p': ampm,
'R': hm,
'r': imsp,
'S': secondsNumberZeroPad,
'T': hms,
't': tab,
'U': weekNumberSundayOrigin,
'u': weekdayMondayOrigin,
'V': weekNumberMondayOriginOneOrigin,
'v': eby,
'W': weekNumberMondayOrigin,
'w': weekdaySundayOrigin,
'X': natReprTime,
'x': natReprDate,
'Y': year,
'y': yearNoCentury,
'Z': timezone,
'z': timezoneOffset,
'%': percent,
}
func populateDefaultSpecifications(ds SpecificationSet) {
for c, handler := range defaultSpecifications {
if err := ds.Set(c, handler); err != nil {
panic(fmt.Sprintf("failed to set default specification for %c: %s", c, err))
}
}
}
func (ds *specificationSet) Lookup(b byte) (Appender, error) {
if ds.mutable {
ds.lock.RLock()
defer ds.lock.RLock()
}
v, ok := ds.store[b]
if !ok {
return nil, errors.Errorf(`lookup failed: '%%%c' was not found in specification set`, b)
}
return v, nil
}
func (ds *specificationSet) Delete(b byte) error {
if !ds.mutable {
return errors.New(`delete failed: this specification set is marked immutable`)
}
ds.lock.Lock()
defer ds.lock.Unlock()
delete(ds.store, b)
return nil
}
func (ds *specificationSet) Set(b byte, a Appender) error {
if !ds.mutable {
return errors.New(`set failed: this specification set is marked immutable`)
}
ds.lock.Lock()
defer ds.lock.Unlock()
ds.store[b] = a
return nil
}

228
vendor/github.com/lestrrat-go/strftime/strftime.go generated vendored Normal file
View File

@@ -0,0 +1,228 @@
package strftime
import (
"io"
"strings"
"sync"
"time"
"github.com/lestrrat-go/strftime/internal/errors"
)
type compileHandler interface {
handle(Appender)
}
// compile, and create an appender list
type appenderListBuilder struct {
list *combiningAppend
}
func (alb *appenderListBuilder) handle(a Appender) {
alb.list.Append(a)
}
// compile, and execute the appenders on the fly
type appenderExecutor struct {
t time.Time
dst []byte
}
func (ae *appenderExecutor) handle(a Appender) {
ae.dst = a.Append(ae.dst, ae.t)
}
func compile(handler compileHandler, p string, ds SpecificationSet) error {
for l := len(p); l > 0; l = len(p) {
// This is a really tight loop, so we don't even calls to
// Verbatim() to cuase extra stuff
var verbatim verbatimw
i := strings.IndexByte(p, '%')
if i < 0 {
verbatim.s = p
handler.handle(&verbatim)
// this is silly, but I don't trust break keywords when there's a
// possibility of this piece of code being rearranged
p = p[l:]
continue
}
if i == l-1 {
return errors.New(`stray % at the end of pattern`)
}
// we found a '%'. we need the next byte to decide what to do next
// we already know that i < l - 1
// everything up to the i is verbatim
if i > 0 {
verbatim.s = p[:i]
handler.handle(&verbatim)
p = p[i:]
}
specification, err := ds.Lookup(p[1])
if err != nil {
return errors.Wrap(err, `pattern compilation failed`)
}
handler.handle(specification)
p = p[2:]
}
return nil
}
func getSpecificationSetFor(options ...Option) (SpecificationSet, error) {
var ds SpecificationSet = defaultSpecificationSet
var extraSpecifications []*optSpecificationPair
for _, option := range options {
switch option.Name() {
case optSpecificationSet:
ds = option.Value().(SpecificationSet)
case optSpecification:
extraSpecifications = append(extraSpecifications, option.Value().(*optSpecificationPair))
}
}
if len(extraSpecifications) > 0 {
// If ds is immutable, we're going to need to create a new
// one. oh what a waste!
if raw, ok := ds.(*specificationSet); ok && !raw.mutable {
ds = NewSpecificationSet()
}
for _, v := range extraSpecifications {
if err := ds.Set(v.name, v.appender); err != nil {
return nil, err
}
}
}
return ds, nil
}
var fmtAppendExecutorPool = sync.Pool{
New: func() interface{} {
var h appenderExecutor
h.dst = make([]byte, 0, 32)
return &h
},
}
func getFmtAppendExecutor() *appenderExecutor {
return fmtAppendExecutorPool.Get().(*appenderExecutor)
}
func releasdeFmtAppendExecutor(v *appenderExecutor) {
// TODO: should we discard the buffer if it's too long?
v.dst = v.dst[:0]
fmtAppendExecutorPool.Put(v)
}
// Format takes the format `s` and the time `t` to produce the
// format date/time. Note that this function re-compiles the
// pattern every time it is called.
//
// If you know beforehand that you will be reusing the pattern
// within your application, consider creating a `Strftime` object
// and reusing it.
func Format(p string, t time.Time, options ...Option) (string, error) {
// TODO: this may be premature optimization
ds, err := getSpecificationSetFor(options...)
if err != nil {
return "", errors.Wrap(err, `failed to get specification set`)
}
h := getFmtAppendExecutor()
defer releasdeFmtAppendExecutor(h)
h.t = t
if err := compile(h, p, ds); err != nil {
return "", errors.Wrap(err, `failed to compile format`)
}
return string(h.dst), nil
}
// Strftime is the object that represents a compiled strftime pattern
type Strftime struct {
pattern string
compiled appenderList
}
// New creates a new Strftime object. If the compilation fails, then
// an error is returned in the second argument.
func New(p string, options ...Option) (*Strftime, error) {
// TODO: this may be premature optimization
ds, err := getSpecificationSetFor(options...)
if err != nil {
return nil, errors.Wrap(err, `failed to get specification set`)
}
var h appenderListBuilder
h.list = &combiningAppend{}
if err := compile(&h, p, ds); err != nil {
return nil, errors.Wrap(err, `failed to compile format`)
}
return &Strftime{
pattern: p,
compiled: h.list.list,
}, nil
}
// Pattern returns the original pattern string
func (f *Strftime) Pattern() string {
return f.pattern
}
// Format takes the destination `dst` and time `t`. It formats the date/time
// using the pre-compiled pattern, and outputs the results to `dst`
func (f *Strftime) Format(dst io.Writer, t time.Time) error {
const bufSize = 64
var b []byte
max := len(f.pattern) + 10
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
if _, err := dst.Write(f.format(b, t)); err != nil {
return err
}
return nil
}
// FormatBuffer is equivalent to Format, but appends the result directly to
// supplied slice dst, returning the updated slice. This avoids any internal
// memory allocation.
func (f *Strftime) FormatBuffer(dst []byte, t time.Time) []byte {
return f.format(dst, t)
}
// Dump outputs the internal structure of the formatter, for debugging purposes.
// Please do NOT assume the output format to be fixed: it is expected to change
// in the future.
func (f *Strftime) Dump(out io.Writer) {
f.compiled.dump(out)
}
func (f *Strftime) format(b []byte, t time.Time) []byte {
for _, w := range f.compiled {
b = w.Append(b, t)
}
return b
}
// FormatString takes the time `t` and formats it, returning the
// string containing the formated data.
func (f *Strftime) FormatString(t time.Time) string {
const bufSize = 64
var b []byte
max := len(f.pattern) + 10
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
return string(f.format(b, t))
}

24
vendor/github.com/pkg/errors/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

10
vendor/github.com/pkg/errors/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,10 @@
language: go
go_import_path: github.com/pkg/errors
go:
- 1.11.x
- 1.12.x
- 1.13.x
- tip
script:
- make check

23
vendor/github.com/pkg/errors/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,23 @@
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

44
vendor/github.com/pkg/errors/Makefile generated vendored Normal file
View File

@@ -0,0 +1,44 @@
PKGS := github.com/pkg/errors
SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS))
GO := go
check: test vet gofmt misspell unconvert staticcheck ineffassign unparam
test:
$(GO) test $(PKGS)
vet: | test
$(GO) vet $(PKGS)
staticcheck:
$(GO) get honnef.co/go/tools/cmd/staticcheck
staticcheck -checks all $(PKGS)
misspell:
$(GO) get github.com/client9/misspell/cmd/misspell
misspell \
-locale GB \
-error \
*.md *.go
unconvert:
$(GO) get github.com/mdempsky/unconvert
unconvert -v $(PKGS)
ineffassign:
$(GO) get github.com/gordonklaus/ineffassign
find $(SRCDIRS) -name '*.go' | xargs ineffassign
pedantic: check errcheck
unparam:
$(GO) get mvdan.cc/unparam
unparam ./...
errcheck:
$(GO) get github.com/kisielk/errcheck
errcheck $(PKGS)
gofmt:
@echo Checking code is gofmted
@test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)"

59
vendor/github.com/pkg/errors/README.md generated vendored Normal file
View File

@@ -0,0 +1,59 @@
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge)
Package errors provides simple error handling primitives.
`go get github.com/pkg/errors`
The traditional error handling idiom in Go is roughly akin to
```go
if err != nil {
return err
}
```
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
## Adding context to an error
The errors.Wrap function returns a new error that adds context to the original error. For example
```go
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
```
## Retrieving the cause of an error
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
```go
type causer interface {
Cause() error
}
```
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
```go
switch err := errors.Cause(err).(type) {
case *MyError:
// handle specifically
default:
// unknown error
}
```
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
## Roadmap
With the upcoming [Go2 error proposals](https://go.googlesource.com/proposal/+/master/design/go2draft.md) this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows:
- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible)
- 1.0. Final release.
## Contributing
Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports.
Before sending a PR, please discuss your change by raising an issue.
## License
BSD-2-Clause

32
vendor/github.com/pkg/errors/appveyor.yml generated vendored Normal file
View File

@@ -0,0 +1,32 @@
version: build-{build}.{branch}
clone_folder: C:\gopath\src\github.com\pkg\errors
shallow_clone: true # for startup speed
environment:
GOPATH: C:\gopath
platform:
- x64
# http://www.appveyor.com/docs/installed-software
install:
# some helpful output for debugging builds
- go version
- go env
# pre-installed MinGW at C:\MinGW is 32bit only
# but MSYS2 at C:\msys64 has mingw64
- set PATH=C:\msys64\mingw64\bin;%PATH%
- gcc --version
- g++ --version
build_script:
- go install -v ./...
test_script:
- set PATH=C:\gopath\bin;%PATH%
- go test -v ./...
#artifacts:
# - path: '%GOPATH%\bin\*.exe'
deploy: off

288
vendor/github.com/pkg/errors/errors.go generated vendored Normal file
View File

@@ -0,0 +1,288 @@
// Package errors provides simple error handling primitives.
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
//
// which when applied recursively up the call stack results in error reports
// without context or debugging information. The errors package allows
// programmers to add context to the failure path in their code in a way
// that does not destroy the original value of the error.
//
// Adding context to an error
//
// The errors.Wrap function returns a new error that adds context to the
// original error by recording a stack trace at the point Wrap is called,
// together with the supplied message. For example
//
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
//
// If additional control is required, the errors.WithStack and
// errors.WithMessage functions destructure errors.Wrap into its component
// operations: annotating an error with a stack trace and with a message,
// respectively.
//
// Retrieving the cause of an error
//
// Using errors.Wrap constructs a stack of errors, adding context to the
// preceding error. Depending on the nature of the error it may be necessary
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error that does not implement causer, which is assumed to be
// the original cause. For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
//
// Although the causer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
// be formatted by the fmt package. The following verbs are supported:
//
// %s print the error. If the error has a Cause it will be
// printed recursively.
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface:
//
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
//
// The returned errors.StackTrace type is defined as
//
// type StackTrace []Frame
//
// The Frame type represents a call site in the stack trace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stack trace of this error. For example:
//
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d\n", f, f)
// }
// }
//
// Although the stackTracer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// See the documentation for Frame.Format for more details.
package errors
import (
"fmt"
"io"
)
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{
msg: message,
stack: callers(),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: callers(),
}
}
// fundamental is an error that has a message and a stack, but no caller.
type fundamental struct {
msg string
*stack
}
func (f *fundamental) Error() string { return f.msg }
func (f *fundamental) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
f.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, f.msg)
case 'q':
fmt.Fprintf(s, "%q", f.msg)
}
}
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
err,
callers(),
}
}
type withStack struct {
error
*stack
}
func (w *withStack) Cause() error { return w.error }
// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withStack) Unwrap() error { return w.error }
func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
w.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, w.Error())
case 'q':
fmt.Fprintf(s, "%q", w.Error())
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: message,
}
return &withStack{
err,
callers(),
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is called, and the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
return &withStack{
err,
callers(),
}
}
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
// WithMessagef annotates err with the format specifier.
// If err is nil, WithMessagef returns nil.
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
type withMessage struct {
cause error
msg string
}
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
func (w *withMessage) Cause() error { return w.cause }
// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withMessage) Unwrap() error { return w.cause }
func (w *withMessage) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", w.Cause())
io.WriteString(s, w.msg)
return
}
fallthrough
case 's', 'q':
io.WriteString(s, w.Error())
}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}

38
vendor/github.com/pkg/errors/go113.go generated vendored Normal file
View File

@@ -0,0 +1,38 @@
// +build go1.13
package errors
import (
stderrors "errors"
)
// Is reports whether any error in err's chain matches target.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool { return stderrors.Is(err, target) }
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target, or if the error has a method As(interface{}) bool such that
// As(target) returns true. In the latter case, the As method is responsible for
// setting target.
//
// As will panic if target is not a non-nil pointer to either a type that implements
// error, or to any interface type. As returns false if err is nil.
func As(err error, target interface{}) bool { return stderrors.As(err, target) }
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
func Unwrap(err error) error {
return stderrors.Unwrap(err)
}

177
vendor/github.com/pkg/errors/stack.go generated vendored Normal file
View File

@@ -0,0 +1,177 @@
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strconv"
"strings"
)
// Frame represents a program counter inside a stack frame.
// For historical reasons if Frame is interpreted as a uintptr
// its value represents the program counter + 1.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
// file returns the full path to the file that contains the
// function for this Frame's pc.
func (f Frame) file() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
file, _ := fn.FileLine(f.pc())
return file
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
// name returns the name of this function, if known.
func (f Frame) name() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
return fn.Name()
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
io.WriteString(s, f.name())
io.WriteString(s, "\n\t")
io.WriteString(s, f.file())
default:
io.WriteString(s, path.Base(f.file()))
}
case 'd':
io.WriteString(s, strconv.Itoa(f.line()))
case 'n':
io.WriteString(s, funcname(f.name()))
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
}
}
// MarshalText formats a stacktrace Frame as a text string. The output is the
// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
func (f Frame) MarshalText() ([]byte, error) {
name := f.name()
if name == "unknown" {
return []byte(name), nil
}
return []byte(fmt.Sprintf("%s %s:%d", name, f.file(), f.line())), nil
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
io.WriteString(s, "\n")
f.Format(s, verb)
}
case s.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
st.formatSlice(s, verb)
}
case 's':
st.formatSlice(s, verb)
}
}
// formatSlice will format this StackTrace into the given buffer as a slice of
// Frame, only valid when called with '%s' or '%v'.
func (st StackTrace) formatSlice(s fmt.State, verb rune) {
io.WriteString(s, "[")
for i, f := range st {
if i > 0 {
io.WriteString(s, " ")
}
f.Format(s, verb)
}
io.WriteString(s, "]")
}
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
f := Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
}
}
}
}
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
}
return f
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}
// funcname removes the path prefix component of a function's name reported by func.Name().
func funcname(name string) string {
i := strings.LastIndex(name, "/")
name = name[i+1:]
i = strings.Index(name, ".")
return name[i+1:]
}

5
vendor/modules.txt vendored
View File

@@ -180,6 +180,10 @@ github.com/labstack/gommon/random
# github.com/leodido/go-urn v1.2.1
## explicit; go 1.13
github.com/leodido/go-urn
# github.com/lestrrat-go/strftime v1.0.6
## explicit; go 1.13
github.com/lestrrat-go/strftime
github.com/lestrrat-go/strftime/internal/errors
# github.com/libdns/libdns v0.2.1
## explicit; go 1.14
github.com/libdns/libdns
@@ -240,6 +244,7 @@ github.com/modern-go/concurrent
github.com/modern-go/reflect2
# github.com/pkg/errors v0.9.1
## explicit
github.com/pkg/errors
# github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
## explicit
github.com/pmezard/go-difflib/difflib