Add Godeps
Signed-off-by: Phus Lu <phus.lu@citrix.com>
This commit is contained in:
parent
6ca049e86c
commit
addd20360c
202
Godeps/Godeps.json
generated
Normal file
202
Godeps/Godeps.json
generated
Normal file
@ -0,0 +1,202 @@
|
||||
{
|
||||
"ImportPath": "github.com/xenserver/packer-builder-xenserver",
|
||||
"GoVersion": "go1.6",
|
||||
"GodepVersion": "v74",
|
||||
"Packages": [
|
||||
"./..."
|
||||
],
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/Sirupsen/logrus",
|
||||
"Comment": "v0.10.0-19-gf3cfb45",
|
||||
"Rev": "f3cfb454f4c209e6668c95216c4744b8fddb2356"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/dylanmei/iso8601",
|
||||
"Comment": "v0.1.0",
|
||||
"Rev": "2075bf119b58e5576c6ed9f867b8f3d17f2e54d4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/dylanmei/winrmtest",
|
||||
"Rev": "025617847eb2cf9bd1d851bc3b22ed28e6245ce5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/errwrap",
|
||||
"Rev": "7554cd9344cec97297fa6649b055a8c98c2a1e55"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/go-multierror",
|
||||
"Rev": "d30f09973e19c1dfcd120b2d9c4f168e68d6b5d5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/go-version",
|
||||
"Rev": "7e3c02b30806fa5779d3bdfc152ce4c6f40e7b38"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/yamux",
|
||||
"Rev": "df949784da9ed028ee76df44652e42d37a09d7e4"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/kr/fs",
|
||||
"Rev": "2788f0dbd16903de03cb8186e5c7d97b69ad387b"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/masterzen/simplexml/dom",
|
||||
"Rev": "95ba30457eb1121fa27753627c774c7cd4e90083"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/masterzen/winrm/soap",
|
||||
"Rev": "54ea5d01478cfc2afccec1504bd0dfcd8c260cfa"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/masterzen/winrm/winrm",
|
||||
"Rev": "54ea5d01478cfc2afccec1504bd0dfcd8c260cfa"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/masterzen/xmlpath",
|
||||
"Rev": "13f4951698adc0fa9c1dda3e275d489a24201161"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/go-fs",
|
||||
"Rev": "a34c1b9334e86165685a9449b782f20465eb8c69"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/go-fs/fat",
|
||||
"Rev": "a34c1b9334e86165685a9449b782f20465eb8c69"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/iochan",
|
||||
"Rev": "87b45ffd0e9581375c491fef3d32130bb15c5bd7"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/mapstructure",
|
||||
"Rev": "281073eb9eb092240d33ef253c404f1cca550309"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/multistep",
|
||||
"Rev": "162146fc57112954184d90266f4733e900ed05a5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/osext",
|
||||
"Rev": "5e2d6d41470f99c881826dedd8c526728b783c9c"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/common",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/common/ssh",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/common/uuid",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/communicator/none",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/communicator/ssh",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/communicator/winrm",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/helper/communicator",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/helper/config",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/packer",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/packer/plugin",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/packer/rpc",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/template",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/packer/template/interpolate",
|
||||
"Comment": "v0.10.1-135-g63edbd4",
|
||||
"Rev": "63edbd40edc5a55394f8532aa9958bbe257b31b5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mitchellh/reflectwalk",
|
||||
"Rev": "eecf4c70c626c7cfbb95c90195bc34d386c74ac6"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/nilshell/xmlrpc",
|
||||
"Rev": "41b9444050f7a0494a44cd5644f5048aa8261b17"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/nu7hatch/gouuid",
|
||||
"Rev": "179d4d0c4d8d407a32af483c2354df1d2c91e6c3"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/packer-community/winrmcp/winrmcp",
|
||||
"Rev": "f1bcf36a69fa2945e65dd099eee11b560fbd3346"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/pkg/sftp",
|
||||
"Rev": "e84cc8c755ca39b7b64f510fe1fffc1b51f210a5"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/satori/go.uuid",
|
||||
"Rev": "d41af8bb6a7704f00bc3b7cba9355ae6a5a80048"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/svanharmelen/gocs",
|
||||
"Comment": "v1.0.0-4-ga53656b",
|
||||
"Rev": "a53656b67c7b6db48362e3b4edb5e335a7162bf0"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/ugorji/go/codec",
|
||||
"Rev": "646ae4a518c1c3be0739df898118d9bccf993858"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/xenserver/go-xenserver-client",
|
||||
"Rev": "42dc9035c2e805b844614741d804f308bacb7490"
|
||||
},
|
||||
{
|
||||
"ImportPath": "golang.org/x/crypto/curve25519",
|
||||
"Rev": "1f22c0103821b9390939b6776727195525381532"
|
||||
},
|
||||
{
|
||||
"ImportPath": "golang.org/x/crypto/ssh",
|
||||
"Rev": "1f22c0103821b9390939b6776727195525381532"
|
||||
},
|
||||
{
|
||||
"ImportPath": "golang.org/x/crypto/ssh/agent",
|
||||
"Rev": "1f22c0103821b9390939b6776727195525381532"
|
||||
},
|
||||
{
|
||||
"ImportPath": "golang.org/x/sys/unix",
|
||||
"Rev": "50c6bc5e4292a1d4e65c6e9be5f53be28bcbe28e"
|
||||
}
|
||||
]
|
||||
}
|
5
Godeps/Readme
generated
Normal file
5
Godeps/Readme
generated
Normal file
@ -0,0 +1,5 @@
|
||||
This directory tree is generated automatically by godep.
|
||||
|
||||
Please do not edit.
|
||||
|
||||
See https://github.com/tools/godep for more information.
|
1
vendor/github.com/Sirupsen/logrus/.gitignore
generated
vendored
Normal file
1
vendor/github.com/Sirupsen/logrus/.gitignore
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
logrus
|
10
vendor/github.com/Sirupsen/logrus/.travis.yml
generated
vendored
Normal file
10
vendor/github.com/Sirupsen/logrus/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
- 1.6
|
||||
- tip
|
||||
install:
|
||||
- go get -t ./...
|
||||
script: GOMAXPROCS=4 GORACE="halt_on_error=1" go test -race -v ./...
|
66
vendor/github.com/Sirupsen/logrus/CHANGELOG.md
generated
vendored
Normal file
66
vendor/github.com/Sirupsen/logrus/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
# 0.10.0
|
||||
|
||||
* feature: Add a test hook (#180)
|
||||
* feature: `ParseLevel` is now case-insensitive (#326)
|
||||
* feature: `FieldLogger` interface that generalizes `Logger` and `Entry` (#308)
|
||||
* performance: avoid re-allocations on `WithFields` (#335)
|
||||
|
||||
# 0.9.0
|
||||
|
||||
* logrus/text_formatter: don't emit empty msg
|
||||
* logrus/hooks/airbrake: move out of main repository
|
||||
* logrus/hooks/sentry: move out of main repository
|
||||
* logrus/hooks/papertrail: move out of main repository
|
||||
* logrus/hooks/bugsnag: move out of main repository
|
||||
* logrus/core: run tests with `-race`
|
||||
* logrus/core: detect TTY based on `stderr`
|
||||
* logrus/core: support `WithError` on logger
|
||||
* logrus/core: Solaris support
|
||||
|
||||
# 0.8.7
|
||||
|
||||
* logrus/core: fix possible race (#216)
|
||||
* logrus/doc: small typo fixes and doc improvements
|
||||
|
||||
|
||||
# 0.8.6
|
||||
|
||||
* hooks/raven: allow passing an initialized client
|
||||
|
||||
# 0.8.5
|
||||
|
||||
* logrus/core: revert #208
|
||||
|
||||
# 0.8.4
|
||||
|
||||
* formatter/text: fix data race (#218)
|
||||
|
||||
# 0.8.3
|
||||
|
||||
* logrus/core: fix entry log level (#208)
|
||||
* logrus/core: improve performance of text formatter by 40%
|
||||
* logrus/core: expose `LevelHooks` type
|
||||
* logrus/core: add support for DragonflyBSD and NetBSD
|
||||
* formatter/text: print structs more verbosely
|
||||
|
||||
# 0.8.2
|
||||
|
||||
* logrus: fix more Fatal family functions
|
||||
|
||||
# 0.8.1
|
||||
|
||||
* logrus: fix not exiting on `Fatalf` and `Fatalln`
|
||||
|
||||
# 0.8.0
|
||||
|
||||
* logrus: defaults to stderr instead of stdout
|
||||
* hooks/sentry: add special field for `*http.Request`
|
||||
* formatter/text: ignore Windows for colors
|
||||
|
||||
# 0.7.3
|
||||
|
||||
* formatter/\*: allow configuration of timestamp layout
|
||||
|
||||
# 0.7.2
|
||||
|
||||
* formatter/text: Add configuration option for time format (#158)
|
21
vendor/github.com/Sirupsen/logrus/LICENSE
generated
vendored
Normal file
21
vendor/github.com/Sirupsen/logrus/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Simon Eskildsen
|
||||
|
||||
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.
|
390
vendor/github.com/Sirupsen/logrus/README.md
generated
vendored
Normal file
390
vendor/github.com/Sirupsen/logrus/README.md
generated
vendored
Normal file
@ -0,0 +1,390 @@
|
||||
# Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Build Status](https://travis-ci.org/Sirupsen/logrus.svg?branch=master)](https://travis-ci.org/Sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/Sirupsen/logrus?status.svg)](https://godoc.org/github.com/Sirupsen/logrus)
|
||||
|
||||
Logrus is a structured logger for Go (golang), completely API compatible with
|
||||
the standard library logger. [Godoc][godoc]. **Please note the Logrus API is not
|
||||
yet stable (pre 1.0). Logrus itself is completely stable and has been used in
|
||||
many large deployments. The core API is unlikely to change much but please
|
||||
version control your Logrus to make sure you aren't fetching latest `master` on
|
||||
every build.**
|
||||
|
||||
Nicely color-coded in development (when a TTY is attached, otherwise just
|
||||
plain text):
|
||||
|
||||
![Colored](http://i.imgur.com/PY7qMwd.png)
|
||||
|
||||
With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash
|
||||
or Splunk:
|
||||
|
||||
```json
|
||||
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the
|
||||
ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}
|
||||
|
||||
{"level":"warning","msg":"The group's number increased tremendously!",
|
||||
"number":122,"omg":true,"time":"2014-03-10 19:57:38.562471297 -0400 EDT"}
|
||||
|
||||
{"animal":"walrus","level":"info","msg":"A giant walrus appears!",
|
||||
"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}
|
||||
|
||||
{"animal":"walrus","level":"info","msg":"Tremendously sized cow enters the ocean.",
|
||||
"size":9,"time":"2014-03-10 19:57:38.562527896 -0400 EDT"}
|
||||
|
||||
{"level":"fatal","msg":"The ice breaks!","number":100,"omg":true,
|
||||
"time":"2014-03-10 19:57:38.562543128 -0400 EDT"}
|
||||
```
|
||||
|
||||
With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not
|
||||
attached, the output is compatible with the
|
||||
[logfmt](http://godoc.org/github.com/kr/logfmt) format:
|
||||
|
||||
```text
|
||||
time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8
|
||||
time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
|
||||
time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true
|
||||
time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4
|
||||
time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009
|
||||
time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It's over 9000!} number=100 omg=true
|
||||
exit status 1
|
||||
```
|
||||
|
||||
#### Example
|
||||
|
||||
The simplest way to use Logrus is simply the package-level exported logger:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
}).Info("A walrus appears")
|
||||
}
|
||||
```
|
||||
|
||||
Note that it's completely api-compatible with the stdlib logger, so you can
|
||||
replace your `log` imports everywhere with `log "github.com/Sirupsen/logrus"`
|
||||
and you'll now have the flexibility of Logrus. You can customize it all you
|
||||
want:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Log as JSON instead of the default ASCII formatter.
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
// Output to stderr instead of stdout, could also be a file.
|
||||
log.SetOutput(os.Stderr)
|
||||
|
||||
// Only log the warning severity or above.
|
||||
log.SetLevel(log.WarnLevel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
"size": 10,
|
||||
}).Info("A group of walrus emerges from the ocean")
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 122,
|
||||
}).Warn("The group's number increased tremendously!")
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 100,
|
||||
}).Fatal("The ice breaks!")
|
||||
|
||||
// A common pattern is to re-use fields between logging statements by re-using
|
||||
// the logrus.Entry returned from WithFields()
|
||||
contextLogger := log.WithFields(log.Fields{
|
||||
"common": "this is a common field",
|
||||
"other": "I also should be logged always",
|
||||
})
|
||||
|
||||
contextLogger.Info("I'll be logged with common and other field")
|
||||
contextLogger.Info("Me too")
|
||||
}
|
||||
```
|
||||
|
||||
For more advanced usage such as logging to multiple locations from the same
|
||||
application, you can also create an instance of the `logrus` Logger:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Create a new instance of the logger. You can have any number of instances.
|
||||
var log = logrus.New()
|
||||
|
||||
func main() {
|
||||
// The API for setting attributes is a little different than the package level
|
||||
// exported logger. See Godoc.
|
||||
log.Out = os.Stderr
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"animal": "walrus",
|
||||
"size": 10,
|
||||
}).Info("A group of walrus emerges from the ocean")
|
||||
}
|
||||
```
|
||||
|
||||
#### Fields
|
||||
|
||||
Logrus encourages careful, structured logging though logging fields instead of
|
||||
long, unparseable error messages. For example, instead of: `log.Fatalf("Failed
|
||||
to send event %s to topic %s with key %d")`, you should log the much more
|
||||
discoverable:
|
||||
|
||||
```go
|
||||
log.WithFields(log.Fields{
|
||||
"event": event,
|
||||
"topic": topic,
|
||||
"key": key,
|
||||
}).Fatal("Failed to send event")
|
||||
```
|
||||
|
||||
We've found this API forces you to think about logging in a way that produces
|
||||
much more useful logging messages. We've been in countless situations where just
|
||||
a single added field to a log statement that was already there would've saved us
|
||||
hours. The `WithFields` call is optional.
|
||||
|
||||
In general, with Logrus using any of the `printf`-family functions should be
|
||||
seen as a hint you should add a field, however, you can still use the
|
||||
`printf`-family functions with Logrus.
|
||||
|
||||
#### Hooks
|
||||
|
||||
You can add hooks for logging levels. For example to send errors to an exception
|
||||
tracking service on `Error`, `Fatal` and `Panic`, info to StatsD or log to
|
||||
multiple places simultaneously, e.g. syslog.
|
||||
|
||||
Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
|
||||
`init`:
|
||||
|
||||
```go
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "aibrake"
|
||||
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
|
||||
"log/syslog"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
// Use the Airbrake hook to report errors that have Error severity or above to
|
||||
// an exception tracker. You can create custom hooks, see the Hooks section.
|
||||
log.AddHook(airbrake.NewHook(123, "xyz", "production"))
|
||||
|
||||
hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
|
||||
if err != nil {
|
||||
log.Error("Unable to connect to local syslog daemon")
|
||||
} else {
|
||||
log.AddHook(hook)
|
||||
}
|
||||
}
|
||||
```
|
||||
Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md).
|
||||
|
||||
| Hook | Description |
|
||||
| ----- | ----------- |
|
||||
| [Airbrake](https://github.com/gemnasium/logrus-airbrake-hook) | Send errors to the Airbrake API V3. Uses the official [`gobrake`](https://github.com/airbrake/gobrake) behind the scenes. |
|
||||
| [Airbrake "legacy"](https://github.com/gemnasium/logrus-airbrake-legacy-hook) | Send errors to an exception tracking service compatible with the Airbrake API V2. Uses [`airbrake-go`](https://github.com/tobi/airbrake-go) behind the scenes. |
|
||||
| [Papertrail](https://github.com/polds/logrus-papertrail-hook) | Send errors to the [Papertrail](https://papertrailapp.com) hosted logging service via UDP. |
|
||||
| [Syslog](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go) | Send errors to remote syslog server. Uses standard library `log/syslog` behind the scenes. |
|
||||
| [Bugsnag](https://github.com/Shopify/logrus-bugsnag/blob/master/bugsnag.go) | Send errors to the Bugsnag exception tracking service. |
|
||||
| [Sentry](https://github.com/evalphobia/logrus_sentry) | Send errors to the Sentry error logging and aggregation service. |
|
||||
| [Hiprus](https://github.com/nubo/hiprus) | Send errors to a channel in hipchat. |
|
||||
| [Logrusly](https://github.com/sebest/logrusly) | Send logs to [Loggly](https://www.loggly.com/) |
|
||||
| [Slackrus](https://github.com/johntdyer/slackrus) | Hook for Slack chat. |
|
||||
| [Journalhook](https://github.com/wercker/journalhook) | Hook for logging to `systemd-journald` |
|
||||
| [Graylog](https://github.com/gemnasium/logrus-graylog-hook) | Hook for logging to [Graylog](http://graylog2.org/) |
|
||||
| [Raygun](https://github.com/squirkle/logrus-raygun-hook) | Hook for logging to [Raygun.io](http://raygun.io/) |
|
||||
| [LFShook](https://github.com/rifflock/lfshook) | Hook for logging to the local filesystem |
|
||||
| [Honeybadger](https://github.com/agonzalezro/logrus_honeybadger) | Hook for sending exceptions to Honeybadger |
|
||||
| [Mail](https://github.com/zbindenren/logrus_mail) | Hook for sending exceptions via mail |
|
||||
| [Rollrus](https://github.com/heroku/rollrus) | Hook for sending errors to rollbar |
|
||||
| [Fluentd](https://github.com/evalphobia/logrus_fluent) | Hook for logging to fluentd |
|
||||
| [Mongodb](https://github.com/weekface/mgorus) | Hook for logging to mongodb |
|
||||
| [Influxus] (http://github.com/vlad-doru/influxus) | Hook for concurrently logging to [InfluxDB] (http://influxdata.com/) |
|
||||
| [InfluxDB](https://github.com/Abramovic/logrus_influxdb) | Hook for logging to influxdb |
|
||||
| [Octokit](https://github.com/dorajistyle/logrus-octokit-hook) | Hook for logging to github via octokit |
|
||||
| [DeferPanic](https://github.com/deferpanic/dp-logrus) | Hook for logging to DeferPanic |
|
||||
| [Redis-Hook](https://github.com/rogierlommers/logrus-redis-hook) | Hook for logging to a ELK stack (through Redis) |
|
||||
| [Amqp-Hook](https://github.com/vladoatanasov/logrus_amqp) | Hook for logging to Amqp broker (Like RabbitMQ) |
|
||||
| [KafkaLogrus](https://github.com/goibibo/KafkaLogrus) | Hook for logging to kafka |
|
||||
| [Typetalk](https://github.com/dragon3/logrus-typetalk-hook) | Hook for logging to [Typetalk](https://www.typetalk.in/) |
|
||||
| [ElasticSearch](https://github.com/sohlich/elogrus) | Hook for logging to ElasticSearch|
|
||||
| [Sumorus](https://github.com/doublefree/sumorus) | Hook for logging to [SumoLogic](https://www.sumologic.com/)|
|
||||
| [Logstash](https://github.com/bshuster-repo/logrus-logstash-hook) | Hook for logging to [Logstash](https://www.elastic.co/products/logstash) |
|
||||
|
||||
#### Level logging
|
||||
|
||||
Logrus has six logging levels: Debug, Info, Warning, Error, Fatal and Panic.
|
||||
|
||||
```go
|
||||
log.Debug("Useful debugging information.")
|
||||
log.Info("Something noteworthy happened!")
|
||||
log.Warn("You should probably take a look at this.")
|
||||
log.Error("Something failed but I'm not quitting.")
|
||||
// Calls os.Exit(1) after logging
|
||||
log.Fatal("Bye.")
|
||||
// Calls panic() after logging
|
||||
log.Panic("I'm bailing.")
|
||||
```
|
||||
|
||||
You can set the logging level on a `Logger`, then it will only log entries with
|
||||
that severity or anything above it:
|
||||
|
||||
```go
|
||||
// Will log anything that is info or above (warn, error, fatal, panic). Default.
|
||||
log.SetLevel(log.InfoLevel)
|
||||
```
|
||||
|
||||
It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
|
||||
environment if your application has that.
|
||||
|
||||
#### Entries
|
||||
|
||||
Besides the fields added with `WithField` or `WithFields` some fields are
|
||||
automatically added to all logging events:
|
||||
|
||||
1. `time`. The timestamp when the entry was created.
|
||||
2. `msg`. The logging message passed to `{Info,Warn,Error,Fatal,Panic}` after
|
||||
the `AddFields` call. E.g. `Failed to send event.`
|
||||
3. `level`. The logging level. E.g. `info`.
|
||||
|
||||
#### Environments
|
||||
|
||||
Logrus has no notion of environment.
|
||||
|
||||
If you wish for hooks and formatters to only be used in specific environments,
|
||||
you should handle that yourself. For example, if your application has a global
|
||||
variable `Environment`, which is a string representation of the environment you
|
||||
could do:
|
||||
|
||||
```go
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
init() {
|
||||
// do something here to set environment depending on an environment variable
|
||||
// or command-line flag
|
||||
if Environment == "production" {
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
} else {
|
||||
// The TextFormatter is default, you don't actually have to do this.
|
||||
log.SetFormatter(&log.TextFormatter{})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This configuration is how `logrus` was intended to be used, but JSON in
|
||||
production is mostly only useful if you do log aggregation with tools like
|
||||
Splunk or Logstash.
|
||||
|
||||
#### Formatters
|
||||
|
||||
The built-in logging formatters are:
|
||||
|
||||
* `logrus.TextFormatter`. Logs the event in colors if stdout is a tty, otherwise
|
||||
without colors.
|
||||
* *Note:* to force colored output when there is no TTY, set the `ForceColors`
|
||||
field to `true`. To force no colored output even if there is a TTY set the
|
||||
`DisableColors` field to `true`
|
||||
* `logrus.JSONFormatter`. Logs fields as JSON.
|
||||
* `logrus/formatters/logstash.LogstashFormatter`. Logs fields as [Logstash](http://logstash.net) Events.
|
||||
|
||||
```go
|
||||
logrus.SetFormatter(&logstash.LogstashFormatter{Type: "application_name"})
|
||||
```
|
||||
|
||||
Third party logging formatters:
|
||||
|
||||
* [`prefixed`](https://github.com/x-cray/logrus-prefixed-formatter). Displays log entry source along with alternative layout.
|
||||
* [`zalgo`](https://github.com/aybabtme/logzalgo). Invoking the P͉̫o̳̼̊w̖͈̰͎e̬͔̭͂r͚̼̹̲ ̫͓͉̳͈ō̠͕͖̚f̝͍̠ ͕̲̞͖͑Z̖̫̤̫ͪa͉̬͈̗l͖͎g̳̥o̰̥̅!̣͔̲̻͊̄ ̙̘̦̹̦.
|
||||
|
||||
You can define your formatter by implementing the `Formatter` interface,
|
||||
requiring a `Format` method. `Format` takes an `*Entry`. `entry.Data` is a
|
||||
`Fields` type (`map[string]interface{}`) with all your fields as well as the
|
||||
default ones (see Entries section above):
|
||||
|
||||
```go
|
||||
type MyJSONFormatter struct {
|
||||
}
|
||||
|
||||
log.SetFormatter(new(MyJSONFormatter))
|
||||
|
||||
func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||
// Note this doesn't include Time, Level and Message which are available on
|
||||
// the Entry. Consult `godoc` on information about those fields or read the
|
||||
// source of the official loggers.
|
||||
serialized, err := json.Marshal(entry.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
||||
}
|
||||
return append(serialized, '\n'), nil
|
||||
}
|
||||
```
|
||||
|
||||
#### Logger as an `io.Writer`
|
||||
|
||||
Logrus can be transformed into an `io.Writer`. That writer is the end of an `io.Pipe` and it is your responsibility to close it.
|
||||
|
||||
```go
|
||||
w := logger.Writer()
|
||||
defer w.Close()
|
||||
|
||||
srv := http.Server{
|
||||
// create a stdlib log.Logger that writes to
|
||||
// logrus.Logger.
|
||||
ErrorLog: log.New(w, "", 0),
|
||||
}
|
||||
```
|
||||
|
||||
Each line written to that writer will be printed the usual way, using formatters
|
||||
and hooks. The level for those entries is `info`.
|
||||
|
||||
#### Rotation
|
||||
|
||||
Log rotation is not provided with Logrus. Log rotation should be done by an
|
||||
external program (like `logrotate(8)`) that can compress and delete old log
|
||||
entries. It should not be a feature of the application-level logger.
|
||||
|
||||
#### Tools
|
||||
|
||||
| Tool | Description |
|
||||
| ---- | ----------- |
|
||||
|[Logrus Mate](https://github.com/gogap/logrus_mate)|Logrus mate is a tool for Logrus to manage loggers, you can initial logger's level, hook and formatter by config file, the logger will generated with different config at different environment.|
|
||||
|
||||
#### Testing
|
||||
|
||||
Logrus has a built in facility for asserting the presence of log messages. This is implemented through the `test` hook and provides:
|
||||
|
||||
* decorators for existing logger (`test.NewLocal` and `test.NewGlobal`) which basically just add the `test` hook
|
||||
* a test logger (`test.NewNullLogger`) that just records log messages (and does not output any):
|
||||
|
||||
```go
|
||||
logger, hook := NewNullLogger()
|
||||
logger.Error("Hello error")
|
||||
|
||||
assert.Equal(1, len(hook.Entries))
|
||||
assert.Equal(logrus.ErrorLevel, hook.LastEntry().Level)
|
||||
assert.Equal("Hello error", hook.LastEntry().Message)
|
||||
|
||||
hook.Reset()
|
||||
assert.Nil(hook.LastEntry())
|
||||
```
|
26
vendor/github.com/Sirupsen/logrus/doc.go
generated
vendored
Normal file
26
vendor/github.com/Sirupsen/logrus/doc.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
|
||||
|
||||
|
||||
The simplest way to use Logrus is simply the package-level exported logger:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
"number": 1,
|
||||
"size": 10,
|
||||
}).Info("A walrus appears")
|
||||
}
|
||||
|
||||
Output:
|
||||
time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10
|
||||
|
||||
For a full guide visit https://github.com/Sirupsen/logrus
|
||||
*/
|
||||
package logrus
|
264
vendor/github.com/Sirupsen/logrus/entry.go
generated
vendored
Normal file
264
vendor/github.com/Sirupsen/logrus/entry.go
generated
vendored
Normal file
@ -0,0 +1,264 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Defines the key when adding errors using WithError.
|
||||
var ErrorKey = "error"
|
||||
|
||||
// An entry is the final or intermediate Logrus logging entry. It contains all
|
||||
// the fields passed with WithField{,s}. It's finally logged when Debug, Info,
|
||||
// Warn, Error, Fatal or Panic is called on it. These objects can be reused and
|
||||
// passed around as much as you wish to avoid field duplication.
|
||||
type Entry struct {
|
||||
Logger *Logger
|
||||
|
||||
// Contains all the fields set by the user.
|
||||
Data Fields
|
||||
|
||||
// Time at which the log entry was created
|
||||
Time time.Time
|
||||
|
||||
// Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic
|
||||
Level Level
|
||||
|
||||
// Message passed to Debug, Info, Warn, Error, Fatal or Panic
|
||||
Message string
|
||||
}
|
||||
|
||||
func NewEntry(logger *Logger) *Entry {
|
||||
return &Entry{
|
||||
Logger: logger,
|
||||
// Default is three fields, give a little extra room
|
||||
Data: make(Fields, 5),
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a reader for the entry, which is a proxy to the formatter.
|
||||
func (entry *Entry) Reader() (*bytes.Buffer, error) {
|
||||
serialized, err := entry.Logger.Formatter.Format(entry)
|
||||
return bytes.NewBuffer(serialized), err
|
||||
}
|
||||
|
||||
// Returns the string representation from the reader and ultimately the
|
||||
// formatter.
|
||||
func (entry *Entry) String() (string, error) {
|
||||
reader, err := entry.Reader()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return reader.String(), err
|
||||
}
|
||||
|
||||
// Add an error as single field (using the key defined in ErrorKey) to the Entry.
|
||||
func (entry *Entry) WithError(err error) *Entry {
|
||||
return entry.WithField(ErrorKey, err)
|
||||
}
|
||||
|
||||
// Add a single field to the Entry.
|
||||
func (entry *Entry) WithField(key string, value interface{}) *Entry {
|
||||
return entry.WithFields(Fields{key: value})
|
||||
}
|
||||
|
||||
// Add a map of fields to the Entry.
|
||||
func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||
data := make(Fields, len(entry.Data)+len(fields))
|
||||
for k, v := range entry.Data {
|
||||
data[k] = v
|
||||
}
|
||||
for k, v := range fields {
|
||||
data[k] = v
|
||||
}
|
||||
return &Entry{Logger: entry.Logger, Data: data}
|
||||
}
|
||||
|
||||
// This function is not declared with a pointer value because otherwise
|
||||
// race conditions will occur when using multiple goroutines
|
||||
func (entry Entry) log(level Level, msg string) {
|
||||
entry.Time = time.Now()
|
||||
entry.Level = level
|
||||
entry.Message = msg
|
||||
|
||||
if err := entry.Logger.Hooks.Fire(level, &entry); err != nil {
|
||||
entry.Logger.mu.Lock()
|
||||
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
|
||||
entry.Logger.mu.Unlock()
|
||||
}
|
||||
|
||||
reader, err := entry.Reader()
|
||||
if err != nil {
|
||||
entry.Logger.mu.Lock()
|
||||
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
|
||||
entry.Logger.mu.Unlock()
|
||||
}
|
||||
|
||||
entry.Logger.mu.Lock()
|
||||
defer entry.Logger.mu.Unlock()
|
||||
|
||||
_, err = io.Copy(entry.Logger.Out, reader)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
|
||||
}
|
||||
|
||||
// To avoid Entry#log() returning a value that only would make sense for
|
||||
// panic() to use in Entry#Panic(), we avoid the allocation by checking
|
||||
// directly here.
|
||||
if level <= PanicLevel {
|
||||
panic(&entry)
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Debug(args ...interface{}) {
|
||||
if entry.Logger.Level >= DebugLevel {
|
||||
entry.log(DebugLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Print(args ...interface{}) {
|
||||
entry.Info(args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Info(args ...interface{}) {
|
||||
if entry.Logger.Level >= InfoLevel {
|
||||
entry.log(InfoLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warn(args ...interface{}) {
|
||||
if entry.Logger.Level >= WarnLevel {
|
||||
entry.log(WarnLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warning(args ...interface{}) {
|
||||
entry.Warn(args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Error(args ...interface{}) {
|
||||
if entry.Logger.Level >= ErrorLevel {
|
||||
entry.log(ErrorLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatal(args ...interface{}) {
|
||||
if entry.Logger.Level >= FatalLevel {
|
||||
entry.log(FatalLevel, fmt.Sprint(args...))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panic(args ...interface{}) {
|
||||
if entry.Logger.Level >= PanicLevel {
|
||||
entry.log(PanicLevel, fmt.Sprint(args...))
|
||||
}
|
||||
panic(fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
// Entry Printf family functions
|
||||
|
||||
func (entry *Entry) Debugf(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= DebugLevel {
|
||||
entry.Debug(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Infof(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= InfoLevel {
|
||||
entry.Info(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Printf(format string, args ...interface{}) {
|
||||
entry.Infof(format, args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= WarnLevel {
|
||||
entry.Warn(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warningf(format string, args ...interface{}) {
|
||||
entry.Warnf(format, args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= ErrorLevel {
|
||||
entry.Error(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= FatalLevel {
|
||||
entry.Fatal(fmt.Sprintf(format, args...))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
||||
if entry.Logger.Level >= PanicLevel {
|
||||
entry.Panic(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
// Entry Println family functions
|
||||
|
||||
func (entry *Entry) Debugln(args ...interface{}) {
|
||||
if entry.Logger.Level >= DebugLevel {
|
||||
entry.Debug(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Infoln(args ...interface{}) {
|
||||
if entry.Logger.Level >= InfoLevel {
|
||||
entry.Info(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Println(args ...interface{}) {
|
||||
entry.Infoln(args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Warnln(args ...interface{}) {
|
||||
if entry.Logger.Level >= WarnLevel {
|
||||
entry.Warn(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warningln(args ...interface{}) {
|
||||
entry.Warnln(args...)
|
||||
}
|
||||
|
||||
func (entry *Entry) Errorln(args ...interface{}) {
|
||||
if entry.Logger.Level >= ErrorLevel {
|
||||
entry.Error(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalln(args ...interface{}) {
|
||||
if entry.Logger.Level >= FatalLevel {
|
||||
entry.Fatal(entry.sprintlnn(args...))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panicln(args ...interface{}) {
|
||||
if entry.Logger.Level >= PanicLevel {
|
||||
entry.Panic(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
// Sprintlnn => Sprint no newline. This is to get the behavior of how
|
||||
// fmt.Sprintln where spaces are always added between operands, regardless of
|
||||
// their type. Instead of vendoring the Sprintln implementation to spare a
|
||||
// string allocation, we do the simplest thing.
|
||||
func (entry *Entry) sprintlnn(args ...interface{}) string {
|
||||
msg := fmt.Sprintln(args...)
|
||||
return msg[:len(msg)-1]
|
||||
}
|
193
vendor/github.com/Sirupsen/logrus/exported.go
generated
vendored
Normal file
193
vendor/github.com/Sirupsen/logrus/exported.go
generated
vendored
Normal file
@ -0,0 +1,193 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
// std is the name of the standard logger in stdlib `log`
|
||||
std = New()
|
||||
)
|
||||
|
||||
func StandardLogger() *Logger {
|
||||
return std
|
||||
}
|
||||
|
||||
// SetOutput sets the standard logger output.
|
||||
func SetOutput(out io.Writer) {
|
||||
std.mu.Lock()
|
||||
defer std.mu.Unlock()
|
||||
std.Out = out
|
||||
}
|
||||
|
||||
// SetFormatter sets the standard logger formatter.
|
||||
func SetFormatter(formatter Formatter) {
|
||||
std.mu.Lock()
|
||||
defer std.mu.Unlock()
|
||||
std.Formatter = formatter
|
||||
}
|
||||
|
||||
// SetLevel sets the standard logger level.
|
||||
func SetLevel(level Level) {
|
||||
std.mu.Lock()
|
||||
defer std.mu.Unlock()
|
||||
std.Level = level
|
||||
}
|
||||
|
||||
// GetLevel returns the standard logger level.
|
||||
func GetLevel() Level {
|
||||
std.mu.Lock()
|
||||
defer std.mu.Unlock()
|
||||
return std.Level
|
||||
}
|
||||
|
||||
// AddHook adds a hook to the standard logger hooks.
|
||||
func AddHook(hook Hook) {
|
||||
std.mu.Lock()
|
||||
defer std.mu.Unlock()
|
||||
std.Hooks.Add(hook)
|
||||
}
|
||||
|
||||
// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
|
||||
func WithError(err error) *Entry {
|
||||
return std.WithField(ErrorKey, err)
|
||||
}
|
||||
|
||||
// WithField creates an entry from the standard logger and adds a field to
|
||||
// it. If you want multiple fields, use `WithFields`.
|
||||
//
|
||||
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||
// or Panic on the Entry it returns.
|
||||
func WithField(key string, value interface{}) *Entry {
|
||||
return std.WithField(key, value)
|
||||
}
|
||||
|
||||
// WithFields creates an entry from the standard logger and adds multiple
|
||||
// fields to it. This is simply a helper for `WithField`, invoking it
|
||||
// once for each field.
|
||||
//
|
||||
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
||||
// or Panic on the Entry it returns.
|
||||
func WithFields(fields Fields) *Entry {
|
||||
return std.WithFields(fields)
|
||||
}
|
||||
|
||||
// Debug logs a message at level Debug on the standard logger.
|
||||
func Debug(args ...interface{}) {
|
||||
std.Debug(args...)
|
||||
}
|
||||
|
||||
// Print logs a message at level Info on the standard logger.
|
||||
func Print(args ...interface{}) {
|
||||
std.Print(args...)
|
||||
}
|
||||
|
||||
// Info logs a message at level Info on the standard logger.
|
||||
func Info(args ...interface{}) {
|
||||
std.Info(args...)
|
||||
}
|
||||
|
||||
// Warn logs a message at level Warn on the standard logger.
|
||||
func Warn(args ...interface{}) {
|
||||
std.Warn(args...)
|
||||
}
|
||||
|
||||
// Warning logs a message at level Warn on the standard logger.
|
||||
func Warning(args ...interface{}) {
|
||||
std.Warning(args...)
|
||||
}
|
||||
|
||||
// Error logs a message at level Error on the standard logger.
|
||||
func Error(args ...interface{}) {
|
||||
std.Error(args...)
|
||||
}
|
||||
|
||||
// Panic logs a message at level Panic on the standard logger.
|
||||
func Panic(args ...interface{}) {
|
||||
std.Panic(args...)
|
||||
}
|
||||
|
||||
// Fatal logs a message at level Fatal on the standard logger.
|
||||
func Fatal(args ...interface{}) {
|
||||
std.Fatal(args...)
|
||||
}
|
||||
|
||||
// Debugf logs a message at level Debug on the standard logger.
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
std.Debugf(format, args...)
|
||||
}
|
||||
|
||||
// Printf logs a message at level Info on the standard logger.
|
||||
func Printf(format string, args ...interface{}) {
|
||||
std.Printf(format, args...)
|
||||
}
|
||||
|
||||
// Infof logs a message at level Info on the standard logger.
|
||||
func Infof(format string, args ...interface{}) {
|
||||
std.Infof(format, args...)
|
||||
}
|
||||
|
||||
// Warnf logs a message at level Warn on the standard logger.
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
std.Warnf(format, args...)
|
||||
}
|
||||
|
||||
// Warningf logs a message at level Warn on the standard logger.
|
||||
func Warningf(format string, args ...interface{}) {
|
||||
std.Warningf(format, args...)
|
||||
}
|
||||
|
||||
// Errorf logs a message at level Error on the standard logger.
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
std.Errorf(format, args...)
|
||||
}
|
||||
|
||||
// Panicf logs a message at level Panic on the standard logger.
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
std.Panicf(format, args...)
|
||||
}
|
||||
|
||||
// Fatalf logs a message at level Fatal on the standard logger.
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
std.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
// Debugln logs a message at level Debug on the standard logger.
|
||||
func Debugln(args ...interface{}) {
|
||||
std.Debugln(args...)
|
||||
}
|
||||
|
||||
// Println logs a message at level Info on the standard logger.
|
||||
func Println(args ...interface{}) {
|
||||
std.Println(args...)
|
||||
}
|
||||
|
||||
// Infoln logs a message at level Info on the standard logger.
|
||||
func Infoln(args ...interface{}) {
|
||||
std.Infoln(args...)
|
||||
}
|
||||
|
||||
// Warnln logs a message at level Warn on the standard logger.
|
||||
func Warnln(args ...interface{}) {
|
||||
std.Warnln(args...)
|
||||
}
|
||||
|
||||
// Warningln logs a message at level Warn on the standard logger.
|
||||
func Warningln(args ...interface{}) {
|
||||
std.Warningln(args...)
|
||||
}
|
||||
|
||||
// Errorln logs a message at level Error on the standard logger.
|
||||
func Errorln(args ...interface{}) {
|
||||
std.Errorln(args...)
|
||||
}
|
||||
|
||||
// Panicln logs a message at level Panic on the standard logger.
|
||||
func Panicln(args ...interface{}) {
|
||||
std.Panicln(args...)
|
||||
}
|
||||
|
||||
// Fatalln logs a message at level Fatal on the standard logger.
|
||||
func Fatalln(args ...interface{}) {
|
||||
std.Fatalln(args...)
|
||||
}
|
45
vendor/github.com/Sirupsen/logrus/formatter.go
generated
vendored
Normal file
45
vendor/github.com/Sirupsen/logrus/formatter.go
generated
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
package logrus
|
||||
|
||||
import "time"
|
||||
|
||||
const DefaultTimestampFormat = time.RFC3339
|
||||
|
||||
// The Formatter interface is used to implement a custom Formatter. It takes an
|
||||
// `Entry`. It exposes all the fields, including the default ones:
|
||||
//
|
||||
// * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
|
||||
// * `entry.Data["time"]`. The timestamp.
|
||||
// * `entry.Data["level"]. The level the entry was logged at.
|
||||
//
|
||||
// Any additional fields added with `WithField` or `WithFields` are also in
|
||||
// `entry.Data`. Format is expected to return an array of bytes which are then
|
||||
// logged to `logger.Out`.
|
||||
type Formatter interface {
|
||||
Format(*Entry) ([]byte, error)
|
||||
}
|
||||
|
||||
// This is to not silently overwrite `time`, `msg` and `level` fields when
|
||||
// dumping it. If this code wasn't there doing:
|
||||
//
|
||||
// logrus.WithField("level", 1).Info("hello")
|
||||
//
|
||||
// Would just silently drop the user provided level. Instead with this code
|
||||
// it'll logged as:
|
||||
//
|
||||
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
|
||||
//
|
||||
// It's not exported because it's still using Data in an opinionated way. It's to
|
||||
// avoid code duplication between the two default formatters.
|
||||
func prefixFieldClashes(data Fields) {
|
||||
if t, ok := data["time"]; ok {
|
||||
data["fields.time"] = t
|
||||
}
|
||||
|
||||
if m, ok := data["msg"]; ok {
|
||||
data["fields.msg"] = m
|
||||
}
|
||||
|
||||
if l, ok := data["level"]; ok {
|
||||
data["fields.level"] = l
|
||||
}
|
||||
}
|
34
vendor/github.com/Sirupsen/logrus/hooks.go
generated
vendored
Normal file
34
vendor/github.com/Sirupsen/logrus/hooks.go
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
package logrus
|
||||
|
||||
// A hook to be fired when logging on the logging levels returned from
|
||||
// `Levels()` on your implementation of the interface. Note that this is not
|
||||
// fired in a goroutine or a channel with workers, you should handle such
|
||||
// functionality yourself if your call is non-blocking and you don't wish for
|
||||
// the logging calls for levels returned from `Levels()` to block.
|
||||
type Hook interface {
|
||||
Levels() []Level
|
||||
Fire(*Entry) error
|
||||
}
|
||||
|
||||
// Internal type for storing the hooks on a logger instance.
|
||||
type LevelHooks map[Level][]Hook
|
||||
|
||||
// Add a hook to an instance of logger. This is called with
|
||||
// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
|
||||
func (hooks LevelHooks) Add(hook Hook) {
|
||||
for _, level := range hook.Levels() {
|
||||
hooks[level] = append(hooks[level], hook)
|
||||
}
|
||||
}
|
||||
|
||||
// Fire all the hooks for the passed level. Used by `entry.log` to fire
|
||||
// appropriate hooks for a log entry.
|
||||
func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
|
||||
for _, hook := range hooks[level] {
|
||||
if err := hook.Fire(entry); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
41
vendor/github.com/Sirupsen/logrus/json_formatter.go
generated
vendored
Normal file
41
vendor/github.com/Sirupsen/logrus/json_formatter.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type JSONFormatter struct {
|
||||
// TimestampFormat sets the format used for marshaling timestamps.
|
||||
TimestampFormat string
|
||||
}
|
||||
|
||||
func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
|
||||
data := make(Fields, len(entry.Data)+3)
|
||||
for k, v := range entry.Data {
|
||||
switch v := v.(type) {
|
||||
case error:
|
||||
// Otherwise errors are ignored by `encoding/json`
|
||||
// https://github.com/Sirupsen/logrus/issues/137
|
||||
data[k] = v.Error()
|
||||
default:
|
||||
data[k] = v
|
||||
}
|
||||
}
|
||||
prefixFieldClashes(data)
|
||||
|
||||
timestampFormat := f.TimestampFormat
|
||||
if timestampFormat == "" {
|
||||
timestampFormat = DefaultTimestampFormat
|
||||
}
|
||||
|
||||
data["time"] = entry.Time.Format(timestampFormat)
|
||||
data["msg"] = entry.Message
|
||||
data["level"] = entry.Level.String()
|
||||
|
||||
serialized, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
|
||||
}
|
||||
return append(serialized, '\n'), nil
|
||||
}
|
212
vendor/github.com/Sirupsen/logrus/logger.go
generated
vendored
Normal file
212
vendor/github.com/Sirupsen/logrus/logger.go
generated
vendored
Normal file
@ -0,0 +1,212 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
||||
// file, or leave it default which is `os.Stderr`. You can also set this to
|
||||
// something more adventorous, such as logging to Kafka.
|
||||
Out io.Writer
|
||||
// Hooks for the logger instance. These allow firing events based on logging
|
||||
// levels and log entries. For example, to send errors to an error tracking
|
||||
// service, log to StatsD or dump the core on fatal errors.
|
||||
Hooks LevelHooks
|
||||
// All log entries pass through the formatter before logged to Out. The
|
||||
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
||||
// TextFormatter is the default. In development (when a TTY is attached) it
|
||||
// logs with colors, but to a file it wouldn't. You can easily implement your
|
||||
// own that implements the `Formatter` interface, see the `README` or included
|
||||
// formatters for examples.
|
||||
Formatter Formatter
|
||||
// The logging level the logger should log at. This is typically (and defaults
|
||||
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
|
||||
// logged. `logrus.Debug` is useful in
|
||||
Level Level
|
||||
// Used to sync writing to the log.
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// Creates a new logger. Configuration should be set by changing `Formatter`,
|
||||
// `Out` and `Hooks` directly on the default logger instance. You can also just
|
||||
// instantiate your own:
|
||||
//
|
||||
// var log = &Logger{
|
||||
// Out: os.Stderr,
|
||||
// Formatter: new(JSONFormatter),
|
||||
// Hooks: make(LevelHooks),
|
||||
// Level: logrus.DebugLevel,
|
||||
// }
|
||||
//
|
||||
// It's recommended to make this a global instance called `log`.
|
||||
func New() *Logger {
|
||||
return &Logger{
|
||||
Out: os.Stderr,
|
||||
Formatter: new(TextFormatter),
|
||||
Hooks: make(LevelHooks),
|
||||
Level: InfoLevel,
|
||||
}
|
||||
}
|
||||
|
||||
// Adds a field to the log entry, note that you it doesn't log until you call
|
||||
// Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry.
|
||||
// If you want multiple fields, use `WithFields`.
|
||||
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
||||
return NewEntry(logger).WithField(key, value)
|
||||
}
|
||||
|
||||
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
||||
// each `Field`.
|
||||
func (logger *Logger) WithFields(fields Fields) *Entry {
|
||||
return NewEntry(logger).WithFields(fields)
|
||||
}
|
||||
|
||||
// Add an error as single field to the log entry. All it does is call
|
||||
// `WithError` for the given `error`.
|
||||
func (logger *Logger) WithError(err error) *Entry {
|
||||
return NewEntry(logger).WithError(err)
|
||||
}
|
||||
|
||||
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
||||
if logger.Level >= DebugLevel {
|
||||
NewEntry(logger).Debugf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Infof(format string, args ...interface{}) {
|
||||
if logger.Level >= InfoLevel {
|
||||
NewEntry(logger).Infof(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Printf(format string, args ...interface{}) {
|
||||
NewEntry(logger).Printf(format, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warnf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warnf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
||||
if logger.Level >= ErrorLevel {
|
||||
NewEntry(logger).Errorf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
||||
if logger.Level >= FatalLevel {
|
||||
NewEntry(logger).Fatalf(format, args...)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
||||
if logger.Level >= PanicLevel {
|
||||
NewEntry(logger).Panicf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Debug(args ...interface{}) {
|
||||
if logger.Level >= DebugLevel {
|
||||
NewEntry(logger).Debug(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Info(args ...interface{}) {
|
||||
if logger.Level >= InfoLevel {
|
||||
NewEntry(logger).Info(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Print(args ...interface{}) {
|
||||
NewEntry(logger).Info(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warn(args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warn(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Warning(args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warn(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Error(args ...interface{}) {
|
||||
if logger.Level >= ErrorLevel {
|
||||
NewEntry(logger).Error(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatal(args ...interface{}) {
|
||||
if logger.Level >= FatalLevel {
|
||||
NewEntry(logger).Fatal(args...)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (logger *Logger) Panic(args ...interface{}) {
|
||||
if logger.Level >= PanicLevel {
|
||||
NewEntry(logger).Panic(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Debugln(args ...interface{}) {
|
||||
if logger.Level >= DebugLevel {
|
||||
NewEntry(logger).Debugln(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Infoln(args ...interface{}) {
|
||||
if logger.Level >= InfoLevel {
|
||||
NewEntry(logger).Infoln(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Println(args ...interface{}) {
|
||||
NewEntry(logger).Println(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warnln(args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warnln(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Warningln(args ...interface{}) {
|
||||
if logger.Level >= WarnLevel {
|
||||
NewEntry(logger).Warnln(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Errorln(args ...interface{}) {
|
||||
if logger.Level >= ErrorLevel {
|
||||
NewEntry(logger).Errorln(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatalln(args ...interface{}) {
|
||||
if logger.Level >= FatalLevel {
|
||||
NewEntry(logger).Fatalln(args...)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func (logger *Logger) Panicln(args ...interface{}) {
|
||||
if logger.Level >= PanicLevel {
|
||||
NewEntry(logger).Panicln(args...)
|
||||
}
|
||||
}
|
143
vendor/github.com/Sirupsen/logrus/logrus.go
generated
vendored
Normal file
143
vendor/github.com/Sirupsen/logrus/logrus.go
generated
vendored
Normal file
@ -0,0 +1,143 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fields type, used to pass to `WithFields`.
|
||||
type Fields map[string]interface{}
|
||||
|
||||
// Level type
|
||||
type Level uint8
|
||||
|
||||
// Convert the Level to a string. E.g. PanicLevel becomes "panic".
|
||||
func (level Level) String() string {
|
||||
switch level {
|
||||
case DebugLevel:
|
||||
return "debug"
|
||||
case InfoLevel:
|
||||
return "info"
|
||||
case WarnLevel:
|
||||
return "warning"
|
||||
case ErrorLevel:
|
||||
return "error"
|
||||
case FatalLevel:
|
||||
return "fatal"
|
||||
case PanicLevel:
|
||||
return "panic"
|
||||
}
|
||||
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// ParseLevel takes a string level and returns the Logrus log level constant.
|
||||
func ParseLevel(lvl string) (Level, error) {
|
||||
switch strings.ToLower(lvl) {
|
||||
case "panic":
|
||||
return PanicLevel, nil
|
||||
case "fatal":
|
||||
return FatalLevel, nil
|
||||
case "error":
|
||||
return ErrorLevel, nil
|
||||
case "warn", "warning":
|
||||
return WarnLevel, nil
|
||||
case "info":
|
||||
return InfoLevel, nil
|
||||
case "debug":
|
||||
return DebugLevel, nil
|
||||
}
|
||||
|
||||
var l Level
|
||||
return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
|
||||
}
|
||||
|
||||
// A constant exposing all logging levels
|
||||
var AllLevels = []Level{
|
||||
PanicLevel,
|
||||
FatalLevel,
|
||||
ErrorLevel,
|
||||
WarnLevel,
|
||||
InfoLevel,
|
||||
DebugLevel,
|
||||
}
|
||||
|
||||
// These are the different logging levels. You can set the logging level to log
|
||||
// on your instance of logger, obtained with `logrus.New()`.
|
||||
const (
|
||||
// PanicLevel level, highest level of severity. Logs and then calls panic with the
|
||||
// message passed to Debug, Info, ...
|
||||
PanicLevel Level = iota
|
||||
// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
|
||||
// logging level is set to Panic.
|
||||
FatalLevel
|
||||
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
||||
// Commonly used for hooks to send errors to an error tracking service.
|
||||
ErrorLevel
|
||||
// WarnLevel level. Non-critical entries that deserve eyes.
|
||||
WarnLevel
|
||||
// InfoLevel level. General operational entries about what's going on inside the
|
||||
// application.
|
||||
InfoLevel
|
||||
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
||||
DebugLevel
|
||||
)
|
||||
|
||||
// Won't compile if StdLogger can't be realized by a log.Logger
|
||||
var (
|
||||
_ StdLogger = &log.Logger{}
|
||||
_ StdLogger = &Entry{}
|
||||
_ StdLogger = &Logger{}
|
||||
)
|
||||
|
||||
// StdLogger is what your logrus-enabled library should take, that way
|
||||
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
||||
// interface, this is the closest we get, unfortunately.
|
||||
type StdLogger interface {
|
||||
Print(...interface{})
|
||||
Printf(string, ...interface{})
|
||||
Println(...interface{})
|
||||
|
||||
Fatal(...interface{})
|
||||
Fatalf(string, ...interface{})
|
||||
Fatalln(...interface{})
|
||||
|
||||
Panic(...interface{})
|
||||
Panicf(string, ...interface{})
|
||||
Panicln(...interface{})
|
||||
}
|
||||
|
||||
// The FieldLogger interface generalizes the Entry and Logger types
|
||||
type FieldLogger interface {
|
||||
WithField(key string, value interface{}) *Entry
|
||||
WithFields(fields Fields) *Entry
|
||||
WithError(err error) *Entry
|
||||
|
||||
Debugf(format string, args ...interface{})
|
||||
Infof(format string, args ...interface{})
|
||||
Printf(format string, args ...interface{})
|
||||
Warnf(format string, args ...interface{})
|
||||
Warningf(format string, args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
Fatalf(format string, args ...interface{})
|
||||
Panicf(format string, args ...interface{})
|
||||
|
||||
Debug(args ...interface{})
|
||||
Info(args ...interface{})
|
||||
Print(args ...interface{})
|
||||
Warn(args ...interface{})
|
||||
Warning(args ...interface{})
|
||||
Error(args ...interface{})
|
||||
Fatal(args ...interface{})
|
||||
Panic(args ...interface{})
|
||||
|
||||
Debugln(args ...interface{})
|
||||
Infoln(args ...interface{})
|
||||
Println(args ...interface{})
|
||||
Warnln(args ...interface{})
|
||||
Warningln(args ...interface{})
|
||||
Errorln(args ...interface{})
|
||||
Fatalln(args ...interface{})
|
||||
Panicln(args ...interface{})
|
||||
}
|
9
vendor/github.com/Sirupsen/logrus/terminal_bsd.go
generated
vendored
Normal file
9
vendor/github.com/Sirupsen/logrus/terminal_bsd.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
// +build darwin freebsd openbsd netbsd dragonfly
|
||||
|
||||
package logrus
|
||||
|
||||
import "syscall"
|
||||
|
||||
const ioctlReadTermios = syscall.TIOCGETA
|
||||
|
||||
type Termios syscall.Termios
|
12
vendor/github.com/Sirupsen/logrus/terminal_linux.go
generated
vendored
Normal file
12
vendor/github.com/Sirupsen/logrus/terminal_linux.go
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
// Based on ssh/terminal:
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package logrus
|
||||
|
||||
import "syscall"
|
||||
|
||||
const ioctlReadTermios = syscall.TCGETS
|
||||
|
||||
type Termios syscall.Termios
|
21
vendor/github.com/Sirupsen/logrus/terminal_notwindows.go
generated
vendored
Normal file
21
vendor/github.com/Sirupsen/logrus/terminal_notwindows.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
// Based on ssh/terminal:
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build linux darwin freebsd openbsd netbsd dragonfly
|
||||
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// IsTerminal returns true if stderr's file descriptor is a terminal.
|
||||
func IsTerminal() bool {
|
||||
fd := syscall.Stderr
|
||||
var termios Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
15
vendor/github.com/Sirupsen/logrus/terminal_solaris.go
generated
vendored
Normal file
15
vendor/github.com/Sirupsen/logrus/terminal_solaris.go
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
// +build solaris
|
||||
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// IsTerminal returns true if the given file descriptor is a terminal.
|
||||
func IsTerminal() bool {
|
||||
_, err := unix.IoctlGetTermios(int(os.Stdout.Fd()), unix.TCGETA)
|
||||
return err == nil
|
||||
}
|
27
vendor/github.com/Sirupsen/logrus/terminal_windows.go
generated
vendored
Normal file
27
vendor/github.com/Sirupsen/logrus/terminal_windows.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
// Based on ssh/terminal:
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build windows
|
||||
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
|
||||
var (
|
||||
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
|
||||
)
|
||||
|
||||
// IsTerminal returns true if stderr's file descriptor is a terminal.
|
||||
func IsTerminal() bool {
|
||||
fd := syscall.Stderr
|
||||
var st uint32
|
||||
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
|
||||
return r != 0 && e == 0
|
||||
}
|
161
vendor/github.com/Sirupsen/logrus/text_formatter.go
generated
vendored
Normal file
161
vendor/github.com/Sirupsen/logrus/text_formatter.go
generated
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
nocolor = 0
|
||||
red = 31
|
||||
green = 32
|
||||
yellow = 33
|
||||
blue = 34
|
||||
gray = 37
|
||||
)
|
||||
|
||||
var (
|
||||
baseTimestamp time.Time
|
||||
isTerminal bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
baseTimestamp = time.Now()
|
||||
isTerminal = IsTerminal()
|
||||
}
|
||||
|
||||
func miniTS() int {
|
||||
return int(time.Since(baseTimestamp) / time.Second)
|
||||
}
|
||||
|
||||
type TextFormatter struct {
|
||||
// Set to true to bypass checking for a TTY before outputting colors.
|
||||
ForceColors bool
|
||||
|
||||
// Force disabling colors.
|
||||
DisableColors bool
|
||||
|
||||
// Disable timestamp logging. useful when output is redirected to logging
|
||||
// system that already adds timestamps.
|
||||
DisableTimestamp bool
|
||||
|
||||
// Enable logging the full timestamp when a TTY is attached instead of just
|
||||
// the time passed since beginning of execution.
|
||||
FullTimestamp bool
|
||||
|
||||
// TimestampFormat to use for display when a full timestamp is printed
|
||||
TimestampFormat string
|
||||
|
||||
// The fields are sorted by default for a consistent output. For applications
|
||||
// that log extremely frequently and don't use the JSON formatter this may not
|
||||
// be desired.
|
||||
DisableSorting bool
|
||||
}
|
||||
|
||||
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||
var keys []string = make([]string, 0, len(entry.Data))
|
||||
for k := range entry.Data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
if !f.DisableSorting {
|
||||
sort.Strings(keys)
|
||||
}
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
prefixFieldClashes(entry.Data)
|
||||
|
||||
isColorTerminal := isTerminal && (runtime.GOOS != "windows")
|
||||
isColored := (f.ForceColors || isColorTerminal) && !f.DisableColors
|
||||
|
||||
timestampFormat := f.TimestampFormat
|
||||
if timestampFormat == "" {
|
||||
timestampFormat = DefaultTimestampFormat
|
||||
}
|
||||
if isColored {
|
||||
f.printColored(b, entry, keys, timestampFormat)
|
||||
} else {
|
||||
if !f.DisableTimestamp {
|
||||
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
|
||||
}
|
||||
f.appendKeyValue(b, "level", entry.Level.String())
|
||||
if entry.Message != "" {
|
||||
f.appendKeyValue(b, "msg", entry.Message)
|
||||
}
|
||||
for _, key := range keys {
|
||||
f.appendKeyValue(b, key, entry.Data[key])
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteByte('\n')
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) {
|
||||
var levelColor int
|
||||
switch entry.Level {
|
||||
case DebugLevel:
|
||||
levelColor = gray
|
||||
case WarnLevel:
|
||||
levelColor = yellow
|
||||
case ErrorLevel, FatalLevel, PanicLevel:
|
||||
levelColor = red
|
||||
default:
|
||||
levelColor = blue
|
||||
}
|
||||
|
||||
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
||||
|
||||
if !f.FullTimestamp {
|
||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message)
|
||||
} else {
|
||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message)
|
||||
}
|
||||
for _, k := range keys {
|
||||
v := entry.Data[k]
|
||||
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%+v", levelColor, k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func needsQuoting(text string) bool {
|
||||
for _, ch := range text {
|
||||
if !((ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= 'A' && ch <= 'Z') ||
|
||||
(ch >= '0' && ch <= '9') ||
|
||||
ch == '-' || ch == '.') {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
|
||||
|
||||
b.WriteString(key)
|
||||
b.WriteByte('=')
|
||||
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
if !needsQuoting(value) {
|
||||
b.WriteString(value)
|
||||
} else {
|
||||
fmt.Fprintf(b, "%q", value)
|
||||
}
|
||||
case error:
|
||||
errmsg := value.Error()
|
||||
if !needsQuoting(errmsg) {
|
||||
b.WriteString(errmsg)
|
||||
} else {
|
||||
fmt.Fprintf(b, "%q", value)
|
||||
}
|
||||
default:
|
||||
fmt.Fprint(b, value)
|
||||
}
|
||||
|
||||
b.WriteByte(' ')
|
||||
}
|
53
vendor/github.com/Sirupsen/logrus/writer.go
generated
vendored
Normal file
53
vendor/github.com/Sirupsen/logrus/writer.go
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
package logrus
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (logger *Logger) Writer() *io.PipeWriter {
|
||||
return logger.WriterLevel(InfoLevel)
|
||||
}
|
||||
|
||||
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
|
||||
reader, writer := io.Pipe()
|
||||
|
||||
var printFunc func(args ...interface{})
|
||||
switch level {
|
||||
case DebugLevel:
|
||||
printFunc = logger.Debug
|
||||
case InfoLevel:
|
||||
printFunc = logger.Info
|
||||
case WarnLevel:
|
||||
printFunc = logger.Warn
|
||||
case ErrorLevel:
|
||||
printFunc = logger.Error
|
||||
case FatalLevel:
|
||||
printFunc = logger.Fatal
|
||||
case PanicLevel:
|
||||
printFunc = logger.Panic
|
||||
default:
|
||||
printFunc = logger.Print
|
||||
}
|
||||
|
||||
go logger.writerScanner(reader, printFunc)
|
||||
runtime.SetFinalizer(writer, writerFinalizer)
|
||||
|
||||
return writer
|
||||
}
|
||||
|
||||
func (logger *Logger) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
printFunc(scanner.Text())
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
logger.Errorf("Error while reading from Writer: %s", err)
|
||||
}
|
||||
reader.Close()
|
||||
}
|
||||
|
||||
func writerFinalizer(writer *io.PipeWriter) {
|
||||
writer.Close()
|
||||
}
|
21
vendor/github.com/dylanmei/iso8601/LICENSE
generated
vendored
Normal file
21
vendor/github.com/dylanmei/iso8601/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Dylan Meissner
|
||||
|
||||
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.
|
9
vendor/github.com/dylanmei/iso8601/README.md
generated
vendored
Normal file
9
vendor/github.com/dylanmei/iso8601/README.md
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
iso 8601 parser and formatter
|
||||
=============================
|
||||
|
||||
An [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) Go utility.
|
||||
|
||||
- *Time* is not yet implemented
|
||||
- *Duration* is mostly implemented
|
||||
|
96
vendor/github.com/dylanmei/iso8601/duration.go
generated
vendored
Normal file
96
vendor/github.com/dylanmei/iso8601/duration.go
generated
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
package iso8601
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrBadFormat is returned when parsing fails
|
||||
ErrBadFormat = errors.New("bad format string")
|
||||
|
||||
// ErrNoMonth is raised when a month is in the format string
|
||||
ErrNoMonth = errors.New("no months allowed")
|
||||
|
||||
full = regexp.MustCompile(`P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?`)
|
||||
week = regexp.MustCompile(`P((?P<week>\d+)W)`)
|
||||
)
|
||||
|
||||
// adapted from https://github.com/BrianHicks/finch/duration
|
||||
func ParseDuration(value string) (time.Duration, error) {
|
||||
var match []string
|
||||
var regex *regexp.Regexp
|
||||
|
||||
if week.MatchString(value) {
|
||||
match = week.FindStringSubmatch(value)
|
||||
regex = week
|
||||
} else if full.MatchString(value) {
|
||||
match = full.FindStringSubmatch(value)
|
||||
regex = full
|
||||
} else {
|
||||
return time.Duration(0), ErrBadFormat
|
||||
}
|
||||
|
||||
d := time.Duration(0)
|
||||
day := time.Hour * 24
|
||||
week := day * 7
|
||||
year := day * 365
|
||||
|
||||
for i, name := range regex.SubexpNames() {
|
||||
part := match[i]
|
||||
if i == 0 || name == "" || part == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
return time.Duration(0), err
|
||||
}
|
||||
switch name {
|
||||
case "year":
|
||||
d += year * time.Duration(value)
|
||||
case "month":
|
||||
return time.Duration(0), ErrNoMonth
|
||||
case "week":
|
||||
d += week * time.Duration(value)
|
||||
case "day":
|
||||
d += day * time.Duration(value)
|
||||
case "hour":
|
||||
d += time.Hour * time.Duration(value)
|
||||
case "minute":
|
||||
d += time.Minute * time.Duration(value)
|
||||
case "second":
|
||||
d += time.Second * time.Duration(value)
|
||||
}
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func FormatDuration(duration time.Duration) string {
|
||||
// we're not doing negative durations
|
||||
if duration.Seconds() <= 0 {
|
||||
return "PT0S"
|
||||
}
|
||||
|
||||
hours := int(duration.Hours())
|
||||
minutes := int(duration.Minutes()) - (hours * 60)
|
||||
seconds := int(duration.Seconds()) - (hours*3600 + minutes*60)
|
||||
|
||||
// we're not doing Y,M,W
|
||||
s := "PT"
|
||||
if hours > 0 {
|
||||
s = fmt.Sprintf("%s%dH", s, hours)
|
||||
}
|
||||
if minutes > 0 {
|
||||
s = fmt.Sprintf("%s%dM", s, minutes)
|
||||
}
|
||||
if seconds > 0 {
|
||||
s = fmt.Sprintf("%s%dS", s, seconds)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
22
vendor/github.com/dylanmei/winrmtest/LICENSE
generated
vendored
Normal file
22
vendor/github.com/dylanmei/winrmtest/LICENSE
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
Copyright (c) 2014-2015 Dylan Meissner
|
||||
|
||||
MIT License
|
||||
|
||||
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.
|
48
vendor/github.com/dylanmei/winrmtest/README.md
generated
vendored
Normal file
48
vendor/github.com/dylanmei/winrmtest/README.md
generated
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
|
||||
# winrmtest
|
||||
|
||||
An in-progress testing package to compliment the [masterzen/winrm](https://github.com/masterzen/winrm) Go-based winrm library.
|
||||
|
||||
My primary use-case for this is for [dylanmei/packer-communicator-winrm](https://github.com/dylanmei/packer-communicator-winrm), a [Packer](http://packer.io) communicator plugin for interacting with machines using Windows Remote Management.
|
||||
|
||||
## Example Use
|
||||
|
||||
A fictitious "Windows tools" package.
|
||||
|
||||
```
|
||||
|
||||
package wintools
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
"github.com/dylanmei/winrmtest"
|
||||
)
|
||||
|
||||
func Test_empty_temp_directory(t *testing.T) {
|
||||
r := winrmtest.NewRemote()
|
||||
defer r.Close()
|
||||
|
||||
r.CommandFunc(wimrmtest.MatchText("dir C:\Temp"), func(out, err io.Writer) int {
|
||||
out.Write([]byte(` Volume in drive C is Windows 2012 R2
|
||||
Volume Serial Number is XXXX-XXXX
|
||||
|
||||
Directory of C:\
|
||||
|
||||
File Not Found`))
|
||||
return 0
|
||||
})
|
||||
|
||||
lister := NewDirectoryLister(r.Host, r.Port)
|
||||
list, _ := lister.TempDirectory()
|
||||
|
||||
if count := len(list.Dirs()); count != 0 {
|
||||
t.Errorf("Expected 0 directories but found %d.\n", count)
|
||||
}
|
||||
|
||||
if count := len(list.Files()); count != 0 {
|
||||
t.Errorf("Expected 0 files but found %d.\n", count)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
79
vendor/github.com/dylanmei/winrmtest/remote.go
generated
vendored
Normal file
79
vendor/github.com/dylanmei/winrmtest/remote.go
generated
vendored
Normal file
@ -0,0 +1,79 @@
|
||||
package winrmtest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Remote respresents a WinRM server
|
||||
type Remote struct {
|
||||
Host string
|
||||
Port int
|
||||
server *httptest.Server
|
||||
service *wsman
|
||||
}
|
||||
|
||||
// NewRemote returns a new initialized Remote
|
||||
func NewRemote() *Remote {
|
||||
mux := http.NewServeMux()
|
||||
srv := httptest.NewServer(mux)
|
||||
|
||||
host, port, _ := splitAddr(srv.URL)
|
||||
remote := Remote{
|
||||
Host: host,
|
||||
Port: port,
|
||||
server: srv,
|
||||
service: &wsman{},
|
||||
}
|
||||
|
||||
mux.Handle("/wsman", remote.service)
|
||||
return &remote
|
||||
}
|
||||
|
||||
// Close closes the WinRM server
|
||||
func (r *Remote) Close() {
|
||||
r.server.Close()
|
||||
}
|
||||
|
||||
// MatcherFunc respresents a function used to match WinRM commands
|
||||
type MatcherFunc func(candidate string) bool
|
||||
|
||||
// MatchText return a new MatcherFunc based on text matching
|
||||
func MatchText(text string) MatcherFunc {
|
||||
return func(candidate string) bool {
|
||||
return text == candidate
|
||||
}
|
||||
}
|
||||
|
||||
// MatchPattern return a new MatcherFunc based on pattern matching
|
||||
func MatchPattern(pattern string) MatcherFunc {
|
||||
r := regexp.MustCompile(pattern)
|
||||
return func(candidate string) bool {
|
||||
return r.MatchString(candidate)
|
||||
}
|
||||
}
|
||||
|
||||
// CommandFunc respresents a function used to mock WinRM commands
|
||||
type CommandFunc func(out, err io.Writer) (exitCode int)
|
||||
|
||||
// CommandFunc adds a WinRM command mock function to the WinRM server
|
||||
func (r *Remote) CommandFunc(m MatcherFunc, f CommandFunc) {
|
||||
r.service.HandleCommand(m, f)
|
||||
}
|
||||
|
||||
func splitAddr(addr string) (host string, port int, err error) {
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
split := strings.Split(u.Host, ":")
|
||||
host = split[0]
|
||||
port, err = strconv.Atoi(split[1])
|
||||
return
|
||||
}
|
170
vendor/github.com/dylanmei/winrmtest/wsman.go
generated
vendored
Normal file
170
vendor/github.com/dylanmei/winrmtest/wsman.go
generated
vendored
Normal file
@ -0,0 +1,170 @@
|
||||
package winrmtest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/masterzen/winrm/soap"
|
||||
"github.com/masterzen/xmlpath"
|
||||
"github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
type wsman struct {
|
||||
commands []*command
|
||||
identitySeed int
|
||||
}
|
||||
|
||||
type command struct {
|
||||
id string
|
||||
matcher MatcherFunc
|
||||
handler CommandFunc
|
||||
}
|
||||
|
||||
func (w *wsman) HandleCommand(m MatcherFunc, f CommandFunc) string {
|
||||
id := uuid.NewV4().String()
|
||||
w.commands = append(w.commands, &command{
|
||||
id: id,
|
||||
matcher: m,
|
||||
handler: f,
|
||||
})
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func (w *wsman) CommandByText(cmd string) *command {
|
||||
for _, c := range w.commands {
|
||||
if c.matcher(cmd) {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *wsman) CommandByID(id string) *command {
|
||||
for _, c := range w.commands {
|
||||
if c.id == id {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *wsman) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Add("Content-Type", "application/soap+xml")
|
||||
|
||||
defer r.Body.Close()
|
||||
env, err := xmlpath.Parse(r.Body)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
action := readAction(env)
|
||||
switch {
|
||||
case strings.HasSuffix(action, "transfer/Create"):
|
||||
// create a new shell
|
||||
|
||||
rw.Write([]byte(`
|
||||
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
|
||||
<rsp:ShellId>123</rsp:ShellId>
|
||||
</env:Envelope>`))
|
||||
|
||||
case strings.HasSuffix(action, "shell/Command"):
|
||||
// execute on behalf of the client
|
||||
text := readCommand(env)
|
||||
cmd := w.CommandByText(text)
|
||||
|
||||
if cmd == nil {
|
||||
fmt.Printf("I don't know this command: Command=%s\n", text)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rw.Write([]byte(fmt.Sprintf(`
|
||||
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
|
||||
<rsp:CommandId>%s</rsp:CommandId>
|
||||
</env:Envelope>`, cmd.id)))
|
||||
|
||||
case strings.HasSuffix(action, "shell/Receive"):
|
||||
// client ready to receive the results
|
||||
|
||||
id := readCommandIDFromDesiredStream(env)
|
||||
cmd := w.CommandByID(id)
|
||||
|
||||
if cmd == nil {
|
||||
fmt.Printf("I don't know this command: CommandId=%s\n", id)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
stdout := new(bytes.Buffer)
|
||||
stderr := new(bytes.Buffer)
|
||||
result := cmd.handler(stdout, stderr)
|
||||
content := base64.StdEncoding.EncodeToString(stdout.Bytes())
|
||||
|
||||
rw.Write([]byte(fmt.Sprintf(`
|
||||
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
|
||||
<rsp:ReceiveResponse>
|
||||
<rsp:Stream Name="stdout" CommandId="%s">%s</rsp:Stream>
|
||||
<rsp:Stream Name="stdout" CommandId="%s" End="true"></rsp:Stream>
|
||||
<rsp:Stream Name="stderr" CommandId="%s" End="true"></rsp:Stream>
|
||||
<rsp:CommandState State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
|
||||
<rsp:ExitCode>%d</rsp:ExitCode>
|
||||
</rsp:CommandState>
|
||||
</rsp:ReceiveResponse>
|
||||
</env:Envelope>`, id, content, id, id, result)))
|
||||
|
||||
case strings.HasSuffix(action, "shell/Signal"):
|
||||
// end of the shell command
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
case strings.HasSuffix(action, "transfer/Delete"):
|
||||
// end of the session
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
fmt.Printf("I don't know this action: %s\n", action)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func readAction(env *xmlpath.Node) string {
|
||||
xpath, err := xmlpath.CompileWithNamespace(
|
||||
"//a:Action", soap.GetAllNamespaces())
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
action, _ := xpath.String(env)
|
||||
return action
|
||||
}
|
||||
|
||||
func readCommand(env *xmlpath.Node) string {
|
||||
xpath, err := xmlpath.CompileWithNamespace(
|
||||
"//rsp:Command", soap.GetAllNamespaces())
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
command, _ := xpath.String(env)
|
||||
if unquoted, err := strconv.Unquote(command); err == nil {
|
||||
return unquoted
|
||||
}
|
||||
return command
|
||||
}
|
||||
|
||||
func readCommandIDFromDesiredStream(env *xmlpath.Node) string {
|
||||
xpath, err := xmlpath.CompileWithNamespace(
|
||||
"//rsp:DesiredStream/@CommandId", soap.GetAllNamespaces())
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
id, _ := xpath.String(env)
|
||||
return id
|
||||
}
|
354
vendor/github.com/hashicorp/errwrap/LICENSE
generated
vendored
Normal file
354
vendor/github.com/hashicorp/errwrap/LICENSE
generated
vendored
Normal file
@ -0,0 +1,354 @@
|
||||
Mozilla Public License, version 2.0
|
||||
|
||||
1. Definitions
|
||||
|
||||
1.1. “Contributor”
|
||||
|
||||
means each individual or legal entity that creates, contributes to the
|
||||
creation of, or owns Covered Software.
|
||||
|
||||
1.2. “Contributor Version”
|
||||
|
||||
means the combination of the Contributions of others (if any) used by a
|
||||
Contributor and that particular Contributor’s Contribution.
|
||||
|
||||
1.3. “Contribution”
|
||||
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. “Covered Software”
|
||||
|
||||
means Source Code Form to which the initial Contributor has attached the
|
||||
notice in Exhibit A, the Executable Form of such Source Code Form, and
|
||||
Modifications of such Source Code Form, in each case including portions
|
||||
thereof.
|
||||
|
||||
1.5. “Incompatible With Secondary Licenses”
|
||||
means
|
||||
|
||||
a. that the initial Contributor has attached the notice described in
|
||||
Exhibit B to the Covered Software; or
|
||||
|
||||
b. that the Covered Software was made available under the terms of version
|
||||
1.1 or earlier of the License, but not also under the terms of a
|
||||
Secondary License.
|
||||
|
||||
1.6. “Executable Form”
|
||||
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. “Larger Work”
|
||||
|
||||
means a work that combines Covered Software with other material, in a separate
|
||||
file or files, that is not Covered Software.
|
||||
|
||||
1.8. “License”
|
||||
|
||||
means this document.
|
||||
|
||||
1.9. “Licensable”
|
||||
|
||||
means having the right to grant, to the maximum extent possible, whether at the
|
||||
time of the initial grant or subsequently, any and all of the rights conveyed by
|
||||
this License.
|
||||
|
||||
1.10. “Modifications”
|
||||
|
||||
means any of the following:
|
||||
|
||||
a. any file in Source Code Form that results from an addition to, deletion
|
||||
from, or modification of the contents of Covered Software; or
|
||||
|
||||
b. any new file in Source Code Form that contains any Covered Software.
|
||||
|
||||
1.11. “Patent Claims” of a Contributor
|
||||
|
||||
means any patent claim(s), including without limitation, method, process,
|
||||
and apparatus claims, in any patent Licensable by such Contributor that
|
||||
would be infringed, but for the grant of the License, by the making,
|
||||
using, selling, offering for sale, having made, import, or transfer of
|
||||
either its Contributions or its Contributor Version.
|
||||
|
||||
1.12. “Secondary License”
|
||||
|
||||
means either the GNU General Public License, Version 2.0, the GNU Lesser
|
||||
General Public License, Version 2.1, the GNU Affero General Public
|
||||
License, Version 3.0, or any later versions of those licenses.
|
||||
|
||||
1.13. “Source Code Form”
|
||||
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. “You” (or “Your”)
|
||||
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, “You” includes any entity that controls, is
|
||||
controlled by, or is under common control with You. For purposes of this
|
||||
definition, “control” means (a) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (b) ownership of more than fifty percent (50%) of the
|
||||
outstanding shares or beneficial ownership of such entity.
|
||||
|
||||
|
||||
2. License Grants and Conditions
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
a. under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or as
|
||||
part of a Larger Work; and
|
||||
|
||||
b. under Patent Claims of such Contributor to make, use, sell, offer for
|
||||
sale, have made, import, and otherwise transfer either its Contributions
|
||||
or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution become
|
||||
effective for each Contribution on the date the Contributor first distributes
|
||||
such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under this
|
||||
License. No additional rights or licenses will be implied from the distribution
|
||||
or licensing of Covered Software under this License. Notwithstanding Section
|
||||
2.1(b) above, no patent license is granted by a Contributor:
|
||||
|
||||
a. for any code that a Contributor has removed from Covered Software; or
|
||||
|
||||
b. for infringements caused by: (i) Your and any other third party’s
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
c. under Patent Claims infringed by Covered Software in the absence of its
|
||||
Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks, or
|
||||
logos of any Contributor (except as may be necessary to comply with the
|
||||
notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this License
|
||||
(see Section 10.2) or under the terms of a Secondary License (if permitted
|
||||
under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its Contributions
|
||||
are its original creation(s) or it has sufficient rights to grant the
|
||||
rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under applicable
|
||||
copyright doctrines of fair use, fair dealing, or other equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
||||
Section 2.1.
|
||||
|
||||
|
||||
3. Responsibilities
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under the
|
||||
terms of this License. You must inform recipients that the Source Code Form
|
||||
of the Covered Software is governed by the terms of this License, and how
|
||||
they can obtain a copy of this License. You may not attempt to alter or
|
||||
restrict the recipients’ rights in the Source Code Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
a. such Covered Software must also be made available in Source Code Form,
|
||||
as described in Section 3.1, and You must inform recipients of the
|
||||
Executable Form how they can obtain a copy of such Source Code Form by
|
||||
reasonable means in a timely manner, at a charge no more than the cost
|
||||
of distribution to the recipient; and
|
||||
|
||||
b. You may distribute such Executable Form under the terms of this License,
|
||||
or sublicense it under different terms, provided that the license for
|
||||
the Executable Form does not attempt to limit or alter the recipients’
|
||||
rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for the
|
||||
Covered Software. If the Larger Work is a combination of Covered Software
|
||||
with a work governed by one or more Secondary Licenses, and the Covered
|
||||
Software is not Incompatible With Secondary Licenses, this License permits
|
||||
You to additionally distribute such Covered Software under the terms of
|
||||
such Secondary License(s), so that the recipient of the Larger Work may, at
|
||||
their option, further distribute the Covered Software under the terms of
|
||||
either this License or such Secondary License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices (including
|
||||
copyright notices, patent notices, disclaimers of warranty, or limitations
|
||||
of liability) contained within the Source Code Form of the Covered
|
||||
Software, except that You may alter any license notices to the extent
|
||||
required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on behalf
|
||||
of any Contributor. You must make it absolutely clear that any such
|
||||
warranty, support, indemnity, or liability obligation is offered by You
|
||||
alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this License
|
||||
with respect to some or all of the Covered Software due to statute, judicial
|
||||
order, or regulation then You must: (a) comply with the terms of this License
|
||||
to the maximum extent possible; and (b) describe the limitations and the code
|
||||
they affect. Such description must be placed in a text file included with all
|
||||
distributions of the Covered Software under this License. Except to the
|
||||
extent prohibited by statute or regulation, such description must be
|
||||
sufficiently detailed for a recipient of ordinary skill to be able to
|
||||
understand it.
|
||||
|
||||
5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically if You
|
||||
fail to comply with any of its terms. However, if You become compliant,
|
||||
then the rights granted under this License from a particular Contributor
|
||||
are reinstated (a) provisionally, unless and until such Contributor
|
||||
explicitly and finally terminates Your grants, and (b) on an ongoing basis,
|
||||
if such Contributor fails to notify You of the non-compliance by some
|
||||
reasonable means prior to 60 days after You have come back into compliance.
|
||||
Moreover, Your grants from a particular Contributor are reinstated on an
|
||||
ongoing basis if such Contributor notifies You of the non-compliance by
|
||||
some reasonable means, this is the first time You have received notice of
|
||||
non-compliance with this License from such Contributor, and You become
|
||||
compliant prior to 30 days after Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions, counter-claims,
|
||||
and cross-claims) alleging that a Contributor Version directly or
|
||||
indirectly infringes any patent, then the rights granted to You by any and
|
||||
all Contributors for the Covered Software under Section 2.1 of this License
|
||||
shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
|
||||
license agreements (excluding distributors and resellers) which have been
|
||||
validly granted by You or Your distributors under this License prior to
|
||||
termination shall survive termination.
|
||||
|
||||
6. Disclaimer of Warranty
|
||||
|
||||
Covered Software is provided under this License on an “as is” basis, without
|
||||
warranty of any kind, either expressed, implied, or statutory, including,
|
||||
without limitation, warranties that the Covered Software is free of defects,
|
||||
merchantable, fit for a particular purpose or non-infringing. The entire
|
||||
risk as to the quality and performance of the Covered Software is with You.
|
||||
Should any Covered Software prove defective in any respect, You (not any
|
||||
Contributor) assume the cost of any necessary servicing, repair, or
|
||||
correction. This disclaimer of warranty constitutes an essential part of this
|
||||
License. No use of any Covered Software is authorized under this License
|
||||
except under this disclaimer.
|
||||
|
||||
7. Limitation of Liability
|
||||
|
||||
Under no circumstances and under no legal theory, whether tort (including
|
||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
||||
distributes Covered Software as permitted above, be liable to You for any
|
||||
direct, indirect, special, incidental, or consequential damages of any
|
||||
character including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses, even if such party shall have been
|
||||
informed of the possibility of such damages. This limitation of liability
|
||||
shall not apply to liability for death or personal injury resulting from such
|
||||
party’s negligence to the extent applicable law prohibits such limitation.
|
||||
Some jurisdictions do not allow the exclusion or limitation of incidental or
|
||||
consequential damages, so this exclusion and limitation may not apply to You.
|
||||
|
||||
8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the courts of
|
||||
a jurisdiction where the defendant maintains its principal place of business
|
||||
and such litigation shall be governed by laws of that jurisdiction, without
|
||||
reference to its conflict-of-law provisions. Nothing in this Section shall
|
||||
prevent a party’s ability to bring cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject matter
|
||||
hereof. If any provision of this License is held to be unenforceable, such
|
||||
provision shall be reformed only to the extent necessary to make it
|
||||
enforceable. Any law or regulation which provides that the language of a
|
||||
contract shall be construed against the drafter shall not be used to construe
|
||||
this License against a Contributor.
|
||||
|
||||
|
||||
10. Versions of the License
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version of
|
||||
the License under which You originally received the Covered Software, or
|
||||
under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a modified
|
||||
version of this License if you rename the license and remove any
|
||||
references to the name of the license steward (except to note that such
|
||||
modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular file, then
|
||||
You may include the notice in a location (such as a LICENSE file in a relevant
|
||||
directory) where a recipient would be likely to look for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - “Incompatible With Secondary Licenses” Notice
|
||||
|
||||
This Source Code Form is “Incompatible
|
||||
With Secondary Licenses”, as defined by
|
||||
the Mozilla Public License, v. 2.0.
|
||||
|
89
vendor/github.com/hashicorp/errwrap/README.md
generated
vendored
Normal file
89
vendor/github.com/hashicorp/errwrap/README.md
generated
vendored
Normal file
@ -0,0 +1,89 @@
|
||||
# errwrap
|
||||
|
||||
`errwrap` is a package for Go that formalizes the pattern of wrapping errors
|
||||
and checking if an error contains another error.
|
||||
|
||||
There is a common pattern in Go of taking a returned `error` value and
|
||||
then wrapping it (such as with `fmt.Errorf`) before returning it. The problem
|
||||
with this pattern is that you completely lose the original `error` structure.
|
||||
|
||||
Arguably the _correct_ approach is that you should make a custom structure
|
||||
implementing the `error` interface, and have the original error as a field
|
||||
on that structure, such [as this example](http://golang.org/pkg/os/#PathError).
|
||||
This is a good approach, but you have to know the entire chain of possible
|
||||
rewrapping that happens, when you might just care about one.
|
||||
|
||||
`errwrap` formalizes this pattern (it doesn't matter what approach you use
|
||||
above) by giving a single interface for wrapping errors, checking if a specific
|
||||
error is wrapped, and extracting that error.
|
||||
|
||||
## Installation and Docs
|
||||
|
||||
Install using `go get github.com/hashicorp/errwrap`.
|
||||
|
||||
Full documentation is available at
|
||||
http://godoc.org/github.com/hashicorp/errwrap
|
||||
|
||||
## Usage
|
||||
|
||||
#### Basic Usage
|
||||
|
||||
Below is a very basic example of its usage:
|
||||
|
||||
```go
|
||||
// A function that always returns an error, but wraps it, like a real
|
||||
// function might.
|
||||
func tryOpen() error {
|
||||
_, err := os.Open("/i/dont/exist")
|
||||
if err != nil {
|
||||
return errwrap.Wrapf("Doesn't exist: {{err}}", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := tryOpen()
|
||||
|
||||
// We can use the Contains helpers to check if an error contains
|
||||
// another error. It is safe to do this with a nil error, or with
|
||||
// an error that doesn't even use the errwrap package.
|
||||
if errwrap.Contains(err, ErrNotExist) {
|
||||
// Do something
|
||||
}
|
||||
if errwrap.ContainsType(err, new(os.PathError)) {
|
||||
// Do something
|
||||
}
|
||||
|
||||
// Or we can use the associated `Get` functions to just extract
|
||||
// a specific error. This would return nil if that specific error doesn't
|
||||
// exist.
|
||||
perr := errwrap.GetType(err, new(os.PathError))
|
||||
}
|
||||
```
|
||||
|
||||
#### Custom Types
|
||||
|
||||
If you're already making custom types that properly wrap errors, then
|
||||
you can get all the functionality of `errwraps.Contains` and such by
|
||||
implementing the `Wrapper` interface with just one function. Example:
|
||||
|
||||
```go
|
||||
type AppError {
|
||||
Code ErrorCode
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *AppError) WrappedErrors() []error {
|
||||
return []error{e.Err}
|
||||
}
|
||||
```
|
||||
|
||||
Now this works:
|
||||
|
||||
```go
|
||||
err := &AppError{Err: fmt.Errorf("an error")}
|
||||
if errwrap.ContainsType(err, fmt.Errorf("")) {
|
||||
// This will work!
|
||||
}
|
||||
```
|
169
vendor/github.com/hashicorp/errwrap/errwrap.go
generated
vendored
Normal file
169
vendor/github.com/hashicorp/errwrap/errwrap.go
generated
vendored
Normal file
@ -0,0 +1,169 @@
|
||||
// Package errwrap implements methods to formalize error wrapping in Go.
|
||||
//
|
||||
// All of the top-level functions that take an `error` are built to be able
|
||||
// to take any error, not just wrapped errors. This allows you to use errwrap
|
||||
// without having to type-check and type-cast everywhere.
|
||||
package errwrap
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WalkFunc is the callback called for Walk.
|
||||
type WalkFunc func(error)
|
||||
|
||||
// Wrapper is an interface that can be implemented by custom types to
|
||||
// have all the Contains, Get, etc. functions in errwrap work.
|
||||
//
|
||||
// When Walk reaches a Wrapper, it will call the callback for every
|
||||
// wrapped error in addition to the wrapper itself. Since all the top-level
|
||||
// functions in errwrap use Walk, this means that all those functions work
|
||||
// with your custom type.
|
||||
type Wrapper interface {
|
||||
WrappedErrors() []error
|
||||
}
|
||||
|
||||
// Wrap defines that outer wraps inner, returning an error type that
|
||||
// can be cleanly used with the other methods in this package, such as
|
||||
// Contains, GetAll, etc.
|
||||
//
|
||||
// This function won't modify the error message at all (the outer message
|
||||
// will be used).
|
||||
func Wrap(outer, inner error) error {
|
||||
return &wrappedError{
|
||||
Outer: outer,
|
||||
Inner: inner,
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapf wraps an error with a formatting message. This is similar to using
|
||||
// `fmt.Errorf` to wrap an error. If you're using `fmt.Errorf` to wrap
|
||||
// errors, you should replace it with this.
|
||||
//
|
||||
// format is the format of the error message. The string '{{err}}' will
|
||||
// be replaced with the original error message.
|
||||
func Wrapf(format string, err error) error {
|
||||
outerMsg := "<nil>"
|
||||
if err != nil {
|
||||
outerMsg = err.Error()
|
||||
}
|
||||
|
||||
outer := errors.New(strings.Replace(
|
||||
format, "{{err}}", outerMsg, -1))
|
||||
|
||||
return Wrap(outer, err)
|
||||
}
|
||||
|
||||
// Contains checks if the given error contains an error with the
|
||||
// message msg. If err is not a wrapped error, this will always return
|
||||
// false unless the error itself happens to match this msg.
|
||||
func Contains(err error, msg string) bool {
|
||||
return len(GetAll(err, msg)) > 0
|
||||
}
|
||||
|
||||
// ContainsType checks if the given error contains an error with
|
||||
// the same concrete type as v. If err is not a wrapped error, this will
|
||||
// check the err itself.
|
||||
func ContainsType(err error, v interface{}) bool {
|
||||
return len(GetAllType(err, v)) > 0
|
||||
}
|
||||
|
||||
// Get is the same as GetAll but returns the deepest matching error.
|
||||
func Get(err error, msg string) error {
|
||||
es := GetAll(err, msg)
|
||||
if len(es) > 0 {
|
||||
return es[len(es)-1]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetType is the same as GetAllType but returns the deepest matching error.
|
||||
func GetType(err error, v interface{}) error {
|
||||
es := GetAllType(err, v)
|
||||
if len(es) > 0 {
|
||||
return es[len(es)-1]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAll gets all the errors that might be wrapped in err with the
|
||||
// given message. The order of the errors is such that the outermost
|
||||
// matching error (the most recent wrap) is index zero, and so on.
|
||||
func GetAll(err error, msg string) []error {
|
||||
var result []error
|
||||
|
||||
Walk(err, func(err error) {
|
||||
if err.Error() == msg {
|
||||
result = append(result, err)
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetAllType gets all the errors that are the same type as v.
|
||||
//
|
||||
// The order of the return value is the same as described in GetAll.
|
||||
func GetAllType(err error, v interface{}) []error {
|
||||
var result []error
|
||||
|
||||
var search string
|
||||
if v != nil {
|
||||
search = reflect.TypeOf(v).String()
|
||||
}
|
||||
Walk(err, func(err error) {
|
||||
var needle string
|
||||
if err != nil {
|
||||
needle = reflect.TypeOf(err).String()
|
||||
}
|
||||
|
||||
if needle == search {
|
||||
result = append(result, err)
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Walk walks all the wrapped errors in err and calls the callback. If
|
||||
// err isn't a wrapped error, this will be called once for err. If err
|
||||
// is a wrapped error, the callback will be called for both the wrapper
|
||||
// that implements error as well as the wrapped error itself.
|
||||
func Walk(err error, cb WalkFunc) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch e := err.(type) {
|
||||
case *wrappedError:
|
||||
cb(e.Outer)
|
||||
Walk(e.Inner, cb)
|
||||
case Wrapper:
|
||||
cb(err)
|
||||
|
||||
for _, err := range e.WrappedErrors() {
|
||||
Walk(err, cb)
|
||||
}
|
||||
default:
|
||||
cb(err)
|
||||
}
|
||||
}
|
||||
|
||||
// wrappedError is an implementation of error that has both the
|
||||
// outer and inner errors.
|
||||
type wrappedError struct {
|
||||
Outer error
|
||||
Inner error
|
||||
}
|
||||
|
||||
func (w *wrappedError) Error() string {
|
||||
return w.Outer.Error()
|
||||
}
|
||||
|
||||
func (w *wrappedError) WrappedErrors() []error {
|
||||
return []error{w.Outer, w.Inner}
|
||||
}
|
353
vendor/github.com/hashicorp/go-multierror/LICENSE
generated
vendored
Normal file
353
vendor/github.com/hashicorp/go-multierror/LICENSE
generated
vendored
Normal file
@ -0,0 +1,353 @@
|
||||
Mozilla Public License, version 2.0
|
||||
|
||||
1. Definitions
|
||||
|
||||
1.1. “Contributor”
|
||||
|
||||
means each individual or legal entity that creates, contributes to the
|
||||
creation of, or owns Covered Software.
|
||||
|
||||
1.2. “Contributor Version”
|
||||
|
||||
means the combination of the Contributions of others (if any) used by a
|
||||
Contributor and that particular Contributor’s Contribution.
|
||||
|
||||
1.3. “Contribution”
|
||||
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. “Covered Software”
|
||||
|
||||
means Source Code Form to which the initial Contributor has attached the
|
||||
notice in Exhibit A, the Executable Form of such Source Code Form, and
|
||||
Modifications of such Source Code Form, in each case including portions
|
||||
thereof.
|
||||
|
||||
1.5. “Incompatible With Secondary Licenses”
|
||||
means
|
||||
|
||||
a. that the initial Contributor has attached the notice described in
|
||||
Exhibit B to the Covered Software; or
|
||||
|
||||
b. that the Covered Software was made available under the terms of version
|
||||
1.1 or earlier of the License, but not also under the terms of a
|
||||
Secondary License.
|
||||
|
||||
1.6. “Executable Form”
|
||||
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. “Larger Work”
|
||||
|
||||
means a work that combines Covered Software with other material, in a separate
|
||||
file or files, that is not Covered Software.
|
||||
|
||||
1.8. “License”
|
||||
|
||||
means this document.
|
||||
|
||||
1.9. “Licensable”
|
||||
|
||||
means having the right to grant, to the maximum extent possible, whether at the
|
||||
time of the initial grant or subsequently, any and all of the rights conveyed by
|
||||
this License.
|
||||
|
||||
1.10. “Modifications”
|
||||
|
||||
means any of the following:
|
||||
|
||||
a. any file in Source Code Form that results from an addition to, deletion
|
||||
from, or modification of the contents of Covered Software; or
|
||||
|
||||
b. any new file in Source Code Form that contains any Covered Software.
|
||||
|
||||
1.11. “Patent Claims” of a Contributor
|
||||
|
||||
means any patent claim(s), including without limitation, method, process,
|
||||
and apparatus claims, in any patent Licensable by such Contributor that
|
||||
would be infringed, but for the grant of the License, by the making,
|
||||
using, selling, offering for sale, having made, import, or transfer of
|
||||
either its Contributions or its Contributor Version.
|
||||
|
||||
1.12. “Secondary License”
|
||||
|
||||
means either the GNU General Public License, Version 2.0, the GNU Lesser
|
||||
General Public License, Version 2.1, the GNU Affero General Public
|
||||
License, Version 3.0, or any later versions of those licenses.
|
||||
|
||||
1.13. “Source Code Form”
|
||||
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. “You” (or “Your”)
|
||||
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, “You” includes any entity that controls, is
|
||||
controlled by, or is under common control with You. For purposes of this
|
||||
definition, “control” means (a) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (b) ownership of more than fifty percent (50%) of the
|
||||
outstanding shares or beneficial ownership of such entity.
|
||||
|
||||
|
||||
2. License Grants and Conditions
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
a. under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or as
|
||||
part of a Larger Work; and
|
||||
|
||||
b. under Patent Claims of such Contributor to make, use, sell, offer for
|
||||
sale, have made, import, and otherwise transfer either its Contributions
|
||||
or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution become
|
||||
effective for each Contribution on the date the Contributor first distributes
|
||||
such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under this
|
||||
License. No additional rights or licenses will be implied from the distribution
|
||||
or licensing of Covered Software under this License. Notwithstanding Section
|
||||
2.1(b) above, no patent license is granted by a Contributor:
|
||||
|
||||
a. for any code that a Contributor has removed from Covered Software; or
|
||||
|
||||
b. for infringements caused by: (i) Your and any other third party’s
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
c. under Patent Claims infringed by Covered Software in the absence of its
|
||||
Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks, or
|
||||
logos of any Contributor (except as may be necessary to comply with the
|
||||
notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this License
|
||||
(see Section 10.2) or under the terms of a Secondary License (if permitted
|
||||
under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its Contributions
|
||||
are its original creation(s) or it has sufficient rights to grant the
|
||||
rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under applicable
|
||||
copyright doctrines of fair use, fair dealing, or other equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
||||
Section 2.1.
|
||||
|
||||
|
||||
3. Responsibilities
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under the
|
||||
terms of this License. You must inform recipients that the Source Code Form
|
||||
of the Covered Software is governed by the terms of this License, and how
|
||||
they can obtain a copy of this License. You may not attempt to alter or
|
||||
restrict the recipients’ rights in the Source Code Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
a. such Covered Software must also be made available in Source Code Form,
|
||||
as described in Section 3.1, and You must inform recipients of the
|
||||
Executable Form how they can obtain a copy of such Source Code Form by
|
||||
reasonable means in a timely manner, at a charge no more than the cost
|
||||
of distribution to the recipient; and
|
||||
|
||||
b. You may distribute such Executable Form under the terms of this License,
|
||||
or sublicense it under different terms, provided that the license for
|
||||
the Executable Form does not attempt to limit or alter the recipients’
|
||||
rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for the
|
||||
Covered Software. If the Larger Work is a combination of Covered Software
|
||||
with a work governed by one or more Secondary Licenses, and the Covered
|
||||
Software is not Incompatible With Secondary Licenses, this License permits
|
||||
You to additionally distribute such Covered Software under the terms of
|
||||
such Secondary License(s), so that the recipient of the Larger Work may, at
|
||||
their option, further distribute the Covered Software under the terms of
|
||||
either this License or such Secondary License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices (including
|
||||
copyright notices, patent notices, disclaimers of warranty, or limitations
|
||||
of liability) contained within the Source Code Form of the Covered
|
||||
Software, except that You may alter any license notices to the extent
|
||||
required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on behalf
|
||||
of any Contributor. You must make it absolutely clear that any such
|
||||
warranty, support, indemnity, or liability obligation is offered by You
|
||||
alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this License
|
||||
with respect to some or all of the Covered Software due to statute, judicial
|
||||
order, or regulation then You must: (a) comply with the terms of this License
|
||||
to the maximum extent possible; and (b) describe the limitations and the code
|
||||
they affect. Such description must be placed in a text file included with all
|
||||
distributions of the Covered Software under this License. Except to the
|
||||
extent prohibited by statute or regulation, such description must be
|
||||
sufficiently detailed for a recipient of ordinary skill to be able to
|
||||
understand it.
|
||||
|
||||
5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically if You
|
||||
fail to comply with any of its terms. However, if You become compliant,
|
||||
then the rights granted under this License from a particular Contributor
|
||||
are reinstated (a) provisionally, unless and until such Contributor
|
||||
explicitly and finally terminates Your grants, and (b) on an ongoing basis,
|
||||
if such Contributor fails to notify You of the non-compliance by some
|
||||
reasonable means prior to 60 days after You have come back into compliance.
|
||||
Moreover, Your grants from a particular Contributor are reinstated on an
|
||||
ongoing basis if such Contributor notifies You of the non-compliance by
|
||||
some reasonable means, this is the first time You have received notice of
|
||||
non-compliance with this License from such Contributor, and You become
|
||||
compliant prior to 30 days after Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions, counter-claims,
|
||||
and cross-claims) alleging that a Contributor Version directly or
|
||||
indirectly infringes any patent, then the rights granted to You by any and
|
||||
all Contributors for the Covered Software under Section 2.1 of this License
|
||||
shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
|
||||
license agreements (excluding distributors and resellers) which have been
|
||||
validly granted by You or Your distributors under this License prior to
|
||||
termination shall survive termination.
|
||||
|
||||
6. Disclaimer of Warranty
|
||||
|
||||
Covered Software is provided under this License on an “as is” basis, without
|
||||
warranty of any kind, either expressed, implied, or statutory, including,
|
||||
without limitation, warranties that the Covered Software is free of defects,
|
||||
merchantable, fit for a particular purpose or non-infringing. The entire
|
||||
risk as to the quality and performance of the Covered Software is with You.
|
||||
Should any Covered Software prove defective in any respect, You (not any
|
||||
Contributor) assume the cost of any necessary servicing, repair, or
|
||||
correction. This disclaimer of warranty constitutes an essential part of this
|
||||
License. No use of any Covered Software is authorized under this License
|
||||
except under this disclaimer.
|
||||
|
||||
7. Limitation of Liability
|
||||
|
||||
Under no circumstances and under no legal theory, whether tort (including
|
||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
||||
distributes Covered Software as permitted above, be liable to You for any
|
||||
direct, indirect, special, incidental, or consequential damages of any
|
||||
character including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses, even if such party shall have been
|
||||
informed of the possibility of such damages. This limitation of liability
|
||||
shall not apply to liability for death or personal injury resulting from such
|
||||
party’s negligence to the extent applicable law prohibits such limitation.
|
||||
Some jurisdictions do not allow the exclusion or limitation of incidental or
|
||||
consequential damages, so this exclusion and limitation may not apply to You.
|
||||
|
||||
8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the courts of
|
||||
a jurisdiction where the defendant maintains its principal place of business
|
||||
and such litigation shall be governed by laws of that jurisdiction, without
|
||||
reference to its conflict-of-law provisions. Nothing in this Section shall
|
||||
prevent a party’s ability to bring cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject matter
|
||||
hereof. If any provision of this License is held to be unenforceable, such
|
||||
provision shall be reformed only to the extent necessary to make it
|
||||
enforceable. Any law or regulation which provides that the language of a
|
||||
contract shall be construed against the drafter shall not be used to construe
|
||||
this License against a Contributor.
|
||||
|
||||
|
||||
10. Versions of the License
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version of
|
||||
the License under which You originally received the Covered Software, or
|
||||
under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a modified
|
||||
version of this License if you rename the license and remove any
|
||||
references to the name of the license steward (except to note that such
|
||||
modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular file, then
|
||||
You may include the notice in a location (such as a LICENSE file in a relevant
|
||||
directory) where a recipient would be likely to look for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - “Incompatible With Secondary Licenses” Notice
|
||||
|
||||
This Source Code Form is “Incompatible
|
||||
With Secondary Licenses”, as defined by
|
||||
the Mozilla Public License, v. 2.0.
|
91
vendor/github.com/hashicorp/go-multierror/README.md
generated
vendored
Normal file
91
vendor/github.com/hashicorp/go-multierror/README.md
generated
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
# go-multierror
|
||||
|
||||
`go-multierror` is a package for Go that provides a mechanism for
|
||||
representing a list of `error` values as a single `error`.
|
||||
|
||||
This allows a function in Go to return an `error` that might actually
|
||||
be a list of errors. If the caller knows this, they can unwrap the
|
||||
list and access the errors. If the caller doesn't know, the error
|
||||
formats to a nice human-readable format.
|
||||
|
||||
`go-multierror` implements the
|
||||
[errwrap](https://github.com/hashicorp/errwrap) interface so that it can
|
||||
be used with that library, as well.
|
||||
|
||||
## Installation and Docs
|
||||
|
||||
Install using `go get github.com/hashicorp/go-multierror`.
|
||||
|
||||
Full documentation is available at
|
||||
http://godoc.org/github.com/hashicorp/go-multierror
|
||||
|
||||
## Usage
|
||||
|
||||
go-multierror is easy to use and purposely built to be unobtrusive in
|
||||
existing Go applications/libraries that may not be aware of it.
|
||||
|
||||
**Building a list of errors**
|
||||
|
||||
The `Append` function is used to create a list of errors. This function
|
||||
behaves a lot like the Go built-in `append` function: it doesn't matter
|
||||
if the first argument is nil, a `multierror.Error`, or any other `error`,
|
||||
the function behaves as you would expect.
|
||||
|
||||
```go
|
||||
var result error
|
||||
|
||||
if err := step1(); err != nil {
|
||||
result = multierror.Append(result, err)
|
||||
}
|
||||
if err := step2(); err != nil {
|
||||
result = multierror.Append(result, err)
|
||||
}
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
**Customizing the formatting of the errors**
|
||||
|
||||
By specifying a custom `ErrorFormat`, you can customize the format
|
||||
of the `Error() string` function:
|
||||
|
||||
```go
|
||||
var result *multierror.Error
|
||||
|
||||
// ... accumulate errors here, maybe using Append
|
||||
|
||||
if result != nil {
|
||||
result.ErrorFormat = func([]error) string {
|
||||
return "errors!"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Accessing the list of errors**
|
||||
|
||||
`multierror.Error` implements `error` so if the caller doesn't know about
|
||||
multierror, it will work just fine. But if you're aware a multierror might
|
||||
be returned, you can use type switches to access the list of errors:
|
||||
|
||||
```go
|
||||
if err := something(); err != nil {
|
||||
if merr, ok := err.(*multierror.Error); ok {
|
||||
// Use merr.Errors
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Returning a multierror only if there are errors**
|
||||
|
||||
If you build a `multierror.Error`, you can use the `ErrorOrNil` function
|
||||
to return an `error` implementation only if there are errors to return:
|
||||
|
||||
```go
|
||||
var result *multierror.Error
|
||||
|
||||
// ... accumulate errors here
|
||||
|
||||
// Return the `error` only if errors were added to the multierror, otherwise
|
||||
// return nil since there are no errors.
|
||||
return result.ErrorOrNil()
|
||||
```
|
37
vendor/github.com/hashicorp/go-multierror/append.go
generated
vendored
Normal file
37
vendor/github.com/hashicorp/go-multierror/append.go
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package multierror
|
||||
|
||||
// Append is a helper function that will append more errors
|
||||
// onto an Error in order to create a larger multi-error.
|
||||
//
|
||||
// If err is not a multierror.Error, then it will be turned into
|
||||
// one. If any of the errs are multierr.Error, they will be flattened
|
||||
// one level into err.
|
||||
func Append(err error, errs ...error) *Error {
|
||||
switch err := err.(type) {
|
||||
case *Error:
|
||||
// Typed nils can reach here, so initialize if we are nil
|
||||
if err == nil {
|
||||
err = new(Error)
|
||||
}
|
||||
|
||||
// Go through each error and flatten
|
||||
for _, e := range errs {
|
||||
switch e := e.(type) {
|
||||
case *Error:
|
||||
err.Errors = append(err.Errors, e.Errors...)
|
||||
default:
|
||||
err.Errors = append(err.Errors, e)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
default:
|
||||
newErrs := make([]error, 0, len(errs)+1)
|
||||
if err != nil {
|
||||
newErrs = append(newErrs, err)
|
||||
}
|
||||
newErrs = append(newErrs, errs...)
|
||||
|
||||
return Append(&Error{}, newErrs...)
|
||||
}
|
||||
}
|
26
vendor/github.com/hashicorp/go-multierror/flatten.go
generated
vendored
Normal file
26
vendor/github.com/hashicorp/go-multierror/flatten.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
package multierror
|
||||
|
||||
// Flatten flattens the given error, merging any *Errors together into
|
||||
// a single *Error.
|
||||
func Flatten(err error) error {
|
||||
// If it isn't an *Error, just return the error as-is
|
||||
if _, ok := err.(*Error); !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// Otherwise, make the result and flatten away!
|
||||
flatErr := new(Error)
|
||||
flatten(err, flatErr)
|
||||
return flatErr
|
||||
}
|
||||
|
||||
func flatten(err error, flatErr *Error) {
|
||||
switch err := err.(type) {
|
||||
case *Error:
|
||||
for _, e := range err.Errors {
|
||||
flatten(e, flatErr)
|
||||
}
|
||||
default:
|
||||
flatErr.Errors = append(flatErr.Errors, err)
|
||||
}
|
||||
}
|
23
vendor/github.com/hashicorp/go-multierror/format.go
generated
vendored
Normal file
23
vendor/github.com/hashicorp/go-multierror/format.go
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
package multierror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrorFormatFunc is a function callback that is called by Error to
|
||||
// turn the list of errors into a string.
|
||||
type ErrorFormatFunc func([]error) string
|
||||
|
||||
// ListFormatFunc is a basic formatter that outputs the number of errors
|
||||
// that occurred along with a bullet point list of the errors.
|
||||
func ListFormatFunc(es []error) string {
|
||||
points := make([]string, len(es))
|
||||
for i, err := range es {
|
||||
points[i] = fmt.Sprintf("* %s", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"%d error(s) occurred:\n\n%s",
|
||||
len(es), strings.Join(points, "\n"))
|
||||
}
|
51
vendor/github.com/hashicorp/go-multierror/multierror.go
generated
vendored
Normal file
51
vendor/github.com/hashicorp/go-multierror/multierror.go
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
package multierror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Error is an error type to track multiple errors. This is used to
|
||||
// accumulate errors in cases and return them as a single "error".
|
||||
type Error struct {
|
||||
Errors []error
|
||||
ErrorFormat ErrorFormatFunc
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
fn := e.ErrorFormat
|
||||
if fn == nil {
|
||||
fn = ListFormatFunc
|
||||
}
|
||||
|
||||
return fn(e.Errors)
|
||||
}
|
||||
|
||||
// ErrorOrNil returns an error interface if this Error represents
|
||||
// a list of errors, or returns nil if the list of errors is empty. This
|
||||
// function is useful at the end of accumulation to make sure that the value
|
||||
// returned represents the existence of errors.
|
||||
func (e *Error) ErrorOrNil() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
if len(e.Errors) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Error) GoString() string {
|
||||
return fmt.Sprintf("*%#v", *e)
|
||||
}
|
||||
|
||||
// WrappedErrors returns the list of errors that this Error is wrapping.
|
||||
// It is an implementatin of the errwrap.Wrapper interface so that
|
||||
// multierror.Error can be used with that library.
|
||||
//
|
||||
// This method is not safe to be called concurrently and is no different
|
||||
// than accessing the Errors field directly. It is implementd only to
|
||||
// satisfy the errwrap.Wrapper interface.
|
||||
func (e *Error) WrappedErrors() []error {
|
||||
return e.Errors
|
||||
}
|
37
vendor/github.com/hashicorp/go-multierror/prefix.go
generated
vendored
Normal file
37
vendor/github.com/hashicorp/go-multierror/prefix.go
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package multierror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/errwrap"
|
||||
)
|
||||
|
||||
// Prefix is a helper function that will prefix some text
|
||||
// to the given error. If the error is a multierror.Error, then
|
||||
// it will be prefixed to each wrapped error.
|
||||
//
|
||||
// This is useful to use when appending multiple multierrors
|
||||
// together in order to give better scoping.
|
||||
func Prefix(err error, prefix string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
format := fmt.Sprintf("%s {{err}}", prefix)
|
||||
switch err := err.(type) {
|
||||
case *Error:
|
||||
// Typed nils can reach here, so initialize if we are nil
|
||||
if err == nil {
|
||||
err = new(Error)
|
||||
}
|
||||
|
||||
// Wrap each of the errors
|
||||
for i, e := range err.Errors {
|
||||
err.Errors[i] = errwrap.Wrapf(format, e)
|
||||
}
|
||||
|
||||
return err
|
||||
default:
|
||||
return errwrap.Wrapf(format, err)
|
||||
}
|
||||
}
|
11
vendor/github.com/hashicorp/go-version/.travis.yml
generated
vendored
Normal file
11
vendor/github.com/hashicorp/go-version/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.0
|
||||
- 1.1
|
||||
- 1.2
|
||||
- 1.3
|
||||
- 1.4
|
||||
|
||||
script:
|
||||
- go test
|
354
vendor/github.com/hashicorp/go-version/LICENSE
generated
vendored
Normal file
354
vendor/github.com/hashicorp/go-version/LICENSE
generated
vendored
Normal file
@ -0,0 +1,354 @@
|
||||
Mozilla Public License, version 2.0
|
||||
|
||||
1. Definitions
|
||||
|
||||
1.1. “Contributor”
|
||||
|
||||
means each individual or legal entity that creates, contributes to the
|
||||
creation of, or owns Covered Software.
|
||||
|
||||
1.2. “Contributor Version”
|
||||
|
||||
means the combination of the Contributions of others (if any) used by a
|
||||
Contributor and that particular Contributor’s Contribution.
|
||||
|
||||
1.3. “Contribution”
|
||||
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. “Covered Software”
|
||||
|
||||
means Source Code Form to which the initial Contributor has attached the
|
||||
notice in Exhibit A, the Executable Form of such Source Code Form, and
|
||||
Modifications of such Source Code Form, in each case including portions
|
||||
thereof.
|
||||
|
||||
1.5. “Incompatible With Secondary Licenses”
|
||||
means
|
||||
|
||||
a. that the initial Contributor has attached the notice described in
|
||||
Exhibit B to the Covered Software; or
|
||||
|
||||
b. that the Covered Software was made available under the terms of version
|
||||
1.1 or earlier of the License, but not also under the terms of a
|
||||
Secondary License.
|
||||
|
||||
1.6. “Executable Form”
|
||||
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. “Larger Work”
|
||||
|
||||
means a work that combines Covered Software with other material, in a separate
|
||||
file or files, that is not Covered Software.
|
||||
|
||||
1.8. “License”
|
||||
|
||||
means this document.
|
||||
|
||||
1.9. “Licensable”
|
||||
|
||||
means having the right to grant, to the maximum extent possible, whether at the
|
||||
time of the initial grant or subsequently, any and all of the rights conveyed by
|
||||
this License.
|
||||
|
||||
1.10. “Modifications”
|
||||
|
||||
means any of the following:
|
||||
|
||||
a. any file in Source Code Form that results from an addition to, deletion
|
||||
from, or modification of the contents of Covered Software; or
|
||||
|
||||
b. any new file in Source Code Form that contains any Covered Software.
|
||||
|
||||
1.11. “Patent Claims” of a Contributor
|
||||
|
||||
means any patent claim(s), including without limitation, method, process,
|
||||
and apparatus claims, in any patent Licensable by such Contributor that
|
||||
would be infringed, but for the grant of the License, by the making,
|
||||
using, selling, offering for sale, having made, import, or transfer of
|
||||
either its Contributions or its Contributor Version.
|
||||
|
||||
1.12. “Secondary License”
|
||||
|
||||
means either the GNU General Public License, Version 2.0, the GNU Lesser
|
||||
General Public License, Version 2.1, the GNU Affero General Public
|
||||
License, Version 3.0, or any later versions of those licenses.
|
||||
|
||||
1.13. “Source Code Form”
|
||||
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. “You” (or “Your”)
|
||||
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, “You” includes any entity that controls, is
|
||||
controlled by, or is under common control with You. For purposes of this
|
||||
definition, “control” means (a) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (b) ownership of more than fifty percent (50%) of the
|
||||
outstanding shares or beneficial ownership of such entity.
|
||||
|
||||
|
||||
2. License Grants and Conditions
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
a. under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or as
|
||||
part of a Larger Work; and
|
||||
|
||||
b. under Patent Claims of such Contributor to make, use, sell, offer for
|
||||
sale, have made, import, and otherwise transfer either its Contributions
|
||||
or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution become
|
||||
effective for each Contribution on the date the Contributor first distributes
|
||||
such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under this
|
||||
License. No additional rights or licenses will be implied from the distribution
|
||||
or licensing of Covered Software under this License. Notwithstanding Section
|
||||
2.1(b) above, no patent license is granted by a Contributor:
|
||||
|
||||
a. for any code that a Contributor has removed from Covered Software; or
|
||||
|
||||
b. for infringements caused by: (i) Your and any other third party’s
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
c. under Patent Claims infringed by Covered Software in the absence of its
|
||||
Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks, or
|
||||
logos of any Contributor (except as may be necessary to comply with the
|
||||
notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this License
|
||||
(see Section 10.2) or under the terms of a Secondary License (if permitted
|
||||
under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its Contributions
|
||||
are its original creation(s) or it has sufficient rights to grant the
|
||||
rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under applicable
|
||||
copyright doctrines of fair use, fair dealing, or other equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
||||
Section 2.1.
|
||||
|
||||
|
||||
3. Responsibilities
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under the
|
||||
terms of this License. You must inform recipients that the Source Code Form
|
||||
of the Covered Software is governed by the terms of this License, and how
|
||||
they can obtain a copy of this License. You may not attempt to alter or
|
||||
restrict the recipients’ rights in the Source Code Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
a. such Covered Software must also be made available in Source Code Form,
|
||||
as described in Section 3.1, and You must inform recipients of the
|
||||
Executable Form how they can obtain a copy of such Source Code Form by
|
||||
reasonable means in a timely manner, at a charge no more than the cost
|
||||
of distribution to the recipient; and
|
||||
|
||||
b. You may distribute such Executable Form under the terms of this License,
|
||||
or sublicense it under different terms, provided that the license for
|
||||
the Executable Form does not attempt to limit or alter the recipients’
|
||||
rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for the
|
||||
Covered Software. If the Larger Work is a combination of Covered Software
|
||||
with a work governed by one or more Secondary Licenses, and the Covered
|
||||
Software is not Incompatible With Secondary Licenses, this License permits
|
||||
You to additionally distribute such Covered Software under the terms of
|
||||
such Secondary License(s), so that the recipient of the Larger Work may, at
|
||||
their option, further distribute the Covered Software under the terms of
|
||||
either this License or such Secondary License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices (including
|
||||
copyright notices, patent notices, disclaimers of warranty, or limitations
|
||||
of liability) contained within the Source Code Form of the Covered
|
||||
Software, except that You may alter any license notices to the extent
|
||||
required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on behalf
|
||||
of any Contributor. You must make it absolutely clear that any such
|
||||
warranty, support, indemnity, or liability obligation is offered by You
|
||||
alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this License
|
||||
with respect to some or all of the Covered Software due to statute, judicial
|
||||
order, or regulation then You must: (a) comply with the terms of this License
|
||||
to the maximum extent possible; and (b) describe the limitations and the code
|
||||
they affect. Such description must be placed in a text file included with all
|
||||
distributions of the Covered Software under this License. Except to the
|
||||
extent prohibited by statute or regulation, such description must be
|
||||
sufficiently detailed for a recipient of ordinary skill to be able to
|
||||
understand it.
|
||||
|
||||
5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically if You
|
||||
fail to comply with any of its terms. However, if You become compliant,
|
||||
then the rights granted under this License from a particular Contributor
|
||||
are reinstated (a) provisionally, unless and until such Contributor
|
||||
explicitly and finally terminates Your grants, and (b) on an ongoing basis,
|
||||
if such Contributor fails to notify You of the non-compliance by some
|
||||
reasonable means prior to 60 days after You have come back into compliance.
|
||||
Moreover, Your grants from a particular Contributor are reinstated on an
|
||||
ongoing basis if such Contributor notifies You of the non-compliance by
|
||||
some reasonable means, this is the first time You have received notice of
|
||||
non-compliance with this License from such Contributor, and You become
|
||||
compliant prior to 30 days after Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions, counter-claims,
|
||||
and cross-claims) alleging that a Contributor Version directly or
|
||||
indirectly infringes any patent, then the rights granted to You by any and
|
||||
all Contributors for the Covered Software under Section 2.1 of this License
|
||||
shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
|
||||
license agreements (excluding distributors and resellers) which have been
|
||||
validly granted by You or Your distributors under this License prior to
|
||||
termination shall survive termination.
|
||||
|
||||
6. Disclaimer of Warranty
|
||||
|
||||
Covered Software is provided under this License on an “as is” basis, without
|
||||
warranty of any kind, either expressed, implied, or statutory, including,
|
||||
without limitation, warranties that the Covered Software is free of defects,
|
||||
merchantable, fit for a particular purpose or non-infringing. The entire
|
||||
risk as to the quality and performance of the Covered Software is with You.
|
||||
Should any Covered Software prove defective in any respect, You (not any
|
||||
Contributor) assume the cost of any necessary servicing, repair, or
|
||||
correction. This disclaimer of warranty constitutes an essential part of this
|
||||
License. No use of any Covered Software is authorized under this License
|
||||
except under this disclaimer.
|
||||
|
||||
7. Limitation of Liability
|
||||
|
||||
Under no circumstances and under no legal theory, whether tort (including
|
||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
||||
distributes Covered Software as permitted above, be liable to You for any
|
||||
direct, indirect, special, incidental, or consequential damages of any
|
||||
character including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses, even if such party shall have been
|
||||
informed of the possibility of such damages. This limitation of liability
|
||||
shall not apply to liability for death or personal injury resulting from such
|
||||
party’s negligence to the extent applicable law prohibits such limitation.
|
||||
Some jurisdictions do not allow the exclusion or limitation of incidental or
|
||||
consequential damages, so this exclusion and limitation may not apply to You.
|
||||
|
||||
8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the courts of
|
||||
a jurisdiction where the defendant maintains its principal place of business
|
||||
and such litigation shall be governed by laws of that jurisdiction, without
|
||||
reference to its conflict-of-law provisions. Nothing in this Section shall
|
||||
prevent a party’s ability to bring cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject matter
|
||||
hereof. If any provision of this License is held to be unenforceable, such
|
||||
provision shall be reformed only to the extent necessary to make it
|
||||
enforceable. Any law or regulation which provides that the language of a
|
||||
contract shall be construed against the drafter shall not be used to construe
|
||||
this License against a Contributor.
|
||||
|
||||
|
||||
10. Versions of the License
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version of
|
||||
the License under which You originally received the Covered Software, or
|
||||
under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a modified
|
||||
version of this License if you rename the license and remove any
|
||||
references to the name of the license steward (except to note that such
|
||||
modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular file, then
|
||||
You may include the notice in a location (such as a LICENSE file in a relevant
|
||||
directory) where a recipient would be likely to look for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - “Incompatible With Secondary Licenses” Notice
|
||||
|
||||
This Source Code Form is “Incompatible
|
||||
With Secondary Licenses”, as defined by
|
||||
the Mozilla Public License, v. 2.0.
|
||||
|
65
vendor/github.com/hashicorp/go-version/README.md
generated
vendored
Normal file
65
vendor/github.com/hashicorp/go-version/README.md
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
# Versioning Library for Go
|
||||
[![Build Status](https://travis-ci.org/hashicorp/go-version.svg?branch=master)](https://travis-ci.org/hashicorp/go-version)
|
||||
|
||||
go-version is a library for parsing versions and version constraints,
|
||||
and verifying versions against a set of constraints. go-version
|
||||
can sort a collection of versions properly, handles prerelease/beta
|
||||
versions, can increment versions, etc.
|
||||
|
||||
Versions used with go-version must follow [SemVer](http://semver.org/).
|
||||
|
||||
## Installation and Usage
|
||||
|
||||
Package documentation can be found on
|
||||
[GoDoc](http://godoc.org/github.com/hashicorp/go-version).
|
||||
|
||||
Installation can be done with a normal `go get`:
|
||||
|
||||
```
|
||||
$ go get github.com/hashicorp/go-version
|
||||
```
|
||||
|
||||
#### Version Parsing and Comparison
|
||||
|
||||
```go
|
||||
v1, err := version.NewVersion("1.2")
|
||||
v2, err := version.NewVersion("1.5+metadata")
|
||||
|
||||
// Comparison example. There is also GreaterThan, Equal, and just
|
||||
// a simple Compare that returns an int allowing easy >=, <=, etc.
|
||||
if v1.LessThan(v2) {
|
||||
fmt.Printf("%s is less than %s", v1, v2)
|
||||
}
|
||||
```
|
||||
|
||||
#### Version Constraints
|
||||
|
||||
```go
|
||||
v1, err := version.NewVersion("1.2")
|
||||
|
||||
// Constraints example.
|
||||
constraints, err := version.NewConstraint(">= 1.0, < 1.4")
|
||||
if constraints.Check(v1) {
|
||||
fmt.Printf("%s satisfies constraints %s", v1, constraints)
|
||||
}
|
||||
```
|
||||
|
||||
#### Version Sorting
|
||||
|
||||
```go
|
||||
versionsRaw := []string{"1.1", "0.7.1", "1.4-beta", "1.4", "2"}
|
||||
versions := make([]*version.Version, len(versionsRaw))
|
||||
for i, raw := range versionsRaw {
|
||||
v, _ := version.NewVersion(raw)
|
||||
versions[i] = v
|
||||
}
|
||||
|
||||
// After this, the versions are properly sorted
|
||||
sort.Sort(version.Collection(versions))
|
||||
```
|
||||
|
||||
## Issues and Contributing
|
||||
|
||||
If you find an issue with this library, please report an issue. If you'd
|
||||
like, we welcome any contributions. Fork this library and submit a pull
|
||||
request.
|
156
vendor/github.com/hashicorp/go-version/constraint.go
generated
vendored
Normal file
156
vendor/github.com/hashicorp/go-version/constraint.go
generated
vendored
Normal file
@ -0,0 +1,156 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Constraint represents a single constraint for a version, such as
|
||||
// ">= 1.0".
|
||||
type Constraint struct {
|
||||
f constraintFunc
|
||||
check *Version
|
||||
original string
|
||||
}
|
||||
|
||||
// Constraints is a slice of constraints. We make a custom type so that
|
||||
// we can add methods to it.
|
||||
type Constraints []*Constraint
|
||||
|
||||
type constraintFunc func(v, c *Version) bool
|
||||
|
||||
var constraintOperators map[string]constraintFunc
|
||||
|
||||
var constraintRegexp *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
constraintOperators = map[string]constraintFunc{
|
||||
"": constraintEqual,
|
||||
"=": constraintEqual,
|
||||
"!=": constraintNotEqual,
|
||||
">": constraintGreaterThan,
|
||||
"<": constraintLessThan,
|
||||
">=": constraintGreaterThanEqual,
|
||||
"<=": constraintLessThanEqual,
|
||||
"~>": constraintPessimistic,
|
||||
}
|
||||
|
||||
ops := make([]string, 0, len(constraintOperators))
|
||||
for k, _ := range constraintOperators {
|
||||
ops = append(ops, regexp.QuoteMeta(k))
|
||||
}
|
||||
|
||||
constraintRegexp = regexp.MustCompile(fmt.Sprintf(
|
||||
`^\s*(%s)\s*(%s)\s*$`,
|
||||
strings.Join(ops, "|"),
|
||||
VersionRegexpRaw))
|
||||
}
|
||||
|
||||
// NewConstraint will parse one or more constraints from the given
|
||||
// constraint string. The string must be a comma-separated list of
|
||||
// constraints.
|
||||
func NewConstraint(v string) (Constraints, error) {
|
||||
vs := strings.Split(v, ",")
|
||||
result := make([]*Constraint, len(vs))
|
||||
for i, single := range vs {
|
||||
c, err := parseSingle(single)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result[i] = c
|
||||
}
|
||||
|
||||
return Constraints(result), nil
|
||||
}
|
||||
|
||||
// Check tests if a version satisfies all the constraints.
|
||||
func (cs Constraints) Check(v *Version) bool {
|
||||
for _, c := range cs {
|
||||
if !c.Check(v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Returns the string format of the constraints
|
||||
func (cs Constraints) String() string {
|
||||
csStr := make([]string, len(cs))
|
||||
for i, c := range cs {
|
||||
csStr[i] = c.String()
|
||||
}
|
||||
|
||||
return strings.Join(csStr, ",")
|
||||
}
|
||||
|
||||
// Check tests if a constraint is validated by the given version.
|
||||
func (c *Constraint) Check(v *Version) bool {
|
||||
return c.f(v, c.check)
|
||||
}
|
||||
|
||||
func (c *Constraint) String() string {
|
||||
return c.original
|
||||
}
|
||||
|
||||
func parseSingle(v string) (*Constraint, error) {
|
||||
matches := constraintRegexp.FindStringSubmatch(v)
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("Malformed constraint: %s", v)
|
||||
}
|
||||
|
||||
check, err := NewVersion(matches[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Constraint{
|
||||
f: constraintOperators[matches[1]],
|
||||
check: check,
|
||||
original: v,
|
||||
}, nil
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Constraint functions
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
func constraintEqual(v, c *Version) bool {
|
||||
return v.Equal(c)
|
||||
}
|
||||
|
||||
func constraintNotEqual(v, c *Version) bool {
|
||||
return !v.Equal(c)
|
||||
}
|
||||
|
||||
func constraintGreaterThan(v, c *Version) bool {
|
||||
return v.Compare(c) == 1
|
||||
}
|
||||
|
||||
func constraintLessThan(v, c *Version) bool {
|
||||
return v.Compare(c) == -1
|
||||
}
|
||||
|
||||
func constraintGreaterThanEqual(v, c *Version) bool {
|
||||
return v.Compare(c) >= 0
|
||||
}
|
||||
|
||||
func constraintLessThanEqual(v, c *Version) bool {
|
||||
return v.Compare(c) <= 0
|
||||
}
|
||||
|
||||
func constraintPessimistic(v, c *Version) bool {
|
||||
if v.LessThan(c) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < c.si-1; i++ {
|
||||
if v.segments[i] != c.segments[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
251
vendor/github.com/hashicorp/go-version/version.go
generated
vendored
Normal file
251
vendor/github.com/hashicorp/go-version/version.go
generated
vendored
Normal file
@ -0,0 +1,251 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The compiled regular expression used to test the validity of a version.
|
||||
var versionRegexp *regexp.Regexp
|
||||
|
||||
// The raw regular expression string used for testing the validity
|
||||
// of a version.
|
||||
const VersionRegexpRaw string = `([0-9]+(\.[0-9]+){0,2})` +
|
||||
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||
`?`
|
||||
|
||||
// Version represents a single version.
|
||||
type Version struct {
|
||||
metadata string
|
||||
pre string
|
||||
segments []int
|
||||
si int
|
||||
}
|
||||
|
||||
func init() {
|
||||
versionRegexp = regexp.MustCompile("^" + VersionRegexpRaw + "$")
|
||||
}
|
||||
|
||||
// NewVersion parses the given version and returns a new
|
||||
// Version.
|
||||
func NewVersion(v string) (*Version, error) {
|
||||
matches := versionRegexp.FindStringSubmatch(v)
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("Malformed version: %s", v)
|
||||
}
|
||||
|
||||
segmentsStr := strings.Split(matches[1], ".")
|
||||
segments := make([]int, len(segmentsStr), 3)
|
||||
si := 0
|
||||
for i, str := range segmentsStr {
|
||||
val, err := strconv.ParseInt(str, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"Error parsing version: %s", err)
|
||||
}
|
||||
|
||||
segments[i] = int(val)
|
||||
si += 1
|
||||
}
|
||||
for i := len(segments); i < 3; i++ {
|
||||
segments = append(segments, 0)
|
||||
}
|
||||
|
||||
return &Version{
|
||||
metadata: matches[7],
|
||||
pre: matches[4],
|
||||
segments: segments,
|
||||
si: si,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Must is a helper that wraps a call to a function returning (*Version, error)
|
||||
// and panics if error is non-nil.
|
||||
func Must(v *Version, err error) *Version {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// Compare compares this version to another version. This
|
||||
// returns -1, 0, or 1 if this version is smaller, equal,
|
||||
// or larger than the other version, respectively.
|
||||
//
|
||||
// If you want boolean results, use the LessThan, Equal,
|
||||
// or GreaterThan methods.
|
||||
func (v *Version) Compare(other *Version) int {
|
||||
// A quick, efficient equality check
|
||||
if v.String() == other.String() {
|
||||
return 0
|
||||
}
|
||||
|
||||
segmentsSelf := v.Segments()
|
||||
segmentsOther := other.Segments()
|
||||
|
||||
// If the segments are the same, we must compare on prerelease info
|
||||
if reflect.DeepEqual(segmentsSelf, segmentsOther) {
|
||||
preSelf := v.Prerelease()
|
||||
preOther := other.Prerelease()
|
||||
if preSelf == "" && preOther == "" {
|
||||
return 0
|
||||
}
|
||||
if preSelf == "" {
|
||||
return 1
|
||||
}
|
||||
if preOther == "" {
|
||||
return -1
|
||||
}
|
||||
|
||||
return comparePrereleases(preSelf, preOther)
|
||||
}
|
||||
|
||||
// Compare the segments
|
||||
for i := 0; i < len(segmentsSelf); i++ {
|
||||
lhs := segmentsSelf[i]
|
||||
rhs := segmentsOther[i]
|
||||
|
||||
if lhs == rhs {
|
||||
continue
|
||||
} else if lhs < rhs {
|
||||
return -1
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
panic("should not be reached")
|
||||
}
|
||||
|
||||
func comparePart(preSelf string, preOther string) int {
|
||||
if preSelf == preOther {
|
||||
return 0
|
||||
}
|
||||
|
||||
// if a part is empty, we use the other to decide
|
||||
if preSelf == "" {
|
||||
_, notIsNumeric := strconv.ParseInt(preOther, 10, 64)
|
||||
if notIsNumeric == nil {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
if preOther == "" {
|
||||
_, notIsNumeric := strconv.ParseInt(preSelf, 10, 64)
|
||||
if notIsNumeric == nil {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
if preSelf > preOther {
|
||||
return 1
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
func comparePrereleases(v string, other string) int {
|
||||
// the same pre release!
|
||||
if v == other {
|
||||
return 0
|
||||
}
|
||||
|
||||
// split both pre releases for analyse their parts
|
||||
selfPreReleaseMeta := strings.Split(v, ".")
|
||||
otherPreReleaseMeta := strings.Split(other, ".")
|
||||
|
||||
selfPreReleaseLen := len(selfPreReleaseMeta)
|
||||
otherPreReleaseLen := len(otherPreReleaseMeta)
|
||||
|
||||
biggestLen := otherPreReleaseLen
|
||||
if selfPreReleaseLen > otherPreReleaseLen {
|
||||
biggestLen = selfPreReleaseLen
|
||||
}
|
||||
|
||||
// loop for parts to find the first difference
|
||||
for i := 0; i < biggestLen; i = i + 1 {
|
||||
partSelfPre := ""
|
||||
if i < selfPreReleaseLen {
|
||||
partSelfPre = selfPreReleaseMeta[i]
|
||||
}
|
||||
|
||||
partOtherPre := ""
|
||||
if i < otherPreReleaseLen {
|
||||
partOtherPre = otherPreReleaseMeta[i]
|
||||
}
|
||||
|
||||
compare := comparePart(partSelfPre, partOtherPre)
|
||||
// if parts are equals, continue the loop
|
||||
if compare != 0 {
|
||||
return compare
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// Equal tests if two versions are equal.
|
||||
func (v *Version) Equal(o *Version) bool {
|
||||
return v.Compare(o) == 0
|
||||
}
|
||||
|
||||
// GreaterThan tests if this version is greater than another version.
|
||||
func (v *Version) GreaterThan(o *Version) bool {
|
||||
return v.Compare(o) > 0
|
||||
}
|
||||
|
||||
// LessThan tests if this version is less than another version.
|
||||
func (v *Version) LessThan(o *Version) bool {
|
||||
return v.Compare(o) < 0
|
||||
}
|
||||
|
||||
// Metadata returns any metadata that was part of the version
|
||||
// string.
|
||||
//
|
||||
// Metadata is anything that comes after the "+" in the version.
|
||||
// For example, with "1.2.3+beta", the metadata is "beta".
|
||||
func (v *Version) Metadata() string {
|
||||
return v.metadata
|
||||
}
|
||||
|
||||
// Prerelease returns any prerelease data that is part of the version,
|
||||
// or blank if there is no prerelease data.
|
||||
//
|
||||
// Prerelease information is anything that comes after the "-" in the
|
||||
// version (but before any metadata). For example, with "1.2.3-beta",
|
||||
// the prerelease information is "beta".
|
||||
func (v *Version) Prerelease() string {
|
||||
return v.pre
|
||||
}
|
||||
|
||||
// Segments returns the numeric segments of the version as a slice.
|
||||
//
|
||||
// This excludes any metadata or pre-release information. For example,
|
||||
// for a version "1.2.3-beta", segments will return a slice of
|
||||
// 1, 2, 3.
|
||||
func (v *Version) Segments() []int {
|
||||
return v.segments
|
||||
}
|
||||
|
||||
// String returns the full version string included pre-release
|
||||
// and metadata information.
|
||||
func (v *Version) String() string {
|
||||
var buf bytes.Buffer
|
||||
fmt.Fprintf(&buf, "%d.%d.%d", v.segments[0], v.segments[1], v.segments[2])
|
||||
if v.pre != "" {
|
||||
fmt.Fprintf(&buf, "-%s", v.pre)
|
||||
}
|
||||
if v.metadata != "" {
|
||||
fmt.Fprintf(&buf, "+%s", v.metadata)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
17
vendor/github.com/hashicorp/go-version/version_collection.go
generated
vendored
Normal file
17
vendor/github.com/hashicorp/go-version/version_collection.go
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
package version
|
||||
|
||||
// Collection is a type that implements the sort.Interface interface
|
||||
// so that versions can be sorted.
|
||||
type Collection []*Version
|
||||
|
||||
func (v Collection) Len() int {
|
||||
return len(v)
|
||||
}
|
||||
|
||||
func (v Collection) Less(i, j int) bool {
|
||||
return v[i].LessThan(v[j])
|
||||
}
|
||||
|
||||
func (v Collection) Swap(i, j int) {
|
||||
v[i], v[j] = v[j], v[i]
|
||||
}
|
23
vendor/github.com/hashicorp/yamux/.gitignore
generated
vendored
Normal file
23
vendor/github.com/hashicorp/yamux/.gitignore
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
# 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
|
362
vendor/github.com/hashicorp/yamux/LICENSE
generated
vendored
Normal file
362
vendor/github.com/hashicorp/yamux/LICENSE
generated
vendored
Normal file
@ -0,0 +1,362 @@
|
||||
Mozilla Public License, version 2.0
|
||||
|
||||
1. Definitions
|
||||
|
||||
1.1. "Contributor"
|
||||
|
||||
means each individual or legal entity that creates, contributes to the
|
||||
creation of, or owns Covered Software.
|
||||
|
||||
1.2. "Contributor Version"
|
||||
|
||||
means the combination of the Contributions of others (if any) used by a
|
||||
Contributor and that particular Contributor's Contribution.
|
||||
|
||||
1.3. "Contribution"
|
||||
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. "Covered Software"
|
||||
|
||||
means Source Code Form to which the initial Contributor has attached the
|
||||
notice in Exhibit A, the Executable Form of such Source Code Form, and
|
||||
Modifications of such Source Code Form, in each case including portions
|
||||
thereof.
|
||||
|
||||
1.5. "Incompatible With Secondary Licenses"
|
||||
means
|
||||
|
||||
a. that the initial Contributor has attached the notice described in
|
||||
Exhibit B to the Covered Software; or
|
||||
|
||||
b. that the Covered Software was made available under the terms of
|
||||
version 1.1 or earlier of the License, but not also under the terms of
|
||||
a Secondary License.
|
||||
|
||||
1.6. "Executable Form"
|
||||
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. "Larger Work"
|
||||
|
||||
means a work that combines Covered Software with other material, in a
|
||||
separate file or files, that is not Covered Software.
|
||||
|
||||
1.8. "License"
|
||||
|
||||
means this document.
|
||||
|
||||
1.9. "Licensable"
|
||||
|
||||
means having the right to grant, to the maximum extent possible, whether
|
||||
at the time of the initial grant or subsequently, any and all of the
|
||||
rights conveyed by this License.
|
||||
|
||||
1.10. "Modifications"
|
||||
|
||||
means any of the following:
|
||||
|
||||
a. any file in Source Code Form that results from an addition to,
|
||||
deletion from, or modification of the contents of Covered Software; or
|
||||
|
||||
b. any new file in Source Code Form that contains any Covered Software.
|
||||
|
||||
1.11. "Patent Claims" of a Contributor
|
||||
|
||||
means any patent claim(s), including without limitation, method,
|
||||
process, and apparatus claims, in any patent Licensable by such
|
||||
Contributor that would be infringed, but for the grant of the License,
|
||||
by the making, using, selling, offering for sale, having made, import,
|
||||
or transfer of either its Contributions or its Contributor Version.
|
||||
|
||||
1.12. "Secondary License"
|
||||
|
||||
means either the GNU General Public License, Version 2.0, the GNU Lesser
|
||||
General Public License, Version 2.1, the GNU Affero General Public
|
||||
License, Version 3.0, or any later versions of those licenses.
|
||||
|
||||
1.13. "Source Code Form"
|
||||
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. "You" (or "Your")
|
||||
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, "You" includes any entity that controls, is
|
||||
controlled by, or is under common control with You. For purposes of this
|
||||
definition, "control" means (a) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (b) ownership of more than fifty percent (50%) of the
|
||||
outstanding shares or beneficial ownership of such entity.
|
||||
|
||||
|
||||
2. License Grants and Conditions
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
a. under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or
|
||||
as part of a Larger Work; and
|
||||
|
||||
b. under Patent Claims of such Contributor to make, use, sell, offer for
|
||||
sale, have made, import, and otherwise transfer either its
|
||||
Contributions or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution
|
||||
become effective for each Contribution on the date the Contributor first
|
||||
distributes such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under
|
||||
this License. No additional rights or licenses will be implied from the
|
||||
distribution or licensing of Covered Software under this License.
|
||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||
Contributor:
|
||||
|
||||
a. for any code that a Contributor has removed from Covered Software; or
|
||||
|
||||
b. for infringements caused by: (i) Your and any other third party's
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
c. under Patent Claims infringed by Covered Software in the absence of
|
||||
its Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks,
|
||||
or logos of any Contributor (except as may be necessary to comply with
|
||||
the notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this
|
||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||
permitted under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its
|
||||
Contributions are its original creation(s) or it has sufficient rights to
|
||||
grant the rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under
|
||||
applicable copyright doctrines of fair use, fair dealing, or other
|
||||
equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
||||
Section 2.1.
|
||||
|
||||
|
||||
3. Responsibilities
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under
|
||||
the terms of this License. You must inform recipients that the Source
|
||||
Code Form of the Covered Software is governed by the terms of this
|
||||
License, and how they can obtain a copy of this License. You may not
|
||||
attempt to alter or restrict the recipients' rights in the Source Code
|
||||
Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
a. such Covered Software must also be made available in Source Code Form,
|
||||
as described in Section 3.1, and You must inform recipients of the
|
||||
Executable Form how they can obtain a copy of such Source Code Form by
|
||||
reasonable means in a timely manner, at a charge no more than the cost
|
||||
of distribution to the recipient; and
|
||||
|
||||
b. You may distribute such Executable Form under the terms of this
|
||||
License, or sublicense it under different terms, provided that the
|
||||
license for the Executable Form does not attempt to limit or alter the
|
||||
recipients' rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for
|
||||
the Covered Software. If the Larger Work is a combination of Covered
|
||||
Software with a work governed by one or more Secondary Licenses, and the
|
||||
Covered Software is not Incompatible With Secondary Licenses, this
|
||||
License permits You to additionally distribute such Covered Software
|
||||
under the terms of such Secondary License(s), so that the recipient of
|
||||
the Larger Work may, at their option, further distribute the Covered
|
||||
Software under the terms of either this License or such Secondary
|
||||
License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices
|
||||
(including copyright notices, patent notices, disclaimers of warranty, or
|
||||
limitations of liability) contained within the Source Code Form of the
|
||||
Covered Software, except that You may alter any license notices to the
|
||||
extent required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on
|
||||
behalf of any Contributor. You must make it absolutely clear that any
|
||||
such warranty, support, indemnity, or liability obligation is offered by
|
||||
You alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this License
|
||||
with respect to some or all of the Covered Software due to statute,
|
||||
judicial order, or regulation then You must: (a) comply with the terms of
|
||||
this License to the maximum extent possible; and (b) describe the
|
||||
limitations and the code they affect. Such description must be placed in a
|
||||
text file included with all distributions of the Covered Software under
|
||||
this License. Except to the extent prohibited by statute or regulation,
|
||||
such description must be sufficiently detailed for a recipient of ordinary
|
||||
skill to be able to understand it.
|
||||
|
||||
5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically if You
|
||||
fail to comply with any of its terms. However, if You become compliant,
|
||||
then the rights granted under this License from a particular Contributor
|
||||
are reinstated (a) provisionally, unless and until such Contributor
|
||||
explicitly and finally terminates Your grants, and (b) on an ongoing
|
||||
basis, if such Contributor fails to notify You of the non-compliance by
|
||||
some reasonable means prior to 60 days after You have come back into
|
||||
compliance. Moreover, Your grants from a particular Contributor are
|
||||
reinstated on an ongoing basis if such Contributor notifies You of the
|
||||
non-compliance by some reasonable means, this is the first time You have
|
||||
received notice of non-compliance with this License from such
|
||||
Contributor, and You become compliant prior to 30 days after Your receipt
|
||||
of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions,
|
||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||
directly or indirectly infringes any patent, then the rights granted to
|
||||
You by any and all Contributors for the Covered Software under Section
|
||||
2.1 of this License shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
|
||||
license agreements (excluding distributors and resellers) which have been
|
||||
validly granted by You or Your distributors under this License prior to
|
||||
termination shall survive termination.
|
||||
|
||||
6. Disclaimer of Warranty
|
||||
|
||||
Covered Software is provided under this License on an "as is" basis,
|
||||
without warranty of any kind, either expressed, implied, or statutory,
|
||||
including, without limitation, warranties that the Covered Software is free
|
||||
of defects, merchantable, fit for a particular purpose or non-infringing.
|
||||
The entire risk as to the quality and performance of the Covered Software
|
||||
is with You. Should any Covered Software prove defective in any respect,
|
||||
You (not any Contributor) assume the cost of any necessary servicing,
|
||||
repair, or correction. This disclaimer of warranty constitutes an essential
|
||||
part of this License. No use of any Covered Software is authorized under
|
||||
this License except under this disclaimer.
|
||||
|
||||
7. Limitation of Liability
|
||||
|
||||
Under no circumstances and under no legal theory, whether tort (including
|
||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
||||
distributes Covered Software as permitted above, be liable to You for any
|
||||
direct, indirect, special, incidental, or consequential damages of any
|
||||
character including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses, even if such party shall have been
|
||||
informed of the possibility of such damages. This limitation of liability
|
||||
shall not apply to liability for death or personal injury resulting from
|
||||
such party's negligence to the extent applicable law prohibits such
|
||||
limitation. Some jurisdictions do not allow the exclusion or limitation of
|
||||
incidental or consequential damages, so this exclusion and limitation may
|
||||
not apply to You.
|
||||
|
||||
8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the courts
|
||||
of a jurisdiction where the defendant maintains its principal place of
|
||||
business and such litigation shall be governed by laws of that
|
||||
jurisdiction, without reference to its conflict-of-law provisions. Nothing
|
||||
in this Section shall prevent a party's ability to bring cross-claims or
|
||||
counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject
|
||||
matter hereof. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable. Any law or regulation which provides that
|
||||
the language of a contract shall be construed against the drafter shall not
|
||||
be used to construe this License against a Contributor.
|
||||
|
||||
|
||||
10. Versions of the License
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version
|
||||
of the License under which You originally received the Covered Software,
|
||||
or under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a
|
||||
modified version of this License if you rename the license and remove
|
||||
any references to the name of the license steward (except to note that
|
||||
such modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||
Licenses If You choose to distribute Source Code Form that is
|
||||
Incompatible With Secondary Licenses under the terms of this version of
|
||||
the License, the notice described in Exhibit B of this License must be
|
||||
attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular file,
|
||||
then You may include the notice in a location (such as a LICENSE file in a
|
||||
relevant directory) where a recipient would be likely to look for such a
|
||||
notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||
|
||||
This Source Code Form is "Incompatible
|
||||
With Secondary Licenses", as defined by
|
||||
the Mozilla Public License, v. 2.0.
|
86
vendor/github.com/hashicorp/yamux/README.md
generated
vendored
Normal file
86
vendor/github.com/hashicorp/yamux/README.md
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
# Yamux
|
||||
|
||||
Yamux (Yet another Multiplexer) is a multiplexing library for Golang.
|
||||
It relies on an underlying connection to provide reliability
|
||||
and ordering, such as TCP or Unix domain sockets, and provides
|
||||
stream-oriented multiplexing. It is inspired by SPDY but is not
|
||||
interoperable with it.
|
||||
|
||||
Yamux features include:
|
||||
|
||||
* Bi-directional streams
|
||||
* Streams can be opened by either client or server
|
||||
* Useful for NAT traversal
|
||||
* Server-side push support
|
||||
* Flow control
|
||||
* Avoid starvation
|
||||
* Back-pressure to prevent overwhelming a receiver
|
||||
* Keep Alives
|
||||
* Enables persistent connections over a load balancer
|
||||
* Efficient
|
||||
* Enables thousands of logical streams with low overhead
|
||||
|
||||
## Documentation
|
||||
|
||||
For complete documentation, see the associated [Godoc](http://godoc.org/github.com/hashicorp/yamux).
|
||||
|
||||
## Specification
|
||||
|
||||
The full specification for Yamux is provided in the `spec.md` file.
|
||||
It can be used as a guide to implementors of interoperable libraries.
|
||||
|
||||
## Usage
|
||||
|
||||
Using Yamux is remarkably simple:
|
||||
|
||||
```go
|
||||
|
||||
func client() {
|
||||
// Get a TCP connection
|
||||
conn, err := net.Dial(...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Setup client side of yamux
|
||||
session, err := yamux.Client(conn, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Open a new stream
|
||||
stream, err := session.Open()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Stream implements net.Conn
|
||||
stream.Write([]byte("ping"))
|
||||
}
|
||||
|
||||
func server() {
|
||||
// Accept a TCP connection
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Setup server side of yamux
|
||||
session, err := yamux.Server(conn, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Accept a stream
|
||||
stream, err := session.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Listen for a message
|
||||
buf := make([]byte, 4)
|
||||
stream.Read(buf)
|
||||
}
|
||||
|
||||
```
|
||||
|
60
vendor/github.com/hashicorp/yamux/addr.go
generated
vendored
Normal file
60
vendor/github.com/hashicorp/yamux/addr.go
generated
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
package yamux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
// hasAddr is used to get the address from the underlying connection
|
||||
type hasAddr interface {
|
||||
LocalAddr() net.Addr
|
||||
RemoteAddr() net.Addr
|
||||
}
|
||||
|
||||
// yamuxAddr is used when we cannot get the underlying address
|
||||
type yamuxAddr struct {
|
||||
Addr string
|
||||
}
|
||||
|
||||
func (*yamuxAddr) Network() string {
|
||||
return "yamux"
|
||||
}
|
||||
|
||||
func (y *yamuxAddr) String() string {
|
||||
return fmt.Sprintf("yamux:%s", y.Addr)
|
||||
}
|
||||
|
||||
// Addr is used to get the address of the listener.
|
||||
func (s *Session) Addr() net.Addr {
|
||||
return s.LocalAddr()
|
||||
}
|
||||
|
||||
// LocalAddr is used to get the local address of the
|
||||
// underlying connection.
|
||||
func (s *Session) LocalAddr() net.Addr {
|
||||
addr, ok := s.conn.(hasAddr)
|
||||
if !ok {
|
||||
return &yamuxAddr{"local"}
|
||||
}
|
||||
return addr.LocalAddr()
|
||||
}
|
||||
|
||||
// RemoteAddr is used to get the address of remote end
|
||||
// of the underlying connection
|
||||
func (s *Session) RemoteAddr() net.Addr {
|
||||
addr, ok := s.conn.(hasAddr)
|
||||
if !ok {
|
||||
return &yamuxAddr{"remote"}
|
||||
}
|
||||
return addr.RemoteAddr()
|
||||
}
|
||||
|
||||
// LocalAddr returns the local address
|
||||
func (s *Stream) LocalAddr() net.Addr {
|
||||
return s.session.LocalAddr()
|
||||
}
|
||||
|
||||
// LocalAddr returns the remote address
|
||||
func (s *Stream) RemoteAddr() net.Addr {
|
||||
return s.session.RemoteAddr()
|
||||
}
|
157
vendor/github.com/hashicorp/yamux/const.go
generated
vendored
Normal file
157
vendor/github.com/hashicorp/yamux/const.go
generated
vendored
Normal file
@ -0,0 +1,157 @@
|
||||
package yamux
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidVersion means we received a frame with an
|
||||
// invalid version
|
||||
ErrInvalidVersion = fmt.Errorf("invalid protocol version")
|
||||
|
||||
// ErrInvalidMsgType means we received a frame with an
|
||||
// invalid message type
|
||||
ErrInvalidMsgType = fmt.Errorf("invalid msg type")
|
||||
|
||||
// ErrSessionShutdown is used if there is a shutdown during
|
||||
// an operation
|
||||
ErrSessionShutdown = fmt.Errorf("session shutdown")
|
||||
|
||||
// ErrStreamsExhausted is returned if we have no more
|
||||
// stream ids to issue
|
||||
ErrStreamsExhausted = fmt.Errorf("streams exhausted")
|
||||
|
||||
// ErrDuplicateStream is used if a duplicate stream is
|
||||
// opened inbound
|
||||
ErrDuplicateStream = fmt.Errorf("duplicate stream initiated")
|
||||
|
||||
// ErrReceiveWindowExceeded indicates the window was exceeded
|
||||
ErrRecvWindowExceeded = fmt.Errorf("recv window exceeded")
|
||||
|
||||
// ErrTimeout is used when we reach an IO deadline
|
||||
ErrTimeout = fmt.Errorf("i/o deadline reached")
|
||||
|
||||
// ErrStreamClosed is returned when using a closed stream
|
||||
ErrStreamClosed = fmt.Errorf("stream closed")
|
||||
|
||||
// ErrUnexpectedFlag is set when we get an unexpected flag
|
||||
ErrUnexpectedFlag = fmt.Errorf("unexpected flag")
|
||||
|
||||
// ErrRemoteGoAway is used when we get a go away from the other side
|
||||
ErrRemoteGoAway = fmt.Errorf("remote end is not accepting connections")
|
||||
|
||||
// ErrConnectionReset is sent if a stream is reset. This can happen
|
||||
// if the backlog is exceeded, or if there was a remote GoAway.
|
||||
ErrConnectionReset = fmt.Errorf("connection reset")
|
||||
|
||||
// ErrConnectionWriteTimeout indicates that we hit the "safety valve"
|
||||
// timeout writing to the underlying stream connection.
|
||||
ErrConnectionWriteTimeout = fmt.Errorf("connection write timeout")
|
||||
|
||||
// ErrKeepAliveTimeout is sent if a missed keepalive caused the stream close
|
||||
ErrKeepAliveTimeout = fmt.Errorf("keepalive timeout")
|
||||
)
|
||||
|
||||
const (
|
||||
// protoVersion is the only version we support
|
||||
protoVersion uint8 = 0
|
||||
)
|
||||
|
||||
const (
|
||||
// Data is used for data frames. They are followed
|
||||
// by length bytes worth of payload.
|
||||
typeData uint8 = iota
|
||||
|
||||
// WindowUpdate is used to change the window of
|
||||
// a given stream. The length indicates the delta
|
||||
// update to the window.
|
||||
typeWindowUpdate
|
||||
|
||||
// Ping is sent as a keep-alive or to measure
|
||||
// the RTT. The StreamID and Length value are echoed
|
||||
// back in the response.
|
||||
typePing
|
||||
|
||||
// GoAway is sent to terminate a session. The StreamID
|
||||
// should be 0 and the length is an error code.
|
||||
typeGoAway
|
||||
)
|
||||
|
||||
const (
|
||||
// SYN is sent to signal a new stream. May
|
||||
// be sent with a data payload
|
||||
flagSYN uint16 = 1 << iota
|
||||
|
||||
// ACK is sent to acknowledge a new stream. May
|
||||
// be sent with a data payload
|
||||
flagACK
|
||||
|
||||
// FIN is sent to half-close the given stream.
|
||||
// May be sent with a data payload.
|
||||
flagFIN
|
||||
|
||||
// RST is used to hard close a given stream.
|
||||
flagRST
|
||||
)
|
||||
|
||||
const (
|
||||
// initialStreamWindow is the initial stream window size
|
||||
initialStreamWindow uint32 = 256 * 1024
|
||||
)
|
||||
|
||||
const (
|
||||
// goAwayNormal is sent on a normal termination
|
||||
goAwayNormal uint32 = iota
|
||||
|
||||
// goAwayProtoErr sent on a protocol error
|
||||
goAwayProtoErr
|
||||
|
||||
// goAwayInternalErr sent on an internal error
|
||||
goAwayInternalErr
|
||||
)
|
||||
|
||||
const (
|
||||
sizeOfVersion = 1
|
||||
sizeOfType = 1
|
||||
sizeOfFlags = 2
|
||||
sizeOfStreamID = 4
|
||||
sizeOfLength = 4
|
||||
headerSize = sizeOfVersion + sizeOfType + sizeOfFlags +
|
||||
sizeOfStreamID + sizeOfLength
|
||||
)
|
||||
|
||||
type header []byte
|
||||
|
||||
func (h header) Version() uint8 {
|
||||
return h[0]
|
||||
}
|
||||
|
||||
func (h header) MsgType() uint8 {
|
||||
return h[1]
|
||||
}
|
||||
|
||||
func (h header) Flags() uint16 {
|
||||
return binary.BigEndian.Uint16(h[2:4])
|
||||
}
|
||||
|
||||
func (h header) StreamID() uint32 {
|
||||
return binary.BigEndian.Uint32(h[4:8])
|
||||
}
|
||||
|
||||
func (h header) Length() uint32 {
|
||||
return binary.BigEndian.Uint32(h[8:12])
|
||||
}
|
||||
|
||||
func (h header) String() string {
|
||||
return fmt.Sprintf("Vsn:%d Type:%d Flags:%d StreamID:%d Length:%d",
|
||||
h.Version(), h.MsgType(), h.Flags(), h.StreamID(), h.Length())
|
||||
}
|
||||
|
||||
func (h header) encode(msgType uint8, flags uint16, streamID uint32, length uint32) {
|
||||
h[0] = protoVersion
|
||||
h[1] = msgType
|
||||
binary.BigEndian.PutUint16(h[2:4], flags)
|
||||
binary.BigEndian.PutUint32(h[4:8], streamID)
|
||||
binary.BigEndian.PutUint32(h[8:12], length)
|
||||
}
|
87
vendor/github.com/hashicorp/yamux/mux.go
generated
vendored
Normal file
87
vendor/github.com/hashicorp/yamux/mux.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
package yamux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config is used to tune the Yamux session
|
||||
type Config struct {
|
||||
// AcceptBacklog is used to limit how many streams may be
|
||||
// waiting an accept.
|
||||
AcceptBacklog int
|
||||
|
||||
// EnableKeepalive is used to do a period keep alive
|
||||
// messages using a ping.
|
||||
EnableKeepAlive bool
|
||||
|
||||
// KeepAliveInterval is how often to perform the keep alive
|
||||
KeepAliveInterval time.Duration
|
||||
|
||||
// ConnectionWriteTimeout is meant to be a "safety valve" timeout after
|
||||
// we which will suspect a problem with the underlying connection and
|
||||
// close it. This is only applied to writes, where's there's generally
|
||||
// an expectation that things will move along quickly.
|
||||
ConnectionWriteTimeout time.Duration
|
||||
|
||||
// MaxStreamWindowSize is used to control the maximum
|
||||
// window size that we allow for a stream.
|
||||
MaxStreamWindowSize uint32
|
||||
|
||||
// LogOutput is used to control the log destination
|
||||
LogOutput io.Writer
|
||||
}
|
||||
|
||||
// DefaultConfig is used to return a default configuration
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
AcceptBacklog: 256,
|
||||
EnableKeepAlive: true,
|
||||
KeepAliveInterval: 30 * time.Second,
|
||||
ConnectionWriteTimeout: 10 * time.Second,
|
||||
MaxStreamWindowSize: initialStreamWindow,
|
||||
LogOutput: os.Stderr,
|
||||
}
|
||||
}
|
||||
|
||||
// VerifyConfig is used to verify the sanity of configuration
|
||||
func VerifyConfig(config *Config) error {
|
||||
if config.AcceptBacklog <= 0 {
|
||||
return fmt.Errorf("backlog must be positive")
|
||||
}
|
||||
if config.KeepAliveInterval == 0 {
|
||||
return fmt.Errorf("keep-alive interval must be positive")
|
||||
}
|
||||
if config.MaxStreamWindowSize < initialStreamWindow {
|
||||
return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Server is used to initialize a new server-side connection.
|
||||
// There must be at most one server-side connection. If a nil config is
|
||||
// provided, the DefaultConfiguration will be used.
|
||||
func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
|
||||
if config == nil {
|
||||
config = DefaultConfig()
|
||||
}
|
||||
if err := VerifyConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newSession(config, conn, false), nil
|
||||
}
|
||||
|
||||
// Client is used to initialize a new client-side connection.
|
||||
// There must be at most one client-side connection.
|
||||
func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
|
||||
if config == nil {
|
||||
config = DefaultConfig()
|
||||
}
|
||||
|
||||
if err := VerifyConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newSession(config, conn, true), nil
|
||||
}
|
598
vendor/github.com/hashicorp/yamux/session.go
generated
vendored
Normal file
598
vendor/github.com/hashicorp/yamux/session.go
generated
vendored
Normal file
@ -0,0 +1,598 @@
|
||||
package yamux
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Session is used to wrap a reliable ordered connection and to
|
||||
// multiplex it into multiple streams.
|
||||
type Session struct {
|
||||
// remoteGoAway indicates the remote side does
|
||||
// not want futher connections. Must be first for alignment.
|
||||
remoteGoAway int32
|
||||
|
||||
// localGoAway indicates that we should stop
|
||||
// accepting futher connections. Must be first for alignment.
|
||||
localGoAway int32
|
||||
|
||||
// nextStreamID is the next stream we should
|
||||
// send. This depends if we are a client/server.
|
||||
nextStreamID uint32
|
||||
|
||||
// config holds our configuration
|
||||
config *Config
|
||||
|
||||
// logger is used for our logs
|
||||
logger *log.Logger
|
||||
|
||||
// conn is the underlying connection
|
||||
conn io.ReadWriteCloser
|
||||
|
||||
// bufRead is a buffered reader
|
||||
bufRead *bufio.Reader
|
||||
|
||||
// pings is used to track inflight pings
|
||||
pings map[uint32]chan struct{}
|
||||
pingID uint32
|
||||
pingLock sync.Mutex
|
||||
|
||||
// streams maps a stream id to a stream
|
||||
streams map[uint32]*Stream
|
||||
streamLock sync.Mutex
|
||||
|
||||
// synCh acts like a semaphore. It is sized to the AcceptBacklog which
|
||||
// is assumed to be symmetric between the client and server. This allows
|
||||
// the client to avoid exceeding the backlog and instead blocks the open.
|
||||
synCh chan struct{}
|
||||
|
||||
// acceptCh is used to pass ready streams to the client
|
||||
acceptCh chan *Stream
|
||||
|
||||
// sendCh is used to mark a stream as ready to send,
|
||||
// or to send a header out directly.
|
||||
sendCh chan sendReady
|
||||
|
||||
// recvDoneCh is closed when recv() exits to avoid a race
|
||||
// between stream registration and stream shutdown
|
||||
recvDoneCh chan struct{}
|
||||
|
||||
// shutdown is used to safely close a session
|
||||
shutdown bool
|
||||
shutdownErr error
|
||||
shutdownCh chan struct{}
|
||||
shutdownLock sync.Mutex
|
||||
}
|
||||
|
||||
// sendReady is used to either mark a stream as ready
|
||||
// or to directly send a header
|
||||
type sendReady struct {
|
||||
Hdr []byte
|
||||
Body io.Reader
|
||||
Err chan error
|
||||
}
|
||||
|
||||
// newSession is used to construct a new session
|
||||
func newSession(config *Config, conn io.ReadWriteCloser, client bool) *Session {
|
||||
s := &Session{
|
||||
config: config,
|
||||
logger: log.New(config.LogOutput, "", log.LstdFlags),
|
||||
conn: conn,
|
||||
bufRead: bufio.NewReader(conn),
|
||||
pings: make(map[uint32]chan struct{}),
|
||||
streams: make(map[uint32]*Stream),
|
||||
synCh: make(chan struct{}, config.AcceptBacklog),
|
||||
acceptCh: make(chan *Stream, config.AcceptBacklog),
|
||||
sendCh: make(chan sendReady, 64),
|
||||
recvDoneCh: make(chan struct{}),
|
||||
shutdownCh: make(chan struct{}),
|
||||
}
|
||||
if client {
|
||||
s.nextStreamID = 1
|
||||
} else {
|
||||
s.nextStreamID = 2
|
||||
}
|
||||
go s.recv()
|
||||
go s.send()
|
||||
if config.EnableKeepAlive {
|
||||
go s.keepalive()
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// IsClosed does a safe check to see if we have shutdown
|
||||
func (s *Session) IsClosed() bool {
|
||||
select {
|
||||
case <-s.shutdownCh:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// NumStreams returns the number of currently open streams
|
||||
func (s *Session) NumStreams() int {
|
||||
s.streamLock.Lock()
|
||||
num := len(s.streams)
|
||||
s.streamLock.Unlock()
|
||||
return num
|
||||
}
|
||||
|
||||
// Open is used to create a new stream as a net.Conn
|
||||
func (s *Session) Open() (net.Conn, error) {
|
||||
conn, err := s.OpenStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// OpenStream is used to create a new stream
|
||||
func (s *Session) OpenStream() (*Stream, error) {
|
||||
if s.IsClosed() {
|
||||
return nil, ErrSessionShutdown
|
||||
}
|
||||
if atomic.LoadInt32(&s.remoteGoAway) == 1 {
|
||||
return nil, ErrRemoteGoAway
|
||||
}
|
||||
|
||||
// Block if we have too many inflight SYNs
|
||||
select {
|
||||
case s.synCh <- struct{}{}:
|
||||
case <-s.shutdownCh:
|
||||
return nil, ErrSessionShutdown
|
||||
}
|
||||
|
||||
GET_ID:
|
||||
// Get and ID, and check for stream exhaustion
|
||||
id := atomic.LoadUint32(&s.nextStreamID)
|
||||
if id >= math.MaxUint32-1 {
|
||||
return nil, ErrStreamsExhausted
|
||||
}
|
||||
if !atomic.CompareAndSwapUint32(&s.nextStreamID, id, id+2) {
|
||||
goto GET_ID
|
||||
}
|
||||
|
||||
// Register the stream
|
||||
stream := newStream(s, id, streamInit)
|
||||
s.streamLock.Lock()
|
||||
s.streams[id] = stream
|
||||
s.streamLock.Unlock()
|
||||
|
||||
// Send the window update to create
|
||||
if err := stream.sendWindowUpdate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return stream, nil
|
||||
}
|
||||
|
||||
// Accept is used to block until the next available stream
|
||||
// is ready to be accepted.
|
||||
func (s *Session) Accept() (net.Conn, error) {
|
||||
conn, err := s.AcceptStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
|
||||
// AcceptStream is used to block until the next available stream
|
||||
// is ready to be accepted.
|
||||
func (s *Session) AcceptStream() (*Stream, error) {
|
||||
select {
|
||||
case stream := <-s.acceptCh:
|
||||
if err := stream.sendWindowUpdate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return stream, nil
|
||||
case <-s.shutdownCh:
|
||||
return nil, s.shutdownErr
|
||||
}
|
||||
}
|
||||
|
||||
// Close is used to close the session and all streams.
|
||||
// Attempts to send a GoAway before closing the connection.
|
||||
func (s *Session) Close() error {
|
||||
s.shutdownLock.Lock()
|
||||
defer s.shutdownLock.Unlock()
|
||||
|
||||
if s.shutdown {
|
||||
return nil
|
||||
}
|
||||
s.shutdown = true
|
||||
if s.shutdownErr == nil {
|
||||
s.shutdownErr = ErrSessionShutdown
|
||||
}
|
||||
close(s.shutdownCh)
|
||||
s.conn.Close()
|
||||
<-s.recvDoneCh
|
||||
|
||||
s.streamLock.Lock()
|
||||
defer s.streamLock.Unlock()
|
||||
for _, stream := range s.streams {
|
||||
stream.forceClose()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// exitErr is used to handle an error that is causing the
|
||||
// session to terminate.
|
||||
func (s *Session) exitErr(err error) {
|
||||
s.shutdownLock.Lock()
|
||||
if s.shutdownErr == nil {
|
||||
s.shutdownErr = err
|
||||
}
|
||||
s.shutdownLock.Unlock()
|
||||
s.Close()
|
||||
}
|
||||
|
||||
// GoAway can be used to prevent accepting further
|
||||
// connections. It does not close the underlying conn.
|
||||
func (s *Session) GoAway() error {
|
||||
return s.waitForSend(s.goAway(goAwayNormal), nil)
|
||||
}
|
||||
|
||||
// goAway is used to send a goAway message
|
||||
func (s *Session) goAway(reason uint32) header {
|
||||
atomic.SwapInt32(&s.localGoAway, 1)
|
||||
hdr := header(make([]byte, headerSize))
|
||||
hdr.encode(typeGoAway, 0, 0, reason)
|
||||
return hdr
|
||||
}
|
||||
|
||||
// Ping is used to measure the RTT response time
|
||||
func (s *Session) Ping() (time.Duration, error) {
|
||||
// Get a channel for the ping
|
||||
ch := make(chan struct{})
|
||||
|
||||
// Get a new ping id, mark as pending
|
||||
s.pingLock.Lock()
|
||||
id := s.pingID
|
||||
s.pingID++
|
||||
s.pings[id] = ch
|
||||
s.pingLock.Unlock()
|
||||
|
||||
// Send the ping request
|
||||
hdr := header(make([]byte, headerSize))
|
||||
hdr.encode(typePing, flagSYN, 0, id)
|
||||
if err := s.waitForSend(hdr, nil); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Wait for a response
|
||||
start := time.Now()
|
||||
select {
|
||||
case <-ch:
|
||||
case <-time.After(s.config.ConnectionWriteTimeout):
|
||||
s.pingLock.Lock()
|
||||
delete(s.pings, id) // Ignore it if a response comes later.
|
||||
s.pingLock.Unlock()
|
||||
return 0, ErrTimeout
|
||||
case <-s.shutdownCh:
|
||||
return 0, ErrSessionShutdown
|
||||
}
|
||||
|
||||
// Compute the RTT
|
||||
return time.Now().Sub(start), nil
|
||||
}
|
||||
|
||||
// keepalive is a long running goroutine that periodically does
|
||||
// a ping to keep the connection alive.
|
||||
func (s *Session) keepalive() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(s.config.KeepAliveInterval):
|
||||
_, err := s.Ping()
|
||||
if err != nil {
|
||||
s.logger.Printf("[ERR] yamux: keepalive failed: %v", err)
|
||||
s.exitErr(ErrKeepAliveTimeout)
|
||||
return
|
||||
}
|
||||
case <-s.shutdownCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// waitForSendErr waits to send a header, checking for a potential shutdown
|
||||
func (s *Session) waitForSend(hdr header, body io.Reader) error {
|
||||
errCh := make(chan error, 1)
|
||||
return s.waitForSendErr(hdr, body, errCh)
|
||||
}
|
||||
|
||||
// waitForSendErr waits to send a header with optional data, checking for a
|
||||
// potential shutdown. Since there's the expectation that sends can happen
|
||||
// in a timely manner, we enforce the connection write timeout here.
|
||||
func (s *Session) waitForSendErr(hdr header, body io.Reader, errCh chan error) error {
|
||||
timer := time.NewTimer(s.config.ConnectionWriteTimeout)
|
||||
defer timer.Stop()
|
||||
|
||||
ready := sendReady{Hdr: hdr, Body: body, Err: errCh}
|
||||
select {
|
||||
case s.sendCh <- ready:
|
||||
case <-s.shutdownCh:
|
||||
return ErrSessionShutdown
|
||||
case <-timer.C:
|
||||
return ErrConnectionWriteTimeout
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
return err
|
||||
case <-s.shutdownCh:
|
||||
return ErrSessionShutdown
|
||||
case <-timer.C:
|
||||
return ErrConnectionWriteTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// sendNoWait does a send without waiting. Since there's the expectation that
|
||||
// the send happens right here, we enforce the connection write timeout if we
|
||||
// can't queue the header to be sent.
|
||||
func (s *Session) sendNoWait(hdr header) error {
|
||||
timer := time.NewTimer(s.config.ConnectionWriteTimeout)
|
||||
defer timer.Stop()
|
||||
|
||||
select {
|
||||
case s.sendCh <- sendReady{Hdr: hdr}:
|
||||
return nil
|
||||
case <-s.shutdownCh:
|
||||
return ErrSessionShutdown
|
||||
case <-timer.C:
|
||||
return ErrConnectionWriteTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// send is a long running goroutine that sends data
|
||||
func (s *Session) send() {
|
||||
for {
|
||||
select {
|
||||
case ready := <-s.sendCh:
|
||||
// Send a header if ready
|
||||
if ready.Hdr != nil {
|
||||
sent := 0
|
||||
for sent < len(ready.Hdr) {
|
||||
n, err := s.conn.Write(ready.Hdr[sent:])
|
||||
if err != nil {
|
||||
s.logger.Printf("[ERR] yamux: Failed to write header: %v", err)
|
||||
asyncSendErr(ready.Err, err)
|
||||
s.exitErr(err)
|
||||
return
|
||||
}
|
||||
sent += n
|
||||
}
|
||||
}
|
||||
|
||||
// Send data from a body if given
|
||||
if ready.Body != nil {
|
||||
_, err := io.Copy(s.conn, ready.Body)
|
||||
if err != nil {
|
||||
s.logger.Printf("[ERR] yamux: Failed to write body: %v", err)
|
||||
asyncSendErr(ready.Err, err)
|
||||
s.exitErr(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// No error, successful send
|
||||
asyncSendErr(ready.Err, nil)
|
||||
case <-s.shutdownCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// recv is a long running goroutine that accepts new data
|
||||
func (s *Session) recv() {
|
||||
if err := s.recvLoop(); err != nil {
|
||||
s.exitErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
// recvLoop continues to receive data until a fatal error is encountered
|
||||
func (s *Session) recvLoop() error {
|
||||
defer close(s.recvDoneCh)
|
||||
hdr := header(make([]byte, headerSize))
|
||||
var handler func(header) error
|
||||
for {
|
||||
// Read the header
|
||||
if _, err := io.ReadFull(s.bufRead, hdr); err != nil {
|
||||
if err != io.EOF && !strings.Contains(err.Error(), "closed") && !strings.Contains(err.Error(), "reset by peer") {
|
||||
s.logger.Printf("[ERR] yamux: Failed to read header: %v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Verify the version
|
||||
if hdr.Version() != protoVersion {
|
||||
s.logger.Printf("[ERR] yamux: Invalid protocol version: %d", hdr.Version())
|
||||
return ErrInvalidVersion
|
||||
}
|
||||
|
||||
// Switch on the type
|
||||
switch hdr.MsgType() {
|
||||
case typeData:
|
||||
handler = s.handleStreamMessage
|
||||
case typeWindowUpdate:
|
||||
handler = s.handleStreamMessage
|
||||
case typeGoAway:
|
||||
handler = s.handleGoAway
|
||||
case typePing:
|
||||
handler = s.handlePing
|
||||
default:
|
||||
return ErrInvalidMsgType
|
||||
}
|
||||
|
||||
// Invoke the handler
|
||||
if err := handler(hdr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleStreamMessage handles either a data or window update frame
|
||||
func (s *Session) handleStreamMessage(hdr header) error {
|
||||
// Check for a new stream creation
|
||||
id := hdr.StreamID()
|
||||
flags := hdr.Flags()
|
||||
if flags&flagSYN == flagSYN {
|
||||
if err := s.incomingStream(id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Get the stream
|
||||
s.streamLock.Lock()
|
||||
stream := s.streams[id]
|
||||
s.streamLock.Unlock()
|
||||
|
||||
// If we do not have a stream, likely we sent a RST
|
||||
if stream == nil {
|
||||
// Drain any data on the wire
|
||||
if hdr.MsgType() == typeData && hdr.Length() > 0 {
|
||||
s.logger.Printf("[WARN] yamux: Discarding data for stream: %d", id)
|
||||
if _, err := io.CopyN(ioutil.Discard, s.bufRead, int64(hdr.Length())); err != nil {
|
||||
s.logger.Printf("[ERR] yamux: Failed to discard data: %v", err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
s.logger.Printf("[WARN] yamux: frame for missing stream: %v", hdr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if this is a window update
|
||||
if hdr.MsgType() == typeWindowUpdate {
|
||||
if err := stream.incrSendWindow(hdr, flags); err != nil {
|
||||
if sendErr := s.sendNoWait(s.goAway(goAwayProtoErr)); sendErr != nil {
|
||||
s.logger.Printf("[WARN] yamux: failed to send go away: %v", sendErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read the new data
|
||||
if err := stream.readData(hdr, flags, s.bufRead); err != nil {
|
||||
if sendErr := s.sendNoWait(s.goAway(goAwayProtoErr)); sendErr != nil {
|
||||
s.logger.Printf("[WARN] yamux: failed to send go away: %v", sendErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handlePing is invokde for a typePing frame
|
||||
func (s *Session) handlePing(hdr header) error {
|
||||
flags := hdr.Flags()
|
||||
pingID := hdr.Length()
|
||||
|
||||
// Check if this is a query, respond back in a separate context so we
|
||||
// don't interfere with the receiving thread blocking for the write.
|
||||
if flags&flagSYN == flagSYN {
|
||||
go func() {
|
||||
hdr := header(make([]byte, headerSize))
|
||||
hdr.encode(typePing, flagACK, 0, pingID)
|
||||
if err := s.sendNoWait(hdr); err != nil {
|
||||
s.logger.Printf("[WARN] yamux: failed to send ping reply: %v", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle a response
|
||||
s.pingLock.Lock()
|
||||
ch := s.pings[pingID]
|
||||
if ch != nil {
|
||||
delete(s.pings, pingID)
|
||||
close(ch)
|
||||
}
|
||||
s.pingLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleGoAway is invokde for a typeGoAway frame
|
||||
func (s *Session) handleGoAway(hdr header) error {
|
||||
code := hdr.Length()
|
||||
switch code {
|
||||
case goAwayNormal:
|
||||
atomic.SwapInt32(&s.remoteGoAway, 1)
|
||||
case goAwayProtoErr:
|
||||
s.logger.Printf("[ERR] yamux: received protocol error go away")
|
||||
return fmt.Errorf("yamux protocol error")
|
||||
case goAwayInternalErr:
|
||||
s.logger.Printf("[ERR] yamux: received internal error go away")
|
||||
return fmt.Errorf("remote yamux internal error")
|
||||
default:
|
||||
s.logger.Printf("[ERR] yamux: received unexpected go away")
|
||||
return fmt.Errorf("unexpected go away received")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// incomingStream is used to create a new incoming stream
|
||||
func (s *Session) incomingStream(id uint32) error {
|
||||
// Reject immediately if we are doing a go away
|
||||
if atomic.LoadInt32(&s.localGoAway) == 1 {
|
||||
hdr := header(make([]byte, headerSize))
|
||||
hdr.encode(typeWindowUpdate, flagRST, id, 0)
|
||||
return s.sendNoWait(hdr)
|
||||
}
|
||||
|
||||
// Allocate a new stream
|
||||
stream := newStream(s, id, streamSYNReceived)
|
||||
|
||||
s.streamLock.Lock()
|
||||
defer s.streamLock.Unlock()
|
||||
|
||||
// Check if stream already exists
|
||||
if _, ok := s.streams[id]; ok {
|
||||
s.logger.Printf("[ERR] yamux: duplicate stream declared")
|
||||
if sendErr := s.sendNoWait(s.goAway(goAwayProtoErr)); sendErr != nil {
|
||||
s.logger.Printf("[WARN] yamux: failed to send go away: %v", sendErr)
|
||||
}
|
||||
return ErrDuplicateStream
|
||||
}
|
||||
|
||||
// Register the stream
|
||||
s.streams[id] = stream
|
||||
|
||||
// Check if we've exceeded the backlog
|
||||
select {
|
||||
case s.acceptCh <- stream:
|
||||
return nil
|
||||
default:
|
||||
// Backlog exceeded! RST the stream
|
||||
s.logger.Printf("[WARN] yamux: backlog exceeded, forcing connection reset")
|
||||
delete(s.streams, id)
|
||||
stream.sendHdr.encode(typeWindowUpdate, flagRST, id, 0)
|
||||
return s.sendNoWait(stream.sendHdr)
|
||||
}
|
||||
}
|
||||
|
||||
// closeStream is used to close a stream once both sides have
|
||||
// issued a close.
|
||||
func (s *Session) closeStream(id uint32) {
|
||||
s.streamLock.Lock()
|
||||
delete(s.streams, id)
|
||||
s.streamLock.Unlock()
|
||||
}
|
||||
|
||||
// establishStream is used to mark a stream that was in the
|
||||
// SYN Sent state as established.
|
||||
func (s *Session) establishStream() {
|
||||
select {
|
||||
case <-s.synCh:
|
||||
default:
|
||||
panic("established stream without inflight syn")
|
||||
}
|
||||
}
|
141
vendor/github.com/hashicorp/yamux/spec.md
generated
vendored
Normal file
141
vendor/github.com/hashicorp/yamux/spec.md
generated
vendored
Normal file
@ -0,0 +1,141 @@
|
||||
# Specification
|
||||
|
||||
We use this document to detail the internal specification of Yamux.
|
||||
This is used both as a guide for implementing Yamux, but also for
|
||||
alternative interoperable libraries to be built.
|
||||
|
||||
# Framing
|
||||
|
||||
Yamux uses a streaming connection underneath, but imposes a message
|
||||
framing so that it can be shared between many logical streams. Each
|
||||
frame contains a header like:
|
||||
|
||||
* Version (8 bits)
|
||||
* Type (8 bits)
|
||||
* Flags (16 bits)
|
||||
* StreamID (32 bits)
|
||||
* Length (32 bits)
|
||||
|
||||
This means that each header has a 12 byte overhead.
|
||||
All fields are encoded in network order (big endian).
|
||||
Each field is described below:
|
||||
|
||||
## Version Field
|
||||
|
||||
The version field is used for future backwards compatibily. At the
|
||||
current time, the field is always set to 0, to indicate the initial
|
||||
version.
|
||||
|
||||
## Type Field
|
||||
|
||||
The type field is used to switch the frame message type. The following
|
||||
message types are supported:
|
||||
|
||||
* 0x0 Data - Used to transmit data. May transmit zero length payloads
|
||||
depending on the flags.
|
||||
|
||||
* 0x1 Window Update - Used to updated the senders receive window size.
|
||||
This is used to implement per-session flow control.
|
||||
|
||||
* 0x2 Ping - Used to measure RTT. It can also be used to heart-beat
|
||||
and do keep-alives over TCP.
|
||||
|
||||
* 0x3 Go Away - Used to close a session.
|
||||
|
||||
## Flag Field
|
||||
|
||||
The flags field is used to provide additional information related
|
||||
to the message type. The following flags are supported:
|
||||
|
||||
* 0x1 SYN - Signals the start of a new stream. May be sent with a data or
|
||||
window update message. Also sent with a ping to indicate outbound.
|
||||
|
||||
* 0x2 ACK - Acknowledges the start of a new stream. May be sent with a data
|
||||
or window update message. Also sent with a ping to indicate response.
|
||||
|
||||
* 0x4 FIN - Performs a half-close of a stream. May be sent with a data
|
||||
message or window update.
|
||||
|
||||
* 0x8 RST - Reset a stream immediately. May be sent with a data or
|
||||
window update message.
|
||||
|
||||
## StreamID Field
|
||||
|
||||
The StreamID field is used to identify the logical stream the frame
|
||||
is addressing. The client side should use odd ID's, and the server even.
|
||||
This prevents any collisions. Additionally, the 0 ID is reserved to represent
|
||||
the session.
|
||||
|
||||
Both Ping and Go Away messages should always use the 0 StreamID.
|
||||
|
||||
## Length Field
|
||||
|
||||
The meaning of the length field depends on the message type:
|
||||
|
||||
* Data - provides the length of bytes following the header
|
||||
* Window update - provides a delta update to the window size
|
||||
* Ping - Contains an opaque value, echoed back
|
||||
* Go Away - Contains an error code
|
||||
|
||||
# Message Flow
|
||||
|
||||
There is no explicit connection setup, as Yamux relies on an underlying
|
||||
transport to be provided. However, there is a distinction between client
|
||||
and server side of the connection.
|
||||
|
||||
## Opening a stream
|
||||
|
||||
To open a stream, an initial data or window update frame is sent
|
||||
with a new StreamID. The SYN flag should be set to signal a new stream.
|
||||
|
||||
The receiver must then reply with either a data or window update frame
|
||||
with the StreamID along with the ACK flag to accept the stream or with
|
||||
the RST flag to reject the stream.
|
||||
|
||||
Because we are relying on the reliable stream underneath, a connection
|
||||
can begin sending data once the SYN flag is sent. The corresponding
|
||||
ACK does not need to be received. This is particularly well suited
|
||||
for an RPC system where a client wants to open a stream and immediately
|
||||
fire a request without wiating for the RTT of the ACK.
|
||||
|
||||
This does introduce the possibility of a connection being rejected
|
||||
after data has been sent already. This is a slight semantic difference
|
||||
from TCP, where the conection cannot be refused after it is opened.
|
||||
Clients should be prepared to handle this by checking for an error
|
||||
that indicates a RST was received.
|
||||
|
||||
## Closing a stream
|
||||
|
||||
To close a stream, either side sends a data or window update frame
|
||||
along with the FIN flag. This does a half-close indicating the sender
|
||||
will send no further data.
|
||||
|
||||
Once both sides have closed the connection, the stream is closed.
|
||||
|
||||
Alternatively, if an error occurs, the RST flag can be used to
|
||||
hard close a stream immediately.
|
||||
|
||||
## Flow Control
|
||||
|
||||
When Yamux is initially starts each stream with a 256KB window size.
|
||||
There is no window size for the session.
|
||||
|
||||
To prevent the streams from stalling, window update frames should be
|
||||
sent regularly. Yamux can be configured to provide a larger limit for
|
||||
windows sizes. Both sides assume the initial 256KB window, but can
|
||||
immediately send a window update as part of the SYN/ACK indicating a
|
||||
larger window.
|
||||
|
||||
Both sides should track the number of bytes sent in Data frames
|
||||
only, as only they are tracked as part of the window size.
|
||||
|
||||
## Session termination
|
||||
|
||||
When a session is being terminated, the Go Away message should
|
||||
be sent. The Length should be set to one of the following to
|
||||
provide an error code:
|
||||
|
||||
* 0x0 Normal termination
|
||||
* 0x1 Protocol error
|
||||
* 0x2 Internal error
|
||||
|
452
vendor/github.com/hashicorp/yamux/stream.go
generated
vendored
Normal file
452
vendor/github.com/hashicorp/yamux/stream.go
generated
vendored
Normal file
@ -0,0 +1,452 @@
|
||||
package yamux
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type streamState int
|
||||
|
||||
const (
|
||||
streamInit streamState = iota
|
||||
streamSYNSent
|
||||
streamSYNReceived
|
||||
streamEstablished
|
||||
streamLocalClose
|
||||
streamRemoteClose
|
||||
streamClosed
|
||||
streamReset
|
||||
)
|
||||
|
||||
// Stream is used to represent a logical stream
|
||||
// within a session.
|
||||
type Stream struct {
|
||||
recvWindow uint32
|
||||
sendWindow uint32
|
||||
|
||||
id uint32
|
||||
session *Session
|
||||
|
||||
state streamState
|
||||
stateLock sync.Mutex
|
||||
|
||||
recvBuf *bytes.Buffer
|
||||
recvLock sync.Mutex
|
||||
|
||||
controlHdr header
|
||||
controlErr chan error
|
||||
controlHdrLock sync.Mutex
|
||||
|
||||
sendHdr header
|
||||
sendErr chan error
|
||||
sendLock sync.Mutex
|
||||
|
||||
recvNotifyCh chan struct{}
|
||||
sendNotifyCh chan struct{}
|
||||
|
||||
readDeadline time.Time
|
||||
writeDeadline time.Time
|
||||
}
|
||||
|
||||
// newStream is used to construct a new stream within
|
||||
// a given session for an ID
|
||||
func newStream(session *Session, id uint32, state streamState) *Stream {
|
||||
s := &Stream{
|
||||
id: id,
|
||||
session: session,
|
||||
state: state,
|
||||
controlHdr: header(make([]byte, headerSize)),
|
||||
controlErr: make(chan error, 1),
|
||||
sendHdr: header(make([]byte, headerSize)),
|
||||
sendErr: make(chan error, 1),
|
||||
recvWindow: initialStreamWindow,
|
||||
sendWindow: initialStreamWindow,
|
||||
recvNotifyCh: make(chan struct{}, 1),
|
||||
sendNotifyCh: make(chan struct{}, 1),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Session returns the associated stream session
|
||||
func (s *Stream) Session() *Session {
|
||||
return s.session
|
||||
}
|
||||
|
||||
// StreamID returns the ID of this stream
|
||||
func (s *Stream) StreamID() uint32 {
|
||||
return s.id
|
||||
}
|
||||
|
||||
// Read is used to read from the stream
|
||||
func (s *Stream) Read(b []byte) (n int, err error) {
|
||||
defer asyncNotify(s.recvNotifyCh)
|
||||
START:
|
||||
s.stateLock.Lock()
|
||||
switch s.state {
|
||||
case streamLocalClose:
|
||||
fallthrough
|
||||
case streamRemoteClose:
|
||||
fallthrough
|
||||
case streamClosed:
|
||||
if s.recvBuf == nil || s.recvBuf.Len() == 0 {
|
||||
s.stateLock.Unlock()
|
||||
return 0, io.EOF
|
||||
}
|
||||
case streamReset:
|
||||
s.stateLock.Unlock()
|
||||
return 0, ErrConnectionReset
|
||||
}
|
||||
s.stateLock.Unlock()
|
||||
|
||||
// If there is no data available, block
|
||||
s.recvLock.Lock()
|
||||
if s.recvBuf == nil || s.recvBuf.Len() == 0 {
|
||||
s.recvLock.Unlock()
|
||||
goto WAIT
|
||||
}
|
||||
|
||||
// Read any bytes
|
||||
n, _ = s.recvBuf.Read(b)
|
||||
s.recvLock.Unlock()
|
||||
|
||||
// Send a window update potentially
|
||||
err = s.sendWindowUpdate()
|
||||
return n, err
|
||||
|
||||
WAIT:
|
||||
var timeout <-chan time.Time
|
||||
if !s.readDeadline.IsZero() {
|
||||
delay := s.readDeadline.Sub(time.Now())
|
||||
timeout = time.After(delay)
|
||||
}
|
||||
select {
|
||||
case <-s.recvNotifyCh:
|
||||
goto START
|
||||
case <-timeout:
|
||||
return 0, ErrTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// Write is used to write to the stream
|
||||
func (s *Stream) Write(b []byte) (n int, err error) {
|
||||
s.sendLock.Lock()
|
||||
defer s.sendLock.Unlock()
|
||||
total := 0
|
||||
for total < len(b) {
|
||||
n, err := s.write(b[total:])
|
||||
total += n
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// write is used to write to the stream, may return on
|
||||
// a short write.
|
||||
func (s *Stream) write(b []byte) (n int, err error) {
|
||||
var flags uint16
|
||||
var max uint32
|
||||
var body io.Reader
|
||||
START:
|
||||
s.stateLock.Lock()
|
||||
switch s.state {
|
||||
case streamLocalClose:
|
||||
fallthrough
|
||||
case streamClosed:
|
||||
s.stateLock.Unlock()
|
||||
return 0, ErrStreamClosed
|
||||
case streamReset:
|
||||
s.stateLock.Unlock()
|
||||
return 0, ErrConnectionReset
|
||||
}
|
||||
s.stateLock.Unlock()
|
||||
|
||||
// If there is no data available, block
|
||||
window := atomic.LoadUint32(&s.sendWindow)
|
||||
if window == 0 {
|
||||
goto WAIT
|
||||
}
|
||||
|
||||
// Determine the flags if any
|
||||
flags = s.sendFlags()
|
||||
|
||||
// Send up to our send window
|
||||
max = min(window, uint32(len(b)))
|
||||
body = bytes.NewReader(b[:max])
|
||||
|
||||
// Send the header
|
||||
s.sendHdr.encode(typeData, flags, s.id, max)
|
||||
if err := s.session.waitForSendErr(s.sendHdr, body, s.sendErr); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Reduce our send window
|
||||
atomic.AddUint32(&s.sendWindow, ^uint32(max-1))
|
||||
|
||||
// Unlock
|
||||
return int(max), err
|
||||
|
||||
WAIT:
|
||||
var timeout <-chan time.Time
|
||||
if !s.writeDeadline.IsZero() {
|
||||
delay := s.writeDeadline.Sub(time.Now())
|
||||
timeout = time.After(delay)
|
||||
}
|
||||
select {
|
||||
case <-s.sendNotifyCh:
|
||||
goto START
|
||||
case <-timeout:
|
||||
return 0, ErrTimeout
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// sendFlags determines any flags that are appropriate
|
||||
// based on the current stream state
|
||||
func (s *Stream) sendFlags() uint16 {
|
||||
s.stateLock.Lock()
|
||||
defer s.stateLock.Unlock()
|
||||
var flags uint16
|
||||
switch s.state {
|
||||
case streamInit:
|
||||
flags |= flagSYN
|
||||
s.state = streamSYNSent
|
||||
case streamSYNReceived:
|
||||
flags |= flagACK
|
||||
s.state = streamEstablished
|
||||
}
|
||||
return flags
|
||||
}
|
||||
|
||||
// sendWindowUpdate potentially sends a window update enabling
|
||||
// further writes to take place. Must be invoked with the lock.
|
||||
func (s *Stream) sendWindowUpdate() error {
|
||||
s.controlHdrLock.Lock()
|
||||
defer s.controlHdrLock.Unlock()
|
||||
|
||||
// Determine the delta update
|
||||
max := s.session.config.MaxStreamWindowSize
|
||||
delta := max - atomic.LoadUint32(&s.recvWindow)
|
||||
|
||||
// Determine the flags if any
|
||||
flags := s.sendFlags()
|
||||
|
||||
// Check if we can omit the update
|
||||
if delta < (max/2) && flags == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update our window
|
||||
atomic.AddUint32(&s.recvWindow, delta)
|
||||
|
||||
// Send the header
|
||||
s.controlHdr.encode(typeWindowUpdate, flags, s.id, delta)
|
||||
if err := s.session.waitForSendErr(s.controlHdr, nil, s.controlErr); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendClose is used to send a FIN
|
||||
func (s *Stream) sendClose() error {
|
||||
s.controlHdrLock.Lock()
|
||||
defer s.controlHdrLock.Unlock()
|
||||
|
||||
flags := s.sendFlags()
|
||||
flags |= flagFIN
|
||||
s.controlHdr.encode(typeWindowUpdate, flags, s.id, 0)
|
||||
if err := s.session.waitForSendErr(s.controlHdr, nil, s.controlErr); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close is used to close the stream
|
||||
func (s *Stream) Close() error {
|
||||
closeStream := false
|
||||
s.stateLock.Lock()
|
||||
switch s.state {
|
||||
// Opened means we need to signal a close
|
||||
case streamSYNSent:
|
||||
fallthrough
|
||||
case streamSYNReceived:
|
||||
fallthrough
|
||||
case streamEstablished:
|
||||
s.state = streamLocalClose
|
||||
goto SEND_CLOSE
|
||||
|
||||
case streamLocalClose:
|
||||
case streamRemoteClose:
|
||||
s.state = streamClosed
|
||||
closeStream = true
|
||||
goto SEND_CLOSE
|
||||
|
||||
case streamClosed:
|
||||
case streamReset:
|
||||
default:
|
||||
panic("unhandled state")
|
||||
}
|
||||
s.stateLock.Unlock()
|
||||
return nil
|
||||
SEND_CLOSE:
|
||||
s.stateLock.Unlock()
|
||||
s.sendClose()
|
||||
s.notifyWaiting()
|
||||
if closeStream {
|
||||
s.session.closeStream(s.id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// forceClose is used for when the session is exiting
|
||||
func (s *Stream) forceClose() {
|
||||
s.stateLock.Lock()
|
||||
s.state = streamClosed
|
||||
s.stateLock.Unlock()
|
||||
s.notifyWaiting()
|
||||
}
|
||||
|
||||
// processFlags is used to update the state of the stream
|
||||
// based on set flags, if any. Lock must be held
|
||||
func (s *Stream) processFlags(flags uint16) error {
|
||||
// Close the stream without holding the state lock
|
||||
closeStream := false
|
||||
defer func() {
|
||||
if closeStream {
|
||||
s.session.closeStream(s.id)
|
||||
}
|
||||
}()
|
||||
|
||||
s.stateLock.Lock()
|
||||
defer s.stateLock.Unlock()
|
||||
if flags&flagACK == flagACK {
|
||||
if s.state == streamSYNSent {
|
||||
s.state = streamEstablished
|
||||
}
|
||||
s.session.establishStream()
|
||||
}
|
||||
if flags&flagFIN == flagFIN {
|
||||
switch s.state {
|
||||
case streamSYNSent:
|
||||
fallthrough
|
||||
case streamSYNReceived:
|
||||
fallthrough
|
||||
case streamEstablished:
|
||||
s.state = streamRemoteClose
|
||||
s.notifyWaiting()
|
||||
case streamLocalClose:
|
||||
s.state = streamClosed
|
||||
closeStream = true
|
||||
s.notifyWaiting()
|
||||
default:
|
||||
s.session.logger.Printf("[ERR] yamux: unexpected FIN flag in state %d", s.state)
|
||||
return ErrUnexpectedFlag
|
||||
}
|
||||
}
|
||||
if flags&flagRST == flagRST {
|
||||
if s.state == streamSYNSent {
|
||||
s.session.establishStream()
|
||||
}
|
||||
s.state = streamReset
|
||||
closeStream = true
|
||||
s.notifyWaiting()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// notifyWaiting notifies all the waiting channels
|
||||
func (s *Stream) notifyWaiting() {
|
||||
asyncNotify(s.recvNotifyCh)
|
||||
asyncNotify(s.sendNotifyCh)
|
||||
}
|
||||
|
||||
// incrSendWindow updates the size of our send window
|
||||
func (s *Stream) incrSendWindow(hdr header, flags uint16) error {
|
||||
if err := s.processFlags(flags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Increase window, unblock a sender
|
||||
atomic.AddUint32(&s.sendWindow, hdr.Length())
|
||||
asyncNotify(s.sendNotifyCh)
|
||||
return nil
|
||||
}
|
||||
|
||||
// readData is used to handle a data frame
|
||||
func (s *Stream) readData(hdr header, flags uint16, conn io.Reader) error {
|
||||
if err := s.processFlags(flags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check that our recv window is not exceeded
|
||||
length := hdr.Length()
|
||||
if length == 0 {
|
||||
return nil
|
||||
}
|
||||
if remain := atomic.LoadUint32(&s.recvWindow); length > remain {
|
||||
s.session.logger.Printf("[ERR] yamux: receive window exceeded (stream: %d, remain: %d, recv: %d)", s.id, remain, length)
|
||||
return ErrRecvWindowExceeded
|
||||
}
|
||||
|
||||
// Wrap in a limited reader
|
||||
conn = &io.LimitedReader{R: conn, N: int64(length)}
|
||||
|
||||
// Copy into buffer
|
||||
s.recvLock.Lock()
|
||||
if s.recvBuf == nil {
|
||||
// Allocate the receive buffer just-in-time to fit the full data frame.
|
||||
// This way we can read in the whole packet without further allocations.
|
||||
s.recvBuf = bytes.NewBuffer(make([]byte, 0, length))
|
||||
}
|
||||
if _, err := io.Copy(s.recvBuf, conn); err != nil {
|
||||
s.session.logger.Printf("[ERR] yamux: Failed to read stream data: %v", err)
|
||||
s.recvLock.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// Decrement the receive window
|
||||
atomic.AddUint32(&s.recvWindow, ^uint32(length-1))
|
||||
s.recvLock.Unlock()
|
||||
|
||||
// Unblock any readers
|
||||
asyncNotify(s.recvNotifyCh)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDeadline sets the read and write deadlines
|
||||
func (s *Stream) SetDeadline(t time.Time) error {
|
||||
if err := s.SetReadDeadline(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.SetWriteDeadline(t); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetReadDeadline sets the deadline for future Read calls.
|
||||
func (s *Stream) SetReadDeadline(t time.Time) error {
|
||||
s.readDeadline = t
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetWriteDeadline sets the deadline for future Write calls
|
||||
func (s *Stream) SetWriteDeadline(t time.Time) error {
|
||||
s.writeDeadline = t
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shrink is used to compact the amount of buffers utilized
|
||||
// This is useful when using Yamux in a connection pool to reduce
|
||||
// the idle memory utilization.
|
||||
func (s *Stream) Shrink() {
|
||||
s.recvLock.Lock()
|
||||
if s.recvBuf != nil && s.recvBuf.Len() == 0 {
|
||||
s.recvBuf = nil
|
||||
}
|
||||
s.recvLock.Unlock()
|
||||
}
|
28
vendor/github.com/hashicorp/yamux/util.go
generated
vendored
Normal file
28
vendor/github.com/hashicorp/yamux/util.go
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
package yamux
|
||||
|
||||
// asyncSendErr is used to try an async send of an error
|
||||
func asyncSendErr(ch chan error, err error) {
|
||||
if ch == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case ch <- err:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// asyncNotify is used to signal a waiting goroutine
|
||||
func asyncNotify(ch chan struct{}) {
|
||||
select {
|
||||
case ch <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// min computes the minimum of two values
|
||||
func min(a, b uint32) uint32 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
27
vendor/github.com/kr/fs/LICENSE
generated
vendored
Normal file
27
vendor/github.com/kr/fs/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
Copyright (c) 2012 The Go Authors. 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.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
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
|
||||
OWNER 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.
|
3
vendor/github.com/kr/fs/Readme
generated
vendored
Normal file
3
vendor/github.com/kr/fs/Readme
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
Filesystem Package
|
||||
|
||||
http://godoc.org/github.com/kr/fs
|
36
vendor/github.com/kr/fs/filesystem.go
generated
vendored
Normal file
36
vendor/github.com/kr/fs/filesystem.go
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// FileSystem defines the methods of an abstract filesystem.
|
||||
type FileSystem interface {
|
||||
|
||||
// ReadDir reads the directory named by dirname and returns a
|
||||
// list of directory entries.
|
||||
ReadDir(dirname string) ([]os.FileInfo, error)
|
||||
|
||||
// Lstat returns a FileInfo describing the named file. If the file is a
|
||||
// symbolic link, the returned FileInfo describes the symbolic link. Lstat
|
||||
// makes no attempt to follow the link.
|
||||
Lstat(name string) (os.FileInfo, error)
|
||||
|
||||
// Join joins any number of path elements into a single path, adding a
|
||||
// separator if necessary. The result is Cleaned; in particular, all
|
||||
// empty strings are ignored.
|
||||
//
|
||||
// The separator is FileSystem specific.
|
||||
Join(elem ...string) string
|
||||
}
|
||||
|
||||
// fs represents a FileSystem provided by the os package.
|
||||
type fs struct{}
|
||||
|
||||
func (f *fs) ReadDir(dirname string) ([]os.FileInfo, error) { return ioutil.ReadDir(dirname) }
|
||||
|
||||
func (f *fs) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
|
||||
|
||||
func (f *fs) Join(elem ...string) string { return filepath.Join(elem...) }
|
95
vendor/github.com/kr/fs/walk.go
generated
vendored
Normal file
95
vendor/github.com/kr/fs/walk.go
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
// Package fs provides filesystem-related functions.
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Walker provides a convenient interface for iterating over the
|
||||
// descendants of a filesystem path.
|
||||
// Successive calls to the Step method will step through each
|
||||
// file or directory in the tree, including the root. The files
|
||||
// are walked in lexical order, which makes the output deterministic
|
||||
// but means that for very large directories Walker can be inefficient.
|
||||
// Walker does not follow symbolic links.
|
||||
type Walker struct {
|
||||
fs FileSystem
|
||||
cur item
|
||||
stack []item
|
||||
descend bool
|
||||
}
|
||||
|
||||
type item struct {
|
||||
path string
|
||||
info os.FileInfo
|
||||
err error
|
||||
}
|
||||
|
||||
// Walk returns a new Walker rooted at root.
|
||||
func Walk(root string) *Walker {
|
||||
return WalkFS(root, new(fs))
|
||||
}
|
||||
|
||||
// WalkFS returns a new Walker rooted at root on the FileSystem fs.
|
||||
func WalkFS(root string, fs FileSystem) *Walker {
|
||||
info, err := fs.Lstat(root)
|
||||
return &Walker{
|
||||
fs: fs,
|
||||
stack: []item{{root, info, err}},
|
||||
}
|
||||
}
|
||||
|
||||
// Step advances the Walker to the next file or directory,
|
||||
// which will then be available through the Path, Stat,
|
||||
// and Err methods.
|
||||
// It returns false when the walk stops at the end of the tree.
|
||||
func (w *Walker) Step() bool {
|
||||
if w.descend && w.cur.err == nil && w.cur.info.IsDir() {
|
||||
list, err := w.fs.ReadDir(w.cur.path)
|
||||
if err != nil {
|
||||
w.cur.err = err
|
||||
w.stack = append(w.stack, w.cur)
|
||||
} else {
|
||||
for i := len(list) - 1; i >= 0; i-- {
|
||||
path := w.fs.Join(w.cur.path, list[i].Name())
|
||||
w.stack = append(w.stack, item{path, list[i], nil})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(w.stack) == 0 {
|
||||
return false
|
||||
}
|
||||
i := len(w.stack) - 1
|
||||
w.cur = w.stack[i]
|
||||
w.stack = w.stack[:i]
|
||||
w.descend = true
|
||||
return true
|
||||
}
|
||||
|
||||
// Path returns the path to the most recent file or directory
|
||||
// visited by a call to Step. It contains the argument to Walk
|
||||
// as a prefix; that is, if Walk is called with "dir", which is
|
||||
// a directory containing the file "a", Path will return "dir/a".
|
||||
func (w *Walker) Path() string {
|
||||
return w.cur.path
|
||||
}
|
||||
|
||||
// Stat returns info for the most recent file or directory
|
||||
// visited by a call to Step.
|
||||
func (w *Walker) Stat() os.FileInfo {
|
||||
return w.cur.info
|
||||
}
|
||||
|
||||
// Err returns the error, if any, for the most recent attempt
|
||||
// by Step to visit a file or directory. If a directory has
|
||||
// an error, w will not descend into that directory.
|
||||
func (w *Walker) Err() error {
|
||||
return w.cur.err
|
||||
}
|
||||
|
||||
// SkipDir causes the currently visited directory to be skipped.
|
||||
// If w is not on a directory, SkipDir has no effect.
|
||||
func (w *Walker) SkipDir() {
|
||||
w.descend = false
|
||||
}
|
202
vendor/github.com/masterzen/simplexml/LICENSE
generated
vendored
Normal file
202
vendor/github.com/masterzen/simplexml/LICENSE
generated
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
35
vendor/github.com/masterzen/simplexml/dom/document.go
generated
vendored
Normal file
35
vendor/github.com/masterzen/simplexml/dom/document.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package dom
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
root *Element
|
||||
PrettyPrint bool
|
||||
Indentation string
|
||||
DocType bool
|
||||
}
|
||||
|
||||
func CreateDocument() *Document {
|
||||
return &Document{ PrettyPrint: false, Indentation: " ", DocType: true }
|
||||
}
|
||||
|
||||
func (doc *Document) SetRoot(node *Element) {
|
||||
node.parent = nil
|
||||
doc.root = node
|
||||
}
|
||||
|
||||
func (doc *Document) String() string {
|
||||
var b bytes.Buffer
|
||||
if doc.DocType {
|
||||
fmt.Fprintln(&b, `<?xml version="1.0" encoding="utf-8" ?>`)
|
||||
}
|
||||
|
||||
if doc.root != nil {
|
||||
doc.root.Bytes(&b, doc.PrettyPrint, doc.Indentation, 0)
|
||||
}
|
||||
|
||||
return string(b.Bytes())
|
||||
}
|
200
vendor/github.com/masterzen/simplexml/dom/element.go
generated
vendored
Normal file
200
vendor/github.com/masterzen/simplexml/dom/element.go
generated
vendored
Normal file
@ -0,0 +1,200 @@
|
||||
package dom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Attr struct {
|
||||
Name xml.Name // Attribute namespace and name.
|
||||
Value string // Attribute value.
|
||||
}
|
||||
|
||||
type Element struct {
|
||||
name xml.Name
|
||||
children []*Element
|
||||
parent *Element
|
||||
content string
|
||||
attributes []*Attr
|
||||
namespaces []*Namespace
|
||||
document *Document
|
||||
}
|
||||
|
||||
func CreateElement(n string) *Element {
|
||||
element := &Element { name: xml.Name { Local: n } }
|
||||
element.children = make([]*Element, 0, 5)
|
||||
element.attributes = make([]*Attr, 0, 10)
|
||||
element.namespaces = make([]*Namespace, 0, 10)
|
||||
return element
|
||||
}
|
||||
|
||||
func (node *Element) AddChild(child *Element) *Element {
|
||||
if child.parent != nil {
|
||||
child.parent.RemoveChild(child)
|
||||
}
|
||||
child.SetParent(node)
|
||||
node.children = append(node.children, child)
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Element) RemoveChild(child *Element) *Element {
|
||||
p := -1
|
||||
for i, v := range node.children {
|
||||
if v == child {
|
||||
p = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if p == -1 {
|
||||
return node
|
||||
}
|
||||
|
||||
copy(node.children[p:], node.children[p+1:])
|
||||
node.children = node.children[0 : len(node.children)-1]
|
||||
child.parent = nil
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Element) SetAttr(name string, value string) *Element {
|
||||
// namespaces?
|
||||
attr := &Attr{ Name: xml.Name { Local: name }, Value: value }
|
||||
node.attributes = append(node.attributes, attr)
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Element) SetParent(parent *Element) *Element {
|
||||
node.parent = parent
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Element) SetContent(content string) *Element {
|
||||
node.content = content
|
||||
return node
|
||||
}
|
||||
|
||||
// Add a namespace declaration to this node
|
||||
func (node *Element) DeclareNamespace(ns Namespace) *Element {
|
||||
// check if we already have it
|
||||
prefix := node.namespacePrefix(ns.Uri)
|
||||
if prefix == ns.Prefix {
|
||||
return node
|
||||
}
|
||||
// add it
|
||||
node.namespaces = append(node.namespaces, &ns)
|
||||
return node
|
||||
}
|
||||
|
||||
func (node *Element) DeclaredNamespaces() []*Namespace {
|
||||
return node.namespaces
|
||||
}
|
||||
|
||||
func (node *Element) SetNamespace(prefix string, uri string) {
|
||||
resolved := node.namespacePrefix(uri)
|
||||
if resolved == "" {
|
||||
// we couldn't find the namespace, let's declare it at this node
|
||||
node.namespaces = append(node.namespaces, &Namespace { Prefix: prefix, Uri: uri })
|
||||
}
|
||||
node.name.Space = uri
|
||||
}
|
||||
|
||||
func (node *Element) Bytes(out *bytes.Buffer, indent bool, indentType string, level int) {
|
||||
empty := len(node.children) == 0 && node.content == ""
|
||||
content := node.content != ""
|
||||
// children := len(node.children) > 0
|
||||
// ns := len(node.namespaces) > 0
|
||||
// attrs := len(node.attributes) > 0
|
||||
|
||||
indentStr := ""
|
||||
nextLine := ""
|
||||
if indent {
|
||||
nextLine = "\n"
|
||||
for i := 0; i < level; i++ {
|
||||
indentStr += indentType
|
||||
}
|
||||
}
|
||||
|
||||
if node.name.Local != "" {
|
||||
if len(node.name.Space) > 0 {
|
||||
// first find if ns has been declared, otherwise
|
||||
prefix := node.namespacePrefix(node.name.Space)
|
||||
fmt.Fprintf(out, "%s<%s:%s", indentStr, prefix, node.name.Local)
|
||||
} else {
|
||||
fmt.Fprintf(out, "%s<%s", indentStr, node.name.Local)
|
||||
}
|
||||
}
|
||||
|
||||
// declared namespaces
|
||||
for _, v := range node.namespaces {
|
||||
prefix := node.namespacePrefix(v.Uri)
|
||||
fmt.Fprintf(out, ` xmlns:%s="%s"`, prefix, v.Uri)
|
||||
}
|
||||
|
||||
// attributes
|
||||
for _, v := range node.attributes {
|
||||
if len(v.Name.Space) > 0 {
|
||||
prefix := node.namespacePrefix(v.Name.Space)
|
||||
fmt.Fprintf(out, ` %s:%s="%s"`, prefix, v.Name.Local, v.Value)
|
||||
} else {
|
||||
fmt.Fprintf(out, ` %s="%s"`, v.Name.Local, v.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// close tag
|
||||
if empty {
|
||||
fmt.Fprintf(out, "/>%s", nextLine)
|
||||
} else {
|
||||
if content {
|
||||
out.WriteRune('>')
|
||||
} else {
|
||||
fmt.Fprintf(out, ">%s", nextLine)
|
||||
}
|
||||
}
|
||||
|
||||
if len(node.children) > 0 {
|
||||
for _, child := range node.children {
|
||||
child.Bytes(out, indent, indentType, level + 1)
|
||||
}
|
||||
} else if node.content != "" {
|
||||
//val := []byte(node.content)
|
||||
//xml.EscapeText(out, val)
|
||||
out.WriteString(node.content)
|
||||
}
|
||||
|
||||
if !empty && len(node.name.Local) > 0 {
|
||||
var indentation string
|
||||
if content {
|
||||
indentation = ""
|
||||
} else {
|
||||
indentation = indentStr
|
||||
}
|
||||
if len(node.name.Space) > 0 {
|
||||
prefix := node.namespacePrefix(node.name.Space)
|
||||
fmt.Fprintf(out, "%s</%s:%s>\n", indentation, prefix, node.name.Local)
|
||||
} else {
|
||||
fmt.Fprintf(out, "%s</%s>\n", indentation, node.name.Local)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Finds the prefix of the given namespace if it has been declared
|
||||
// in this node or in one of its parent
|
||||
func (node *Element) namespacePrefix(uri string) string {
|
||||
for _, ns := range node.namespaces {
|
||||
if ns.Uri == uri {
|
||||
return ns.Prefix
|
||||
}
|
||||
}
|
||||
if node.parent == nil {
|
||||
return ""
|
||||
}
|
||||
return node.parent.namespacePrefix(uri)
|
||||
}
|
||||
|
||||
|
||||
func (node *Element) String() string {
|
||||
var b bytes.Buffer
|
||||
node.Bytes(&b, false, "", 0)
|
||||
return string(b.Bytes())
|
||||
}
|
10
vendor/github.com/masterzen/simplexml/dom/namespace.go
generated
vendored
Normal file
10
vendor/github.com/masterzen/simplexml/dom/namespace.go
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package dom
|
||||
|
||||
type Namespace struct {
|
||||
Prefix string
|
||||
Uri string
|
||||
}
|
||||
|
||||
func (ns *Namespace) SetTo(node *Element) {
|
||||
node.SetNamespace(ns.Prefix, ns.Uri)
|
||||
}
|
202
vendor/github.com/masterzen/winrm/LICENSE
generated
vendored
Normal file
202
vendor/github.com/masterzen/winrm/LICENSE
generated
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
181
vendor/github.com/masterzen/winrm/soap/header.go
generated
vendored
Normal file
181
vendor/github.com/masterzen/winrm/soap/header.go
generated
vendored
Normal file
@ -0,0 +1,181 @@
|
||||
package soap
|
||||
|
||||
import (
|
||||
"github.com/masterzen/simplexml/dom"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type HeaderOption struct {
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
func NewHeaderOption(name string, value string) *HeaderOption {
|
||||
return &HeaderOption{key: name, value: value}
|
||||
}
|
||||
|
||||
type SoapHeader struct {
|
||||
to string
|
||||
replyTo string
|
||||
maxEnvelopeSize string
|
||||
timeout string
|
||||
locale string
|
||||
id string
|
||||
action string
|
||||
shellId string
|
||||
resourceURI string
|
||||
options []HeaderOption
|
||||
message *SoapMessage
|
||||
}
|
||||
|
||||
type HeaderBuilder interface {
|
||||
To(string) *SoapHeader
|
||||
ReplyTo(string) *SoapHeader
|
||||
MaxEnvelopeSize(int) *SoapHeader
|
||||
Timeout(string) *SoapHeader
|
||||
Locale(string) *SoapHeader
|
||||
Id(string) *SoapHeader
|
||||
Action(string) *SoapHeader
|
||||
ShellId(string) *SoapHeader
|
||||
resourceURI(string) *SoapHeader
|
||||
AddOption(*HeaderOption) *SoapHeader
|
||||
Options([]HeaderOption) *SoapHeader
|
||||
Build(*SoapMessage) *SoapMessage
|
||||
}
|
||||
|
||||
func (self *SoapHeader) To(uri string) *SoapHeader {
|
||||
self.to = uri
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) ReplyTo(uri string) *SoapHeader {
|
||||
self.replyTo = uri
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) MaxEnvelopeSize(size int) *SoapHeader {
|
||||
self.maxEnvelopeSize = strconv.Itoa(size)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Timeout(timeout string) *SoapHeader {
|
||||
self.timeout = timeout
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Id(id string) *SoapHeader {
|
||||
self.id = id
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Action(action string) *SoapHeader {
|
||||
self.action = action
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Locale(locale string) *SoapHeader {
|
||||
self.locale = locale
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) ShellId(shellId string) *SoapHeader {
|
||||
self.shellId = shellId
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) ResourceURI(resourceURI string) *SoapHeader {
|
||||
self.resourceURI = resourceURI
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) AddOption(option *HeaderOption) *SoapHeader {
|
||||
self.options = append(self.options, *option)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Options(options []HeaderOption) *SoapHeader {
|
||||
self.options = options
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *SoapHeader) Build() *SoapMessage {
|
||||
header := self.createElement(self.message.envelope, "Header", NS_SOAP_ENV)
|
||||
|
||||
if self.to != "" {
|
||||
to := self.createElement(header, "To", NS_ADDRESSING)
|
||||
to.SetContent(self.to)
|
||||
}
|
||||
|
||||
if self.replyTo != "" {
|
||||
replyTo := self.createElement(header, "ReplyTo", NS_ADDRESSING)
|
||||
a := self.createMUElement(replyTo, "Address", NS_ADDRESSING, true)
|
||||
a.SetContent(self.replyTo)
|
||||
}
|
||||
|
||||
if self.maxEnvelopeSize != "" {
|
||||
envelope := self.createMUElement(header, "MaxEnvelopeSize", NS_WSMAN_DMTF, true)
|
||||
envelope.SetContent(self.maxEnvelopeSize)
|
||||
}
|
||||
|
||||
if self.timeout != "" {
|
||||
timeout := self.createElement(header, "OperationTimeout", NS_WSMAN_DMTF)
|
||||
timeout.SetContent(self.timeout)
|
||||
}
|
||||
|
||||
if self.id != "" {
|
||||
id := self.createElement(header, "MessageID", NS_ADDRESSING)
|
||||
id.SetContent(self.id)
|
||||
}
|
||||
|
||||
if self.locale != "" {
|
||||
locale := self.createMUElement(header, "Locale", NS_WSMAN_DMTF, false)
|
||||
locale.SetAttr("xml:lang", self.locale)
|
||||
datalocale := self.createMUElement(header, "DataLocale", NS_WSMAN_MSFT, false)
|
||||
datalocale.SetAttr("xml:lang", self.locale)
|
||||
}
|
||||
|
||||
if self.action != "" {
|
||||
action := self.createMUElement(header, "Action", NS_ADDRESSING, true)
|
||||
action.SetContent(self.action)
|
||||
}
|
||||
|
||||
if self.shellId != "" {
|
||||
selectorSet := self.createElement(header, "SelectorSet", NS_WSMAN_DMTF)
|
||||
selector := self.createElement(selectorSet, "Selector", NS_WSMAN_DMTF)
|
||||
selector.SetAttr("Name", "ShellId")
|
||||
selector.SetContent(self.shellId)
|
||||
}
|
||||
|
||||
if self.resourceURI != "" {
|
||||
resource := self.createMUElement(header, "ResourceURI", NS_WSMAN_DMTF, true)
|
||||
resource.SetContent(self.resourceURI)
|
||||
}
|
||||
|
||||
if len(self.options) > 0 {
|
||||
set := self.createElement(header, "OptionSet", NS_WSMAN_DMTF)
|
||||
for _, option := range self.options {
|
||||
e := self.createElement(set, "Option", NS_WSMAN_DMTF)
|
||||
e.SetAttr("Name", option.key)
|
||||
e.SetContent(option.value)
|
||||
}
|
||||
}
|
||||
|
||||
return self.message
|
||||
}
|
||||
|
||||
func (self *SoapHeader) createElement(parent *dom.Element, name string, ns dom.Namespace) (element *dom.Element) {
|
||||
element = dom.CreateElement(name)
|
||||
parent.AddChild(element)
|
||||
ns.SetTo(element)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *SoapHeader) createMUElement(parent *dom.Element, name string, ns dom.Namespace, mustUnderstand bool) (element *dom.Element) {
|
||||
element = self.createElement(parent, name, ns)
|
||||
value := "false"
|
||||
if mustUnderstand {
|
||||
value = "true"
|
||||
}
|
||||
element.SetAttr("mustUnderstand", value)
|
||||
return
|
||||
}
|
74
vendor/github.com/masterzen/winrm/soap/message.go
generated
vendored
Normal file
74
vendor/github.com/masterzen/winrm/soap/message.go
generated
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
package soap
|
||||
|
||||
import (
|
||||
"github.com/masterzen/simplexml/dom"
|
||||
)
|
||||
|
||||
type SoapMessage struct {
|
||||
document *dom.Document
|
||||
envelope *dom.Element
|
||||
header *SoapHeader
|
||||
body *dom.Element
|
||||
}
|
||||
|
||||
type MessageBuilder interface {
|
||||
SetBody(*dom.Element)
|
||||
NewBody() *dom.Element
|
||||
CreateElement(*dom.Element, string, dom.Namespace) *dom.Element
|
||||
CreateBodyElement(string, dom.Namespace) *dom.Element
|
||||
Header() *SoapHeader
|
||||
Doc() *dom.Document
|
||||
Free()
|
||||
|
||||
String() string
|
||||
}
|
||||
|
||||
func NewMessage() (message *SoapMessage) {
|
||||
doc := dom.CreateDocument()
|
||||
e := dom.CreateElement("Envelope")
|
||||
doc.SetRoot(e)
|
||||
AddUsualNamespaces(e)
|
||||
NS_SOAP_ENV.SetTo(e)
|
||||
|
||||
message = &SoapMessage{document: doc, envelope: e}
|
||||
return
|
||||
}
|
||||
|
||||
func (message *SoapMessage) NewBody() (body *dom.Element) {
|
||||
body = dom.CreateElement("Body")
|
||||
message.envelope.AddChild(body)
|
||||
NS_SOAP_ENV.SetTo(body)
|
||||
return
|
||||
}
|
||||
|
||||
func (message *SoapMessage) String() string {
|
||||
return message.document.String()
|
||||
}
|
||||
|
||||
func (message *SoapMessage) Doc() *dom.Document {
|
||||
return message.document
|
||||
}
|
||||
|
||||
func (message *SoapMessage) Free() {
|
||||
}
|
||||
|
||||
func (message *SoapMessage) CreateElement(parent *dom.Element, name string, ns dom.Namespace) (element *dom.Element) {
|
||||
element = dom.CreateElement(name)
|
||||
parent.AddChild(element)
|
||||
ns.SetTo(element)
|
||||
return
|
||||
}
|
||||
|
||||
func (message *SoapMessage) CreateBodyElement(name string, ns dom.Namespace) (element *dom.Element) {
|
||||
if message.body == nil {
|
||||
message.body = message.NewBody()
|
||||
}
|
||||
return message.CreateElement(message.body, name, ns)
|
||||
}
|
||||
|
||||
func (message *SoapMessage) Header() *SoapHeader {
|
||||
if message.header == nil {
|
||||
message.header = &SoapHeader{message: message}
|
||||
}
|
||||
return message.header
|
||||
}
|
37
vendor/github.com/masterzen/winrm/soap/namespaces.go
generated
vendored
Normal file
37
vendor/github.com/masterzen/winrm/soap/namespaces.go
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package soap
|
||||
|
||||
import (
|
||||
"github.com/masterzen/simplexml/dom"
|
||||
"github.com/masterzen/xmlpath"
|
||||
)
|
||||
|
||||
var (
|
||||
NS_SOAP_ENV = dom.Namespace{"env", "http://www.w3.org/2003/05/soap-envelope"}
|
||||
NS_ADDRESSING = dom.Namespace{"a", "http://schemas.xmlsoap.org/ws/2004/08/addressing"}
|
||||
NS_CIMBINDING = dom.Namespace{"b", "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"}
|
||||
NS_ENUM = dom.Namespace{"n", "http://schemas.xmlsoap.org/ws/2004/09/enumeration"}
|
||||
NS_TRANSFER = dom.Namespace{"x", "http://schemas.xmlsoap.org/ws/2004/09/transfer"}
|
||||
NS_WSMAN_DMTF = dom.Namespace{"w", "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"}
|
||||
NS_WSMAN_MSFT = dom.Namespace{"p", "http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"}
|
||||
NS_SCHEMA_INST = dom.Namespace{"xsi", "http://www.w3.org/2001/XMLSchema-instance"}
|
||||
NS_WIN_SHELL = dom.Namespace{"rsp", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell"}
|
||||
NS_WSMAN_FAULT = dom.Namespace{"f", "http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"}
|
||||
)
|
||||
|
||||
var MostUsed = [...]dom.Namespace{NS_SOAP_ENV, NS_ADDRESSING, NS_WIN_SHELL, NS_WSMAN_DMTF, NS_WSMAN_MSFT}
|
||||
|
||||
func AddUsualNamespaces(node *dom.Element) {
|
||||
for _, ns := range MostUsed {
|
||||
node.DeclareNamespace(ns)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllNamespaces() []xmlpath.Namespace {
|
||||
var ns = []dom.Namespace{NS_WIN_SHELL, NS_ADDRESSING, NS_WSMAN_DMTF, NS_WSMAN_MSFT, NS_SOAP_ENV}
|
||||
|
||||
var xmlpathNs = make([]xmlpath.Namespace, 0, 4)
|
||||
for _, namespace := range ns {
|
||||
xmlpathNs = append(xmlpathNs, xmlpath.Namespace{Prefix: namespace.Prefix, Uri: namespace.Uri})
|
||||
}
|
||||
return xmlpathNs
|
||||
}
|
166
vendor/github.com/masterzen/winrm/winrm/client.go
generated
vendored
Normal file
166
vendor/github.com/masterzen/winrm/winrm/client.go
generated
vendored
Normal file
@ -0,0 +1,166 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/masterzen/winrm/soap"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Parameters
|
||||
username string
|
||||
password string
|
||||
useHTTPS bool
|
||||
url string
|
||||
http HttpPost
|
||||
transport http.RoundTripper
|
||||
}
|
||||
|
||||
// NewClient will create a new remote client on url, connecting with user and password
|
||||
// This function doesn't connect (connection happens only when CreateShell is called)
|
||||
func NewClient(endpoint *Endpoint, user, password string) (client *Client, err error) {
|
||||
params := DefaultParameters()
|
||||
client, err = NewClientWithParameters(endpoint, user, password, params)
|
||||
return
|
||||
}
|
||||
|
||||
// NewClient will create a new remote client on url, connecting with user and password
|
||||
// This function doesn't connect (connection happens only when CreateShell is called)
|
||||
func NewClientWithParameters(endpoint *Endpoint, user, password string, params *Parameters) (client *Client, err error) {
|
||||
transport, err := newTransport(endpoint)
|
||||
|
||||
client = &Client{
|
||||
Parameters: *params,
|
||||
username: user,
|
||||
password: password,
|
||||
url: endpoint.url(),
|
||||
http: Http_post,
|
||||
useHTTPS: endpoint.HTTPS,
|
||||
transport: transport,
|
||||
}
|
||||
|
||||
if params.TransportDecorator != nil {
|
||||
client.transport = params.TransportDecorator(transport)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// newTransport will create a new HTTP Transport, with options specified within the endpoint configuration
|
||||
func newTransport(endpoint *Endpoint) (*http.Transport, error) {
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: endpoint.Insecure,
|
||||
},
|
||||
}
|
||||
|
||||
if endpoint.CACert != nil && len(*endpoint.CACert) > 0 {
|
||||
certPool, err := readCACerts(endpoint.CACert)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
transport.TLSClientConfig.RootCAs = certPool
|
||||
}
|
||||
|
||||
return transport, nil
|
||||
}
|
||||
|
||||
func readCACerts(certs *[]byte) (*x509.CertPool, error) {
|
||||
certPool := x509.NewCertPool()
|
||||
|
||||
if !certPool.AppendCertsFromPEM(*certs) {
|
||||
return nil, fmt.Errorf("Unable to read certificates")
|
||||
}
|
||||
|
||||
return certPool, nil
|
||||
}
|
||||
|
||||
// CreateShell will create a WinRM Shell, which is the prealable for running
|
||||
// commands.
|
||||
func (client *Client) CreateShell() (shell *Shell, err error) {
|
||||
request := NewOpenShellRequest(client.url, &client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
response, err := client.sendRequest(request)
|
||||
if err == nil {
|
||||
var shellId string
|
||||
if shellId, err = ParseOpenShellResponse(response); err == nil {
|
||||
shell = &Shell{client: client, ShellId: shellId}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) sendRequest(request *soap.SoapMessage) (response string, err error) {
|
||||
return client.http(client, request)
|
||||
}
|
||||
|
||||
// Run will run command on the the remote host, writing the process stdout and stderr to
|
||||
// the given writers. Note with this method it isn't possible to inject stdin.
|
||||
func (client *Client) Run(command string, stdout io.Writer, stderr io.Writer) (exitCode int, err error) {
|
||||
shell, err := client.CreateShell()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var cmd *Command
|
||||
cmd, err = shell.Execute(command)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
go io.Copy(stdout, cmd.Stdout)
|
||||
go io.Copy(stderr, cmd.Stderr)
|
||||
cmd.Wait()
|
||||
shell.Close()
|
||||
return cmd.ExitCode(), cmd.err
|
||||
}
|
||||
|
||||
// Run will run command on the the remote host, returning the process stdout and stderr
|
||||
// as strings, and using the input stdin string as the process input
|
||||
func (client *Client) RunWithString(command string, stdin string) (stdout string, stderr string, exitCode int, err error) {
|
||||
shell, err := client.CreateShell()
|
||||
if err != nil {
|
||||
return "", "", 0, err
|
||||
}
|
||||
defer shell.Close()
|
||||
var cmd *Command
|
||||
cmd, err = shell.Execute(command)
|
||||
if err != nil {
|
||||
return "", "", 0, err
|
||||
}
|
||||
if len(stdin) > 0 {
|
||||
cmd.Stdin.Write([]byte(stdin))
|
||||
}
|
||||
var outWriter, errWriter bytes.Buffer
|
||||
go io.Copy(&outWriter, cmd.Stdout)
|
||||
go io.Copy(&errWriter, cmd.Stderr)
|
||||
cmd.Wait()
|
||||
return outWriter.String(), errWriter.String(), cmd.ExitCode(), cmd.err
|
||||
}
|
||||
|
||||
// Run will run command on the the remote host, writing the process stdout and stderr to
|
||||
// the given writers, and injecting the process stdin with the stdin reader.
|
||||
// Warning stdin (not stdout/stderr) are bufferized, which means reading only one byte in stdin will
|
||||
// send a winrm http packet to the remote host. If stdin is a pipe, it might be better for
|
||||
// performance reasons to buffer it.
|
||||
func (client *Client) RunWithInput(command string, stdout io.Writer, stderr io.Writer, stdin io.Reader) (exitCode int, err error) {
|
||||
shell, err := client.CreateShell()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer shell.Close()
|
||||
var cmd *Command
|
||||
cmd, err = shell.Execute(command)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
go io.Copy(cmd.Stdin, stdin)
|
||||
go io.Copy(stdout, cmd.Stdout)
|
||||
go io.Copy(stderr, cmd.Stderr)
|
||||
cmd.Wait()
|
||||
return cmd.ExitCode(), cmd.err
|
||||
}
|
210
vendor/github.com/masterzen/winrm/winrm/command.go
generated
vendored
Normal file
210
vendor/github.com/masterzen/winrm/winrm/command.go
generated
vendored
Normal file
@ -0,0 +1,210 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type commandWriter struct {
|
||||
*Command
|
||||
eof bool
|
||||
}
|
||||
|
||||
type commandReader struct {
|
||||
*Command
|
||||
write *io.PipeWriter
|
||||
read *io.PipeReader
|
||||
stream string
|
||||
}
|
||||
|
||||
// Command represents a given command running on a Shell. This structure allows to get access
|
||||
// to the various stdout, stderr and stdin pipes.
|
||||
type Command struct {
|
||||
client *Client
|
||||
shell *Shell
|
||||
commandId string
|
||||
exitCode int
|
||||
finished bool
|
||||
err error
|
||||
|
||||
Stdin *commandWriter
|
||||
Stdout *commandReader
|
||||
Stderr *commandReader
|
||||
|
||||
done chan struct{}
|
||||
cancel chan struct{}
|
||||
}
|
||||
|
||||
func newCommand(shell *Shell, commandId string) *Command {
|
||||
command := &Command{shell: shell, client: shell.client, commandId: commandId, exitCode: 1, err: nil, done: make(chan struct{}), cancel: make(chan struct{})}
|
||||
command.Stdin = &commandWriter{Command: command, eof: false}
|
||||
command.Stdout = newCommandReader("stdout", command)
|
||||
command.Stderr = newCommandReader("stderr", command)
|
||||
|
||||
go fetchOutput(command)
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
func newCommandReader(stream string, command *Command) *commandReader {
|
||||
read, write := io.Pipe()
|
||||
return &commandReader{Command: command, stream: stream, write: write, read: read}
|
||||
}
|
||||
|
||||
func fetchOutput(command *Command) {
|
||||
for {
|
||||
select {
|
||||
case <-command.cancel:
|
||||
close(command.done)
|
||||
return
|
||||
default:
|
||||
finished, err := command.slurpAllOutput()
|
||||
if finished {
|
||||
command.err = err
|
||||
close(command.done)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (command *Command) check() (err error) {
|
||||
if command.commandId == "" {
|
||||
return errors.New("Command has already been closed")
|
||||
}
|
||||
if command.shell == nil {
|
||||
return errors.New("Command has no associated shell")
|
||||
}
|
||||
if command.client == nil {
|
||||
return errors.New("Command has no associated client")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close will terminate the running command
|
||||
func (command *Command) Close() (err error) {
|
||||
if err = command.check(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
select { // close cancel channel if it's still open
|
||||
case <-command.cancel:
|
||||
default:
|
||||
close(command.cancel)
|
||||
}
|
||||
|
||||
request := NewSignalRequest(command.client.url, command.shell.ShellId, command.commandId, &command.client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
_, err = command.client.sendRequest(request)
|
||||
return err
|
||||
}
|
||||
|
||||
func (command *Command) slurpAllOutput() (finished bool, err error) {
|
||||
if err = command.check(); err != nil {
|
||||
command.Stderr.write.CloseWithError(err)
|
||||
command.Stdout.write.CloseWithError(err)
|
||||
return true, err
|
||||
}
|
||||
|
||||
request := NewGetOutputRequest(command.client.url, command.shell.ShellId, command.commandId, "stdout stderr", &command.client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
response, err := command.client.sendRequest(request)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "OperationTimeout") {
|
||||
// Operation timeout because there was no command output
|
||||
return
|
||||
}
|
||||
|
||||
command.Stderr.write.CloseWithError(err)
|
||||
command.Stdout.write.CloseWithError(err)
|
||||
return true, err
|
||||
}
|
||||
|
||||
var exitCode int
|
||||
var stdout, stderr bytes.Buffer
|
||||
finished, exitCode, err = ParseSlurpOutputErrResponse(response, &stdout, &stderr)
|
||||
if err != nil {
|
||||
command.Stderr.write.CloseWithError(err)
|
||||
command.Stdout.write.CloseWithError(err)
|
||||
return true, err
|
||||
}
|
||||
if stdout.Len() > 0 {
|
||||
command.Stdout.write.Write(stdout.Bytes())
|
||||
}
|
||||
if stderr.Len() > 0 {
|
||||
command.Stderr.write.Write(stderr.Bytes())
|
||||
}
|
||||
if finished {
|
||||
command.exitCode = exitCode
|
||||
command.Stderr.write.Close()
|
||||
command.Stdout.write.Close()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (command *Command) sendInput(data []byte) (err error) {
|
||||
if err = command.check(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request := NewSendInputRequest(command.client.url, command.shell.ShellId, command.commandId, data, &command.client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
_, err = command.client.sendRequest(request)
|
||||
return
|
||||
}
|
||||
|
||||
// ExitCode returns command exit code when it is finished. Before that the result is always 0.
|
||||
func (command *Command) ExitCode() int {
|
||||
return command.exitCode
|
||||
}
|
||||
|
||||
// Calling this function will block the current goroutine until the remote command terminates.
|
||||
func (command *Command) Wait() {
|
||||
// block until finished
|
||||
<-command.done
|
||||
}
|
||||
|
||||
// Write data to this Pipe
|
||||
func (w *commandWriter) Write(data []byte) (written int, err error) {
|
||||
for len(data) > 0 {
|
||||
if w.eof {
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
// never send more data than our EnvelopeSize.
|
||||
n := min(w.client.Parameters.EnvelopeSize-1000, len(data))
|
||||
if err = w.sendInput(data[:n]); err != nil {
|
||||
break
|
||||
}
|
||||
data = data[n:]
|
||||
written += int(n)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func min(a int, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (w *commandWriter) Close() error {
|
||||
w.eof = true
|
||||
return w.Close()
|
||||
}
|
||||
|
||||
// Read data from this Pipe
|
||||
func (r *commandReader) Read(buf []byte) (int, error) {
|
||||
n, err := r.read.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return 0, err
|
||||
}
|
||||
return n, err
|
||||
}
|
22
vendor/github.com/masterzen/winrm/winrm/endpoint.go
generated
vendored
Normal file
22
vendor/github.com/masterzen/winrm/winrm/endpoint.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package winrm
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Endpoint struct {
|
||||
Host string
|
||||
Port int
|
||||
HTTPS bool
|
||||
Insecure bool
|
||||
CACert *[]byte
|
||||
}
|
||||
|
||||
func (ep *Endpoint) url() string {
|
||||
var scheme string
|
||||
if ep.HTTPS {
|
||||
scheme = "https"
|
||||
} else {
|
||||
scheme = "http"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s://%s:%d/wsman", scheme, ep.Host, ep.Port)
|
||||
}
|
60
vendor/github.com/masterzen/winrm/winrm/http.go
generated
vendored
Normal file
60
vendor/github.com/masterzen/winrm/winrm/http.go
generated
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/masterzen/winrm/soap"
|
||||
)
|
||||
|
||||
var soapXML string = "application/soap+xml"
|
||||
|
||||
type HttpPost func(*Client, *soap.SoapMessage) (string, error)
|
||||
|
||||
func body(response *http.Response) (content string, err error) {
|
||||
contentType := response.Header.Get("Content-Type")
|
||||
if strings.HasPrefix(contentType, soapXML) {
|
||||
var body []byte
|
||||
body, err = ioutil.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error while reading request body %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
content = string(body)
|
||||
return
|
||||
} else {
|
||||
err = fmt.Errorf("invalid content-type: %s", contentType)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Http_post(client *Client, request *soap.SoapMessage) (response string, err error) {
|
||||
httpClient := &http.Client{Transport: client.transport}
|
||||
|
||||
req, err := http.NewRequest("POST", client.url, strings.NewReader(request.String()))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("impossible to create http request %s", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", soapXML+";charset=UTF-8")
|
||||
req.SetBasicAuth(client.username, client.password)
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unknown error %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if resp.StatusCode == 200 {
|
||||
response, err = body(resp)
|
||||
} else {
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
err = fmt.Errorf("http error: %d - %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
20
vendor/github.com/masterzen/winrm/winrm/parameters.go
generated
vendored
Normal file
20
vendor/github.com/masterzen/winrm/winrm/parameters.go
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Parameters struct {
|
||||
Timeout string
|
||||
Locale string
|
||||
EnvelopeSize int
|
||||
TransportDecorator func(*http.Transport) http.RoundTripper
|
||||
}
|
||||
|
||||
func DefaultParameters() *Parameters {
|
||||
return NewParameters("PT60S", "en-US", 153600)
|
||||
}
|
||||
|
||||
func NewParameters(timeout string, locale string, envelopeSize int) *Parameters {
|
||||
return &Parameters{Timeout: timeout, Locale: locale, EnvelopeSize: envelopeSize}
|
||||
}
|
22
vendor/github.com/masterzen/winrm/winrm/powershell.go
generated
vendored
Normal file
22
vendor/github.com/masterzen/winrm/winrm/powershell.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Wraps a PowerShell script and prepares it for execution by the winrm client
|
||||
func Powershell(psCmd string) string {
|
||||
// 2 byte chars to make PowerShell happy
|
||||
wideCmd := ""
|
||||
for _, b := range []byte(psCmd) {
|
||||
wideCmd += string(b) + "\x00"
|
||||
}
|
||||
|
||||
// Base64 encode the command
|
||||
input := []uint8(wideCmd)
|
||||
encodedCmd := base64.StdEncoding.EncodeToString(input)
|
||||
|
||||
// Create the powershell.exe command line to execute the script
|
||||
return fmt.Sprintf("powershell.exe -EncodedCommand %s", encodedCmd)
|
||||
}
|
116
vendor/github.com/masterzen/winrm/winrm/request.go
generated
vendored
Normal file
116
vendor/github.com/masterzen/winrm/winrm/request.go
generated
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/masterzen/winrm/soap"
|
||||
"github.com/nu7hatch/gouuid"
|
||||
)
|
||||
|
||||
func genUUID() string {
|
||||
uuid, _ := uuid.NewV4()
|
||||
return "uuid:" + uuid.String()
|
||||
}
|
||||
|
||||
func defaultHeaders(message *soap.SoapMessage, url string, params *Parameters) (h *soap.SoapHeader) {
|
||||
h = message.Header()
|
||||
h.To(url).ReplyTo("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous").MaxEnvelopeSize(params.EnvelopeSize).Id(genUUID()).Locale(params.Locale).Timeout(params.Timeout)
|
||||
return
|
||||
}
|
||||
|
||||
func NewOpenShellRequest(uri string, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Create").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").AddOption(soap.NewHeaderOption("WINRS_NOPROFILE", "FALSE")).AddOption(soap.NewHeaderOption("WINRS_CODEPAGE", "65001")).Build()
|
||||
|
||||
body := message.CreateBodyElement("Shell", soap.NS_WIN_SHELL)
|
||||
input := message.CreateElement(body, "InputStreams", soap.NS_WIN_SHELL)
|
||||
input.SetContent("stdin")
|
||||
output := message.CreateElement(body, "OutputStreams", soap.NS_WIN_SHELL)
|
||||
output.SetContent("stdout stderr")
|
||||
return
|
||||
}
|
||||
|
||||
func NewDeleteShellRequest(uri string, shellId string, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete").ShellId(shellId).ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").Build()
|
||||
|
||||
message.NewBody()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func NewExecuteCommandRequest(uri, shellId, command string, arguments []string, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).AddOption(soap.NewHeaderOption("WINRS_CONSOLEMODE_STDIN", "TRUE")).AddOption(soap.NewHeaderOption("WINRS_SKIP_CMD_SHELL", "FALSE")).Build()
|
||||
body := message.CreateBodyElement("CommandLine", soap.NS_WIN_SHELL)
|
||||
|
||||
// ensure special characters like & don't mangle the request XML
|
||||
command = "<![CDATA[" + command + "]]>"
|
||||
commandElement := message.CreateElement(body, "Command", soap.NS_WIN_SHELL)
|
||||
commandElement.SetContent(command)
|
||||
|
||||
for _, arg := range arguments {
|
||||
arg = "<![CDATA[" + arg + "]]>"
|
||||
argumentsElement := message.CreateElement(body, "Arguments", soap.NS_WIN_SHELL)
|
||||
argumentsElement.SetContent(arg)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func NewGetOutputRequest(uri string, shellId string, commandId string, streams string, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
|
||||
|
||||
receive := message.CreateBodyElement("Receive", soap.NS_WIN_SHELL)
|
||||
desiredStreams := message.CreateElement(receive, "DesiredStream", soap.NS_WIN_SHELL)
|
||||
desiredStreams.SetAttr("CommandId", commandId)
|
||||
desiredStreams.SetContent(streams)
|
||||
return
|
||||
}
|
||||
|
||||
func NewSendInputRequest(uri string, shellId string, commandId string, input []byte, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
|
||||
|
||||
content := base64.StdEncoding.EncodeToString(input)
|
||||
|
||||
send := message.CreateBodyElement("Send", soap.NS_WIN_SHELL)
|
||||
streams := message.CreateElement(send, "Stream", soap.NS_WIN_SHELL)
|
||||
streams.SetAttr("Name", "stdin")
|
||||
streams.SetAttr("CommandId", commandId)
|
||||
streams.SetContent(content)
|
||||
return
|
||||
}
|
||||
|
||||
func NewSignalRequest(uri string, shellId string, commandId string, params *Parameters) (message *soap.SoapMessage) {
|
||||
if params == nil {
|
||||
params = DefaultParameters()
|
||||
}
|
||||
message = soap.NewMessage()
|
||||
|
||||
defaultHeaders(message, uri, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
|
||||
|
||||
signal := message.CreateBodyElement("Signal", soap.NS_WIN_SHELL)
|
||||
signal.SetAttr("CommandId", commandId)
|
||||
code := message.CreateElement(signal, "Code", soap.NS_WIN_SHELL)
|
||||
code.SetContent("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")
|
||||
|
||||
return
|
||||
}
|
111
vendor/github.com/masterzen/winrm/winrm/response.go
generated
vendored
Normal file
111
vendor/github.com/masterzen/winrm/winrm/response.go
generated
vendored
Normal file
@ -0,0 +1,111 @@
|
||||
package winrm
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"github.com/masterzen/winrm/soap"
|
||||
"github.com/masterzen/xmlpath"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func first(node *xmlpath.Node, xpath string) (content string, err error) {
|
||||
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
content, _ = path.String(node)
|
||||
return
|
||||
}
|
||||
|
||||
func any(node *xmlpath.Node, xpath string) (found bool, err error) {
|
||||
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
found = path.Exists(node)
|
||||
return
|
||||
}
|
||||
|
||||
func xpath(node *xmlpath.Node, xpath string) (nodes []xmlpath.Node, err error) {
|
||||
path, err := xmlpath.CompileWithNamespace(xpath, soap.GetAllNamespaces())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
nodes = make([]xmlpath.Node, 0, 1)
|
||||
iter := path.Iter(node)
|
||||
for iter.Next() {
|
||||
nodes = append(nodes, *(iter.Node()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ParseOpenShellResponse(response string) (shellId string, err error) {
|
||||
doc, err := xmlpath.Parse(strings.NewReader(response))
|
||||
|
||||
shellId, err = first(doc, "//w:Selector[@Name='ShellId']")
|
||||
return
|
||||
}
|
||||
|
||||
func ParseExecuteCommandResponse(response string) (commandId string, err error) {
|
||||
doc, err := xmlpath.Parse(strings.NewReader(response))
|
||||
|
||||
commandId, err = first(doc, "//rsp:CommandId")
|
||||
return
|
||||
}
|
||||
|
||||
func ParseSlurpOutputErrResponse(response string, stdout io.Writer, stderr io.Writer) (finished bool, exitCode int, err error) {
|
||||
doc, err := xmlpath.Parse(strings.NewReader(response))
|
||||
|
||||
stdouts, _ := xpath(doc, "//rsp:Stream[@Name='stdout']")
|
||||
for _, node := range stdouts {
|
||||
content, _ := base64.StdEncoding.DecodeString(node.String())
|
||||
stdout.Write(content)
|
||||
}
|
||||
stderrs, _ := xpath(doc, "//rsp:Stream[@Name='stderr']")
|
||||
for _, node := range stderrs {
|
||||
content, _ := base64.StdEncoding.DecodeString(node.String())
|
||||
stderr.Write(content)
|
||||
}
|
||||
|
||||
ended, _ := any(doc, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")
|
||||
|
||||
if ended {
|
||||
finished = ended
|
||||
if exitBool, _ := any(doc, "//rsp:ExitCode"); exitBool {
|
||||
exit, _ := first(doc, "//rsp:ExitCode")
|
||||
exitCode, _ = strconv.Atoi(exit)
|
||||
}
|
||||
} else {
|
||||
finished = false
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ParseSlurpOutputResponse(response string, stream io.Writer, streamType string) (finished bool, exitCode int, err error) {
|
||||
doc, err := xmlpath.Parse(strings.NewReader(response))
|
||||
|
||||
nodes, _ := xpath(doc, fmt.Sprintf("//rsp:Stream[@Name='%s']", streamType))
|
||||
for _, node := range nodes {
|
||||
content, _ := base64.StdEncoding.DecodeString(node.String())
|
||||
stream.Write(content)
|
||||
}
|
||||
|
||||
ended, _ := any(doc, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")
|
||||
|
||||
if ended {
|
||||
finished = ended
|
||||
if exitBool, _ := any(doc, "//rsp:ExitCode"); exitBool {
|
||||
exit, _ := first(doc, "//rsp:ExitCode")
|
||||
exitCode, _ = strconv.Atoi(exit)
|
||||
}
|
||||
} else {
|
||||
finished = false
|
||||
}
|
||||
|
||||
return
|
||||
}
|
31
vendor/github.com/masterzen/winrm/winrm/shell.go
generated
vendored
Normal file
31
vendor/github.com/masterzen/winrm/winrm/shell.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
package winrm
|
||||
|
||||
// Shell is the local view of a WinRM Shell of a given Client
|
||||
type Shell struct {
|
||||
client *Client
|
||||
ShellId string
|
||||
}
|
||||
|
||||
// Execute command on the given Shell, returning either an error or a Command
|
||||
func (shell *Shell) Execute(command string, arguments ...string) (cmd *Command, err error) {
|
||||
request := NewExecuteCommandRequest(shell.client.url, shell.ShellId, command, arguments, &shell.client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
response, err := shell.client.sendRequest(request)
|
||||
if err == nil {
|
||||
var commandId string
|
||||
if commandId, err = ParseExecuteCommandResponse(response); err == nil {
|
||||
cmd = newCommand(shell, commandId)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close will terminate this shell. No commands can be issued once the shell is closed.
|
||||
func (shell *Shell) Close() (err error) {
|
||||
request := NewDeleteShellRequest(shell.client.url, shell.ShellId, &shell.client.Parameters)
|
||||
defer request.Free()
|
||||
|
||||
_, err = shell.client.sendRequest(request)
|
||||
return
|
||||
}
|
185
vendor/github.com/masterzen/xmlpath/LICENSE
generated
vendored
Normal file
185
vendor/github.com/masterzen/xmlpath/LICENSE
generated
vendored
Normal file
@ -0,0 +1,185 @@
|
||||
This software is licensed under the LGPLv3, included below.
|
||||
|
||||
As a special exception to the GNU Lesser General Public License version 3
|
||||
("LGPL3"), the copyright holders of this Library give you permission to
|
||||
convey to a third party a Combined Work that links statically or dynamically
|
||||
to this Library without providing any Minimal Corresponding Source or
|
||||
Minimal Application Code as set out in 4d or providing the installation
|
||||
information set out in section 4e, provided that you comply with the other
|
||||
provisions of LGPL3 and provided that you meet, for the Application the
|
||||
terms and conditions of the license(s) which apply to the Application.
|
||||
|
||||
Except as stated in this special exception, the provisions of LGPL3 will
|
||||
continue to comply in full to this Library. If you modify this Library, you
|
||||
may apply this exception to your version of this Library, but you are not
|
||||
obliged to do so. If you do not wish to do so, delete this exception
|
||||
statement from your version. This exception does not (and cannot) modify any
|
||||
license terms which apply to the Application, with which you must still
|
||||
comply.
|
||||
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
95
vendor/github.com/masterzen/xmlpath/doc.go
generated
vendored
Normal file
95
vendor/github.com/masterzen/xmlpath/doc.go
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
// Package xmlpath implements a strict subset of the XPath specification for the Go language.
|
||||
//
|
||||
// The XPath specification is available at:
|
||||
//
|
||||
// http://www.w3.org/TR/xpath
|
||||
//
|
||||
// Path expressions supported by this package are in the following format,
|
||||
// with all components being optional:
|
||||
//
|
||||
// /axis-name::node-test[predicate]/axis-name::node-test[predicate]
|
||||
//
|
||||
// At the moment, xmlpath is compatible with the XPath specification
|
||||
// to the following extent:
|
||||
//
|
||||
// - All axes are supported ("child", "following-sibling", etc)
|
||||
// - All abbreviated forms are supported (".", "//", etc)
|
||||
// - All node types except for namespace are supported
|
||||
// - Predicates are restricted to [N], [path], and [path=literal] forms
|
||||
// - Only a single predicate is supported per path step
|
||||
// - Namespaces are experimentally supported
|
||||
// - Richer expressions
|
||||
//
|
||||
// For example, assuming the following document:
|
||||
//
|
||||
// <library>
|
||||
// <!-- Great book. -->
|
||||
// <book id="b0836217462" available="true">
|
||||
// <isbn>0836217462</isbn>
|
||||
// <title lang="en">Being a Dog Is a Full-Time Job</title>
|
||||
// <quote>I'd dog paddle the deepest ocean.</quote>
|
||||
// <author id="CMS">
|
||||
// <?echo "go rocks"?>
|
||||
// <name>Charles M Schulz</name>
|
||||
// <born>1922-11-26</born>
|
||||
// <dead>2000-02-12</dead>
|
||||
// </author>
|
||||
// <character id="PP">
|
||||
// <name>Peppermint Patty</name>
|
||||
// <born>1966-08-22</born>
|
||||
// <qualification>bold, brash and tomboyish</qualification>
|
||||
// </character>
|
||||
// <character id="Snoopy">
|
||||
// <name>Snoopy</name>
|
||||
// <born>1950-10-04</born>
|
||||
// <qualification>extroverted beagle</qualification>
|
||||
// </character>
|
||||
// </book>
|
||||
// </library>
|
||||
//
|
||||
// The following examples are valid path expressions, and the first
|
||||
// match has the indicated value:
|
||||
//
|
||||
// /library/book/isbn => "0836217462"
|
||||
// library/*/isbn => "0836217462"
|
||||
// /library/book/../book/./isbn => "0836217462"
|
||||
// /library/book/character[2]/name => "Snoopy"
|
||||
// /library/book/character[born='1950-10-04']/name => "Snoopy"
|
||||
// /library/book//node()[@id='PP']/name => "Peppermint Patty"
|
||||
// //book[author/@id='CMS']/title => "Being a Dog Is a Full-Time Job"},
|
||||
// /library/book/preceding::comment() => " Great book. "
|
||||
//
|
||||
// To run an expression, compile it, and then apply the compiled path to any
|
||||
// number of context nodes, from one or more parsed xml documents:
|
||||
//
|
||||
// path := xmlpath.MustCompile("/library/book/isbn")
|
||||
// root, err := xmlpath.Parse(file)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// if value, ok := path.String(root); ok {
|
||||
// fmt.Println("Found:", value)
|
||||
// }
|
||||
//
|
||||
// To use xmlpath with namespaces, it is required to give the supported set of namespace
|
||||
// when compiling:
|
||||
//
|
||||
//
|
||||
// var namespaces = []xmlpath.Namespace {
|
||||
// { "s", "http://www.w3.org/2003/05/soap-envelope" },
|
||||
// { "a", "http://schemas.xmlsoap.org/ws/2004/08/addressing" },
|
||||
// }
|
||||
// path, err := xmlpath.CompileWithNamespace("/s:Header/a:To", namespaces)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// root, err := xmlpath.Parse(file)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// if value, ok := path.String(root); ok {
|
||||
// fmt.Println("Found:", value)
|
||||
// }
|
||||
//
|
||||
|
||||
package xmlpath
|
233
vendor/github.com/masterzen/xmlpath/parser.go
generated
vendored
Normal file
233
vendor/github.com/masterzen/xmlpath/parser.go
generated
vendored
Normal file
@ -0,0 +1,233 @@
|
||||
package xmlpath
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Node is an item in an xml tree that was compiled to
|
||||
// be processed via xml paths. A node may represent:
|
||||
//
|
||||
// - An element in the xml document (<body>)
|
||||
// - An attribute of an element in the xml document (href="...")
|
||||
// - A comment in the xml document (<!--...-->)
|
||||
// - A processing instruction in the xml document (<?...?>)
|
||||
// - Some text within the xml document
|
||||
//
|
||||
type Node struct {
|
||||
kind nodeKind
|
||||
name xml.Name
|
||||
attr string
|
||||
text []byte
|
||||
|
||||
nodes []Node
|
||||
pos int
|
||||
end int
|
||||
|
||||
up *Node
|
||||
down []*Node
|
||||
}
|
||||
|
||||
type nodeKind int
|
||||
|
||||
const (
|
||||
anyNode nodeKind = iota
|
||||
startNode
|
||||
endNode
|
||||
attrNode
|
||||
textNode
|
||||
commentNode
|
||||
procInstNode
|
||||
)
|
||||
|
||||
// String returns the string value of node.
|
||||
//
|
||||
// The string value of a node is:
|
||||
//
|
||||
// - For element nodes, the concatenation of all text nodes within the element.
|
||||
// - For text nodes, the text itself.
|
||||
// - For attribute nodes, the attribute value.
|
||||
// - For comment nodes, the text within the comment delimiters.
|
||||
// - For processing instruction nodes, the content of the instruction.
|
||||
//
|
||||
func (node *Node) String() string {
|
||||
if node.kind == attrNode {
|
||||
return node.attr
|
||||
}
|
||||
return string(node.Bytes())
|
||||
}
|
||||
|
||||
// Bytes returns the string value of node as a byte slice.
|
||||
// See Node.String for a description of what the string value of a node is.
|
||||
func (node *Node) Bytes() []byte {
|
||||
if node.kind == attrNode {
|
||||
return []byte(node.attr)
|
||||
}
|
||||
if node.kind != startNode {
|
||||
return node.text
|
||||
}
|
||||
var text []byte
|
||||
for i := node.pos; i < node.end; i++ {
|
||||
if node.nodes[i].kind == textNode {
|
||||
text = append(text, node.nodes[i].text...)
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
// equals returns whether the string value of node is equal to s,
|
||||
// without allocating memory.
|
||||
func (node *Node) equals(s string) bool {
|
||||
if node.kind == attrNode {
|
||||
return s == node.attr
|
||||
}
|
||||
if node.kind != startNode {
|
||||
if len(s) != len(node.text) {
|
||||
return false
|
||||
}
|
||||
for i := range s {
|
||||
if s[i] != node.text[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
si := 0
|
||||
for i := node.pos; i < node.end; i++ {
|
||||
if node.nodes[i].kind == textNode {
|
||||
for _, c := range node.nodes[i].text {
|
||||
if si > len(s) {
|
||||
return false
|
||||
}
|
||||
if s[si] != c {
|
||||
return false
|
||||
}
|
||||
si++
|
||||
}
|
||||
}
|
||||
}
|
||||
return si == len(s)
|
||||
}
|
||||
|
||||
// Parse reads an xml document from r, parses it, and returns its root node.
|
||||
func Parse(r io.Reader) (*Node, error) {
|
||||
return ParseDecoder(xml.NewDecoder(r))
|
||||
}
|
||||
|
||||
// ParseHTML reads an HTML-like document from r, parses it, and returns
|
||||
// its root node.
|
||||
func ParseHTML(r io.Reader) (*Node, error) {
|
||||
d := xml.NewDecoder(r)
|
||||
d.Strict = false
|
||||
d.AutoClose = xml.HTMLAutoClose
|
||||
d.Entity = xml.HTMLEntity
|
||||
return ParseDecoder(d)
|
||||
}
|
||||
|
||||
// ParseDecoder parses the xml document being decoded by d and returns
|
||||
// its root node.
|
||||
func ParseDecoder(d *xml.Decoder) (*Node, error) {
|
||||
var nodes []Node
|
||||
var text []byte
|
||||
|
||||
// The root node.
|
||||
nodes = append(nodes, Node{kind: startNode})
|
||||
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch t := t.(type) {
|
||||
case xml.EndElement:
|
||||
nodes = append(nodes, Node{
|
||||
kind: endNode,
|
||||
})
|
||||
case xml.StartElement:
|
||||
nodes = append(nodes, Node{
|
||||
kind: startNode,
|
||||
name: t.Name,
|
||||
})
|
||||
for _, attr := range t.Attr {
|
||||
nodes = append(nodes, Node{
|
||||
kind: attrNode,
|
||||
name: attr.Name,
|
||||
attr: attr.Value,
|
||||
})
|
||||
}
|
||||
case xml.CharData:
|
||||
texti := len(text)
|
||||
text = append(text, t...)
|
||||
nodes = append(nodes, Node{
|
||||
kind: textNode,
|
||||
text: text[texti : texti+len(t)],
|
||||
})
|
||||
case xml.Comment:
|
||||
texti := len(text)
|
||||
text = append(text, t...)
|
||||
nodes = append(nodes, Node{
|
||||
kind: commentNode,
|
||||
text: text[texti : texti+len(t)],
|
||||
})
|
||||
case xml.ProcInst:
|
||||
texti := len(text)
|
||||
text = append(text, t.Inst...)
|
||||
nodes = append(nodes, Node{
|
||||
kind: procInstNode,
|
||||
name: xml.Name{Local: t.Target},
|
||||
text: text[texti : texti+len(t.Inst)],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Close the root node.
|
||||
nodes = append(nodes, Node{kind: endNode})
|
||||
|
||||
stack := make([]*Node, 0, len(nodes))
|
||||
downs := make([]*Node, len(nodes))
|
||||
downCount := 0
|
||||
|
||||
for pos := range nodes {
|
||||
|
||||
switch nodes[pos].kind {
|
||||
|
||||
case startNode, attrNode, textNode, commentNode, procInstNode:
|
||||
node := &nodes[pos]
|
||||
node.nodes = nodes
|
||||
node.pos = pos
|
||||
if len(stack) > 0 {
|
||||
node.up = stack[len(stack)-1]
|
||||
}
|
||||
if node.kind == startNode {
|
||||
stack = append(stack, node)
|
||||
} else {
|
||||
node.end = pos + 1
|
||||
}
|
||||
|
||||
case endNode:
|
||||
node := stack[len(stack)-1]
|
||||
node.end = pos
|
||||
stack = stack[:len(stack)-1]
|
||||
|
||||
// Compute downs. Doing that here is what enables the
|
||||
// use of a slice of a contiguous pre-allocated block.
|
||||
node.down = downs[downCount:downCount]
|
||||
for i := node.pos + 1; i < node.end; i++ {
|
||||
if nodes[i].up == node {
|
||||
switch nodes[i].kind {
|
||||
case startNode, textNode, commentNode, procInstNode:
|
||||
node.down = append(node.down, &nodes[i])
|
||||
downCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(stack) == 0 {
|
||||
return node, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, io.EOF
|
||||
}
|
642
vendor/github.com/masterzen/xmlpath/path.go
generated
vendored
Normal file
642
vendor/github.com/masterzen/xmlpath/path.go
generated
vendored
Normal file
@ -0,0 +1,642 @@
|
||||
package xmlpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Namespace represents a given XML Namespace
|
||||
type Namespace struct {
|
||||
Prefix string
|
||||
Uri string
|
||||
}
|
||||
|
||||
// Path is a compiled path that can be applied to a context
|
||||
// node to obtain a matching node set.
|
||||
// A single Path can be applied concurrently to any number
|
||||
// of context nodes.
|
||||
type Path struct {
|
||||
path string
|
||||
steps []pathStep
|
||||
}
|
||||
|
||||
// Iter returns an iterator that goes over the list of nodes
|
||||
// that p matches on the given context.
|
||||
func (p *Path) Iter(context *Node) *Iter {
|
||||
iter := Iter{
|
||||
make([]pathStepState, len(p.steps)),
|
||||
make([]bool, len(context.nodes)),
|
||||
}
|
||||
for i := range p.steps {
|
||||
iter.state[i].step = &p.steps[i]
|
||||
}
|
||||
iter.state[0].init(context)
|
||||
return &iter
|
||||
}
|
||||
|
||||
// Exists returns whether any nodes match p on the given context.
|
||||
func (p *Path) Exists(context *Node) bool {
|
||||
return p.Iter(context).Next()
|
||||
}
|
||||
|
||||
// String returns the string value of the first node matched
|
||||
// by p on the given context.
|
||||
//
|
||||
// See the documentation of Node.String.
|
||||
func (p *Path) String(context *Node) (s string, ok bool) {
|
||||
iter := p.Iter(context)
|
||||
if iter.Next() {
|
||||
return iter.Node().String(), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Bytes returns as a byte slice the string value of the first
|
||||
// node matched by p on the given context.
|
||||
//
|
||||
// See the documentation of Node.String.
|
||||
func (p *Path) Bytes(node *Node) (b []byte, ok bool) {
|
||||
iter := p.Iter(node)
|
||||
if iter.Next() {
|
||||
return iter.Node().Bytes(), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Iter iterates over node sets.
|
||||
type Iter struct {
|
||||
state []pathStepState
|
||||
seen []bool
|
||||
}
|
||||
|
||||
// Node returns the current node.
|
||||
// Must only be called after Iter.Next returns true.
|
||||
func (iter *Iter) Node() *Node {
|
||||
state := iter.state[len(iter.state)-1]
|
||||
if state.pos == 0 {
|
||||
panic("Iter.Node called before Iter.Next")
|
||||
}
|
||||
if state.node == nil {
|
||||
panic("Iter.Node called after Iter.Next false")
|
||||
}
|
||||
return state.node
|
||||
}
|
||||
|
||||
// Next iterates to the next node in the set, if any, and
|
||||
// returns whether there is a node available.
|
||||
func (iter *Iter) Next() bool {
|
||||
tip := len(iter.state) - 1
|
||||
outer:
|
||||
for {
|
||||
for !iter.state[tip].next() {
|
||||
tip--
|
||||
if tip == -1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for tip < len(iter.state)-1 {
|
||||
tip++
|
||||
iter.state[tip].init(iter.state[tip-1].node)
|
||||
if !iter.state[tip].next() {
|
||||
tip--
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
if iter.seen[iter.state[tip].node.pos] {
|
||||
continue
|
||||
}
|
||||
iter.seen[iter.state[tip].node.pos] = true
|
||||
return true
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
type pathStepState struct {
|
||||
step *pathStep
|
||||
node *Node
|
||||
pos int
|
||||
idx int
|
||||
aux int
|
||||
}
|
||||
|
||||
func (s *pathStepState) init(node *Node) {
|
||||
s.node = node
|
||||
s.pos = 0
|
||||
s.idx = 0
|
||||
s.aux = 0
|
||||
}
|
||||
|
||||
func (s *pathStepState) next() bool {
|
||||
for s._next() {
|
||||
s.pos++
|
||||
if s.step.pred == nil {
|
||||
return true
|
||||
}
|
||||
if s.step.pred.bval {
|
||||
if s.step.pred.path.Exists(s.node) {
|
||||
return true
|
||||
}
|
||||
} else if s.step.pred.path != nil {
|
||||
iter := s.step.pred.path.Iter(s.node)
|
||||
for iter.Next() {
|
||||
if iter.Node().equals(s.step.pred.sval) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if s.step.pred.ival == s.pos {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *pathStepState) _next() bool {
|
||||
if s.node == nil {
|
||||
return false
|
||||
}
|
||||
if s.step.root && s.idx == 0 {
|
||||
for s.node.up != nil {
|
||||
s.node = s.node.up
|
||||
}
|
||||
}
|
||||
|
||||
switch s.step.axis {
|
||||
|
||||
case "self":
|
||||
if s.idx == 0 && s.step.match(s.node) {
|
||||
s.idx++
|
||||
return true
|
||||
}
|
||||
|
||||
case "parent":
|
||||
if s.idx == 0 && s.node.up != nil && s.step.match(s.node.up) {
|
||||
s.idx++
|
||||
s.node = s.node.up
|
||||
return true
|
||||
}
|
||||
|
||||
case "ancestor", "ancestor-or-self":
|
||||
if s.idx == 0 && s.step.axis == "ancestor-or-self" {
|
||||
s.idx++
|
||||
if s.step.match(s.node) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for s.node.up != nil {
|
||||
s.node = s.node.up
|
||||
s.idx++
|
||||
if s.step.match(s.node) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "child":
|
||||
var down []*Node
|
||||
if s.idx == 0 {
|
||||
down = s.node.down
|
||||
} else {
|
||||
down = s.node.up.down
|
||||
}
|
||||
for s.idx < len(down) {
|
||||
node := down[s.idx]
|
||||
s.idx++
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "descendant", "descendant-or-self":
|
||||
if s.idx == 0 {
|
||||
s.idx = s.node.pos
|
||||
s.aux = s.node.end
|
||||
if s.step.axis == "descendant" {
|
||||
s.idx++
|
||||
}
|
||||
}
|
||||
for s.idx < s.aux {
|
||||
node := &s.node.nodes[s.idx]
|
||||
s.idx++
|
||||
if node.kind == attrNode {
|
||||
continue
|
||||
}
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "following":
|
||||
if s.idx == 0 {
|
||||
s.idx = s.node.end
|
||||
}
|
||||
for s.idx < len(s.node.nodes) {
|
||||
node := &s.node.nodes[s.idx]
|
||||
s.idx++
|
||||
if node.kind == attrNode {
|
||||
continue
|
||||
}
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "following-sibling":
|
||||
var down []*Node
|
||||
if s.node.up != nil {
|
||||
down = s.node.up.down
|
||||
if s.idx == 0 {
|
||||
for s.idx < len(down) {
|
||||
node := down[s.idx]
|
||||
s.idx++
|
||||
if node == s.node {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for s.idx < len(down) {
|
||||
node := down[s.idx]
|
||||
s.idx++
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "preceding":
|
||||
if s.idx == 0 {
|
||||
s.aux = s.node.pos // Detect ancestors.
|
||||
s.idx = s.node.pos - 1
|
||||
}
|
||||
for s.idx >= 0 {
|
||||
node := &s.node.nodes[s.idx]
|
||||
s.idx--
|
||||
if node.kind == attrNode {
|
||||
continue
|
||||
}
|
||||
if node == s.node.nodes[s.aux].up {
|
||||
s.aux = s.node.nodes[s.aux].up.pos
|
||||
continue
|
||||
}
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "preceding-sibling":
|
||||
var down []*Node
|
||||
if s.node.up != nil {
|
||||
down = s.node.up.down
|
||||
if s.aux == 0 {
|
||||
s.aux = 1
|
||||
for s.idx < len(down) {
|
||||
node := down[s.idx]
|
||||
s.idx++
|
||||
if node == s.node {
|
||||
s.idx--
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for s.idx >= 0 {
|
||||
node := down[s.idx]
|
||||
s.idx--
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
case "attribute":
|
||||
if s.idx == 0 {
|
||||
s.idx = s.node.pos + 1
|
||||
s.aux = s.node.end
|
||||
}
|
||||
for s.idx < s.aux {
|
||||
node := &s.node.nodes[s.idx]
|
||||
s.idx++
|
||||
if node.kind != attrNode {
|
||||
break
|
||||
}
|
||||
if s.step.match(node) {
|
||||
s.node = node
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
s.node = nil
|
||||
return false
|
||||
}
|
||||
|
||||
type pathPredicate struct {
|
||||
path *Path
|
||||
sval string
|
||||
ival int
|
||||
bval bool
|
||||
}
|
||||
|
||||
type pathStep struct {
|
||||
root bool
|
||||
axis string
|
||||
name string
|
||||
prefix string
|
||||
uri string
|
||||
kind nodeKind
|
||||
pred *pathPredicate
|
||||
}
|
||||
|
||||
func (step *pathStep) match(node *Node) bool {
|
||||
return node.kind != endNode &&
|
||||
(step.kind == anyNode || step.kind == node.kind) &&
|
||||
(step.name == "*" || (node.name.Local == step.name && (node.name.Space != "" && node.name.Space == step.uri || node.name.Space == "")))
|
||||
}
|
||||
|
||||
// MustCompile returns the compiled path, and panics if
|
||||
// there are any errors.
|
||||
func MustCompile(path string) *Path {
|
||||
e, err := Compile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Compile returns the compiled path.
|
||||
func Compile(path string) (*Path, error) {
|
||||
c := pathCompiler{path, 0, []Namespace{} }
|
||||
if path == "" {
|
||||
return nil, c.errorf("empty path")
|
||||
}
|
||||
p, err := c.parsePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Compile the path with the knowledge of the given namespaces
|
||||
func CompileWithNamespace(path string, ns []Namespace) (*Path, error) {
|
||||
c := pathCompiler{path, 0, ns}
|
||||
if path == "" {
|
||||
return nil, c.errorf("empty path")
|
||||
}
|
||||
p, err := c.parsePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
type pathCompiler struct {
|
||||
path string
|
||||
i int
|
||||
ns []Namespace
|
||||
}
|
||||
|
||||
func (c *pathCompiler) errorf(format string, args ...interface{}) error {
|
||||
return fmt.Errorf("compiling xml path %q:%d: %s", c.path, c.i, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (c *pathCompiler) parsePath() (path *Path, err error) {
|
||||
var steps []pathStep
|
||||
var start = c.i
|
||||
for {
|
||||
step := pathStep{axis: "child"}
|
||||
|
||||
if c.i == 0 && c.skipByte('/') {
|
||||
step.root = true
|
||||
if len(c.path) == 1 {
|
||||
step.name = "*"
|
||||
}
|
||||
}
|
||||
if c.peekByte('/') {
|
||||
step.axis = "descendant-or-self"
|
||||
step.name = "*"
|
||||
} else if c.skipByte('@') {
|
||||
mark := c.i
|
||||
if !c.skipName() {
|
||||
return nil, c.errorf("missing name after @")
|
||||
}
|
||||
step.axis = "attribute"
|
||||
step.name = c.path[mark:c.i]
|
||||
step.kind = attrNode
|
||||
} else {
|
||||
mark := c.i
|
||||
if c.skipName() {
|
||||
step.name = c.path[mark:c.i]
|
||||
}
|
||||
if step.name == "" {
|
||||
return nil, c.errorf("missing name")
|
||||
} else if step.name == "*" {
|
||||
step.kind = startNode
|
||||
} else if step.name == "." {
|
||||
step.axis = "self"
|
||||
step.name = "*"
|
||||
} else if step.name == ".." {
|
||||
step.axis = "parent"
|
||||
step.name = "*"
|
||||
} else {
|
||||
if c.skipByte(':') {
|
||||
if !c.skipByte(':') {
|
||||
mark = c.i
|
||||
if c.skipName() {
|
||||
step.prefix = step.name
|
||||
step.name = c.path[mark:c.i]
|
||||
// check prefix
|
||||
found := false
|
||||
for _, ns := range c.ns {
|
||||
if ns.Prefix == step.prefix {
|
||||
step.uri = ns.Uri
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return nil, c.errorf("unknown namespace prefix: %s", step.prefix)
|
||||
}
|
||||
} else {
|
||||
return nil, c.errorf("missing name after namespace prefix")
|
||||
}
|
||||
} else {
|
||||
switch step.name {
|
||||
case "attribute":
|
||||
step.kind = attrNode
|
||||
case "self", "child", "parent":
|
||||
case "descendant", "descendant-or-self":
|
||||
case "ancestor", "ancestor-or-self":
|
||||
case "following", "following-sibling":
|
||||
case "preceding", "preceding-sibling":
|
||||
default:
|
||||
return nil, c.errorf("unsupported axis: %q", step.name)
|
||||
}
|
||||
step.axis = step.name
|
||||
|
||||
mark = c.i
|
||||
if !c.skipName() {
|
||||
return nil, c.errorf("missing name")
|
||||
}
|
||||
step.name = c.path[mark:c.i]
|
||||
}
|
||||
}
|
||||
if c.skipByte('(') {
|
||||
conflict := step.kind != anyNode
|
||||
switch step.name {
|
||||
case "node":
|
||||
// must be anyNode
|
||||
case "text":
|
||||
step.kind = textNode
|
||||
case "comment":
|
||||
step.kind = commentNode
|
||||
case "processing-instruction":
|
||||
step.kind = procInstNode
|
||||
default:
|
||||
return nil, c.errorf("unsupported expression: %s()", step.name)
|
||||
}
|
||||
if conflict {
|
||||
return nil, c.errorf("%s() cannot succeed on axis %q", step.name, step.axis)
|
||||
}
|
||||
|
||||
literal, err := c.parseLiteral()
|
||||
if err == errNoLiteral {
|
||||
step.name = "*"
|
||||
} else if err != nil {
|
||||
return nil, c.errorf("%v", err)
|
||||
} else if step.kind == procInstNode {
|
||||
step.name = literal
|
||||
} else {
|
||||
return nil, c.errorf("%s() has no arguments", step.name)
|
||||
}
|
||||
if !c.skipByte(')') {
|
||||
return nil, c.errorf("missing )")
|
||||
}
|
||||
} else if step.name == "*" && step.kind == anyNode {
|
||||
step.kind = startNode
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.skipByte('[') {
|
||||
step.pred = &pathPredicate{}
|
||||
if ival, ok := c.parseInt(); ok {
|
||||
if ival == 0 {
|
||||
return nil, c.errorf("positions start at 1")
|
||||
}
|
||||
step.pred.ival = ival
|
||||
} else {
|
||||
path, err := c.parsePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if path.path[0] == '-' {
|
||||
if _, err = strconv.Atoi(path.path); err == nil {
|
||||
return nil, c.errorf("positions must be positive")
|
||||
}
|
||||
}
|
||||
step.pred.path = path
|
||||
if c.skipByte('=') {
|
||||
sval, err := c.parseLiteral()
|
||||
if err != nil {
|
||||
return nil, c.errorf("%v", err)
|
||||
}
|
||||
step.pred.sval = sval
|
||||
} else {
|
||||
step.pred.bval = true
|
||||
}
|
||||
}
|
||||
if !c.skipByte(']') {
|
||||
return nil, c.errorf("expected ']'")
|
||||
}
|
||||
}
|
||||
steps = append(steps, step)
|
||||
//fmt.Printf("step: %#v\n", step)
|
||||
if !c.skipByte('/') {
|
||||
if (start == 0 || start == c.i) && c.i < len(c.path) {
|
||||
return nil, c.errorf("unexpected %q", c.path[c.i])
|
||||
}
|
||||
return &Path{steps: steps, path: c.path[start:c.i]}, nil
|
||||
}
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
var errNoLiteral = fmt.Errorf("expected a literal string")
|
||||
|
||||
func (c *pathCompiler) parseLiteral() (string, error) {
|
||||
if c.skipByte('"') {
|
||||
mark := c.i
|
||||
if !c.skipByteFind('"') {
|
||||
return "", fmt.Errorf(`missing '"'`)
|
||||
}
|
||||
return c.path[mark:c.i-1], nil
|
||||
}
|
||||
if c.skipByte('\'') {
|
||||
mark := c.i
|
||||
if !c.skipByteFind('\'') {
|
||||
return "", fmt.Errorf(`missing "'"`)
|
||||
}
|
||||
return c.path[mark:c.i-1], nil
|
||||
}
|
||||
return "", errNoLiteral
|
||||
}
|
||||
|
||||
func (c *pathCompiler) parseInt() (v int, ok bool) {
|
||||
mark := c.i
|
||||
for c.i < len(c.path) && c.path[c.i] >= '0' && c.path[c.i] <= '9' {
|
||||
v *= 10
|
||||
v += int(c.path[c.i]) - '0'
|
||||
c.i++
|
||||
}
|
||||
if c.i == mark {
|
||||
return 0, false
|
||||
}
|
||||
return v, true
|
||||
}
|
||||
|
||||
func (c *pathCompiler) skipByte(b byte) bool {
|
||||
if c.i < len(c.path) && c.path[c.i] == b {
|
||||
c.i++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *pathCompiler) skipByteFind(b byte) bool {
|
||||
for i := c.i; i < len(c.path); i++ {
|
||||
if c.path[i] == b {
|
||||
c.i = i+1
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *pathCompiler) peekByte(b byte) bool {
|
||||
return c.i < len(c.path) && c.path[c.i] == b
|
||||
}
|
||||
|
||||
func (c *pathCompiler) skipName() bool {
|
||||
if c.i >= len(c.path) {
|
||||
return false
|
||||
}
|
||||
if c.path[c.i] == '*' {
|
||||
c.i++
|
||||
return true
|
||||
}
|
||||
start := c.i
|
||||
for c.i < len(c.path) && (c.path[c.i] >= utf8.RuneSelf || isNameByte(c.path[c.i])) {
|
||||
c.i++
|
||||
}
|
||||
return c.i > start
|
||||
}
|
||||
|
||||
func isNameByte(c byte) bool {
|
||||
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '_' || c == '.' || c == '-'
|
||||
}
|
21
vendor/github.com/mitchellh/go-fs/LICENSE
generated
vendored
Normal file
21
vendor/github.com/mitchellh/go-fs/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Mitchell Hashimoto
|
||||
|
||||
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.
|
90
vendor/github.com/mitchellh/go-fs/README.md
generated
vendored
Normal file
90
vendor/github.com/mitchellh/go-fs/README.md
generated
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
# FAT Filesystem Library for Go
|
||||
|
||||
This library implements the ability to create, read, and write
|
||||
FAT filesystems using pure Go.
|
||||
|
||||
**WARNING:** While the implementation works (to some degree, see the
|
||||
limitations section below), I highly recommend you **don't** use this
|
||||
library, since it has many limitations and is generally a terrible
|
||||
implementation of FAT. For educational purposes, however, this library
|
||||
may be interesting.
|
||||
|
||||
In this library's current state, it is very good for _reading_ FAT
|
||||
filesystems, and minimally useful for _creating_ FAT filesystems. See
|
||||
the features and limitations below.
|
||||
|
||||
## Features & Limitations
|
||||
|
||||
Features:
|
||||
|
||||
* Format a brand new FAT filesystem on a file backed device
|
||||
* Create files and directories
|
||||
* Traverse filesystem
|
||||
|
||||
Limitations:
|
||||
|
||||
This library has several limitations. They're easily able to be overcome,
|
||||
but because I didn't need them for my use case, I didn't bother:
|
||||
|
||||
* Files/directories cannot be deleted or renamed.
|
||||
* Files never shrink in size.
|
||||
* Deleted file/directory entries are never reclaimed, so fragmentation
|
||||
grows towards infinity. Eventually, your "disk" will become full even
|
||||
if you just create and delete a single file.
|
||||
* There are some serious corruption possibilities in error cases. Cleanup
|
||||
is not good.
|
||||
* Incomplete FAT32 implementation (although FAT12 and FAT16 are complete).
|
||||
|
||||
## Usage
|
||||
|
||||
Here is some example usage where an existing disk image is read and
|
||||
a file is created in the root directory:
|
||||
|
||||
```go
|
||||
// Assume this file was created already with a FAT filesystem
|
||||
f, err := os.OpenFile("FLOPPY.dmg", os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// BlockDevice backed by a file
|
||||
device, err := fs.NewFileDisk(f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
filesys, err := fat.New(device)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rootDir, err := filesys.RootDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
subEntry, err := rootDir.AddFile("HELLO_WORLD")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
file, err := subEntry.File()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = io.WriteString(file, "I am the contents of this file.")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
```
|
||||
|
||||
## Thanks
|
||||
|
||||
Thanks to the following resources which helped in the creation of this
|
||||
library:
|
||||
|
||||
* [fat32-lib](https://code.google.com/p/fat32-lib/)
|
||||
* [File Allocation Table on Wikipedia](http://en.wikipedia.org/wiki/File_Allocation_Table)
|
||||
* Microsoft FAT filesystem specification
|
22
vendor/github.com/mitchellh/go-fs/block_device.go
generated
vendored
Normal file
22
vendor/github.com/mitchellh/go-fs/block_device.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package fs
|
||||
|
||||
// A BlockDevice is the raw device that is meant to store a filesystem.
|
||||
type BlockDevice interface {
|
||||
// Closes this block device. No more methods may be called on a
|
||||
// closed device.
|
||||
Close() error
|
||||
|
||||
// Len returns the number of bytes in this block device.
|
||||
Len() int64
|
||||
|
||||
// SectorSize returns the size of a single sector on this device.
|
||||
SectorSize() int
|
||||
|
||||
// ReadAt reads data from the block device from the given
|
||||
// offset. See io.ReaderAt for more information on this function.
|
||||
ReadAt(p []byte, off int64) (n int, err error)
|
||||
|
||||
// WriteAt writes data to the block device at the given offset.
|
||||
// See io.WriterAt for more information on this function.
|
||||
WriteAt(p []byte, off int64) (n int, err error)
|
||||
}
|
18
vendor/github.com/mitchellh/go-fs/directory.go
generated
vendored
Normal file
18
vendor/github.com/mitchellh/go-fs/directory.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package fs
|
||||
|
||||
// Directory is an entry in a filesystem that stores files.
|
||||
type Directory interface {
|
||||
Entry(name string) DirectoryEntry
|
||||
Entries() []DirectoryEntry
|
||||
AddDirectory(name string) (DirectoryEntry, error)
|
||||
AddFile(name string) (DirectoryEntry, error)
|
||||
}
|
||||
|
||||
// DirectoryEntry represents a single entry within a directory,
|
||||
// which can be either another Directory or a File.
|
||||
type DirectoryEntry interface {
|
||||
Name() string
|
||||
IsDir() bool
|
||||
Dir() (Directory, error)
|
||||
File() (File, error)
|
||||
}
|
347
vendor/github.com/mitchellh/go-fs/fat/boot_sector.go
generated
vendored
Normal file
347
vendor/github.com/mitchellh/go-fs/fat/boot_sector.go
generated
vendored
Normal file
@ -0,0 +1,347 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-fs"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type MediaType uint8
|
||||
|
||||
// The standard value for "fixed", non-removable media, directly
|
||||
// from the FAT specification.
|
||||
const MediaFixed MediaType = 0xF8
|
||||
|
||||
type BootSectorCommon struct {
|
||||
OEMName string
|
||||
BytesPerSector uint16
|
||||
SectorsPerCluster uint8
|
||||
ReservedSectorCount uint16
|
||||
NumFATs uint8
|
||||
RootEntryCount uint16
|
||||
TotalSectors uint32
|
||||
Media MediaType
|
||||
SectorsPerFat uint32
|
||||
SectorsPerTrack uint16
|
||||
NumHeads uint16
|
||||
}
|
||||
|
||||
// DecodeBootSector takes a BlockDevice and decodes the FAT boot sector
|
||||
// from it.
|
||||
func DecodeBootSector(device fs.BlockDevice) (*BootSectorCommon, error) {
|
||||
var sector [512]byte
|
||||
if _, err := device.ReadAt(sector[:], 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sector[510] != 0x55 || sector[511] != 0xAA {
|
||||
return nil, errors.New("corrupt boot sector signature")
|
||||
}
|
||||
|
||||
result := new(BootSectorCommon)
|
||||
|
||||
// BS_OEMName
|
||||
result.OEMName = string(sector[3:11])
|
||||
|
||||
// BPB_BytsPerSec
|
||||
result.BytesPerSector = binary.LittleEndian.Uint16(sector[11:13])
|
||||
|
||||
// BPB_SecPerClus
|
||||
result.SectorsPerCluster = sector[13]
|
||||
|
||||
// BPB_RsvdSecCnt
|
||||
result.ReservedSectorCount = binary.LittleEndian.Uint16(sector[14:16])
|
||||
|
||||
// BPB_NumFATs
|
||||
result.NumFATs = sector[16]
|
||||
|
||||
// BPB_RootEntCnt
|
||||
result.RootEntryCount = binary.LittleEndian.Uint16(sector[17:19])
|
||||
|
||||
// BPB_Media
|
||||
result.Media = MediaType(sector[21])
|
||||
|
||||
// BPB_SecPerTrk
|
||||
result.SectorsPerTrack = binary.LittleEndian.Uint16(sector[24:26])
|
||||
|
||||
// BPB_NumHeads
|
||||
result.NumHeads = binary.LittleEndian.Uint16(sector[26:28])
|
||||
|
||||
// BPB_TotSec16 / BPB_TotSec32
|
||||
result.TotalSectors = uint32(binary.LittleEndian.Uint16(sector[19:21]))
|
||||
if result.TotalSectors == 0 {
|
||||
result.TotalSectors = binary.LittleEndian.Uint32(sector[32:36])
|
||||
}
|
||||
|
||||
// BPB_FATSz16 / BPB_FATSz32
|
||||
result.SectorsPerFat = uint32(binary.LittleEndian.Uint16(sector[22:24]))
|
||||
if result.SectorsPerFat == 0 {
|
||||
result.SectorsPerFat = binary.LittleEndian.Uint32(sector[36:40])
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (b *BootSectorCommon) Bytes() ([]byte, error) {
|
||||
var sector [512]byte
|
||||
|
||||
// BS_jmpBoot
|
||||
sector[0] = 0xEB
|
||||
sector[1] = 0x3C
|
||||
sector[2] = 0x90
|
||||
|
||||
// BS_OEMName
|
||||
if len(b.OEMName) > 8 {
|
||||
return nil, errors.New("OEMName must be 8 bytes or less")
|
||||
}
|
||||
|
||||
for i, r := range b.OEMName {
|
||||
if r > unicode.MaxASCII {
|
||||
return nil, fmt.Errorf("'%s' in OEM name not a valid ASCII char. Must be ASCII.", r)
|
||||
}
|
||||
|
||||
sector[0x3+i] = byte(r)
|
||||
}
|
||||
|
||||
// BPB_BytsPerSec
|
||||
binary.LittleEndian.PutUint16(sector[11:13], b.BytesPerSector)
|
||||
|
||||
// BPB_SecPerClus
|
||||
sector[13] = uint8(b.SectorsPerCluster)
|
||||
|
||||
// BPB_RsvdSecCnt
|
||||
binary.LittleEndian.PutUint16(sector[14:16], b.ReservedSectorCount)
|
||||
|
||||
// BPB_NumFATs
|
||||
sector[16] = b.NumFATs
|
||||
|
||||
// BPB_RootEntCnt
|
||||
binary.LittleEndian.PutUint16(sector[17:19], b.RootEntryCount)
|
||||
|
||||
// BPB_Media
|
||||
sector[21] = byte(b.Media)
|
||||
|
||||
// BPB_SecPerTrk
|
||||
binary.LittleEndian.PutUint16(sector[24:26], b.SectorsPerTrack)
|
||||
|
||||
// BPB_Numheads
|
||||
binary.LittleEndian.PutUint16(sector[26:28], b.NumHeads)
|
||||
|
||||
// BPB_Hiddsec
|
||||
// sector[28:32] - it is always set to 0 because we don't partition drives yet.
|
||||
|
||||
// Important signature of every FAT boot sector
|
||||
sector[510] = 0x55
|
||||
sector[511] = 0xAA
|
||||
|
||||
return sector[:], nil
|
||||
}
|
||||
|
||||
// BytesPerCluster returns the number of bytes per cluster.
|
||||
func (b *BootSectorCommon) BytesPerCluster() uint32 {
|
||||
return uint32(b.SectorsPerCluster) * uint32(b.BytesPerSector)
|
||||
}
|
||||
|
||||
// ClusterOffset returns the offset of the data section of a particular
|
||||
// cluster.
|
||||
func (b *BootSectorCommon) ClusterOffset(n int) uint32 {
|
||||
offset := b.DataOffset()
|
||||
offset += (uint32(n) - FirstCluster) * b.BytesPerCluster()
|
||||
return offset
|
||||
}
|
||||
|
||||
// DataOffset returns the offset of the data section of the disk.
|
||||
func (b *BootSectorCommon) DataOffset() uint32 {
|
||||
offset := uint32(b.RootDirOffset())
|
||||
offset += uint32(b.RootEntryCount * DirectoryEntrySize)
|
||||
return offset
|
||||
}
|
||||
|
||||
// FATOffset returns the offset in bytes for the given index of the FAT
|
||||
func (b *BootSectorCommon) FATOffset(n int) int {
|
||||
offset := uint32(b.ReservedSectorCount * b.BytesPerSector)
|
||||
offset += b.SectorsPerFat * uint32(b.BytesPerSector) * uint32(n)
|
||||
return int(offset)
|
||||
}
|
||||
|
||||
// Calculates the FAT type that this boot sector represents.
|
||||
func (b *BootSectorCommon) FATType() FATType {
|
||||
var rootDirSectors uint32
|
||||
rootDirSectors = (uint32(b.RootEntryCount) * 32) + (uint32(b.BytesPerSector) - 1)
|
||||
rootDirSectors /= uint32(b.BytesPerSector)
|
||||
dataSectors := b.SectorsPerFat * uint32(b.NumFATs)
|
||||
dataSectors += uint32(b.ReservedSectorCount)
|
||||
dataSectors += rootDirSectors
|
||||
dataSectors = b.TotalSectors - dataSectors
|
||||
countClusters := dataSectors / uint32(b.SectorsPerCluster)
|
||||
|
||||
switch {
|
||||
case countClusters < 4085:
|
||||
return FAT12
|
||||
case countClusters < 65525:
|
||||
return FAT16
|
||||
default:
|
||||
return FAT32
|
||||
}
|
||||
}
|
||||
|
||||
// RootDirOffset returns the byte offset when the root directory
|
||||
// entries for FAT12/16 filesystems start. NOTE: This is absolutely useless
|
||||
// for FAT32 because the root directory is just the beginning of the data
|
||||
// region.
|
||||
func (b *BootSectorCommon) RootDirOffset() int {
|
||||
offset := b.FATOffset(0)
|
||||
offset += int(uint32(b.NumFATs) * b.SectorsPerFat * uint32(b.BytesPerSector))
|
||||
return offset
|
||||
}
|
||||
|
||||
// BootSectorFat16 is the BootSector for FAT12 and FAT16 filesystems.
|
||||
// It contains the common fields to all FAT filesystems and also some
|
||||
// unique.
|
||||
type BootSectorFat16 struct {
|
||||
BootSectorCommon
|
||||
|
||||
DriveNumber uint8
|
||||
VolumeID uint32
|
||||
VolumeLabel string
|
||||
FileSystemTypeLabel string
|
||||
}
|
||||
|
||||
func (b *BootSectorFat16) Bytes() ([]byte, error) {
|
||||
sector, err := b.BootSectorCommon.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// BPB_TotSec16 AND BPB_TotSec32
|
||||
if b.TotalSectors < 0x10000 {
|
||||
binary.LittleEndian.PutUint16(sector[19:21], uint16(b.TotalSectors))
|
||||
} else {
|
||||
binary.LittleEndian.PutUint32(sector[32:36], b.TotalSectors)
|
||||
}
|
||||
|
||||
// BPB_FATSz16
|
||||
if b.SectorsPerFat > 0x10000 {
|
||||
return nil, fmt.Errorf("SectorsPerFat value too big for non-FAT32: %d", b.SectorsPerFat)
|
||||
}
|
||||
|
||||
binary.LittleEndian.PutUint16(sector[22:24], uint16(b.SectorsPerFat))
|
||||
|
||||
// BS_DrvNum
|
||||
sector[36] = b.DriveNumber
|
||||
|
||||
// BS_BootSig
|
||||
sector[38] = 0x29
|
||||
|
||||
// BS_VolID
|
||||
binary.LittleEndian.PutUint32(sector[39:43], b.VolumeID)
|
||||
|
||||
// BS_VolLab
|
||||
if len(b.VolumeLabel) > 11 {
|
||||
return nil, errors.New("VolumeLabel must be 11 bytes or less")
|
||||
}
|
||||
|
||||
for i, r := range b.VolumeLabel {
|
||||
if r > unicode.MaxASCII {
|
||||
return nil, fmt.Errorf("'%s' in VolumeLabel not a valid ASCII char. Must be ASCII.", r)
|
||||
}
|
||||
|
||||
sector[43+i] = byte(r)
|
||||
}
|
||||
|
||||
// BS_FilSysType
|
||||
if len(b.FileSystemTypeLabel) > 8 {
|
||||
return nil, errors.New("FileSystemTypeLabel must be 8 bytes or less")
|
||||
}
|
||||
|
||||
for i, r := range b.FileSystemTypeLabel {
|
||||
if r > unicode.MaxASCII {
|
||||
return nil, fmt.Errorf("'%s' in FileSystemTypeLabel not a valid ASCII char. Must be ASCII.", r)
|
||||
}
|
||||
|
||||
sector[54+i] = byte(r)
|
||||
}
|
||||
|
||||
return sector, nil
|
||||
}
|
||||
|
||||
type BootSectorFat32 struct {
|
||||
BootSectorCommon
|
||||
|
||||
RootCluster uint32
|
||||
FSInfoSector uint16
|
||||
BackupBootSector uint16
|
||||
DriveNumber uint8
|
||||
VolumeID uint32
|
||||
VolumeLabel string
|
||||
FileSystemTypeLabel string
|
||||
}
|
||||
|
||||
func (b *BootSectorFat32) Bytes() ([]byte, error) {
|
||||
sector, err := b.BootSectorCommon.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// BPB_RootEntCount - must be 0
|
||||
sector[17] = 0
|
||||
sector[18] = 0
|
||||
|
||||
// BPB_FATSz32
|
||||
binary.LittleEndian.PutUint32(sector[36:40], b.SectorsPerFat)
|
||||
|
||||
// BPB_ExtFlags - Unused?
|
||||
|
||||
// BPB_FSVer. Explicitly set to 0 because that is really important
|
||||
// to get correct.
|
||||
sector[42] = 0
|
||||
sector[43] = 0
|
||||
|
||||
// BPB_RootClus
|
||||
binary.LittleEndian.PutUint32(sector[44:48], b.RootCluster)
|
||||
|
||||
// BPB_FSInfo
|
||||
binary.LittleEndian.PutUint16(sector[48:50], b.FSInfoSector)
|
||||
|
||||
// BPB_BkBootSec
|
||||
binary.LittleEndian.PutUint16(sector[50:52], b.BackupBootSector)
|
||||
|
||||
// BS_DrvNum
|
||||
sector[64] = b.DriveNumber
|
||||
|
||||
// BS_BootSig
|
||||
sector[66] = 0x29
|
||||
|
||||
// BS_VolID
|
||||
binary.LittleEndian.PutUint32(sector[67:71], b.VolumeID)
|
||||
|
||||
// BS_VolLab
|
||||
if len(b.VolumeLabel) > 11 {
|
||||
return nil, errors.New("VolumeLabel must be 11 bytes or less")
|
||||
}
|
||||
|
||||
for i, r := range b.VolumeLabel {
|
||||
if r > unicode.MaxASCII {
|
||||
return nil, fmt.Errorf("'%s' in VolumeLabel not a valid ASCII char. Must be ASCII.", r)
|
||||
}
|
||||
|
||||
sector[71+i] = byte(r)
|
||||
}
|
||||
|
||||
// BS_FilSysType
|
||||
if len(b.FileSystemTypeLabel) > 8 {
|
||||
return nil, errors.New("FileSystemTypeLabel must be 8 bytes or less")
|
||||
}
|
||||
|
||||
for i, r := range b.FileSystemTypeLabel {
|
||||
if r > unicode.MaxASCII {
|
||||
return nil, fmt.Errorf("'%s' in FileSystemTypeLabel not a valid ASCII char. Must be ASCII.", r)
|
||||
}
|
||||
|
||||
sector[82+i] = byte(r)
|
||||
}
|
||||
|
||||
return sector, nil
|
||||
}
|
91
vendor/github.com/mitchellh/go-fs/fat/cluster_chain.go
generated
vendored
Normal file
91
vendor/github.com/mitchellh/go-fs/fat/cluster_chain.go
generated
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/go-fs"
|
||||
"io"
|
||||
"math"
|
||||
)
|
||||
|
||||
type ClusterChain struct {
|
||||
device fs.BlockDevice
|
||||
fat *FAT
|
||||
startCluster uint32
|
||||
readOffset uint32
|
||||
writeOffset uint32
|
||||
}
|
||||
|
||||
func (c *ClusterChain) Read(p []byte) (n int, err error) {
|
||||
bpc := c.fat.bs.BytesPerCluster()
|
||||
chain := c.fat.Chain(c.startCluster)
|
||||
|
||||
dataOffset := uint32(0)
|
||||
for dataOffset < uint32(len(p)) {
|
||||
chainIdx := c.readOffset / bpc
|
||||
if int(chainIdx) >= len(chain) {
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
|
||||
clusterOffset := c.fat.bs.ClusterOffset(int(chain[chainIdx]))
|
||||
clusterOffset += c.readOffset % bpc
|
||||
dataOffsetEnd := dataOffset + bpc
|
||||
dataOffsetEnd -= c.readOffset % bpc
|
||||
dataOffsetEnd = uint32(math.Min(float64(dataOffsetEnd), float64(len(p))))
|
||||
|
||||
var nw int
|
||||
nw, err = c.device.ReadAt(p[dataOffset:dataOffsetEnd], int64(clusterOffset))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.readOffset += uint32(nw)
|
||||
dataOffset += uint32(nw)
|
||||
n += nw
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Write will write to the cluster chain, expanding it if necessary.
|
||||
func (c *ClusterChain) Write(p []byte) (n int, err error) {
|
||||
bpc := c.fat.bs.BytesPerCluster()
|
||||
chain := c.fat.Chain(c.startCluster)
|
||||
chainLength := uint32(len(chain)) * bpc
|
||||
|
||||
if chainLength < c.writeOffset+uint32(len(p)) {
|
||||
// We need to grow the chain
|
||||
bytesNeeded := (c.writeOffset + uint32(len(p))) - chainLength
|
||||
clustersNeeded := int(math.Ceil(float64(bytesNeeded) / float64(bpc)))
|
||||
chain, err = c.fat.ResizeChain(c.startCluster, len(chain)+clustersNeeded)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Write the FAT out
|
||||
if err = c.fat.WriteToDevice(c.device); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
dataOffset := uint32(0)
|
||||
for dataOffset < uint32(len(p)) {
|
||||
chainIdx := c.writeOffset / bpc
|
||||
clusterOffset := c.fat.bs.ClusterOffset(int(chain[chainIdx]))
|
||||
clusterOffset += c.writeOffset % bpc
|
||||
dataOffsetEnd := dataOffset + bpc
|
||||
dataOffsetEnd -= c.writeOffset % bpc
|
||||
dataOffsetEnd = uint32(math.Min(float64(dataOffsetEnd), float64(len(p))))
|
||||
|
||||
var nw int
|
||||
nw, err = c.device.WriteAt(p[dataOffset:dataOffsetEnd], int64(clusterOffset))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.writeOffset += uint32(nw)
|
||||
dataOffset += uint32(nw)
|
||||
n += nw
|
||||
}
|
||||
|
||||
return
|
||||
}
|
278
vendor/github.com/mitchellh/go-fs/fat/directory.go
generated
vendored
Normal file
278
vendor/github.com/mitchellh/go-fs/fat/directory.go
generated
vendored
Normal file
@ -0,0 +1,278 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-fs"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Directory implements fs.Directory and is used to interface with
|
||||
// a directory on a FAT filesystem.
|
||||
type Directory struct {
|
||||
device fs.BlockDevice
|
||||
dirCluster *DirectoryCluster
|
||||
fat *FAT
|
||||
}
|
||||
|
||||
// DirectoryEntry implements fs.DirectoryEntry and represents a single
|
||||
// file/folder within a directory in a FAT filesystem. Note that the
|
||||
// underlying directory entry data structures on the disk may be more
|
||||
// than one to accomodate for long filenames.
|
||||
type DirectoryEntry struct {
|
||||
dir *Directory
|
||||
lfnEntries []*DirectoryClusterEntry
|
||||
entry *DirectoryClusterEntry
|
||||
|
||||
name string
|
||||
}
|
||||
|
||||
// DecodeDirectoryEntry takes a list of entries, decodes the next full
|
||||
// DirectoryEntry, and returns the newly created entry, the remaining
|
||||
// entries, and an error, if there was one.
|
||||
func DecodeDirectoryEntry(d *Directory, entries []*DirectoryClusterEntry) (*DirectoryEntry, []*DirectoryClusterEntry, error) {
|
||||
var lfnEntries []*DirectoryClusterEntry
|
||||
var entry *DirectoryClusterEntry
|
||||
var name string
|
||||
|
||||
// Skip all the deleted entries
|
||||
for len(entries) > 0 && entries[0].deleted {
|
||||
entries = entries[1:]
|
||||
}
|
||||
|
||||
// Skip the volume ID
|
||||
if len(entries) > 0 && entries[0].IsVolumeId() {
|
||||
entries = entries[1:]
|
||||
}
|
||||
|
||||
if len(entries) == 0 {
|
||||
return nil, entries, nil
|
||||
}
|
||||
|
||||
// We have a long entry, so we have to traverse to the point where
|
||||
// we're done. Also, calculate out the name and such.
|
||||
if entries[0].IsLong() {
|
||||
lfnEntries := make([]*DirectoryClusterEntry, 0, 3)
|
||||
for entries[0].IsLong() {
|
||||
lfnEntries = append(lfnEntries, entries[0])
|
||||
entries = entries[1:]
|
||||
}
|
||||
|
||||
var nameBytes []rune
|
||||
nameBytes = make([]rune, 13*len(lfnEntries))
|
||||
for i := len(lfnEntries) - 1; i >= 0; i-- {
|
||||
for _, char := range lfnEntries[i].longName {
|
||||
nameBytes = append(nameBytes, char)
|
||||
}
|
||||
}
|
||||
|
||||
name = string(nameBytes)
|
||||
}
|
||||
|
||||
// Get the short entry
|
||||
entry = entries[0]
|
||||
entries = entries[1:]
|
||||
|
||||
// If the short entry is deleted, ignore everything
|
||||
if entry.deleted {
|
||||
return nil, entries, nil
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(entry.name)
|
||||
ext := strings.TrimSpace(entry.ext)
|
||||
if ext != "" {
|
||||
name = fmt.Sprintf("%s.%s", name, ext)
|
||||
}
|
||||
}
|
||||
|
||||
result := &DirectoryEntry{
|
||||
dir: d,
|
||||
lfnEntries: lfnEntries,
|
||||
entry: entry,
|
||||
name: name,
|
||||
}
|
||||
|
||||
return result, entries, nil
|
||||
}
|
||||
|
||||
func (d *DirectoryEntry) Dir() (fs.Directory, error) {
|
||||
if !d.IsDir() {
|
||||
panic("not a directory")
|
||||
}
|
||||
|
||||
dirCluster, err := DecodeDirectoryCluster(
|
||||
d.entry.cluster, d.dir.device, d.dir.fat)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &Directory{
|
||||
device: d.dir.device,
|
||||
dirCluster: dirCluster,
|
||||
fat: d.dir.fat,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (d *DirectoryEntry) File() (fs.File, error) {
|
||||
if d.IsDir() {
|
||||
panic("not a file")
|
||||
}
|
||||
|
||||
result := &File{
|
||||
chain: &ClusterChain{
|
||||
device: d.dir.device,
|
||||
fat: d.dir.fat,
|
||||
startCluster: d.entry.cluster,
|
||||
},
|
||||
dir: d.dir,
|
||||
entry: d.entry,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (d *DirectoryEntry) IsDir() bool {
|
||||
return (d.entry.attr & AttrDirectory) == AttrDirectory
|
||||
}
|
||||
|
||||
func (d *DirectoryEntry) Name() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
func (d *DirectoryEntry) ShortName() string {
|
||||
if d.entry.name == "." || d.entry.name == ".." {
|
||||
return d.entry.name
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s.%s", d.entry.name, d.entry.ext)
|
||||
}
|
||||
|
||||
func (d *Directory) AddDirectory(name string) (fs.DirectoryEntry, error) {
|
||||
entry, err := d.addEntry(name, AttrDirectory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create the new directory cluster
|
||||
newDirCluster := NewDirectoryCluster(
|
||||
entry.entry.cluster, d.dirCluster.startCluster, entry.entry.createTime)
|
||||
|
||||
if err := newDirCluster.WriteToDevice(d.device, d.fat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (d *Directory) AddFile(name string) (fs.DirectoryEntry, error) {
|
||||
entry, err := d.addEntry(name, DirectoryAttr(0))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (d *Directory) Entries() []fs.DirectoryEntry {
|
||||
entries := d.dirCluster.entries
|
||||
result := make([]fs.DirectoryEntry, 0, len(entries)/2)
|
||||
for len(entries) > 0 {
|
||||
var entry *DirectoryEntry
|
||||
entry, entries, _ = DecodeDirectoryEntry(d, entries)
|
||||
if entry != nil {
|
||||
result = append(result, entry)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (d *Directory) Entry(name string) fs.DirectoryEntry {
|
||||
name = strings.ToUpper(name)
|
||||
|
||||
for _, entry := range d.Entries() {
|
||||
if strings.ToUpper(entry.Name()) == name {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Directory) addEntry(name string, attr DirectoryAttr) (*DirectoryEntry, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
|
||||
entries := d.Entries()
|
||||
usedNames := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if strings.ToUpper(entry.Name()) == strings.ToUpper(name) {
|
||||
return nil, fmt.Errorf("name already exists: %s", name)
|
||||
}
|
||||
|
||||
// Add it to the list of used names
|
||||
dirEntry := entry.(*DirectoryEntry)
|
||||
usedNames = append(usedNames, dirEntry.ShortName())
|
||||
}
|
||||
|
||||
shortName, err := generateShortName(name, usedNames)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var lfnEntries []*DirectoryClusterEntry
|
||||
if shortName != strings.ToUpper(name) {
|
||||
lfnEntries, err = NewLongDirectoryClusterEntry(name, shortName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate space for a cluster
|
||||
startCluster, err := d.fat.AllocChain()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createTime := time.Now()
|
||||
|
||||
// Create the entry for the short name
|
||||
shortParts := strings.Split(shortName, ".")
|
||||
if len(shortParts) == 1 {
|
||||
shortParts = append(shortParts, "")
|
||||
}
|
||||
|
||||
shortEntry := new(DirectoryClusterEntry)
|
||||
shortEntry.attr = attr
|
||||
shortEntry.name = shortParts[0]
|
||||
shortEntry.ext = shortParts[1]
|
||||
shortEntry.cluster = startCluster
|
||||
shortEntry.accessTime = createTime
|
||||
shortEntry.createTime = createTime
|
||||
shortEntry.writeTime = createTime
|
||||
|
||||
// Write the new FAT out
|
||||
if err := d.fat.WriteToDevice(d.device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Write the entries out in this directory
|
||||
if lfnEntries != nil {
|
||||
d.dirCluster.entries = append(d.dirCluster.entries, lfnEntries...)
|
||||
}
|
||||
d.dirCluster.entries = append(d.dirCluster.entries, shortEntry)
|
||||
|
||||
if err := d.dirCluster.WriteToDevice(d.device, d.fat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newEntry := &DirectoryEntry{
|
||||
dir: d,
|
||||
lfnEntries: lfnEntries,
|
||||
entry: shortEntry,
|
||||
}
|
||||
|
||||
return newEntry, nil
|
||||
}
|
438
vendor/github.com/mitchellh/go-fs/fat/directory_cluster.go
generated
vendored
Normal file
438
vendor/github.com/mitchellh/go-fs/fat/directory_cluster.go
generated
vendored
Normal file
@ -0,0 +1,438 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-fs"
|
||||
"math"
|
||||
"time"
|
||||
"unicode/utf16"
|
||||
)
|
||||
|
||||
type DirectoryAttr uint8
|
||||
|
||||
const (
|
||||
AttrReadOnly DirectoryAttr = 0x01
|
||||
AttrHidden = 0x02
|
||||
AttrSystem = 0x04
|
||||
AttrVolumeId = 0x08
|
||||
AttrDirectory = 0x10
|
||||
AttrArchive = 0x20
|
||||
AttrLongName = AttrReadOnly | AttrHidden | AttrSystem | AttrVolumeId
|
||||
)
|
||||
|
||||
// The size in bytes of a single directory entry.
|
||||
const DirectoryEntrySize = 32
|
||||
|
||||
// Mask applied to the ord of the last long entry.
|
||||
const LastLongEntryMask = 0x40
|
||||
|
||||
// DirectoryCluster represents a cluster on the disk that contains
|
||||
// entries/contents.
|
||||
type DirectoryCluster struct {
|
||||
entries []*DirectoryClusterEntry
|
||||
fat16Root bool
|
||||
startCluster uint32
|
||||
}
|
||||
|
||||
// DirectoryClusterEntry is a single 32-byte entry that is part of the
|
||||
// chain of entries in a directory cluster.
|
||||
type DirectoryClusterEntry struct {
|
||||
name string
|
||||
ext string
|
||||
attr DirectoryAttr
|
||||
createTime time.Time
|
||||
accessTime time.Time
|
||||
writeTime time.Time
|
||||
cluster uint32
|
||||
fileSize uint32
|
||||
deleted bool
|
||||
|
||||
longOrd uint8
|
||||
longName string
|
||||
longChecksum uint8
|
||||
}
|
||||
|
||||
func DecodeDirectoryCluster(startCluster uint32, device fs.BlockDevice, fat *FAT) (*DirectoryCluster, error) {
|
||||
bs := fat.bs
|
||||
chain := fat.Chain(startCluster)
|
||||
data := make([]byte, uint32(len(chain))*bs.BytesPerCluster())
|
||||
for i, clusterNumber := range chain {
|
||||
dataOffset := uint32(i) * bs.BytesPerCluster()
|
||||
devOffset := int64(bs.ClusterOffset(int(clusterNumber)))
|
||||
chainData := data[dataOffset : dataOffset+bs.BytesPerCluster()]
|
||||
|
||||
if _, err := device.ReadAt(chainData, devOffset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result, err := decodeDirectoryCluster(data, bs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.startCluster = startCluster
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// DecodeFAT16RootDirectory decodes the FAT16 root directory structure
|
||||
// from the device.
|
||||
func DecodeFAT16RootDirectoryCluster(device fs.BlockDevice, bs *BootSectorCommon) (*DirectoryCluster, error) {
|
||||
data := make([]byte, DirectoryEntrySize*bs.RootEntryCount)
|
||||
if _, err := device.ReadAt(data, int64(bs.RootDirOffset())); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := decodeDirectoryCluster(data, bs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.fat16Root = true
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func decodeDirectoryCluster(data []byte, bs *BootSectorCommon) (*DirectoryCluster, error) {
|
||||
entries := make([]*DirectoryClusterEntry, 0, bs.RootEntryCount)
|
||||
for i := uint16(0); i < uint16(len(data)/DirectoryEntrySize); i++ {
|
||||
offset := i * DirectoryEntrySize
|
||||
entryData := data[offset : offset+DirectoryEntrySize]
|
||||
if entryData[0] == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
entry, err := DecodeDirectoryClusterEntry(entryData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
|
||||
result := &DirectoryCluster{
|
||||
entries: entries,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func NewDirectoryCluster(start uint32, parent uint32, t time.Time) *DirectoryCluster {
|
||||
cluster := new(DirectoryCluster)
|
||||
cluster.startCluster = start
|
||||
|
||||
// Create the "." and ".." entries
|
||||
cluster.entries = []*DirectoryClusterEntry{
|
||||
&DirectoryClusterEntry{
|
||||
accessTime: t,
|
||||
attr: AttrDirectory,
|
||||
cluster: start,
|
||||
createTime: t,
|
||||
name: ".",
|
||||
writeTime: t,
|
||||
},
|
||||
&DirectoryClusterEntry{
|
||||
accessTime: t,
|
||||
attr: AttrDirectory,
|
||||
cluster: parent,
|
||||
createTime: t,
|
||||
name: "..",
|
||||
writeTime: t,
|
||||
},
|
||||
}
|
||||
|
||||
return cluster
|
||||
}
|
||||
|
||||
// NewFat16RootDirectory creates a new DirectoryCluster that is meant only
|
||||
// to be the root directory of a FAT12/FAT16 filesystem.
|
||||
func NewFat16RootDirectoryCluster(bs *BootSectorCommon, label string) (*DirectoryCluster, error) {
|
||||
if bs.RootEntryCount == 0 {
|
||||
return nil, errors.New("root entry count is 0 in boot sector")
|
||||
}
|
||||
|
||||
result := &DirectoryCluster{
|
||||
entries: make([]*DirectoryClusterEntry, 1, bs.RootEntryCount),
|
||||
}
|
||||
|
||||
// Create the volume ID entry
|
||||
result.entries[0] = &DirectoryClusterEntry{
|
||||
attr: AttrVolumeId,
|
||||
name: label,
|
||||
cluster: 0,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Bytes returns the on-disk byte data for this directory structure.
|
||||
func (d *DirectoryCluster) Bytes() []byte {
|
||||
result := make([]byte, cap(d.entries)*DirectoryEntrySize)
|
||||
|
||||
for i, entry := range d.entries {
|
||||
offset := i * DirectoryEntrySize
|
||||
entryBytes := entry.Bytes()
|
||||
copy(result[offset:offset+DirectoryEntrySize], entryBytes)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// WriteToDevice writes the cluster to the device.
|
||||
func (d *DirectoryCluster) WriteToDevice(device fs.BlockDevice, fat *FAT) error {
|
||||
if d.fat16Root {
|
||||
// Write the cluster to the FAT16 root directory location
|
||||
offset := int64(fat.bs.RootDirOffset())
|
||||
if _, err := device.WriteAt(d.Bytes(), offset); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
chain := &ClusterChain{
|
||||
device: device,
|
||||
fat: fat,
|
||||
startCluster: d.startCluster,
|
||||
}
|
||||
|
||||
if _, err := chain.Write(d.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bytes returns the on-disk byte data for this directory entry.
|
||||
func (d *DirectoryClusterEntry) Bytes() []byte {
|
||||
var result [DirectoryEntrySize]byte
|
||||
|
||||
if d.longName != "" {
|
||||
runes := bytes.Runes([]byte(d.longName))
|
||||
|
||||
// The name must be zero-terminated then padded with 0xFF
|
||||
// up to 13 characters
|
||||
if len(runes) < 13 {
|
||||
runes = append(runes, 0)
|
||||
for len(runes) < 13 {
|
||||
runes = append(runes, 0xFFFF)
|
||||
}
|
||||
}
|
||||
|
||||
// LDIR_Ord
|
||||
result[0] = d.longOrd
|
||||
|
||||
// LDIR_Name1
|
||||
for i := 0; i < int(math.Min(float64(len(runes)), 5)); i++ {
|
||||
offset := 1 + (i * 2)
|
||||
data := result[offset : offset+2]
|
||||
binary.LittleEndian.PutUint16(data, uint16(runes[i]))
|
||||
}
|
||||
|
||||
// LDIR_Attr
|
||||
result[11] = byte(AttrLongName)
|
||||
|
||||
// LDIR_Type
|
||||
result[12] = 0
|
||||
|
||||
// LDIR_Chksum
|
||||
result[13] = d.longChecksum
|
||||
|
||||
// LDIR_Name2
|
||||
for i := 0; i < 6; i++ {
|
||||
offset := 14 + (i * 2)
|
||||
data := result[offset : offset+2]
|
||||
binary.LittleEndian.PutUint16(data, uint16(runes[i+5]))
|
||||
}
|
||||
|
||||
// LDIR_FstClusLO
|
||||
result[26] = 0
|
||||
result[27] = 0
|
||||
|
||||
// LDIR_Name3
|
||||
for i := 0; i < 2; i++ {
|
||||
offset := 28 + (i * 2)
|
||||
data := result[offset : offset+2]
|
||||
binary.LittleEndian.PutUint16(data, uint16(runes[i+11]))
|
||||
}
|
||||
} else {
|
||||
// DIR_Name
|
||||
var simpleName string
|
||||
if d.name == "." || d.name == ".." {
|
||||
simpleName = d.name
|
||||
} else {
|
||||
simpleName = fmt.Sprintf("%s.%s", d.name, d.ext)
|
||||
}
|
||||
copy(result[0:11], shortNameEntryValue(simpleName))
|
||||
|
||||
// DIR_Attr
|
||||
result[11] = byte(d.attr)
|
||||
|
||||
// DIR_CrtTime
|
||||
crtDate, crtTime, crtTenths := encodeDOSTime(d.createTime)
|
||||
result[13] = crtTenths
|
||||
binary.LittleEndian.PutUint16(result[14:16], crtTime)
|
||||
binary.LittleEndian.PutUint16(result[16:18], crtDate)
|
||||
|
||||
// DIR_LstAccDate
|
||||
accDate, _, _ := encodeDOSTime(d.accessTime)
|
||||
binary.LittleEndian.PutUint16(result[18:20], accDate)
|
||||
|
||||
// DIR_FstClusHI
|
||||
binary.LittleEndian.PutUint16(result[20:22], uint16(d.cluster>>16))
|
||||
|
||||
// DIR_WrtTime and DIR_WrtDate
|
||||
wrtDate, wrtTime, _ := encodeDOSTime(d.writeTime)
|
||||
binary.LittleEndian.PutUint16(result[22:24], wrtTime)
|
||||
binary.LittleEndian.PutUint16(result[24:26], wrtDate)
|
||||
|
||||
// DIR_FstClusLO
|
||||
binary.LittleEndian.PutUint16(result[26:28], uint16(d.cluster&0xFFFF))
|
||||
|
||||
// DIR_FileSize
|
||||
binary.LittleEndian.PutUint32(result[28:32], d.fileSize)
|
||||
}
|
||||
|
||||
return result[:]
|
||||
}
|
||||
|
||||
// IsLong returns true if this is a long entry.
|
||||
func (d *DirectoryClusterEntry) IsLong() bool {
|
||||
return (d.attr & AttrLongName) == AttrLongName
|
||||
}
|
||||
|
||||
func (d *DirectoryClusterEntry) IsVolumeId() bool {
|
||||
return (d.attr & AttrVolumeId) == AttrVolumeId
|
||||
}
|
||||
|
||||
// DecodeDirectoryClusterEntry decodes a single directory entry in the
|
||||
// Directory structure.
|
||||
func DecodeDirectoryClusterEntry(data []byte) (*DirectoryClusterEntry, error) {
|
||||
var result DirectoryClusterEntry
|
||||
|
||||
// Do the attributes so we can determine if we're dealing with long names
|
||||
result.attr = DirectoryAttr(data[11])
|
||||
if (result.attr & AttrLongName) == AttrLongName {
|
||||
result.longOrd = data[0]
|
||||
|
||||
chars := make([]uint16, 13)
|
||||
for i := 0; i < 5; i++ {
|
||||
offset := 1 + (i * 2)
|
||||
chars[i] = binary.LittleEndian.Uint16(data[offset : offset+2])
|
||||
}
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
offset := 14 + (i * 2)
|
||||
chars[i+5] = binary.LittleEndian.Uint16(data[offset : offset+2])
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
offset := 28 + (i * 2)
|
||||
chars[i+11] = binary.LittleEndian.Uint16(data[offset : offset+2])
|
||||
}
|
||||
|
||||
result.longName = string(utf16.Decode(chars))
|
||||
result.longChecksum = data[13]
|
||||
} else {
|
||||
result.deleted = data[0] == 0xE5
|
||||
|
||||
// Basic attributes
|
||||
if data[0] == 0x05 {
|
||||
data[0] = 0xE5
|
||||
}
|
||||
|
||||
result.name = string(data[0:8])
|
||||
result.ext = string(data[8:11])
|
||||
|
||||
// Creation time
|
||||
createTimeTenths := data[13]
|
||||
createTimeWord := binary.LittleEndian.Uint16(data[14:16])
|
||||
createDateWord := binary.LittleEndian.Uint16(data[16:18])
|
||||
result.createTime = decodeDOSTime(createDateWord, createTimeWord, createTimeTenths)
|
||||
|
||||
// Access time
|
||||
accessDateWord := binary.LittleEndian.Uint16(data[18:20])
|
||||
result.accessTime = decodeDOSTime(accessDateWord, 0, 0)
|
||||
|
||||
// Write time
|
||||
writeTimeWord := binary.LittleEndian.Uint16(data[22:24])
|
||||
writeDateWord := binary.LittleEndian.Uint16(data[24:26])
|
||||
result.writeTime = decodeDOSTime(writeDateWord, writeTimeWord, 0)
|
||||
|
||||
// Cluster
|
||||
result.cluster = uint32(binary.LittleEndian.Uint16(data[20:22]))
|
||||
result.cluster <<= 4
|
||||
result.cluster |= uint32(binary.LittleEndian.Uint16(data[26:28]))
|
||||
|
||||
// File size
|
||||
result.fileSize = binary.LittleEndian.Uint32(data[28:32])
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// NewLongDirectoryClusterEntry returns the series of directory cluster
|
||||
// entries that need to be written for a long directory entry. This list
|
||||
// of entries does NOT contain the short name entry.
|
||||
func NewLongDirectoryClusterEntry(name string, shortName string) ([]*DirectoryClusterEntry, error) {
|
||||
// Split up the shortName properly
|
||||
checksum := checksumShortName(shortNameEntryValue(shortName))
|
||||
|
||||
// Calcualte the number of entries we'll actually need to store
|
||||
// the long name.
|
||||
numLongEntries := len(name) / 13
|
||||
if len(name)%13 != 0 {
|
||||
numLongEntries++
|
||||
}
|
||||
|
||||
entries := make([]*DirectoryClusterEntry, numLongEntries)
|
||||
for i := 0; i < numLongEntries; i++ {
|
||||
entries[i] = new(DirectoryClusterEntry)
|
||||
entry := entries[i]
|
||||
entry.attr = AttrLongName
|
||||
entry.longOrd = uint8(numLongEntries - i)
|
||||
|
||||
if i == 0 {
|
||||
entry.longOrd |= LastLongEntryMask
|
||||
}
|
||||
|
||||
// Calculate the offsets of the string for this entry
|
||||
j := (numLongEntries - i - 1) * 13
|
||||
k := j + 13
|
||||
if k > len(name) {
|
||||
k = len(name)
|
||||
}
|
||||
|
||||
entry.longChecksum = checksum
|
||||
entry.longName = name[j:k]
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func decodeDOSTime(date, dosTime uint16, tenths uint8) time.Time {
|
||||
return time.Date(
|
||||
1980+int(date>>9),
|
||||
time.Month((date>>5)&0x0F),
|
||||
int(date&0x1F),
|
||||
int(dosTime>>11),
|
||||
int((dosTime>>5)&0x3F),
|
||||
int((dosTime&0x1F)*2),
|
||||
int(tenths)*10*int(time.Millisecond),
|
||||
time.Local)
|
||||
}
|
||||
|
||||
func encodeDOSTime(t time.Time) (uint16, uint16, uint8) {
|
||||
var date uint16 = uint16((t.Year() - 1980) << 9)
|
||||
date |= uint16(t.Month()) << 5
|
||||
date += uint16(t.Day() & 0xFF)
|
||||
|
||||
var time uint16 = uint16(t.Hour() << 11)
|
||||
time |= uint16(t.Minute() << 5)
|
||||
time += uint16(t.Second() / 2)
|
||||
|
||||
var tenths uint8
|
||||
// TODO(mitchellh): Do tenths
|
||||
|
||||
return date, time, tenths
|
||||
}
|
268
vendor/github.com/mitchellh/go-fs/fat/fat.go
generated
vendored
Normal file
268
vendor/github.com/mitchellh/go-fs/fat/fat.go
generated
vendored
Normal file
@ -0,0 +1,268 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-fs"
|
||||
"math"
|
||||
)
|
||||
|
||||
// The first cluster that can really hold user data is always 2
|
||||
const FirstCluster = 2
|
||||
|
||||
// FAT is the actual file allocation table data structure that is
|
||||
// stored on disk to describe the various clusters on the disk.
|
||||
type FAT struct {
|
||||
bs *BootSectorCommon
|
||||
entries []uint32
|
||||
}
|
||||
|
||||
func DecodeFAT(device fs.BlockDevice, bs *BootSectorCommon, n int) (*FAT, error) {
|
||||
if n > int(bs.NumFATs) {
|
||||
return nil, fmt.Errorf("FAT #%d greater than total FATs: %d", n, bs.NumFATs)
|
||||
}
|
||||
|
||||
data := make([]byte, bs.SectorsPerFat*uint32(bs.BytesPerSector))
|
||||
if _, err := device.ReadAt(data, int64(bs.FATOffset(n))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &FAT{
|
||||
bs: bs,
|
||||
entries: make([]uint32, FATEntryCount(bs)),
|
||||
}
|
||||
|
||||
fatType := bs.FATType()
|
||||
for i := 0; i < int(FATEntryCount(bs)); i++ {
|
||||
var entryData uint32
|
||||
switch fatType {
|
||||
case FAT12:
|
||||
entryData = fatReadEntry12(data, i)
|
||||
case FAT16:
|
||||
entryData = fatReadEntry16(data, i)
|
||||
default:
|
||||
entryData = fatReadEntry32(data, i)
|
||||
}
|
||||
|
||||
result.entries[i] = entryData
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// NewFAT creates a new FAT data structure, properly initialized.
|
||||
func NewFAT(bs *BootSectorCommon) (*FAT, error) {
|
||||
result := &FAT{
|
||||
bs: bs,
|
||||
entries: make([]uint32, FATEntryCount(bs)),
|
||||
}
|
||||
|
||||
// Set the initial two entries according to spec
|
||||
result.entries[0] = (uint32(bs.Media) & 0xFF) |
|
||||
(0xFFFFFF00 & result.entryMask())
|
||||
result.entries[1] = 0xFFFFFFFF & result.entryMask()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Bytes returns the raw bytes for the FAT that should be written to
|
||||
// the block device.
|
||||
func (f *FAT) Bytes() []byte {
|
||||
result := make([]byte, f.bs.SectorsPerFat*uint32(f.bs.BytesPerSector))
|
||||
|
||||
for i, entry := range f.entries {
|
||||
switch f.bs.FATType() {
|
||||
case FAT12:
|
||||
f.writeEntry12(result, i, entry)
|
||||
case FAT16:
|
||||
f.writeEntry16(result, i, entry)
|
||||
default:
|
||||
f.writeEntry32(result, i, entry)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (f *FAT) AllocChain() (uint32, error) {
|
||||
return f.allocNew()
|
||||
}
|
||||
|
||||
func (f *FAT) allocNew() (uint32, error) {
|
||||
dataSize := (f.bs.TotalSectors * uint32(f.bs.BytesPerSector))
|
||||
dataSize -= f.bs.DataOffset()
|
||||
clusterCount := dataSize / f.bs.BytesPerCluster()
|
||||
lastClusterIndex := clusterCount + FirstCluster
|
||||
|
||||
var availIdx uint32
|
||||
found := false
|
||||
for i := uint32(FirstCluster); i < lastClusterIndex; i++ {
|
||||
if f.entries[i] == 0 {
|
||||
availIdx = i
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return 0, errors.New("FAT FULL")
|
||||
}
|
||||
|
||||
// Mark that this is now in use
|
||||
f.entries[availIdx] = 0xFFFFFFFF & f.entryMask()
|
||||
|
||||
return availIdx, nil
|
||||
}
|
||||
|
||||
// Chain returns the chain of clusters starting at a certain cluster.
|
||||
func (f *FAT) Chain(start uint32) []uint32 {
|
||||
chain := make([]uint32, 0, 2)
|
||||
|
||||
cluster := start
|
||||
for {
|
||||
chain = append(chain, cluster)
|
||||
cluster = f.entries[cluster]
|
||||
|
||||
if f.isEofCluster(cluster) || cluster == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return chain
|
||||
}
|
||||
|
||||
// ResizeChain takes a given cluster number and resizes the chain
|
||||
// to the given length. It returns the new chain of clusters.
|
||||
func (f *FAT) ResizeChain(start uint32, length int) ([]uint32, error) {
|
||||
chain := f.Chain(start)
|
||||
if len(chain) == length {
|
||||
return chain, nil
|
||||
}
|
||||
|
||||
change := int(math.Abs(float64(length - len(chain))))
|
||||
if length > len(chain) {
|
||||
var lastCluster uint32
|
||||
|
||||
lastCluster = chain[0]
|
||||
for i := 1; i < len(chain); i++ {
|
||||
if f.isEofCluster(f.entries[lastCluster]) {
|
||||
break
|
||||
}
|
||||
|
||||
lastCluster = chain[i]
|
||||
}
|
||||
|
||||
for i := 0; i < change; i++ {
|
||||
newCluster, err := f.allocNew()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.entries[lastCluster] = newCluster
|
||||
lastCluster = newCluster
|
||||
}
|
||||
} else {
|
||||
panic("making chains smaller not implemented yet")
|
||||
}
|
||||
|
||||
return f.Chain(start), nil
|
||||
}
|
||||
|
||||
func (f *FAT) WriteToDevice(device fs.BlockDevice) error {
|
||||
fatBytes := f.Bytes()
|
||||
for i := 0; i < int(f.bs.NumFATs); i++ {
|
||||
offset := int64(f.bs.FATOffset(i))
|
||||
if _, err := device.WriteAt(fatBytes, offset); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FAT) entryMask() uint32 {
|
||||
switch f.bs.FATType() {
|
||||
case FAT12:
|
||||
return 0x0FFF
|
||||
case FAT16:
|
||||
return 0xFFFF
|
||||
default:
|
||||
return 0x0FFFFFFF
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FAT) isEofCluster(cluster uint32) bool {
|
||||
return cluster >= (0xFFFFFF8 & f.entryMask())
|
||||
}
|
||||
|
||||
func (f *FAT) writeEntry12(data []byte, idx int, entry uint32) {
|
||||
dataIdx := idx + (idx / 2)
|
||||
data = data[dataIdx : dataIdx+2]
|
||||
|
||||
if idx%2 == 1 {
|
||||
// ODD
|
||||
data[0] |= byte((entry & 0x0F) << 4)
|
||||
data[1] = byte((entry >> 4) & 0xFF)
|
||||
} else {
|
||||
// Even
|
||||
data[0] = byte(entry & 0xFF)
|
||||
data[1] = byte((entry >> 8) & 0x0F)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FAT) writeEntry16(data []byte, idx int, entry uint32) {
|
||||
idx <<= 1
|
||||
data[idx] = byte(entry & 0xFF)
|
||||
data[idx+1] = byte((entry >> 8) & 0xFF)
|
||||
}
|
||||
|
||||
func (f *FAT) writeEntry32(data []byte, idx int, entry uint32) {
|
||||
idx <<= 2
|
||||
data[idx] = byte(entry & 0xFF)
|
||||
data[idx+1] = byte((entry >> 8) & 0xFF)
|
||||
data[idx+2] = byte((entry >> 16) & 0xFF)
|
||||
data[idx+3] = byte((entry >> 24) & 0xFF)
|
||||
}
|
||||
|
||||
// FATEntryCount returns the number of entries per fat for the given
|
||||
// boot sector.
|
||||
func FATEntryCount(bs *BootSectorCommon) uint32 {
|
||||
// Determine the number of entries that'll go in the FAT.
|
||||
var entryCount uint32 = bs.SectorsPerFat * uint32(bs.BytesPerSector)
|
||||
switch bs.FATType() {
|
||||
case FAT12:
|
||||
entryCount = uint32((uint64(entryCount) * 8) / 12)
|
||||
case FAT16:
|
||||
entryCount /= 2
|
||||
case FAT32:
|
||||
entryCount /= 4
|
||||
default:
|
||||
panic("impossible fat type")
|
||||
}
|
||||
|
||||
return entryCount
|
||||
}
|
||||
|
||||
func fatReadEntry12(data []byte, idx int) uint32 {
|
||||
idx += idx / 2
|
||||
|
||||
var result uint32 = (uint32(data[idx+1]) << 8) | uint32(data[idx])
|
||||
if idx%2 == 0 {
|
||||
return result & 0xFFF
|
||||
} else {
|
||||
return result >> 4
|
||||
}
|
||||
}
|
||||
|
||||
func fatReadEntry16(data []byte, idx int) uint32 {
|
||||
idx <<= 1
|
||||
return (uint32(data[idx+1]) << 8) | uint32(data[idx])
|
||||
}
|
||||
|
||||
func fatReadEntry32(data []byte, idx int) uint32 {
|
||||
idx <<= 2
|
||||
return (uint32(data[idx+3]) << 24) |
|
||||
(uint32(data[idx+2]) << 16) |
|
||||
(uint32(data[idx+1]) << 8) |
|
||||
uint32(data[idx+0])
|
||||
}
|
26
vendor/github.com/mitchellh/go-fs/fat/file.go
generated
vendored
Normal file
26
vendor/github.com/mitchellh/go-fs/fat/file.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
package fat
|
||||
|
||||
type File struct {
|
||||
chain *ClusterChain
|
||||
dir *Directory
|
||||
entry *DirectoryClusterEntry
|
||||
}
|
||||
|
||||
func (f *File) Read(p []byte) (n int, err error) {
|
||||
return f.chain.Read(p)
|
||||
}
|
||||
|
||||
func (f *File) Write(p []byte) (n int, err error) {
|
||||
lastByte := f.chain.writeOffset + uint32(len(p))
|
||||
if lastByte > f.entry.fileSize {
|
||||
// Increase the file size since we're writing past the end of the file
|
||||
f.entry.fileSize = lastByte
|
||||
|
||||
// Write the entry out
|
||||
if err := f.dir.dirCluster.WriteToDevice(f.dir.device, f.dir.fat); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return f.chain.Write(p)
|
||||
}
|
57
vendor/github.com/mitchellh/go-fs/fat/filesystem.go
generated
vendored
Normal file
57
vendor/github.com/mitchellh/go-fs/fat/filesystem.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/go-fs"
|
||||
)
|
||||
|
||||
// FileSystem is the implementation of fs.FileSystem that can read a
|
||||
// FAT filesystem.
|
||||
type FileSystem struct {
|
||||
bs *BootSectorCommon
|
||||
device fs.BlockDevice
|
||||
fat *FAT
|
||||
rootDir *DirectoryCluster
|
||||
}
|
||||
|
||||
// New returns a new FileSystem for accessing a previously created
|
||||
// FAT filesystem.
|
||||
func New(device fs.BlockDevice) (*FileSystem, error) {
|
||||
bs, err := DecodeBootSector(device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fat, err := DecodeFAT(device, bs, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rootDir *DirectoryCluster
|
||||
if bs.FATType() == FAT32 {
|
||||
panic("FAT32 not implemented yet")
|
||||
} else {
|
||||
rootDir, err = DecodeFAT16RootDirectoryCluster(device, bs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result := &FileSystem{
|
||||
bs: bs,
|
||||
device: device,
|
||||
fat: fat,
|
||||
rootDir: rootDir,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f *FileSystem) RootDir() (fs.Directory, error) {
|
||||
dir := &Directory{
|
||||
device: f.device,
|
||||
dirCluster: f.rootDir,
|
||||
fat: f.fat,
|
||||
}
|
||||
|
||||
return dir, nil
|
||||
}
|
166
vendor/github.com/mitchellh/go-fs/fat/short_name.go
generated
vendored
Normal file
166
vendor/github.com/mitchellh/go-fs/fat/short_name.go
generated
vendored
Normal file
@ -0,0 +1,166 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// checksumShortName returns the checksum for the shortname that is used
|
||||
// for the long name entries.
|
||||
func checksumShortName(name string) uint8 {
|
||||
var sum uint8 = name[0]
|
||||
for i := uint8(1); i < 11; i++ {
|
||||
sum = name[i] + (((sum & 1) << 7) + ((sum & 0xFE) >> 1))
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
// generateShortName takes a list of existing short names and a long
|
||||
// name and generates the next valid short name. This process is done
|
||||
// according to the MS specification.
|
||||
func generateShortName(longName string, used []string) (string, error) {
|
||||
longName = strings.ToUpper(longName)
|
||||
|
||||
// Split the string at the final "."
|
||||
dotIdx := strings.LastIndex(longName, ".")
|
||||
|
||||
var ext string
|
||||
if dotIdx == -1 {
|
||||
dotIdx = len(longName)
|
||||
} else {
|
||||
ext = longName[dotIdx+1 : len(longName)]
|
||||
}
|
||||
|
||||
ext = cleanShortString(ext)
|
||||
ext = ext[0:len(ext)]
|
||||
rawName := longName[0:dotIdx]
|
||||
name := cleanShortString(rawName)
|
||||
simpleName := fmt.Sprintf("%s.%s", name, ext)
|
||||
if ext == "" {
|
||||
simpleName = simpleName[0 : len(simpleName)-1]
|
||||
}
|
||||
|
||||
doSuffix := name != rawName || len(name) > 8
|
||||
if !doSuffix {
|
||||
for _, usedSingle := range used {
|
||||
if strings.ToUpper(usedSingle) == simpleName {
|
||||
doSuffix = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if doSuffix {
|
||||
found := false
|
||||
for i := 1; i < 99999; i++ {
|
||||
serial := fmt.Sprintf("~%d", i)
|
||||
|
||||
nameOffset := 8 - len(serial)
|
||||
if len(name) < nameOffset {
|
||||
nameOffset = len(name)
|
||||
}
|
||||
|
||||
serialName := fmt.Sprintf("%s%s", name[0:nameOffset], serial)
|
||||
simpleName = fmt.Sprintf("%s.%s", serialName, ext)
|
||||
|
||||
exists := false
|
||||
for _, usedSingle := range used {
|
||||
if strings.ToUpper(usedSingle) == simpleName {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return "", fmt.Errorf("could not generate short name for %s", longName)
|
||||
}
|
||||
}
|
||||
|
||||
return simpleName, nil
|
||||
}
|
||||
|
||||
// shortNameEntryValue returns the proper formatted short name value
|
||||
// for the directory cluster entry.
|
||||
func shortNameEntryValue(name string) string {
|
||||
var shortParts []string
|
||||
if name == "." || name == ".." {
|
||||
shortParts = []string{name, ""}
|
||||
} else {
|
||||
shortParts = strings.Split(name, ".")
|
||||
}
|
||||
|
||||
if len(shortParts) == 1 {
|
||||
shortParts = append(shortParts, "")
|
||||
}
|
||||
|
||||
if len(shortParts[0]) < 8 {
|
||||
var temp bytes.Buffer
|
||||
temp.WriteString(shortParts[0])
|
||||
for i := 0; i < 8-len(shortParts[0]); i++ {
|
||||
temp.WriteRune(' ')
|
||||
}
|
||||
|
||||
shortParts[0] = temp.String()
|
||||
}
|
||||
|
||||
if len(shortParts[1]) < 3 {
|
||||
var temp bytes.Buffer
|
||||
temp.WriteString(shortParts[1])
|
||||
for i := 0; i < 3-len(shortParts[1]); i++ {
|
||||
temp.WriteRune(' ')
|
||||
}
|
||||
|
||||
shortParts[1] = temp.String()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", shortParts[0], shortParts[1])
|
||||
}
|
||||
|
||||
func cleanShortString(v string) string {
|
||||
var result bytes.Buffer
|
||||
for _, char := range v {
|
||||
// We skip these chars
|
||||
if char == '.' || char == ' ' {
|
||||
continue
|
||||
}
|
||||
|
||||
if !validShortChar(char) {
|
||||
char = '_'
|
||||
}
|
||||
|
||||
result.WriteRune(char)
|
||||
}
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func validShortChar(char rune) bool {
|
||||
if char >= 'A' && char <= 'Z' {
|
||||
return true
|
||||
}
|
||||
|
||||
if char >= '0' && char <= '9' {
|
||||
return true
|
||||
}
|
||||
|
||||
validShortSymbols := []rune{
|
||||
'_', '^', '$', '~', '!', '#', '%', '&', '-', '{', '}', '(',
|
||||
')', '@', '\'', '`',
|
||||
}
|
||||
|
||||
for _, valid := range validShortSymbols {
|
||||
if char == valid {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
250
vendor/github.com/mitchellh/go-fs/fat/super_floppy.go
generated
vendored
Normal file
250
vendor/github.com/mitchellh/go-fs/fat/super_floppy.go
generated
vendored
Normal file
@ -0,0 +1,250 @@
|
||||
package fat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-fs"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SuperFloppyConfig is the configuration for various properties of
|
||||
// a new super floppy formatted block device. Once this configuration is used
|
||||
// to format a device, it must not be modified.
|
||||
type SuperFloppyConfig struct {
|
||||
// The type of FAT filesystem to use.
|
||||
FATType FATType
|
||||
|
||||
// The label of the drive. Defaults to "NONAME"
|
||||
Label string
|
||||
|
||||
// The OEM name for the FAT filesystem. Defaults to "gofs" if not set.
|
||||
OEMName string
|
||||
}
|
||||
|
||||
// Formats an fs.BlockDevice with the "super floppy" format according
|
||||
// to the given configuration. The "super floppy" standard means that the
|
||||
// device will be formatted so that it does not contain a partition table.
|
||||
// Instead, the entire device holds a single FAT file system.
|
||||
func FormatSuperFloppy(device fs.BlockDevice, config *SuperFloppyConfig) error {
|
||||
formatter := &superFloppyFormatter{
|
||||
config: config,
|
||||
device: device,
|
||||
}
|
||||
|
||||
return formatter.format()
|
||||
}
|
||||
|
||||
// An internal struct that helps maintain state and perform calculations
|
||||
// during a single formatting pass.
|
||||
type superFloppyFormatter struct {
|
||||
config *SuperFloppyConfig
|
||||
device fs.BlockDevice
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) format() error {
|
||||
// First, create the boot sector on the device. Start by configuring
|
||||
// the common elements of the boot sector.
|
||||
sectorsPerCluster, err := f.SectorsPerCluster()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bsCommon := BootSectorCommon{
|
||||
BytesPerSector: uint16(f.device.SectorSize()),
|
||||
Media: MediaFixed,
|
||||
NumFATs: 2,
|
||||
NumHeads: 16,
|
||||
OEMName: f.config.OEMName,
|
||||
ReservedSectorCount: f.ReservedSectorCount(),
|
||||
SectorsPerCluster: sectorsPerCluster,
|
||||
SectorsPerTrack: 32,
|
||||
TotalSectors: uint32(f.device.Len() / int64(f.device.SectorSize())),
|
||||
}
|
||||
|
||||
// Next, fill in the FAT-type specific boot sector information
|
||||
switch f.config.FATType {
|
||||
case FAT12, FAT16:
|
||||
// Determine the filesystem type label, standard from the spec sheet
|
||||
var label string
|
||||
if f.config.FATType == FAT12 {
|
||||
label = "FAT12 "
|
||||
} else {
|
||||
label = "FAT16 "
|
||||
}
|
||||
|
||||
// Determine the number of root directory entries
|
||||
if f.device.Len() > 512*5*32 {
|
||||
bsCommon.RootEntryCount = 512
|
||||
} else {
|
||||
bsCommon.RootEntryCount = uint16(f.device.Len() / (5 * 32))
|
||||
}
|
||||
|
||||
bsCommon.SectorsPerFat = f.sectorsPerFat(bsCommon.RootEntryCount, sectorsPerCluster)
|
||||
|
||||
bs := &BootSectorFat16{
|
||||
BootSectorCommon: bsCommon,
|
||||
FileSystemTypeLabel: label,
|
||||
VolumeLabel: f.config.Label,
|
||||
}
|
||||
|
||||
// Write the boot sector
|
||||
bsBytes, err := bs.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := f.device.WriteAt(bsBytes, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
case FAT32:
|
||||
bsCommon.SectorsPerFat = f.sectorsPerFat(0, sectorsPerCluster)
|
||||
|
||||
bs := &BootSectorFat32{
|
||||
BootSectorCommon: bsCommon,
|
||||
FileSystemTypeLabel: "FAT32 ",
|
||||
FSInfoSector: 1,
|
||||
VolumeID: uint32(time.Now().Unix()),
|
||||
VolumeLabel: f.config.Label,
|
||||
}
|
||||
|
||||
// Write the boot sector
|
||||
bsBytes, err := bs.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := f.device.WriteAt(bsBytes, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO(mitchellh): Create the fsinfo structure
|
||||
// TODO(mitchellh): write the boot sector copy
|
||||
default:
|
||||
return fmt.Errorf("Unknown FAT type: %d", f.config.FATType)
|
||||
}
|
||||
|
||||
// Create the FATs
|
||||
fat, err := NewFAT(&bsCommon)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write the FAT
|
||||
if err := fat.WriteToDevice(f.device); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rootDir *DirectoryCluster
|
||||
if f.config.FATType == FAT32 {
|
||||
panic("TODO")
|
||||
} else {
|
||||
rootDir, err = NewFat16RootDirectoryCluster(&bsCommon, f.config.Label)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
offset := int64(bsCommon.RootDirOffset())
|
||||
if _, err := f.device.WriteAt(rootDir.Bytes(), offset); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) ReservedSectorCount() uint16 {
|
||||
if f.config.FATType == FAT32 {
|
||||
return 32
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) SectorsPerCluster() (uint8, error) {
|
||||
if f.config.FATType == FAT12 {
|
||||
return f.defaultSectorsPerCluster12()
|
||||
} else if f.config.FATType == FAT16 {
|
||||
return f.defaultSectorsPerCluster16()
|
||||
} else {
|
||||
return f.defaultSectorsPerCluster32()
|
||||
}
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) defaultSectorsPerCluster12() (uint8, error) {
|
||||
var result uint8 = 1
|
||||
sectors := f.device.Len() / int64(f.device.SectorSize())
|
||||
|
||||
for (sectors / int64(result)) > 4084 {
|
||||
result *= 2
|
||||
if int(result)*f.device.SectorSize() > 4096 {
|
||||
return 0, errors.New("disk too large for FAT12")
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) defaultSectorsPerCluster16() (uint8, error) {
|
||||
sectors := f.device.Len() / int64(f.device.SectorSize())
|
||||
|
||||
if sectors <= 8400 {
|
||||
return 0, errors.New("disk too small for FAT16")
|
||||
} else if sectors > 4194304 {
|
||||
return 0, errors.New("disk too large for FAT16")
|
||||
}
|
||||
|
||||
switch {
|
||||
case sectors > 2097152:
|
||||
return 64, nil
|
||||
case sectors > 1048576:
|
||||
return 32, nil
|
||||
case sectors > 524288:
|
||||
return 16, nil
|
||||
case sectors > 262144:
|
||||
return 8, nil
|
||||
case sectors > 32680:
|
||||
return 4, nil
|
||||
default:
|
||||
return 2, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) defaultSectorsPerCluster32() (uint8, error) {
|
||||
sectors := f.device.Len() / int64(f.device.SectorSize())
|
||||
|
||||
if sectors <= 66600 {
|
||||
return 0, errors.New("disk too small for FAT32")
|
||||
}
|
||||
|
||||
switch {
|
||||
case sectors > 67108864:
|
||||
return 64, nil
|
||||
case sectors > 33554432:
|
||||
return 32, nil
|
||||
case sectors > 16777216:
|
||||
return 16, nil
|
||||
case sectors > 532480:
|
||||
return 8, nil
|
||||
default:
|
||||
return 1, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) fatCount() uint8 {
|
||||
return 2
|
||||
}
|
||||
|
||||
func (f *superFloppyFormatter) sectorsPerFat(rootEntCount uint16, sectorsPerCluster uint8) uint32 {
|
||||
bytesPerSec := f.device.SectorSize()
|
||||
totalSectors := int(f.device.Len()) / bytesPerSec
|
||||
rootDirSectors := ((int(rootEntCount) * 32) + (bytesPerSec - 1)) / bytesPerSec
|
||||
|
||||
tmp1 := totalSectors - (int(f.ReservedSectorCount()) + rootDirSectors)
|
||||
tmp2 := (256 * int(sectorsPerCluster)) + int(f.fatCount())
|
||||
|
||||
if f.config.FATType == FAT32 {
|
||||
tmp2 /= 2
|
||||
}
|
||||
|
||||
return uint32((tmp1 + (tmp2 - 1)) / tmp2)
|
||||
}
|
26
vendor/github.com/mitchellh/go-fs/fat/type.go
generated
vendored
Normal file
26
vendor/github.com/mitchellh/go-fs/fat/type.go
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
package fat
|
||||
|
||||
import "github.com/mitchellh/go-fs"
|
||||
|
||||
// FATType is a simple enum of the available FAT filesystem types.
|
||||
type FATType uint8
|
||||
|
||||
const (
|
||||
FAT12 FATType = iota
|
||||
FAT16
|
||||
FAT32
|
||||
)
|
||||
|
||||
// TypeForDevice determines the usable FAT type based solely on
|
||||
// size information about the block device.
|
||||
func TypeForDevice(device fs.BlockDevice) FATType {
|
||||
sizeInMB := device.Len() / (1024 * 1024)
|
||||
switch {
|
||||
case sizeInMB < 4:
|
||||
return FAT12
|
||||
case sizeInMB < 512:
|
||||
return FAT16
|
||||
default:
|
||||
return FAT32
|
||||
}
|
||||
}
|
9
vendor/github.com/mitchellh/go-fs/file.go
generated
vendored
Normal file
9
vendor/github.com/mitchellh/go-fs/file.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
package fs
|
||||
|
||||
import "io"
|
||||
|
||||
// File is a single file within a filesystem.
|
||||
type File interface {
|
||||
io.Reader
|
||||
io.Writer
|
||||
}
|
52
vendor/github.com/mitchellh/go-fs/file_disk.go
generated
vendored
Normal file
52
vendor/github.com/mitchellh/go-fs/file_disk.go
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
// A FileDisk is an implementation of a BlockDevice that uses a
|
||||
// *os.File as its backing store.
|
||||
type FileDisk struct {
|
||||
f *os.File
|
||||
size int64
|
||||
}
|
||||
|
||||
// NewFileDisk creates a new FileDisk from the given *os.File. The
|
||||
// file must already be created and set the to the proper size.
|
||||
func NewFileDisk(f *os.File) (*FileDisk, error) {
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
return nil, errors.New("file is a directory")
|
||||
}
|
||||
|
||||
return &FileDisk{
|
||||
f: f,
|
||||
size: fi.Size(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *FileDisk) Close() error {
|
||||
return f.f.Close()
|
||||
}
|
||||
|
||||
func (f *FileDisk) Len() int64 {
|
||||
return f.size
|
||||
}
|
||||
|
||||
func (f *FileDisk) ReadAt(p []byte, off int64) (int, error) {
|
||||
return f.f.ReadAt(p, off)
|
||||
}
|
||||
|
||||
func (f *FileDisk) SectorSize() int {
|
||||
// Hardcoded for now, one day we may want to make this customizable
|
||||
return 512
|
||||
}
|
||||
|
||||
func (f *FileDisk) WriteAt(p []byte, off int64) (int, error) {
|
||||
return f.f.WriteAt(p, off)
|
||||
}
|
8
vendor/github.com/mitchellh/go-fs/filesystem.go
generated
vendored
Normal file
8
vendor/github.com/mitchellh/go-fs/filesystem.go
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
package fs
|
||||
|
||||
// A FileSystem provides access to a tree hierarchy of directories
|
||||
// and files.
|
||||
type FileSystem interface {
|
||||
// RootDir returns the single root directory.
|
||||
RootDir() (Directory, error)
|
||||
}
|
21
vendor/github.com/mitchellh/iochan/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/mitchellh/iochan/LICENSE.md
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Mitchell Hashimoto
|
||||
|
||||
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.
|
13
vendor/github.com/mitchellh/iochan/README.md
generated
vendored
Normal file
13
vendor/github.com/mitchellh/iochan/README.md
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
# iochan
|
||||
|
||||
iochan is a Go library for treating `io` readers and writers like channels.
|
||||
This is useful when sometimes you wish to use `io.Reader` and such in `select`
|
||||
statements.
|
||||
|
||||
## Installation
|
||||
|
||||
Standard `go get`:
|
||||
|
||||
```
|
||||
$ go get github.com/mitchellh/iochan
|
||||
```
|
41
vendor/github.com/mitchellh/iochan/iochan.go
generated
vendored
Normal file
41
vendor/github.com/mitchellh/iochan/iochan.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package iochan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
)
|
||||
|
||||
// DelimReader takes an io.Reader and produces the contents of the reader
|
||||
// on the returned channel. The contents on the channel will be returned
|
||||
// on boundaries specified by the delim parameter, and will include this
|
||||
// delimiter.
|
||||
//
|
||||
// If an error occurs while reading from the reader, the reading will end.
|
||||
//
|
||||
// In the case of an EOF or error, the channel will be closed.
|
||||
//
|
||||
// This must only be called once for any individual reader. The behavior is
|
||||
// unknown and will be unexpected if this is called multiple times with the
|
||||
// same reader.
|
||||
func DelimReader(r io.Reader, delim byte) <-chan string {
|
||||
ch := make(chan string)
|
||||
|
||||
go func() {
|
||||
buf := bufio.NewReader(r)
|
||||
|
||||
for {
|
||||
line, err := buf.ReadString(delim)
|
||||
if line != "" {
|
||||
ch <- line
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user