-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
50 lines (42 loc) · 831 Bytes
/
cli.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
package cli
import (
"fmt"
"io"
"github.com/oinume/lekcije/backend/errors"
)
const (
ExitOK = 0
ExitError = 1
)
type Main struct {
command Commander
}
type Commander interface {
SetOutStream(io.Writer)
SetErrStream(io.Writer)
//SetFlagSet(*flag.FlagSet)
//Name() string
Run([]string) error
}
func NewMain(command Commander, out, err io.Writer) *Main {
command.SetOutStream(out)
command.SetErrStream(err)
return &Main{
command: command,
}
}
func (m *Main) Run(args []string) error {
return m.command.Run(args)
}
func WriteError(w io.Writer, err error) {
fmt.Fprintf(w, "%v", err.Error())
fmt.Fprint(w, "\n--- stacktrace ---")
switch e := err.(type) {
case *errors.AnnotatedError:
if e.OutputStackTrace() {
fmt.Fprintf(w, "%+v\n", e.StackTrace())
}
default:
fmt.Fprintf(w, "%+v", err)
}
}