/
app.go
62 lines (52 loc) · 1.31 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package siftool
import (
"io"
"os"
)
// appOpts contains configured options.
type appOpts struct {
out io.Writer
err io.Writer
}
// AppOpt are used to configure optional behavior.
type AppOpt func(*appOpts) error
// App holds state and configured options.
type App struct {
opts appOpts
}
// OptAppOutput specifies that output should be written to w.
func OptAppOutput(w io.Writer) AppOpt {
return func(o *appOpts) error {
o.out = w
return nil
}
}
// OptAppError specifies that errors should be written to w.
func OptAppError(w io.Writer) AppOpt {
return func(o *appOpts) error {
o.err = w
return nil
}
}
// New creates a new App configured with opts.
//
// By default, application output and errors are written to os.Stdout and os.Stderr respectively.
// To modify this behavior, consider using OptAppOutput and/or OptAppError.
func New(opts ...AppOpt) (*App, error) {
a := App{
opts: appOpts{
out: os.Stdout,
err: os.Stderr,
},
}
for _, opt := range opts {
if err := opt(&a.opts); err != nil {
return nil, err
}
}
return &a, nil
}