@reegaviljoen/eldlock 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +285 -0
  2. package/bin/eldlock +11 -0
  3. package/docs/architecture.md +164 -0
  4. package/docs/threat-model.md +47 -0
  5. package/eldlock-cli/README.md +56 -0
  6. package/eldlock-cli/bin/eldlock +3 -0
  7. package/eldlock-cli/package-lock.json +805 -0
  8. package/eldlock-cli/package.json +71 -0
  9. package/eldlock-cli/src/api.ts +250 -0
  10. package/eldlock-cli/src/cli.ts +490 -0
  11. package/eldlock-cli/src/main.ts +10 -0
  12. package/eldlock-cli/src/tui.ts +676 -0
  13. package/eldlock-cli/tsconfig.json +13 -0
  14. package/eldlock-cli/vendor/npm/ansi-regex-6.2.2.tgz +0 -0
  15. package/eldlock-cli/vendor/npm/bun-ffi-structs-0.2.2.tgz +0 -0
  16. package/eldlock-cli/vendor/npm/diff-9.0.0.tgz +0 -0
  17. package/eldlock-cli/vendor/npm/emoji-regex-10.6.0.tgz +0 -0
  18. package/eldlock-cli/vendor/npm/esbuild-0.28.0.tgz +0 -0
  19. package/eldlock-cli/vendor/npm/esbuild-darwin-arm64-0.28.0.tgz +0 -0
  20. package/eldlock-cli/vendor/npm/esbuild-darwin-x64-0.28.0.tgz +0 -0
  21. package/eldlock-cli/vendor/npm/esbuild-linux-arm64-0.28.0.tgz +0 -0
  22. package/eldlock-cli/vendor/npm/esbuild-linux-x64-0.28.0.tgz +0 -0
  23. package/eldlock-cli/vendor/npm/fsevents-2.3.3.tgz +0 -0
  24. package/eldlock-cli/vendor/npm/get-east-asian-width-1.6.0.tgz +0 -0
  25. package/eldlock-cli/vendor/npm/marked-17.0.1.tgz +0 -0
  26. package/eldlock-cli/vendor/npm/opentui-core-0.3.1.tgz +0 -0
  27. package/eldlock-cli/vendor/npm/opentui-core-darwin-arm64-0.3.1.tgz +0 -0
  28. package/eldlock-cli/vendor/npm/opentui-core-darwin-x64-0.3.1.tgz +0 -0
  29. package/eldlock-cli/vendor/npm/opentui-core-linux-arm64-0.3.1.tgz +0 -0
  30. package/eldlock-cli/vendor/npm/opentui-core-linux-x64-0.3.1.tgz +0 -0
  31. package/eldlock-cli/vendor/npm/string-width-7.2.0.tgz +0 -0
  32. package/eldlock-cli/vendor/npm/strip-ansi-7.1.2.tgz +0 -0
  33. package/eldlock-cli/vendor/npm/tsx-4.22.4.tgz +0 -0
  34. package/eldlock-cli/vendor/npm/types-node-22.19.19.tgz +0 -0
  35. package/eldlock-cli/vendor/npm/typescript-5.9.3.tgz +0 -0
  36. package/eldlock-cli/vendor/npm/undici-types-6.21.0.tgz +0 -0
  37. package/eldlock-cli/vendor/npm/web-tree-sitter-0.25.10.tgz +0 -0
  38. package/eldlock-cli/vendor/npm/yoga-layout-3.2.1.tgz +0 -0
  39. package/eldlock-server/cmd/eldlock-server/main.go +132 -0
  40. package/eldlock-server/go.mod +10 -0
  41. package/eldlock-server/go.sum +11 -0
  42. package/eldlock-server/internal/api/README.md +14 -0
  43. package/eldlock-server/internal/api/core.go +126 -0
  44. package/eldlock-server/internal/api/exec.go +97 -0
  45. package/eldlock-server/internal/api/secrets.go +358 -0
  46. package/eldlock-server/internal/api/server.go +72 -0
  47. package/eldlock-server/internal/api/service_test.go +416 -0
  48. package/eldlock-server/internal/api/types.go +48 -0
  49. package/eldlock-server/internal/api/vault.go +69 -0
  50. package/eldlock-server/internal/api/vendor.go +44 -0
  51. package/eldlock-server/internal/libfido2/LICENSE +21 -0
  52. package/eldlock-server/internal/libfido2/README.md +127 -0
  53. package/eldlock-server/internal/libfido2/examples_test.go +614 -0
  54. package/eldlock-server/internal/libfido2/fido2.go +1234 -0
  55. package/eldlock-server/internal/libfido2/fido2_darwin.go +7 -0
  56. package/eldlock-server/internal/libfido2/fido2_other.go +9 -0
  57. package/eldlock-server/internal/libfido2/fido2_test.go +101 -0
  58. package/eldlock-server/internal/libfido2/go.mod +10 -0
  59. package/eldlock-server/internal/libfido2/go.sum +16 -0
  60. package/eldlock-server/internal/libfido2/log.go +87 -0
  61. package/eldlock-server/internal/store/README.md +7 -0
  62. package/eldlock-server/internal/store/store.go +434 -0
  63. package/eldlock-server/internal/store/store_test.go +125 -0
  64. package/eldlock-server/internal/yubikey/README.md +25 -0
  65. package/eldlock-server/internal/yubikey/default_fido2.go +7 -0
  66. package/eldlock-server/internal/yubikey/default_stub.go +7 -0
  67. package/eldlock-server/internal/yubikey/fido2_disabled.go +9 -0
  68. package/eldlock-server/internal/yubikey/fido2_libfido2.go +225 -0
  69. package/eldlock-server/internal/yubikey/fido2_libfido2_test.go +66 -0
  70. package/eldlock-server/internal/yubikey/passkey.go +139 -0
  71. package/eldlock-server/internal/yubikey/passkey_test.go +36 -0
  72. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/LICENSE +21 -0
  73. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/README.md +127 -0
  74. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/fido2.go +1234 -0
  75. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/fido2_darwin.go +7 -0
  76. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/fido2_other.go +9 -0
  77. package/eldlock-server/vendor/github.com/keys-pub/go-libfido2/log.go +87 -0
  78. package/eldlock-server/vendor/github.com/pkg/errors/.travis.yml +10 -0
  79. package/eldlock-server/vendor/github.com/pkg/errors/LICENSE +23 -0
  80. package/eldlock-server/vendor/github.com/pkg/errors/Makefile +44 -0
  81. package/eldlock-server/vendor/github.com/pkg/errors/README.md +59 -0
  82. package/eldlock-server/vendor/github.com/pkg/errors/appveyor.yml +32 -0
  83. package/eldlock-server/vendor/github.com/pkg/errors/errors.go +288 -0
  84. package/eldlock-server/vendor/github.com/pkg/errors/go113.go +38 -0
  85. package/eldlock-server/vendor/github.com/pkg/errors/stack.go +177 -0
  86. package/eldlock-server/vendor/modules.txt +7 -0
  87. package/examples/eldlock.toml +17 -0
  88. package/install.sh +66 -0
  89. package/package.json +66 -0
  90. package/scripts/build-production.mjs +177 -0
  91. package/scripts/postinstall-production.mjs +23 -0
@@ -0,0 +1,7 @@
1
+ package libfido2
2
+
3
+ /*
4
+ #cgo darwin pkg-config: libfido2
5
+ #cgo darwin LDFLAGS: -framework CoreFoundation -framework IOKit
6
+ */
7
+ import "C"
@@ -0,0 +1,9 @@
1
+ package libfido2
2
+
3
+ /*
4
+ #cgo linux LDFLAGS: -L/usr/lib/x86_64-linux-gnu -lfido2
5
+ #cgo linux CFLAGS: -I/usr/include/fido
6
+ #cgo windows LDFLAGS: -L${SRCDIR}/windows/lib -lfido2
7
+ #cgo windows CFLAGS: -I${SRCDIR}/windows/include
8
+ */
9
+ import "C"
@@ -0,0 +1,87 @@
1
+ package libfido2
2
+
3
+ import (
4
+ pkglog "log"
5
+ )
6
+
7
+ var logger = NewLogger(ErrLevel)
8
+
9
+ // SetLogger sets logger for the package.
10
+ func SetLogger(l Logger) {
11
+ logger = l
12
+ }
13
+
14
+ // Logger interface used in this package.
15
+ type Logger interface {
16
+ Debugf(format string, args ...interface{})
17
+ Infof(format string, args ...interface{})
18
+ Warningf(format string, args ...interface{})
19
+ Errorf(format string, args ...interface{})
20
+ Fatalf(format string, args ...interface{})
21
+ }
22
+
23
+ // LogLevel ...
24
+ type LogLevel int
25
+
26
+ const (
27
+ // DebugLevel ...
28
+ DebugLevel LogLevel = 3
29
+ // InfoLevel ...
30
+ InfoLevel LogLevel = 2
31
+ // WarnLevel ...
32
+ WarnLevel LogLevel = 1
33
+ // ErrLevel ...
34
+ ErrLevel LogLevel = 0
35
+ )
36
+
37
+ // NewLogger ...
38
+ func NewLogger(lev LogLevel) Logger {
39
+ return &defaultLog{Level: lev}
40
+ }
41
+
42
+ func (l LogLevel) String() string {
43
+ switch l {
44
+ case DebugLevel:
45
+ return "debug"
46
+ case InfoLevel:
47
+ return "info"
48
+ case WarnLevel:
49
+ return "warn"
50
+ case ErrLevel:
51
+ return "err"
52
+ default:
53
+ return ""
54
+ }
55
+ }
56
+
57
+ type defaultLog struct {
58
+ Level LogLevel
59
+ }
60
+
61
+ func (l defaultLog) Debugf(format string, args ...interface{}) {
62
+ if l.Level >= 3 {
63
+ pkglog.Printf("[DEBG] "+format+"\n", args...)
64
+ }
65
+ }
66
+
67
+ func (l defaultLog) Infof(format string, args ...interface{}) {
68
+ if l.Level >= 2 {
69
+ pkglog.Printf("[INFO] "+format+"\n", args...)
70
+ }
71
+ }
72
+
73
+ func (l defaultLog) Warningf(format string, args ...interface{}) {
74
+ if l.Level >= 1 {
75
+ pkglog.Printf("[WARN] "+format+"\n", args...)
76
+ }
77
+ }
78
+
79
+ func (l defaultLog) Errorf(format string, args ...interface{}) {
80
+ if l.Level >= 0 {
81
+ pkglog.Printf("[ERR] "+format+"\n", args...)
82
+ }
83
+ }
84
+
85
+ func (l defaultLog) Fatalf(format string, args ...interface{}) {
86
+ pkglog.Fatalf(format, args...)
87
+ }
@@ -0,0 +1,10 @@
1
+ language: go
2
+ go_import_path: github.com/pkg/errors
3
+ go:
4
+ - 1.11.x
5
+ - 1.12.x
6
+ - 1.13.x
7
+ - tip
8
+
9
+ script:
10
+ - make check
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015, Dave Cheney <dave@cheney.net>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,44 @@
1
+ PKGS := github.com/pkg/errors
2
+ SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS))
3
+ GO := go
4
+
5
+ check: test vet gofmt misspell unconvert staticcheck ineffassign unparam
6
+
7
+ test:
8
+ $(GO) test $(PKGS)
9
+
10
+ vet: | test
11
+ $(GO) vet $(PKGS)
12
+
13
+ staticcheck:
14
+ $(GO) get honnef.co/go/tools/cmd/staticcheck
15
+ staticcheck -checks all $(PKGS)
16
+
17
+ misspell:
18
+ $(GO) get github.com/client9/misspell/cmd/misspell
19
+ misspell \
20
+ -locale GB \
21
+ -error \
22
+ *.md *.go
23
+
24
+ unconvert:
25
+ $(GO) get github.com/mdempsky/unconvert
26
+ unconvert -v $(PKGS)
27
+
28
+ ineffassign:
29
+ $(GO) get github.com/gordonklaus/ineffassign
30
+ find $(SRCDIRS) -name '*.go' | xargs ineffassign
31
+
32
+ pedantic: check errcheck
33
+
34
+ unparam:
35
+ $(GO) get mvdan.cc/unparam
36
+ unparam ./...
37
+
38
+ errcheck:
39
+ $(GO) get github.com/kisielk/errcheck
40
+ errcheck $(PKGS)
41
+
42
+ gofmt:
43
+ @echo Checking code is gofmted
44
+ @test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)"
@@ -0,0 +1,59 @@
1
+ # 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)
2
+
3
+ Package errors provides simple error handling primitives.
4
+
5
+ `go get github.com/pkg/errors`
6
+
7
+ The traditional error handling idiom in Go is roughly akin to
8
+ ```go
9
+ if err != nil {
10
+ return err
11
+ }
12
+ ```
13
+ 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.
14
+
15
+ ## Adding context to an error
16
+
17
+ The errors.Wrap function returns a new error that adds context to the original error. For example
18
+ ```go
19
+ _, err := ioutil.ReadAll(r)
20
+ if err != nil {
21
+ return errors.Wrap(err, "read failed")
22
+ }
23
+ ```
24
+ ## Retrieving the cause of an error
25
+
26
+ 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`.
27
+ ```go
28
+ type causer interface {
29
+ Cause() error
30
+ }
31
+ ```
32
+ `errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
33
+ ```go
34
+ switch err := errors.Cause(err).(type) {
35
+ case *MyError:
36
+ // handle specifically
37
+ default:
38
+ // unknown error
39
+ }
40
+ ```
41
+
42
+ [Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
43
+
44
+ ## Roadmap
45
+
46
+ 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:
47
+
48
+ - 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible)
49
+ - 1.0. Final release.
50
+
51
+ ## Contributing
52
+
53
+ 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.
54
+
55
+ Before sending a PR, please discuss your change by raising an issue.
56
+
57
+ ## License
58
+
59
+ BSD-2-Clause
@@ -0,0 +1,32 @@
1
+ version: build-{build}.{branch}
2
+
3
+ clone_folder: C:\gopath\src\github.com\pkg\errors
4
+ shallow_clone: true # for startup speed
5
+
6
+ environment:
7
+ GOPATH: C:\gopath
8
+
9
+ platform:
10
+ - x64
11
+
12
+ # http://www.appveyor.com/docs/installed-software
13
+ install:
14
+ # some helpful output for debugging builds
15
+ - go version
16
+ - go env
17
+ # pre-installed MinGW at C:\MinGW is 32bit only
18
+ # but MSYS2 at C:\msys64 has mingw64
19
+ - set PATH=C:\msys64\mingw64\bin;%PATH%
20
+ - gcc --version
21
+ - g++ --version
22
+
23
+ build_script:
24
+ - go install -v ./...
25
+
26
+ test_script:
27
+ - set PATH=C:\gopath\bin;%PATH%
28
+ - go test -v ./...
29
+
30
+ #artifacts:
31
+ # - path: '%GOPATH%\bin\*.exe'
32
+ deploy: off
@@ -0,0 +1,288 @@
1
+ // Package errors provides simple error handling primitives.
2
+ //
3
+ // The traditional error handling idiom in Go is roughly akin to
4
+ //
5
+ // if err != nil {
6
+ // return err
7
+ // }
8
+ //
9
+ // which when applied recursively up the call stack results in error reports
10
+ // without context or debugging information. The errors package allows
11
+ // programmers to add context to the failure path in their code in a way
12
+ // that does not destroy the original value of the error.
13
+ //
14
+ // Adding context to an error
15
+ //
16
+ // The errors.Wrap function returns a new error that adds context to the
17
+ // original error by recording a stack trace at the point Wrap is called,
18
+ // together with the supplied message. For example
19
+ //
20
+ // _, err := ioutil.ReadAll(r)
21
+ // if err != nil {
22
+ // return errors.Wrap(err, "read failed")
23
+ // }
24
+ //
25
+ // If additional control is required, the errors.WithStack and
26
+ // errors.WithMessage functions destructure errors.Wrap into its component
27
+ // operations: annotating an error with a stack trace and with a message,
28
+ // respectively.
29
+ //
30
+ // Retrieving the cause of an error
31
+ //
32
+ // Using errors.Wrap constructs a stack of errors, adding context to the
33
+ // preceding error. Depending on the nature of the error it may be necessary
34
+ // to reverse the operation of errors.Wrap to retrieve the original error
35
+ // for inspection. Any error value which implements this interface
36
+ //
37
+ // type causer interface {
38
+ // Cause() error
39
+ // }
40
+ //
41
+ // can be inspected by errors.Cause. errors.Cause will recursively retrieve
42
+ // the topmost error that does not implement causer, which is assumed to be
43
+ // the original cause. For example:
44
+ //
45
+ // switch err := errors.Cause(err).(type) {
46
+ // case *MyError:
47
+ // // handle specifically
48
+ // default:
49
+ // // unknown error
50
+ // }
51
+ //
52
+ // Although the causer interface is not exported by this package, it is
53
+ // considered a part of its stable public interface.
54
+ //
55
+ // Formatted printing of errors
56
+ //
57
+ // All error values returned from this package implement fmt.Formatter and can
58
+ // be formatted by the fmt package. The following verbs are supported:
59
+ //
60
+ // %s print the error. If the error has a Cause it will be
61
+ // printed recursively.
62
+ // %v see %s
63
+ // %+v extended format. Each Frame of the error's StackTrace will
64
+ // be printed in detail.
65
+ //
66
+ // Retrieving the stack trace of an error or wrapper
67
+ //
68
+ // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
69
+ // invoked. This information can be retrieved with the following interface:
70
+ //
71
+ // type stackTracer interface {
72
+ // StackTrace() errors.StackTrace
73
+ // }
74
+ //
75
+ // The returned errors.StackTrace type is defined as
76
+ //
77
+ // type StackTrace []Frame
78
+ //
79
+ // The Frame type represents a call site in the stack trace. Frame supports
80
+ // the fmt.Formatter interface that can be used for printing information about
81
+ // the stack trace of this error. For example:
82
+ //
83
+ // if err, ok := err.(stackTracer); ok {
84
+ // for _, f := range err.StackTrace() {
85
+ // fmt.Printf("%+s:%d\n", f, f)
86
+ // }
87
+ // }
88
+ //
89
+ // Although the stackTracer interface is not exported by this package, it is
90
+ // considered a part of its stable public interface.
91
+ //
92
+ // See the documentation for Frame.Format for more details.
93
+ package errors
94
+
95
+ import (
96
+ "fmt"
97
+ "io"
98
+ )
99
+
100
+ // New returns an error with the supplied message.
101
+ // New also records the stack trace at the point it was called.
102
+ func New(message string) error {
103
+ return &fundamental{
104
+ msg: message,
105
+ stack: callers(),
106
+ }
107
+ }
108
+
109
+ // Errorf formats according to a format specifier and returns the string
110
+ // as a value that satisfies error.
111
+ // Errorf also records the stack trace at the point it was called.
112
+ func Errorf(format string, args ...interface{}) error {
113
+ return &fundamental{
114
+ msg: fmt.Sprintf(format, args...),
115
+ stack: callers(),
116
+ }
117
+ }
118
+
119
+ // fundamental is an error that has a message and a stack, but no caller.
120
+ type fundamental struct {
121
+ msg string
122
+ *stack
123
+ }
124
+
125
+ func (f *fundamental) Error() string { return f.msg }
126
+
127
+ func (f *fundamental) Format(s fmt.State, verb rune) {
128
+ switch verb {
129
+ case 'v':
130
+ if s.Flag('+') {
131
+ io.WriteString(s, f.msg)
132
+ f.stack.Format(s, verb)
133
+ return
134
+ }
135
+ fallthrough
136
+ case 's':
137
+ io.WriteString(s, f.msg)
138
+ case 'q':
139
+ fmt.Fprintf(s, "%q", f.msg)
140
+ }
141
+ }
142
+
143
+ // WithStack annotates err with a stack trace at the point WithStack was called.
144
+ // If err is nil, WithStack returns nil.
145
+ func WithStack(err error) error {
146
+ if err == nil {
147
+ return nil
148
+ }
149
+ return &withStack{
150
+ err,
151
+ callers(),
152
+ }
153
+ }
154
+
155
+ type withStack struct {
156
+ error
157
+ *stack
158
+ }
159
+
160
+ func (w *withStack) Cause() error { return w.error }
161
+
162
+ // Unwrap provides compatibility for Go 1.13 error chains.
163
+ func (w *withStack) Unwrap() error { return w.error }
164
+
165
+ func (w *withStack) Format(s fmt.State, verb rune) {
166
+ switch verb {
167
+ case 'v':
168
+ if s.Flag('+') {
169
+ fmt.Fprintf(s, "%+v", w.Cause())
170
+ w.stack.Format(s, verb)
171
+ return
172
+ }
173
+ fallthrough
174
+ case 's':
175
+ io.WriteString(s, w.Error())
176
+ case 'q':
177
+ fmt.Fprintf(s, "%q", w.Error())
178
+ }
179
+ }
180
+
181
+ // Wrap returns an error annotating err with a stack trace
182
+ // at the point Wrap is called, and the supplied message.
183
+ // If err is nil, Wrap returns nil.
184
+ func Wrap(err error, message string) error {
185
+ if err == nil {
186
+ return nil
187
+ }
188
+ err = &withMessage{
189
+ cause: err,
190
+ msg: message,
191
+ }
192
+ return &withStack{
193
+ err,
194
+ callers(),
195
+ }
196
+ }
197
+
198
+ // Wrapf returns an error annotating err with a stack trace
199
+ // at the point Wrapf is called, and the format specifier.
200
+ // If err is nil, Wrapf returns nil.
201
+ func Wrapf(err error, format string, args ...interface{}) error {
202
+ if err == nil {
203
+ return nil
204
+ }
205
+ err = &withMessage{
206
+ cause: err,
207
+ msg: fmt.Sprintf(format, args...),
208
+ }
209
+ return &withStack{
210
+ err,
211
+ callers(),
212
+ }
213
+ }
214
+
215
+ // WithMessage annotates err with a new message.
216
+ // If err is nil, WithMessage returns nil.
217
+ func WithMessage(err error, message string) error {
218
+ if err == nil {
219
+ return nil
220
+ }
221
+ return &withMessage{
222
+ cause: err,
223
+ msg: message,
224
+ }
225
+ }
226
+
227
+ // WithMessagef annotates err with the format specifier.
228
+ // If err is nil, WithMessagef returns nil.
229
+ func WithMessagef(err error, format string, args ...interface{}) error {
230
+ if err == nil {
231
+ return nil
232
+ }
233
+ return &withMessage{
234
+ cause: err,
235
+ msg: fmt.Sprintf(format, args...),
236
+ }
237
+ }
238
+
239
+ type withMessage struct {
240
+ cause error
241
+ msg string
242
+ }
243
+
244
+ func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
245
+ func (w *withMessage) Cause() error { return w.cause }
246
+
247
+ // Unwrap provides compatibility for Go 1.13 error chains.
248
+ func (w *withMessage) Unwrap() error { return w.cause }
249
+
250
+ func (w *withMessage) Format(s fmt.State, verb rune) {
251
+ switch verb {
252
+ case 'v':
253
+ if s.Flag('+') {
254
+ fmt.Fprintf(s, "%+v\n", w.Cause())
255
+ io.WriteString(s, w.msg)
256
+ return
257
+ }
258
+ fallthrough
259
+ case 's', 'q':
260
+ io.WriteString(s, w.Error())
261
+ }
262
+ }
263
+
264
+ // Cause returns the underlying cause of the error, if possible.
265
+ // An error value has a cause if it implements the following
266
+ // interface:
267
+ //
268
+ // type causer interface {
269
+ // Cause() error
270
+ // }
271
+ //
272
+ // If the error does not implement Cause, the original error will
273
+ // be returned. If the error is nil, nil will be returned without further
274
+ // investigation.
275
+ func Cause(err error) error {
276
+ type causer interface {
277
+ Cause() error
278
+ }
279
+
280
+ for err != nil {
281
+ cause, ok := err.(causer)
282
+ if !ok {
283
+ break
284
+ }
285
+ err = cause.Cause()
286
+ }
287
+ return err
288
+ }
@@ -0,0 +1,38 @@
1
+ // +build go1.13
2
+
3
+ package errors
4
+
5
+ import (
6
+ stderrors "errors"
7
+ )
8
+
9
+ // Is reports whether any error in err's chain matches target.
10
+ //
11
+ // The chain consists of err itself followed by the sequence of errors obtained by
12
+ // repeatedly calling Unwrap.
13
+ //
14
+ // An error is considered to match a target if it is equal to that target or if
15
+ // it implements a method Is(error) bool such that Is(target) returns true.
16
+ func Is(err, target error) bool { return stderrors.Is(err, target) }
17
+
18
+ // As finds the first error in err's chain that matches target, and if so, sets
19
+ // target to that error value and returns true.
20
+ //
21
+ // The chain consists of err itself followed by the sequence of errors obtained by
22
+ // repeatedly calling Unwrap.
23
+ //
24
+ // An error matches target if the error's concrete value is assignable to the value
25
+ // pointed to by target, or if the error has a method As(interface{}) bool such that
26
+ // As(target) returns true. In the latter case, the As method is responsible for
27
+ // setting target.
28
+ //
29
+ // As will panic if target is not a non-nil pointer to either a type that implements
30
+ // error, or to any interface type. As returns false if err is nil.
31
+ func As(err error, target interface{}) bool { return stderrors.As(err, target) }
32
+
33
+ // Unwrap returns the result of calling the Unwrap method on err, if err's
34
+ // type contains an Unwrap method returning error.
35
+ // Otherwise, Unwrap returns nil.
36
+ func Unwrap(err error) error {
37
+ return stderrors.Unwrap(err)
38
+ }