├── .travis.yml ├── .pre-commit-config.yaml ├── Makefile ├── nop_test.go ├── handlers.go ├── README.md ├── events.go ├── handlers_test.go ├── events_test.go └── LICENSE /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: go 3 | sudo: false 4 | notifications: 5 | email: false 6 | go: 7 | - 1.6 8 | install: make deps 9 | script: make validate && make test 10 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | - repo: git://github.com/pre-commit/pre-commit-hooks 2 | sha: 44e1753f98b0da305332abe26856c3e621c5c439 3 | hooks: 4 | - id: detect-private-key 5 | - repo: git://github.com/containous/pre-commit-hooks 6 | sha: 35e641b5107671e94102b0ce909648559e568d61 7 | hooks: 8 | - id: goFmt 9 | - id: goLint 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all deps test validate vet lint fmt 2 | 3 | all: deps validate test-unit ## get dependencies, validate all checks and run tests 4 | 5 | deps: ## get dependencies 6 | go get -t ./... 7 | go get github.com/golang/lint/golint 8 | 9 | test-unit: ## run tests 10 | go test -timeout 10s -v -race -cover ./... 11 | 12 | validate: vet lint fmt ## validate gofmt, golint and go vet 13 | 14 | vet: 15 | go vet ./... 16 | 17 | lint: 18 | out="$$(golint ./...)"; \ 19 | if [ -n "$$(golint ./...)" ]; then \ 20 | echo "$$out"; \ 21 | exit 1; \ 22 | fi 23 | 24 | fmt: 25 | test -z "$(gofmt -s -l . | tee /dev/stderr)" 26 | 27 | help: ## this help 28 | @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) 29 | -------------------------------------------------------------------------------- /nop_test.go: -------------------------------------------------------------------------------- 1 | package events 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | 7 | "golang.org/x/net/context" 8 | 9 | "github.com/docker/engine-api/types" 10 | ) 11 | 12 | var ( 13 | errNoEngine = errors.New("Engine no longer exists") 14 | ) 15 | 16 | // NopClient is a nop API Client based on engine-api 17 | type NopClient struct { 18 | } 19 | 20 | // NewNopClient creates a new nop client 21 | func NewNopClient() *NopClient { 22 | return &NopClient{} 23 | } 24 | 25 | // ClientVersion returns the version string associated with this instance of the Client 26 | func (client *NopClient) ClientVersion() string { 27 | return "" 28 | } 29 | 30 | // Events returns a stream of events in the daemon in a ReadCloser 31 | func (client *NopClient) Events(ctx context.Context, options types.EventsOptions) (io.ReadCloser, error) { 32 | return nil, errNoEngine 33 | } 34 | 35 | // Info returns information about the docker server 36 | func (client *NopClient) Info(ctx context.Context) (types.Info, error) { 37 | return types.Info{}, errNoEngine 38 | } 39 | 40 | // RegistryLogin authenticates the docker server with a given docker registry 41 | func (client *NopClient) RegistryLogin(ctx context.Context, auth types.AuthConfig) (types.AuthResponse, error) { 42 | return types.AuthResponse{}, errNoEngine 43 | } 44 | -------------------------------------------------------------------------------- /handlers.go: -------------------------------------------------------------------------------- 1 | package events 2 | 3 | import ( 4 | "sync" 5 | 6 | eventtypes "github.com/docker/engine-api/types/events" 7 | ) 8 | 9 | // NewHandler creates an event handler using the specified function to qualify the message 10 | // and to route it to the correct handler. 11 | func NewHandler(fun func(eventtypes.Message) string) *Handler { 12 | return &Handler{ 13 | keyFunc: fun, 14 | handlers: make(map[string]func(eventtypes.Message)), 15 | } 16 | } 17 | 18 | // ByType is a qualify function based on message type. 19 | func ByType(e eventtypes.Message) string { 20 | return e.Type 21 | } 22 | 23 | // ByAction is a qualify function based on message action. 24 | func ByAction(e eventtypes.Message) string { 25 | return e.Action 26 | } 27 | 28 | // Handler is a struct holding the handlers by keys, and the function to get the 29 | // key from the message. 30 | type Handler struct { 31 | keyFunc func(eventtypes.Message) string 32 | handlers map[string]func(eventtypes.Message) 33 | mu sync.Mutex 34 | } 35 | 36 | // Handle registers a function has handler for the specified key. 37 | func (w *Handler) Handle(key string, h func(eventtypes.Message)) { 38 | w.mu.Lock() 39 | w.handlers[key] = h 40 | w.mu.Unlock() 41 | } 42 | 43 | // Watch ranges over the passed in event chan and processes the events based on the 44 | // handlers created for a given action. 45 | // To stop watching, close the event chan. 46 | func (w *Handler) Watch(c <-chan eventtypes.Message) { 47 | for e := range c { 48 | w.mu.Lock() 49 | h, exists := w.handlers[w.keyFunc(e)] 50 | w.mu.Unlock() 51 | if !exists { 52 | continue 53 | } 54 | go h(e) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-events 2 | [![GoDoc](https://godoc.org/github.com/vdemeester/docker-events?status.png)](https://godoc.org/github.com/vdemeester/docker-events) 3 | [![Build Status](https://travis-ci.org/vdemeester/docker-events.svg?branch=master)](https://travis-ci.org/vdemeester/docker-events) 4 | [![Go Report Card](https://goreportcard.com/badge/github.com/vdemeester/docker-events)](https://goreportcard.com/report/github.com/vdemeester/docker-events) 5 | [![License](https://img.shields.io/github/license/vdemeester/docker-events.svg)]() 6 | 7 | **With 8 | [moving event stream processing to engine api](https://github.com/docker/docker/pull/25853) merged 9 | in docker/docker, this project is obsolete and won't work with the 10 | latest docker/docker/client api.** 11 | 12 | A really small library with the intent to ease the use of `Events` 13 | method of `engine-api`. 14 | 15 | ## Usage 16 | 17 | It should be pretty straighforward to use : 18 | 19 | ```go 20 | import "events" 21 | 22 | // […] 23 | 24 | cli, err := client.NewEnvClient() 25 | if err != nil { 26 | // Do something.. 27 | } 28 | 29 | cxt, cancel := context.WithCancel(context.Background()) 30 | // Call cancel() to get out of the monitor 31 | 32 | errChan := events.Monitor(ctx, cli, types.EventsOptions{}, func(event eventtypes.Message) { 33 | fmt.Printf("%v\n", event) 34 | }) 35 | 36 | if err := <-errChan; err != nil { 37 | // Do something 38 | } 39 | ``` 40 | 41 | It's also possible to do a little more advanced stuff using 42 | `EventHandler` : 43 | 44 | ```go 45 | import "events" 46 | 47 | // […] 48 | 49 | cli, err := client.NewEnvClient() 50 | if err != nil { 51 | // Do something.. 52 | } 53 | 54 | // Setup the event handler 55 | eventHandler := events.NewHandler(events.ByAction) 56 | eventHandler.Handle("create", func(m eventtypes.Message) { 57 | // Do something in case of create message 58 | }) 59 | 60 | stoppedOrDead := func(m eventtypes.Message) { 61 | // Do something in case of stop or die message as it might be the 62 | // same way to react. 63 | } 64 | 65 | eventHandler.Handle("die", stoppedOrDead) 66 | eventHandler.Handle("stop", stoppedOrDead) 67 | 68 | // The other type of message will be discard. 69 | 70 | // Filter the events we wams so receive 71 | filters := filters.NewArgs() 72 | filters.Add("type", "container") 73 | options := types.EventsOptions{ 74 | Filters: filters, 75 | } 76 | 77 | cxt, cancel := context.WithCancel(context.Background()) 78 | // Call cancel() to get out of the monitor 79 | 80 | errChan := events.MonitorWithHandler(ctx, cli, options, eventHandler) 81 | 82 | if err := <-errChan; err != nil { 83 | // Do something 84 | } 85 | ``` 86 | -------------------------------------------------------------------------------- /events.go: -------------------------------------------------------------------------------- 1 | package events 2 | 3 | import ( 4 | "encoding/json" 5 | "io" 6 | 7 | "golang.org/x/net/context" 8 | 9 | "github.com/docker/engine-api/client" 10 | "github.com/docker/engine-api/types" 11 | eventtypes "github.com/docker/engine-api/types/events" 12 | ) 13 | 14 | // Monitor subscribes to the docker events api using engine api and will execute the 15 | // specified function on each message. 16 | // It will pass the specified options to the underline method (i.e Events). 17 | func Monitor(ctx context.Context, cli client.SystemAPIClient, options types.EventsOptions, fun func(m eventtypes.Message)) chan error { 18 | handler := NewHandler(func(_ eventtypes.Message) string { 19 | // Let's return always the same thing to not filter at all 20 | return "" 21 | }) 22 | handler.Handle("", fun) 23 | 24 | return MonitorWithHandler(ctx, cli, options, handler) 25 | } 26 | 27 | // MonitorWithHandler subscribes to the docker events api using engine api and will pass the message 28 | // to the specified Handler, that will take care of it. 29 | // It will pass the specified options to the underline method (i.e Events). 30 | func MonitorWithHandler(ctx context.Context, cli client.SystemAPIClient, options types.EventsOptions, handler *Handler) chan error { 31 | eventChan := make(chan eventtypes.Message) 32 | errChan := make(chan error) 33 | started := make(chan struct{}) 34 | 35 | go handler.Watch(eventChan) 36 | go monitorEvents(ctx, cli, options, started, eventChan, errChan) 37 | 38 | go func() { 39 | for { 40 | select { 41 | case <-ctx.Done(): 42 | // close(eventChan) 43 | errChan <- nil 44 | } 45 | } 46 | }() 47 | 48 | <-started 49 | return errChan 50 | } 51 | 52 | func monitorEvents(ctx context.Context, cli client.SystemAPIClient, options types.EventsOptions, started chan struct{}, eventChan chan eventtypes.Message, errChan chan error) { 53 | body, err := cli.Events(ctx, options) 54 | // Whether we successfully subscribed to events or not, we can now 55 | // unblock the main goroutine. 56 | close(started) 57 | if err != nil { 58 | errChan <- err 59 | return 60 | } 61 | defer body.Close() 62 | 63 | if err := decodeEvents(body, func(event eventtypes.Message, err error) error { 64 | if err != nil { 65 | return err 66 | } 67 | eventChan <- event 68 | return nil 69 | }); err != nil { 70 | errChan <- err 71 | return 72 | } 73 | } 74 | 75 | type eventProcessor func(event eventtypes.Message, err error) error 76 | 77 | func decodeEvents(input io.Reader, ep eventProcessor) error { 78 | dec := json.NewDecoder(input) 79 | for { 80 | var event eventtypes.Message 81 | err := dec.Decode(&event) 82 | if err != nil && err == io.EOF { 83 | break 84 | } 85 | 86 | if procErr := ep(event, err); procErr != nil { 87 | return procErr 88 | } 89 | } 90 | return nil 91 | } 92 | -------------------------------------------------------------------------------- /handlers_test.go: -------------------------------------------------------------------------------- 1 | package events 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | "time" 7 | 8 | eventtypes "github.com/docker/engine-api/types/events" 9 | ) 10 | 11 | func TestByType(t *testing.T) { 12 | cases := []struct { 13 | message eventtypes.Message 14 | expected string 15 | }{ 16 | { 17 | message: eventtypes.Message{}, 18 | expected: "", 19 | }, 20 | { 21 | message: eventtypes.Message{ 22 | Type: "container", 23 | }, 24 | expected: "container", 25 | }, 26 | { 27 | message: eventtypes.Message{ 28 | Type: "image", 29 | }, 30 | expected: "image", 31 | }, 32 | } 33 | for _, c := range cases { 34 | actual := ByType(c.message) 35 | if actual != c.expected { 36 | t.Fatalf("expected %s, got %s", c.expected, actual) 37 | } 38 | } 39 | } 40 | 41 | func TestAction(t *testing.T) { 42 | cases := []struct { 43 | message eventtypes.Message 44 | expected string 45 | }{ 46 | { 47 | message: eventtypes.Message{}, 48 | expected: "", 49 | }, 50 | { 51 | message: eventtypes.Message{ 52 | Action: "start", 53 | }, 54 | expected: "start", 55 | }, 56 | { 57 | message: eventtypes.Message{ 58 | Action: "die", 59 | }, 60 | expected: "die", 61 | }, 62 | } 63 | for _, c := range cases { 64 | actual := ByAction(c.message) 65 | if actual != c.expected { 66 | t.Fatalf("expected %s, got %s", c.expected, actual) 67 | } 68 | } 69 | } 70 | 71 | func TestWatchNoFiltering(t *testing.T) { 72 | safeActual := &safeSlice{ 73 | data: []string{}, 74 | } 75 | expectedEvents := []string{ 76 | "container-create", 77 | "container-start", 78 | "network-create", 79 | } 80 | eventChan := make(chan eventtypes.Message) 81 | 82 | go func() { 83 | eventChan <- eventtypes.Message{ 84 | Type: "container", 85 | Action: "create", 86 | } 87 | time.Sleep(1 * time.Millisecond) 88 | eventChan <- eventtypes.Message{ 89 | Type: "container", 90 | Action: "start", 91 | } 92 | time.Sleep(1 * time.Millisecond) 93 | eventChan <- eventtypes.Message{ 94 | Type: "network", 95 | Action: "create", 96 | } 97 | time.Sleep(1 * time.Millisecond) 98 | close(eventChan) 99 | }() 100 | 101 | h := NewHandler(func(e eventtypes.Message) string { return "" }) 102 | h.Handle("", func(e eventtypes.Message) { 103 | safeActual.Add(e.Type + "-" + e.Action) 104 | }) 105 | h.Watch(eventChan) 106 | 107 | actualEvents := safeActual.Read() 108 | if !reflect.DeepEqual(actualEvents, expectedEvents) { 109 | t.Fatalf("expected %v, got %v", expectedEvents, actualEvents) 110 | } 111 | } 112 | 113 | func TestWatchFiltering(t *testing.T) { 114 | safeActual := &safeSlice{ 115 | data: []string{}, 116 | } 117 | expectedEvents := []string{ 118 | "container-create", 119 | "container-start", 120 | } 121 | eventChan := make(chan eventtypes.Message) 122 | 123 | go func() { 124 | eventChan <- eventtypes.Message{ 125 | Type: "container", 126 | Action: "create", 127 | } 128 | time.Sleep(1 * time.Millisecond) 129 | eventChan <- eventtypes.Message{ 130 | Type: "container", 131 | Action: "start", 132 | } 133 | time.Sleep(1 * time.Millisecond) 134 | eventChan <- eventtypes.Message{ 135 | Type: "network", 136 | Action: "create", 137 | } 138 | time.Sleep(1 * time.Millisecond) 139 | close(eventChan) 140 | }() 141 | 142 | h := NewHandler(func(e eventtypes.Message) string { return e.Type }) 143 | h.Handle("container", func(e eventtypes.Message) { 144 | safeActual.Add(e.Type + "-" + e.Action) 145 | }) 146 | h.Watch(eventChan) 147 | 148 | actualEvents := safeActual.Read() 149 | if !reflect.DeepEqual(actualEvents, expectedEvents) { 150 | t.Fatalf("expected %v, got %v", expectedEvents, actualEvents) 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /events_test.go: -------------------------------------------------------------------------------- 1 | package events 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io" 7 | "io/ioutil" 8 | "reflect" 9 | "sync" 10 | "testing" 11 | "time" 12 | 13 | "golang.org/x/net/context" 14 | 15 | "github.com/docker/engine-api/client" 16 | "github.com/docker/engine-api/types" 17 | eventtypes "github.com/docker/engine-api/types/events" 18 | ) 19 | 20 | func TestMonitorError(t *testing.T) { 21 | cli := &NopClient{} 22 | 23 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 24 | defer cancel() 25 | 26 | errChan := Monitor(ctx, cli, types.EventsOptions{}, func(m eventtypes.Message) { 27 | // Do nothing 28 | }) 29 | 30 | if err := <-errChan; err == nil { 31 | t.Fatal("expected an error, got nothing") 32 | } 33 | 34 | } 35 | func TestMonitorErrorDecoding(t *testing.T) { 36 | cli := &errorEventClient{} 37 | 38 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 39 | defer cancel() 40 | 41 | errChan := Monitor(ctx, cli, types.EventsOptions{}, func(m eventtypes.Message) { 42 | // Do nothing 43 | }) 44 | 45 | if err := <-errChan; err == nil { 46 | t.Fatal("expected an error, got nothing") 47 | } 48 | 49 | } 50 | 51 | type errorEventClient struct { 52 | NopClient 53 | } 54 | 55 | func (c *errorEventClient) Events(ctx context.Context, options types.EventsOptions) (io.ReadCloser, error) { 56 | pr, pw := io.Pipe() 57 | 58 | go func() { 59 | enc := json.NewEncoder(pw) 60 | 61 | enc.Encode("") 62 | 63 | pw.Close() 64 | }() 65 | 66 | return ioutil.NopCloser(pr), nil 67 | } 68 | 69 | func TestMonitor(t *testing.T) { 70 | cases := []struct { 71 | expected []string 72 | events []eventtypes.Message 73 | }{ 74 | { 75 | expected: []string{}, 76 | events: []eventtypes.Message{}, 77 | }, 78 | { 79 | expected: []string{ 80 | "container-create", 81 | }, 82 | events: []eventtypes.Message{ 83 | { 84 | Type: "container", 85 | Action: "create", 86 | }, 87 | }, 88 | }, 89 | { 90 | expected: []string{ 91 | "container-create", 92 | "network-create", 93 | "volume-create", 94 | "container-destroy", 95 | }, 96 | events: []eventtypes.Message{ 97 | { 98 | Type: "container", 99 | Action: "create", 100 | }, 101 | { 102 | Type: "network", 103 | Action: "create", 104 | }, 105 | { 106 | Type: "volume", 107 | Action: "create", 108 | }, 109 | { 110 | Type: "container", 111 | Action: "destroy", 112 | }, 113 | }, 114 | }, 115 | } 116 | 117 | for _, c := range cases { 118 | safeActual := &safeSlice{ 119 | data: []string{}, 120 | } 121 | cli := &fakeEventClient{ 122 | events: c.events, 123 | } 124 | 125 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 126 | defer cancel() 127 | 128 | errChan := Monitor(ctx, cli, types.EventsOptions{}, func(m eventtypes.Message) { 129 | safeActual.Add(m.Type + "-" + m.Action) 130 | }) 131 | 132 | if err := <-errChan; err != nil { 133 | t.Fatal(err) 134 | } 135 | 136 | actual := safeActual.Read() 137 | if !reflect.DeepEqual(c.expected, actual) { 138 | t.Fatalf("expected %v, got %v", c.expected, actual) 139 | } 140 | } 141 | } 142 | 143 | func ExampleMonitor() { 144 | cli, err := client.NewEnvClient() 145 | if err != nil { 146 | // Do something.. 147 | } 148 | 149 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 150 | defer cancel() 151 | 152 | errChan := Monitor(ctx, cli, types.EventsOptions{}, func(event eventtypes.Message) { 153 | fmt.Printf("%v\n", event) 154 | }) 155 | 156 | if err := <-errChan; err != nil { 157 | // Do something 158 | } 159 | } 160 | 161 | type fakeEventClient struct { 162 | NopClient 163 | events []eventtypes.Message 164 | } 165 | 166 | func (c *fakeEventClient) Events(ctx context.Context, options types.EventsOptions) (io.ReadCloser, error) { 167 | pr, pw := io.Pipe() 168 | 169 | go func() { 170 | enc := json.NewEncoder(pw) 171 | 172 | for _, event := range c.events { 173 | enc.Encode(event) 174 | time.Sleep(1 * time.Millisecond) 175 | } 176 | 177 | pw.Close() 178 | }() 179 | 180 | return ioutil.NopCloser(pr), nil 181 | } 182 | 183 | type safeSlice struct { 184 | mu sync.RWMutex 185 | data []string 186 | } 187 | 188 | func (s *safeSlice) Add(element string) { 189 | s.mu.Lock() 190 | s.data = append(s.data, element) 191 | s.mu.Unlock() 192 | } 193 | 194 | func (s *safeSlice) Read() []string { 195 | s.mu.RLock() 196 | defer s.mu.RUnlock() 197 | return s.data 198 | } 199 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | https://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | Copyright 2015-2016 Vincent Demeester 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | https://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | --------------------------------------------------------------------------------