mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Vendoring dependencies
This commit is contained in:
parent
122ea31e45
commit
2b19ccea1c
202
vendor/cloud.google.com/go/LICENSE
generated
vendored
Normal file
202
vendor/cloud.google.com/go/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.
|
513
vendor/cloud.google.com/go/compute/metadata/metadata.go
generated
vendored
Normal file
513
vendor/cloud.google.com/go/compute/metadata/metadata.go
generated
vendored
Normal file
@ -0,0 +1,513 @@
|
||||
// Copyright 2014 Google LLC
|
||||
//
|
||||
// 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.
|
||||
|
||||
// Package metadata provides access to Google Compute Engine (GCE)
|
||||
// metadata and API service accounts.
|
||||
//
|
||||
// This package is a wrapper around the GCE metadata service,
|
||||
// as documented at https://developers.google.com/compute/docs/metadata.
|
||||
package metadata // import "cloud.google.com/go/compute/metadata"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// metadataIP is the documented metadata server IP address.
|
||||
metadataIP = "169.254.169.254"
|
||||
|
||||
// metadataHostEnv is the environment variable specifying the
|
||||
// GCE metadata hostname. If empty, the default value of
|
||||
// metadataIP ("169.254.169.254") is used instead.
|
||||
// This is variable name is not defined by any spec, as far as
|
||||
// I know; it was made up for the Go package.
|
||||
metadataHostEnv = "GCE_METADATA_HOST"
|
||||
|
||||
userAgent = "gcloud-golang/0.1"
|
||||
)
|
||||
|
||||
type cachedValue struct {
|
||||
k string
|
||||
trim bool
|
||||
mu sync.Mutex
|
||||
v string
|
||||
}
|
||||
|
||||
var (
|
||||
projID = &cachedValue{k: "project/project-id", trim: true}
|
||||
projNum = &cachedValue{k: "project/numeric-project-id", trim: true}
|
||||
instID = &cachedValue{k: "instance/id", trim: true}
|
||||
)
|
||||
|
||||
var (
|
||||
defaultClient = &Client{hc: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 2 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).Dial,
|
||||
ResponseHeaderTimeout: 2 * time.Second,
|
||||
},
|
||||
}}
|
||||
subscribeClient = &Client{hc: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 2 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).Dial,
|
||||
},
|
||||
}}
|
||||
)
|
||||
|
||||
// NotDefinedError is returned when requested metadata is not defined.
|
||||
//
|
||||
// The underlying string is the suffix after "/computeMetadata/v1/".
|
||||
//
|
||||
// This error is not returned if the value is defined to be the empty
|
||||
// string.
|
||||
type NotDefinedError string
|
||||
|
||||
func (suffix NotDefinedError) Error() string {
|
||||
return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
|
||||
}
|
||||
|
||||
func (c *cachedValue) get(cl *Client) (v string, err error) {
|
||||
defer c.mu.Unlock()
|
||||
c.mu.Lock()
|
||||
if c.v != "" {
|
||||
return c.v, nil
|
||||
}
|
||||
if c.trim {
|
||||
v, err = cl.getTrimmed(c.k)
|
||||
} else {
|
||||
v, err = cl.Get(c.k)
|
||||
}
|
||||
if err == nil {
|
||||
c.v = v
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
onGCEOnce sync.Once
|
||||
onGCE bool
|
||||
)
|
||||
|
||||
// OnGCE reports whether this process is running on Google Compute Engine.
|
||||
func OnGCE() bool {
|
||||
onGCEOnce.Do(initOnGCE)
|
||||
return onGCE
|
||||
}
|
||||
|
||||
func initOnGCE() {
|
||||
onGCE = testOnGCE()
|
||||
}
|
||||
|
||||
func testOnGCE() bool {
|
||||
// The user explicitly said they're on GCE, so trust them.
|
||||
if os.Getenv(metadataHostEnv) != "" {
|
||||
return true
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
resc := make(chan bool, 2)
|
||||
|
||||
// Try two strategies in parallel.
|
||||
// See https://github.com/googleapis/google-cloud-go/issues/194
|
||||
go func() {
|
||||
req, _ := http.NewRequest("GET", "http://"+metadataIP, nil)
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
res, err := defaultClient.hc.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
resc <- false
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
resc <- res.Header.Get("Metadata-Flavor") == "Google"
|
||||
}()
|
||||
|
||||
go func() {
|
||||
addrs, err := net.LookupHost("metadata.google.internal")
|
||||
if err != nil || len(addrs) == 0 {
|
||||
resc <- false
|
||||
return
|
||||
}
|
||||
resc <- strsContains(addrs, metadataIP)
|
||||
}()
|
||||
|
||||
tryHarder := systemInfoSuggestsGCE()
|
||||
if tryHarder {
|
||||
res := <-resc
|
||||
if res {
|
||||
// The first strategy succeeded, so let's use it.
|
||||
return true
|
||||
}
|
||||
// Wait for either the DNS or metadata server probe to
|
||||
// contradict the other one and say we are running on
|
||||
// GCE. Give it a lot of time to do so, since the system
|
||||
// info already suggests we're running on a GCE BIOS.
|
||||
timer := time.NewTimer(5 * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case res = <-resc:
|
||||
return res
|
||||
case <-timer.C:
|
||||
// Too slow. Who knows what this system is.
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// There's no hint from the system info that we're running on
|
||||
// GCE, so use the first probe's result as truth, whether it's
|
||||
// true or false. The goal here is to optimize for speed for
|
||||
// users who are NOT running on GCE. We can't assume that
|
||||
// either a DNS lookup or an HTTP request to a blackholed IP
|
||||
// address is fast. Worst case this should return when the
|
||||
// metaClient's Transport.ResponseHeaderTimeout or
|
||||
// Transport.Dial.Timeout fires (in two seconds).
|
||||
return <-resc
|
||||
}
|
||||
|
||||
// systemInfoSuggestsGCE reports whether the local system (without
|
||||
// doing network requests) suggests that we're running on GCE. If this
|
||||
// returns true, testOnGCE tries a bit harder to reach its metadata
|
||||
// server.
|
||||
func systemInfoSuggestsGCE() bool {
|
||||
if runtime.GOOS != "linux" {
|
||||
// We don't have any non-Linux clues available, at least yet.
|
||||
return false
|
||||
}
|
||||
slurp, _ := ioutil.ReadFile("/sys/class/dmi/id/product_name")
|
||||
name := strings.TrimSpace(string(slurp))
|
||||
return name == "Google" || name == "Google Compute Engine"
|
||||
}
|
||||
|
||||
// Subscribe calls Client.Subscribe on a client designed for subscribing (one with no
|
||||
// ResponseHeaderTimeout).
|
||||
func Subscribe(suffix string, fn func(v string, ok bool) error) error {
|
||||
return subscribeClient.Subscribe(suffix, fn)
|
||||
}
|
||||
|
||||
// Get calls Client.Get on the default client.
|
||||
func Get(suffix string) (string, error) { return defaultClient.Get(suffix) }
|
||||
|
||||
// ProjectID returns the current instance's project ID string.
|
||||
func ProjectID() (string, error) { return defaultClient.ProjectID() }
|
||||
|
||||
// NumericProjectID returns the current instance's numeric project ID.
|
||||
func NumericProjectID() (string, error) { return defaultClient.NumericProjectID() }
|
||||
|
||||
// InternalIP returns the instance's primary internal IP address.
|
||||
func InternalIP() (string, error) { return defaultClient.InternalIP() }
|
||||
|
||||
// ExternalIP returns the instance's primary external (public) IP address.
|
||||
func ExternalIP() (string, error) { return defaultClient.ExternalIP() }
|
||||
|
||||
// Hostname returns the instance's hostname. This will be of the form
|
||||
// "<instanceID>.c.<projID>.internal".
|
||||
func Hostname() (string, error) { return defaultClient.Hostname() }
|
||||
|
||||
// InstanceTags returns the list of user-defined instance tags,
|
||||
// assigned when initially creating a GCE instance.
|
||||
func InstanceTags() ([]string, error) { return defaultClient.InstanceTags() }
|
||||
|
||||
// InstanceID returns the current VM's numeric instance ID.
|
||||
func InstanceID() (string, error) { return defaultClient.InstanceID() }
|
||||
|
||||
// InstanceName returns the current VM's instance ID string.
|
||||
func InstanceName() (string, error) { return defaultClient.InstanceName() }
|
||||
|
||||
// Zone returns the current VM's zone, such as "us-central1-b".
|
||||
func Zone() (string, error) { return defaultClient.Zone() }
|
||||
|
||||
// InstanceAttributes calls Client.InstanceAttributes on the default client.
|
||||
func InstanceAttributes() ([]string, error) { return defaultClient.InstanceAttributes() }
|
||||
|
||||
// ProjectAttributes calls Client.ProjectAttributes on the default client.
|
||||
func ProjectAttributes() ([]string, error) { return defaultClient.ProjectAttributes() }
|
||||
|
||||
// InstanceAttributeValue calls Client.InstanceAttributeValue on the default client.
|
||||
func InstanceAttributeValue(attr string) (string, error) {
|
||||
return defaultClient.InstanceAttributeValue(attr)
|
||||
}
|
||||
|
||||
// ProjectAttributeValue calls Client.ProjectAttributeValue on the default client.
|
||||
func ProjectAttributeValue(attr string) (string, error) {
|
||||
return defaultClient.ProjectAttributeValue(attr)
|
||||
}
|
||||
|
||||
// Scopes calls Client.Scopes on the default client.
|
||||
func Scopes(serviceAccount string) ([]string, error) { return defaultClient.Scopes(serviceAccount) }
|
||||
|
||||
func strsContains(ss []string, s string) bool {
|
||||
for _, v := range ss {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// A Client provides metadata.
|
||||
type Client struct {
|
||||
hc *http.Client
|
||||
}
|
||||
|
||||
// NewClient returns a Client that can be used to fetch metadata. All HTTP requests
|
||||
// will use the given http.Client instead of the default client.
|
||||
func NewClient(c *http.Client) *Client {
|
||||
return &Client{hc: c}
|
||||
}
|
||||
|
||||
// getETag returns a value from the metadata service as well as the associated ETag.
|
||||
// This func is otherwise equivalent to Get.
|
||||
func (c *Client) getETag(suffix string) (value, etag string, err error) {
|
||||
// Using a fixed IP makes it very difficult to spoof the metadata service in
|
||||
// a container, which is an important use-case for local testing of cloud
|
||||
// deployments. To enable spoofing of the metadata service, the environment
|
||||
// variable GCE_METADATA_HOST is first inspected to decide where metadata
|
||||
// requests shall go.
|
||||
host := os.Getenv(metadataHostEnv)
|
||||
if host == "" {
|
||||
// Using 169.254.169.254 instead of "metadata" here because Go
|
||||
// binaries built with the "netgo" tag and without cgo won't
|
||||
// know the search suffix for "metadata" is
|
||||
// ".google.internal", and this IP address is documented as
|
||||
// being stable anyway.
|
||||
host = metadataIP
|
||||
}
|
||||
u := "http://" + host + "/computeMetadata/v1/" + suffix
|
||||
req, _ := http.NewRequest("GET", u, nil)
|
||||
req.Header.Set("Metadata-Flavor", "Google")
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
res, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode == http.StatusNotFound {
|
||||
return "", "", NotDefinedError(suffix)
|
||||
}
|
||||
all, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
return "", "", &Error{Code: res.StatusCode, Message: string(all)}
|
||||
}
|
||||
return string(all), res.Header.Get("Etag"), nil
|
||||
}
|
||||
|
||||
// Get returns a value from the metadata service.
|
||||
// The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
|
||||
//
|
||||
// If the GCE_METADATA_HOST environment variable is not defined, a default of
|
||||
// 169.254.169.254 will be used instead.
|
||||
//
|
||||
// If the requested metadata is not defined, the returned error will
|
||||
// be of type NotDefinedError.
|
||||
func (c *Client) Get(suffix string) (string, error) {
|
||||
val, _, err := c.getETag(suffix)
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (c *Client) getTrimmed(suffix string) (s string, err error) {
|
||||
s, err = c.Get(suffix)
|
||||
s = strings.TrimSpace(s)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) lines(suffix string) ([]string, error) {
|
||||
j, err := c.Get(suffix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := strings.Split(strings.TrimSpace(j), "\n")
|
||||
for i := range s {
|
||||
s[i] = strings.TrimSpace(s[i])
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// ProjectID returns the current instance's project ID string.
|
||||
func (c *Client) ProjectID() (string, error) { return projID.get(c) }
|
||||
|
||||
// NumericProjectID returns the current instance's numeric project ID.
|
||||
func (c *Client) NumericProjectID() (string, error) { return projNum.get(c) }
|
||||
|
||||
// InstanceID returns the current VM's numeric instance ID.
|
||||
func (c *Client) InstanceID() (string, error) { return instID.get(c) }
|
||||
|
||||
// InternalIP returns the instance's primary internal IP address.
|
||||
func (c *Client) InternalIP() (string, error) {
|
||||
return c.getTrimmed("instance/network-interfaces/0/ip")
|
||||
}
|
||||
|
||||
// ExternalIP returns the instance's primary external (public) IP address.
|
||||
func (c *Client) ExternalIP() (string, error) {
|
||||
return c.getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
|
||||
}
|
||||
|
||||
// Hostname returns the instance's hostname. This will be of the form
|
||||
// "<instanceID>.c.<projID>.internal".
|
||||
func (c *Client) Hostname() (string, error) {
|
||||
return c.getTrimmed("instance/hostname")
|
||||
}
|
||||
|
||||
// InstanceTags returns the list of user-defined instance tags,
|
||||
// assigned when initially creating a GCE instance.
|
||||
func (c *Client) InstanceTags() ([]string, error) {
|
||||
var s []string
|
||||
j, err := c.Get("instance/tags")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// InstanceName returns the current VM's instance ID string.
|
||||
func (c *Client) InstanceName() (string, error) {
|
||||
host, err := c.Hostname()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.Split(host, ".")[0], nil
|
||||
}
|
||||
|
||||
// Zone returns the current VM's zone, such as "us-central1-b".
|
||||
func (c *Client) Zone() (string, error) {
|
||||
zone, err := c.getTrimmed("instance/zone")
|
||||
// zone is of the form "projects/<projNum>/zones/<zoneName>".
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return zone[strings.LastIndex(zone, "/")+1:], nil
|
||||
}
|
||||
|
||||
// InstanceAttributes returns the list of user-defined attributes,
|
||||
// assigned when initially creating a GCE VM instance. The value of an
|
||||
// attribute can be obtained with InstanceAttributeValue.
|
||||
func (c *Client) InstanceAttributes() ([]string, error) { return c.lines("instance/attributes/") }
|
||||
|
||||
// ProjectAttributes returns the list of user-defined attributes
|
||||
// applying to the project as a whole, not just this VM. The value of
|
||||
// an attribute can be obtained with ProjectAttributeValue.
|
||||
func (c *Client) ProjectAttributes() ([]string, error) { return c.lines("project/attributes/") }
|
||||
|
||||
// InstanceAttributeValue returns the value of the provided VM
|
||||
// instance attribute.
|
||||
//
|
||||
// If the requested attribute is not defined, the returned error will
|
||||
// be of type NotDefinedError.
|
||||
//
|
||||
// InstanceAttributeValue may return ("", nil) if the attribute was
|
||||
// defined to be the empty string.
|
||||
func (c *Client) InstanceAttributeValue(attr string) (string, error) {
|
||||
return c.Get("instance/attributes/" + attr)
|
||||
}
|
||||
|
||||
// ProjectAttributeValue returns the value of the provided
|
||||
// project attribute.
|
||||
//
|
||||
// If the requested attribute is not defined, the returned error will
|
||||
// be of type NotDefinedError.
|
||||
//
|
||||
// ProjectAttributeValue may return ("", nil) if the attribute was
|
||||
// defined to be the empty string.
|
||||
func (c *Client) ProjectAttributeValue(attr string) (string, error) {
|
||||
return c.Get("project/attributes/" + attr)
|
||||
}
|
||||
|
||||
// Scopes returns the service account scopes for the given account.
|
||||
// The account may be empty or the string "default" to use the instance's
|
||||
// main account.
|
||||
func (c *Client) Scopes(serviceAccount string) ([]string, error) {
|
||||
if serviceAccount == "" {
|
||||
serviceAccount = "default"
|
||||
}
|
||||
return c.lines("instance/service-accounts/" + serviceAccount + "/scopes")
|
||||
}
|
||||
|
||||
// Subscribe subscribes to a value from the metadata service.
|
||||
// The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
|
||||
// The suffix may contain query parameters.
|
||||
//
|
||||
// Subscribe calls fn with the latest metadata value indicated by the provided
|
||||
// suffix. If the metadata value is deleted, fn is called with the empty string
|
||||
// and ok false. Subscribe blocks until fn returns a non-nil error or the value
|
||||
// is deleted. Subscribe returns the error value returned from the last call to
|
||||
// fn, which may be nil when ok == false.
|
||||
func (c *Client) Subscribe(suffix string, fn func(v string, ok bool) error) error {
|
||||
const failedSubscribeSleep = time.Second * 5
|
||||
|
||||
// First check to see if the metadata value exists at all.
|
||||
val, lastETag, err := c.getETag(suffix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := fn(val, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ok := true
|
||||
if strings.ContainsRune(suffix, '?') {
|
||||
suffix += "&wait_for_change=true&last_etag="
|
||||
} else {
|
||||
suffix += "?wait_for_change=true&last_etag="
|
||||
}
|
||||
for {
|
||||
val, etag, err := c.getETag(suffix + url.QueryEscape(lastETag))
|
||||
if err != nil {
|
||||
if _, deleted := err.(NotDefinedError); !deleted {
|
||||
time.Sleep(failedSubscribeSleep)
|
||||
continue // Retry on other errors.
|
||||
}
|
||||
ok = false
|
||||
}
|
||||
lastETag = etag
|
||||
|
||||
if err := fn(val, ok); err != nil || !ok {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error contains an error response from the server.
|
||||
type Error struct {
|
||||
// Code is the HTTP response status code.
|
||||
Code int
|
||||
// Message is the server response message.
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return fmt.Sprintf("compute: Received %d `%s`", e.Code, e.Message)
|
||||
}
|
201
vendor/code.cloudfoundry.org/bytefmt/LICENSE
generated
vendored
Normal file
201
vendor/code.cloudfoundry.org/bytefmt/LICENSE
generated
vendored
Normal file
@ -0,0 +1,201 @@
|
||||
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.
|
20
vendor/code.cloudfoundry.org/bytefmt/NOTICE
generated
vendored
Normal file
20
vendor/code.cloudfoundry.org/bytefmt/NOTICE
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
Copyright (c) 2015-Present CloudFoundry.org Foundation, Inc. All Rights Reserved.
|
||||
|
||||
This project contains software that is Copyright (c) 2013-2015 Pivotal Software, Inc.
|
||||
|
||||
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.
|
||||
|
||||
This project may include a number of subcomponents with separate
|
||||
copyright notices and license terms. Your use of these subcomponents
|
||||
is subject to the terms and conditions of each subcomponent's license,
|
||||
as noted in the LICENSE file.
|
15
vendor/code.cloudfoundry.org/bytefmt/README.md
generated
vendored
Normal file
15
vendor/code.cloudfoundry.org/bytefmt/README.md
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
bytefmt
|
||||
=======
|
||||
|
||||
**Note**: This repository should be imported as `code.cloudfoundry.org/bytefmt`.
|
||||
|
||||
Human-readable byte formatter.
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
bytefmt.ByteSize(100.5*bytefmt.MEGABYTE) // returns "100.5M"
|
||||
bytefmt.ByteSize(uint64(1024)) // returns "1K"
|
||||
```
|
||||
|
||||
For documentation, please see http://godoc.org/code.cloudfoundry.org/bytefmt
|
105
vendor/code.cloudfoundry.org/bytefmt/bytes.go
generated
vendored
Normal file
105
vendor/code.cloudfoundry.org/bytefmt/bytes.go
generated
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
// Package bytefmt contains helper methods and constants for converting to and from a human-readable byte format.
|
||||
//
|
||||
// bytefmt.ByteSize(100.5*bytefmt.MEGABYTE) // "100.5M"
|
||||
// bytefmt.ByteSize(uint64(1024)) // "1K"
|
||||
//
|
||||
package bytefmt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
BYTE = 1 << (10 * iota)
|
||||
KILOBYTE
|
||||
MEGABYTE
|
||||
GIGABYTE
|
||||
TERABYTE
|
||||
)
|
||||
|
||||
var invalidByteQuantityError = errors.New("byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB")
|
||||
|
||||
// ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:
|
||||
// T: Terabyte
|
||||
// G: Gigabyte
|
||||
// M: Megabyte
|
||||
// K: Kilobyte
|
||||
// B: Byte
|
||||
// The unit that results in the smallest number greater than or equal to 1 is always chosen.
|
||||
func ByteSize(bytes uint64) string {
|
||||
unit := ""
|
||||
value := float64(bytes)
|
||||
|
||||
switch {
|
||||
case bytes >= TERABYTE:
|
||||
unit = "T"
|
||||
value = value / TERABYTE
|
||||
case bytes >= GIGABYTE:
|
||||
unit = "G"
|
||||
value = value / GIGABYTE
|
||||
case bytes >= MEGABYTE:
|
||||
unit = "M"
|
||||
value = value / MEGABYTE
|
||||
case bytes >= KILOBYTE:
|
||||
unit = "K"
|
||||
value = value / KILOBYTE
|
||||
case bytes >= BYTE:
|
||||
unit = "B"
|
||||
case bytes == 0:
|
||||
return "0"
|
||||
}
|
||||
|
||||
result := strconv.FormatFloat(value, 'f', 1, 64)
|
||||
result = strings.TrimSuffix(result, ".0")
|
||||
return result + unit
|
||||
}
|
||||
|
||||
// ToMegabytes parses a string formatted by ByteSize as megabytes.
|
||||
func ToMegabytes(s string) (uint64, error) {
|
||||
bytes, err := ToBytes(s)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return bytes / MEGABYTE, nil
|
||||
}
|
||||
|
||||
// ToBytes parses a string formatted by ByteSize as bytes. Note binary-prefixed and SI prefixed units both mean a base-2 units
|
||||
// KB = K = KiB = 1024
|
||||
// MB = M = MiB = 1024 * K
|
||||
// GB = G = GiB = 1024 * M
|
||||
// TB = T = TiB = 1024 * G
|
||||
func ToBytes(s string) (uint64, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
s = strings.ToUpper(s)
|
||||
|
||||
i := strings.IndexFunc(s, unicode.IsLetter)
|
||||
|
||||
if i == -1 {
|
||||
return 0, invalidByteQuantityError
|
||||
}
|
||||
|
||||
bytesString, multiple := s[:i], s[i:]
|
||||
bytes, err := strconv.ParseFloat(bytesString, 64)
|
||||
if err != nil || bytes <= 0 {
|
||||
return 0, invalidByteQuantityError
|
||||
}
|
||||
|
||||
switch multiple {
|
||||
case "T", "TB", "TIB":
|
||||
return uint64(bytes * TERABYTE), nil
|
||||
case "G", "GB", "GIB":
|
||||
return uint64(bytes * GIGABYTE), nil
|
||||
case "M", "MB", "MIB":
|
||||
return uint64(bytes * MEGABYTE), nil
|
||||
case "K", "KB", "KIB":
|
||||
return uint64(bytes * KILOBYTE), nil
|
||||
case "B":
|
||||
return uint64(bytes), nil
|
||||
default:
|
||||
return 0, invalidByteQuantityError
|
||||
}
|
||||
}
|
1
vendor/code.cloudfoundry.org/bytefmt/package.go
generated
vendored
Normal file
1
vendor/code.cloudfoundry.org/bytefmt/package.go
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
package bytefmt // import "code.cloudfoundry.org/bytefmt"
|
2
vendor/github.com/PagerDuty/go-pagerduty/.gitignore
generated
vendored
Normal file
2
vendor/github.com/PagerDuty/go-pagerduty/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
bin/*
|
||||
*.swp
|
33
vendor/github.com/PagerDuty/go-pagerduty/.goreleaser.yml
generated
vendored
Normal file
33
vendor/github.com/PagerDuty/go-pagerduty/.goreleaser.yml
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
project_name: go-pagerduty
|
||||
release:
|
||||
github:
|
||||
owner: PagerDuty
|
||||
name: go-pagerduty
|
||||
draft: false
|
||||
prerelease: true
|
||||
name_template: "{{.ProjectName}}-v{{.Version}}"
|
||||
builds:
|
||||
- goos:
|
||||
- linux
|
||||
- darwin
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
- 386
|
||||
main: ./command/*.go
|
||||
binary: pd
|
||||
archive:
|
||||
format: tar.gz
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
replacements:
|
||||
amd64: 64-bit
|
||||
darwin: macOS
|
||||
linux: Tux
|
||||
name_template: "{{.Binary}}_{{.Version}}_{{.Os}}-{{.Arch}}"
|
||||
files:
|
||||
- LICENSE.txt
|
||||
- README.md
|
||||
- examples/*
|
||||
- CHANGELOG.md
|
89
vendor/github.com/PagerDuty/go-pagerduty/CHANGELOG.md
generated
vendored
Normal file
89
vendor/github.com/PagerDuty/go-pagerduty/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,89 @@
|
||||
# Change Log
|
||||
|
||||
## [1.0.0](https://github.com/PagerDuty/go-pagerduty/tree/1.0.0) (2018-05-28)
|
||||
**Fixed bugs:**
|
||||
|
||||
- Escalation Policy's repeat\_enabled Is Ignored [\#57](https://github.com/PagerDuty/go-pagerduty/issues/57)
|
||||
- Problems running freshly built pd utility [\#39](https://github.com/PagerDuty/go-pagerduty/issues/39)
|
||||
- Manage Incident gives error [\#32](https://github.com/PagerDuty/go-pagerduty/issues/32)
|
||||
- Added missing slash to delete integration method url [\#59](https://github.com/PagerDuty/go-pagerduty/pull/59) ([jescochu](https://github.com/jescochu))
|
||||
|
||||
**Closed issues:**
|
||||
|
||||
- Trouble creating an integration [\#102](https://github.com/PagerDuty/go-pagerduty/issues/102)
|
||||
- Client does not trigger events [\#101](https://github.com/PagerDuty/go-pagerduty/issues/101)
|
||||
- Paging help [\#94](https://github.com/PagerDuty/go-pagerduty/issues/94)
|
||||
- Help with incident creation API [\#89](https://github.com/PagerDuty/go-pagerduty/issues/89)
|
||||
- Memory leak because of response body is not closed [\#66](https://github.com/PagerDuty/go-pagerduty/issues/66)
|
||||
- Since and Until don't work for log\_entries [\#61](https://github.com/PagerDuty/go-pagerduty/issues/61)
|
||||
- service: auto\_resolve\_timeout & acknowledgement\_timeout cannot be set to null [\#51](https://github.com/PagerDuty/go-pagerduty/issues/51)
|
||||
- Possible to create new service and integration together [\#42](https://github.com/PagerDuty/go-pagerduty/issues/42)
|
||||
- Documentation does not match code [\#16](https://github.com/PagerDuty/go-pagerduty/issues/16)
|
||||
- Typo in repo description [\#15](https://github.com/PagerDuty/go-pagerduty/issues/15)
|
||||
- Webhook decoder [\#14](https://github.com/PagerDuty/go-pagerduty/issues/14)
|
||||
- incident\_key for create\_event [\#13](https://github.com/PagerDuty/go-pagerduty/issues/13)
|
||||
|
||||
**Merged pull requests:**
|
||||
|
||||
- Fix pagination for ListOnCalls [\#90](https://github.com/PagerDuty/go-pagerduty/pull/90) ([IainCole](https://github.com/IainCole))
|
||||
- Revert "Fix inconsistency with some REST Options objects passed by reference …" [\#88](https://github.com/PagerDuty/go-pagerduty/pull/88) ([mimato](https://github.com/mimato))
|
||||
- Adding travis config, fixup Makefile [\#87](https://github.com/PagerDuty/go-pagerduty/pull/87) ([mimato](https://github.com/mimato))
|
||||
- Fixed invalid JSON descriptor for FirstTriggerLogEntry [\#86](https://github.com/PagerDuty/go-pagerduty/pull/86) ([mwisniewski0](https://github.com/mwisniewski0))
|
||||
- \[incidents\] fix entries typo in a few places [\#85](https://github.com/PagerDuty/go-pagerduty/pull/85) ([joeyparsons](https://github.com/joeyparsons))
|
||||
- Fix inconsistency with some REST Options objects passed by reference … [\#79](https://github.com/PagerDuty/go-pagerduty/pull/79) ([lowesoftware](https://github.com/lowesoftware))
|
||||
- Explicit JSON reference to schedules [\#77](https://github.com/PagerDuty/go-pagerduty/pull/77) ([domudall](https://github.com/domudall))
|
||||
- Adding AlertCreation to Service struct [\#76](https://github.com/PagerDuty/go-pagerduty/pull/76) ([domudall](https://github.com/domudall))
|
||||
- Add support for escalation rules [\#71](https://github.com/PagerDuty/go-pagerduty/pull/71) ([heimweh](https://github.com/heimweh))
|
||||
- Fix maintenance window JSON [\#69](https://github.com/PagerDuty/go-pagerduty/pull/69) ([domudall](https://github.com/domudall))
|
||||
- Fixing Maintenance typo [\#68](https://github.com/PagerDuty/go-pagerduty/pull/68) ([domudall](https://github.com/domudall))
|
||||
- Update event.go - fix a memory leak [\#65](https://github.com/PagerDuty/go-pagerduty/pull/65) ([AngelRefael](https://github.com/AngelRefael))
|
||||
- Add query to vendor [\#64](https://github.com/PagerDuty/go-pagerduty/pull/64) ([heimweh](https://github.com/heimweh))
|
||||
- Fix JSON decode \(errorObject\) [\#63](https://github.com/PagerDuty/go-pagerduty/pull/63) ([heimweh](https://github.com/heimweh))
|
||||
- fix since and until by adding them to url scheme [\#60](https://github.com/PagerDuty/go-pagerduty/pull/60) ([ethansommer](https://github.com/ethansommer))
|
||||
- fix webhook struct member name [\#58](https://github.com/PagerDuty/go-pagerduty/pull/58) ([pgray](https://github.com/pgray))
|
||||
- Incident - Add status field to incident [\#56](https://github.com/PagerDuty/go-pagerduty/pull/56) ([heimweh](https://github.com/heimweh))
|
||||
- enable fetch log entries via incident api [\#55](https://github.com/PagerDuty/go-pagerduty/pull/55) ([flyinprogrammer](https://github.com/flyinprogrammer))
|
||||
- Allow service timeouts to be disabled [\#53](https://github.com/PagerDuty/go-pagerduty/pull/53) ([heimweh](https://github.com/heimweh))
|
||||
- Schedule restriction - Add support for start\_day\_of\_week [\#52](https://github.com/PagerDuty/go-pagerduty/pull/52) ([heimweh](https://github.com/heimweh))
|
||||
- Add vendor support [\#49](https://github.com/PagerDuty/go-pagerduty/pull/49) ([heimweh](https://github.com/heimweh))
|
||||
- Add schedules listing [\#46](https://github.com/PagerDuty/go-pagerduty/pull/46) ([Marc-Morata-Fite](https://github.com/Marc-Morata-Fite))
|
||||
- dont declare main twice in examples [\#45](https://github.com/PagerDuty/go-pagerduty/pull/45) ([ranjib](https://github.com/ranjib))
|
||||
- add service show [\#44](https://github.com/PagerDuty/go-pagerduty/pull/44) ([cmluciano](https://github.com/cmluciano))
|
||||
- \(feat\)implement integration creation [\#43](https://github.com/PagerDuty/go-pagerduty/pull/43) ([ranjib](https://github.com/ranjib))
|
||||
- \(chore\)add create event example [\#41](https://github.com/PagerDuty/go-pagerduty/pull/41) ([ranjib](https://github.com/ranjib))
|
||||
- \(bug\)Add test. fix version issue [\#40](https://github.com/PagerDuty/go-pagerduty/pull/40) ([ranjib](https://github.com/ranjib))
|
||||
- Remove subdomain argument from escalation\_policy example. [\#38](https://github.com/PagerDuty/go-pagerduty/pull/38) ([cmluciano](https://github.com/cmluciano))
|
||||
- Skip JSON encoding if no payload was given [\#37](https://github.com/PagerDuty/go-pagerduty/pull/37) ([heimweh](https://github.com/heimweh))
|
||||
- \(feat\)add ability API and CLI [\#36](https://github.com/PagerDuty/go-pagerduty/pull/36) ([ranjib](https://github.com/ranjib))
|
||||
- Make updates to Escalation Policies work [\#35](https://github.com/PagerDuty/go-pagerduty/pull/35) ([heimweh](https://github.com/heimweh))
|
||||
- Fix misspelling in User struct and add JSON tags [\#34](https://github.com/PagerDuty/go-pagerduty/pull/34) ([heimweh](https://github.com/heimweh))
|
||||
- \(bug\)allow passing headers in http do call. fix manage incident call [\#33](https://github.com/PagerDuty/go-pagerduty/pull/33) ([ranjib](https://github.com/ranjib))
|
||||
- \(chore\)get rid of logrus from all core structs except CLI entries. fix schedule override command [\#31](https://github.com/PagerDuty/go-pagerduty/pull/31) ([ranjib](https://github.com/ranjib))
|
||||
- \(bug\)rename override struct [\#30](https://github.com/PagerDuty/go-pagerduty/pull/30) ([ranjib](https://github.com/ranjib))
|
||||
- \(bug\)implement schedule override [\#29](https://github.com/PagerDuty/go-pagerduty/pull/29) ([ranjib](https://github.com/ranjib))
|
||||
- fix misspelling in trigger\_summary\_data's JSON key. [\#28](https://github.com/PagerDuty/go-pagerduty/pull/28) ([tomwans](https://github.com/tomwans))
|
||||
- Correctly set meta flag for incident list [\#26](https://github.com/PagerDuty/go-pagerduty/pull/26) ([afirth](https://github.com/afirth))
|
||||
- Add \*.swp to gitignore [\#25](https://github.com/PagerDuty/go-pagerduty/pull/25) ([afirth](https://github.com/afirth))
|
||||
- Support the /oncalls endpoint in the CLI [\#24](https://github.com/PagerDuty/go-pagerduty/pull/24) ([afirth](https://github.com/afirth))
|
||||
- Refactor to work correctly with V2 API [\#23](https://github.com/PagerDuty/go-pagerduty/pull/23) ([dthagard](https://github.com/dthagard))
|
||||
- \(feat\)Add webhook decoding capability [\#22](https://github.com/PagerDuty/go-pagerduty/pull/22) ([ranjib](https://github.com/ranjib))
|
||||
- \(chore\)Decode event API response. [\#21](https://github.com/PagerDuty/go-pagerduty/pull/21) ([ranjib](https://github.com/ranjib))
|
||||
- \(bug\)add incident\_key field in event api client [\#20](https://github.com/PagerDuty/go-pagerduty/pull/20) ([ranjib](https://github.com/ranjib))
|
||||
- \(chore\)nuke sub domain, v2 api does not need one [\#19](https://github.com/PagerDuty/go-pagerduty/pull/19) ([ranjib](https://github.com/ranjib))
|
||||
- Implement list users CLI [\#17](https://github.com/PagerDuty/go-pagerduty/pull/17) ([ranjib](https://github.com/ranjib))
|
||||
- Add team\_ids\[\] query string arg [\#12](https://github.com/PagerDuty/go-pagerduty/pull/12) ([marklap](https://github.com/marklap))
|
||||
- Incidents fix [\#11](https://github.com/PagerDuty/go-pagerduty/pull/11) ([jareksm](https://github.com/jareksm))
|
||||
- Added APIListObject to Option types to allow setting offset and [\#10](https://github.com/PagerDuty/go-pagerduty/pull/10) ([jareksm](https://github.com/jareksm))
|
||||
- fix typo [\#9](https://github.com/PagerDuty/go-pagerduty/pull/9) ([sjansen](https://github.com/sjansen))
|
||||
- implement incident list cli. event posting api [\#8](https://github.com/PagerDuty/go-pagerduty/pull/8) ([ranjib](https://github.com/ranjib))
|
||||
- CLI for create escalation policy, maintainenance window , schedule ov… [\#7](https://github.com/PagerDuty/go-pagerduty/pull/7) ([ranjib](https://github.com/ranjib))
|
||||
- \(feat\)implement create service cli [\#6](https://github.com/PagerDuty/go-pagerduty/pull/6) ([ranjib](https://github.com/ranjib))
|
||||
- \(feat\)list service cli [\#5](https://github.com/PagerDuty/go-pagerduty/pull/5) ([ranjib](https://github.com/ranjib))
|
||||
- \(feat\)implement addon update/delete [\#4](https://github.com/PagerDuty/go-pagerduty/pull/4) ([ranjib](https://github.com/ranjib))
|
||||
- \(feat\)Show addon cli [\#3](https://github.com/PagerDuty/go-pagerduty/pull/3) ([ranjib](https://github.com/ranjib))
|
||||
- \(feat\) addon list api. create cli [\#2](https://github.com/PagerDuty/go-pagerduty/pull/2) ([ranjib](https://github.com/ranjib))
|
||||
- \(chore\) list addon [\#1](https://github.com/PagerDuty/go-pagerduty/pull/1) ([ranjib](https://github.com/ranjib))
|
||||
|
||||
|
||||
|
||||
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
4
vendor/github.com/PagerDuty/go-pagerduty/Dockerfile
generated
vendored
Normal file
4
vendor/github.com/PagerDuty/go-pagerduty/Dockerfile
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
FROM golang
|
||||
ADD . /go/src/github.com/PagerDuty/go-pagerduty
|
||||
WORKDIR /go/src/github.com/PagerDuty/go-pagerduty
|
||||
RUN go get ./... && go test -v -race -cover ./...
|
14
vendor/github.com/PagerDuty/go-pagerduty/LICENSE.txt
generated
vendored
Normal file
14
vendor/github.com/PagerDuty/go-pagerduty/LICENSE.txt
generated
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
Copyright:: Copyright (c) 2016 PagerDuty, Inc.
|
||||
License:: Apache License, Version 2.0
|
||||
|
||||
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.
|
23
vendor/github.com/PagerDuty/go-pagerduty/Makefile
generated
vendored
Normal file
23
vendor/github.com/PagerDuty/go-pagerduty/Makefile
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
# SOURCEDIR=.
|
||||
# SOURCES = $(shell find $(SOURCEDIR) -name '*.go')
|
||||
# VERSION=$(git describe --always --tags)
|
||||
# BINARY=bin/pd
|
||||
|
||||
# bin: $(BINARY)
|
||||
|
||||
# $(BINARY): $(SOURCES)
|
||||
# go build -o $(BINARY) command/*
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go get ./...
|
||||
# go test -v -race -cover ./...
|
||||
# go tool vet $(SOURCES)
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
go test ./...
|
||||
|
||||
deploy:
|
||||
- curl -sL https://git.io/goreleaser | bash
|
||||
|
84
vendor/github.com/PagerDuty/go-pagerduty/README.md
generated
vendored
Normal file
84
vendor/github.com/PagerDuty/go-pagerduty/README.md
generated
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
[](http://godoc.org/github.com/PagerDuty/go-pagerduty) [](https://goreportcard.com/report/github.com/PagerDuty/go-pagerduty) [](https://github.com/gojp/goreportcard/blob/master/LICENSE)
|
||||
# go-pagerduty
|
||||
|
||||
go-pagerduty is a CLI and [go](https://golang.org/) client library for the [PagerDuty v2 API](https://v2.developer.pagerduty.com/v2/page/api-reference).
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
go get github.com/PagerDuty/go-pagerduty
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### CLI
|
||||
|
||||
The CLI requires an [authentication token](https://v2.developer.pagerduty.com/docs/authentication), which can be sepcified in `.pd.yml`
|
||||
file in the home directory of the user, or passed as a command-line argument.
|
||||
Example of config file:
|
||||
|
||||
```yaml
|
||||
---
|
||||
authtoken: fooBar
|
||||
```
|
||||
|
||||
#### Install
|
||||
```cli
|
||||
cd $GOPATH/github.com/PagerDuty/go-pagerduty
|
||||
go build -o $GOPATH/bin/pd command/*
|
||||
```
|
||||
|
||||
#### Commands
|
||||
`pd` command provides a single entrypoint for all the API endpoints, with individual
|
||||
API represented by their own sub commands. For an exhaustive list of sub-commands, try:
|
||||
```
|
||||
pd --help
|
||||
```
|
||||
|
||||
An example of the `service` sub-command
|
||||
|
||||
```
|
||||
pd service list
|
||||
```
|
||||
|
||||
|
||||
### Client Library
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/PagerDuty/go-pagerduty"
|
||||
)
|
||||
|
||||
var authtoken = "" // Set your auth token here
|
||||
|
||||
func main() {
|
||||
var opts pagerduty.ListEscalationPoliciesOptions
|
||||
client := pagerduty.NewClient(authtoken)
|
||||
if eps, err := client.ListEscalationPolicies(opts); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
for _, p := range eps.EscalationPolicies {
|
||||
fmt.Println(p.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The PagerDuty API client also exposes its HTTP client as the `HTTPClient` field.
|
||||
If you need to use your own HTTP client, for doing things like defining your own
|
||||
transport settings, you can replace the default HTTP client with your own by
|
||||
simply by setting a new value in the `HTTPClient` field.
|
||||
|
||||
## Contributing
|
||||
|
||||
1. Fork it ( https://github.com/PagerDuty/go-pagerduty/fork )
|
||||
2. Create your feature branch (`git checkout -b my-new-feature`)
|
||||
3. Commit your changes (`git commit -am 'Add some feature'`)
|
||||
4. Push to the branch (`git push origin my-new-feature`)
|
||||
5. Create a new Pull Request
|
||||
|
||||
## License
|
||||
[Apache 2](http://www.apache.org/licenses/LICENSE-2.0)
|
22
vendor/github.com/PagerDuty/go-pagerduty/ability.go
generated
vendored
Normal file
22
vendor/github.com/PagerDuty/go-pagerduty/ability.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package pagerduty
|
||||
|
||||
// ListAbilityResponse is the response when calling the ListAbility API endpoint.
|
||||
type ListAbilityResponse struct {
|
||||
Abilities []string `json:"abilities"`
|
||||
}
|
||||
|
||||
// ListAbilities lists all abilities on your account.
|
||||
func (c *Client) ListAbilities() (*ListAbilityResponse, error) {
|
||||
resp, err := c.get("/abilities")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListAbilityResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// TestAbility Check if your account has the given ability.
|
||||
func (c *Client) TestAbility(ability string) error {
|
||||
_, err := c.get("/abilities/" + ability)
|
||||
return err
|
||||
}
|
97
vendor/github.com/PagerDuty/go-pagerduty/addon.go
generated
vendored
Normal file
97
vendor/github.com/PagerDuty/go-pagerduty/addon.go
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Addon is a third-party add-on to PagerDuty's UI.
|
||||
type Addon struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
Src string `json:"src,omitempty"`
|
||||
Services []APIObject `json:"services,omitempty"`
|
||||
}
|
||||
|
||||
// ListAddonOptions are the options available when calling the ListAddons API endpoint.
|
||||
type ListAddonOptions struct {
|
||||
APIListObject
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
|
||||
Filter string `url:"filter,omitempty"`
|
||||
}
|
||||
|
||||
// ListAddonResponse is the response when calling the ListAddons API endpoint.
|
||||
type ListAddonResponse struct {
|
||||
APIListObject
|
||||
Addons []Addon `json:"addons"`
|
||||
}
|
||||
|
||||
// ListAddons lists all of the add-ons installed on your account.
|
||||
func (c *Client) ListAddons(o ListAddonOptions) (*ListAddonResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/addons?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListAddonResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// InstallAddon installs an add-on for your account.
|
||||
func (c *Client) InstallAddon(a Addon) (*Addon, error) {
|
||||
data := make(map[string]Addon)
|
||||
data["addon"] = a
|
||||
resp, err := c.post("/addons", data, nil)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode)
|
||||
}
|
||||
return getAddonFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// DeleteAddon deletes an add-on from your account.
|
||||
func (c *Client) DeleteAddon(id string) error {
|
||||
_, err := c.delete("/addons/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAddon gets details about an existing add-on.
|
||||
func (c *Client) GetAddon(id string) (*Addon, error) {
|
||||
resp, err := c.get("/addons/" + id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getAddonFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// UpdateAddon updates an existing add-on.
|
||||
func (c *Client) UpdateAddon(id string, a Addon) (*Addon, error) {
|
||||
v := make(map[string]Addon)
|
||||
v["addon"] = a
|
||||
resp, err := c.put("/addons/"+id, v, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getAddonFromResponse(c, resp)
|
||||
}
|
||||
|
||||
func getAddonFromResponse(c *Client, resp *http.Response) (*Addon, error) {
|
||||
var result map[string]Addon
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a, ok := result["addon"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have 'addon' field")
|
||||
}
|
||||
return &a, nil
|
||||
}
|
179
vendor/github.com/PagerDuty/go-pagerduty/client.go
generated
vendored
Normal file
179
vendor/github.com/PagerDuty/go-pagerduty/client.go
generated
vendored
Normal file
@ -0,0 +1,179 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
apiEndpoint = "https://api.pagerduty.com"
|
||||
)
|
||||
|
||||
// APIObject represents generic api json response that is shared by most
|
||||
// domain object (like escalation
|
||||
type APIObject struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Self string `json:"self,omitempty"`
|
||||
HTMLURL string `json:"html_url,omitempty"`
|
||||
}
|
||||
|
||||
// APIListObject are the fields used to control pagination when listing objects.
|
||||
type APIListObject struct {
|
||||
Limit uint `url:"limit,omitempty"`
|
||||
Offset uint `url:"offset,omitempty"`
|
||||
More bool `url:"more,omitempty"`
|
||||
Total uint `url:"total,omitempty"`
|
||||
}
|
||||
|
||||
// APIReference are the fields required to reference another API object.
|
||||
type APIReference struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
type APIDetails struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Details string `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
type errorObject struct {
|
||||
Code int `json:"code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Errors interface{} `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
func newDefaultHTTPClient() *http.Client {
|
||||
return &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 10,
|
||||
IdleConnTimeout: 60 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// HTTPClient is an interface which declares the functionality we need from an
|
||||
// HTTP client. This is to allow consumers to provide their own HTTP client as
|
||||
// needed, without restricting them to only using *http.Client.
|
||||
type HTTPClient interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// defaultHTTPClient is our own default HTTP client. We use this, instead of
|
||||
// http.DefaultClient, to avoid other packages tweaks to http.DefaultClient
|
||||
// causing issues with our HTTP calls. This also allows us to tweak the
|
||||
// transport values to be more resilient without making changes to the
|
||||
// http.DefaultClient.
|
||||
//
|
||||
// Keep this unexported so consumers of the package can't make changes to it.
|
||||
var defaultHTTPClient HTTPClient = newDefaultHTTPClient()
|
||||
|
||||
// Client wraps http client
|
||||
type Client struct {
|
||||
authToken string
|
||||
|
||||
// HTTPClient is the HTTP client used for making requests against the
|
||||
// PagerDuty API. You can use either *http.Client here, or your own
|
||||
// implementation.
|
||||
HTTPClient HTTPClient
|
||||
}
|
||||
|
||||
// NewClient creates an API client
|
||||
func NewClient(authToken string) *Client {
|
||||
return &Client{
|
||||
authToken: authToken,
|
||||
HTTPClient: defaultHTTPClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) delete(path string) (*http.Response, error) {
|
||||
return c.do("DELETE", path, nil, nil)
|
||||
}
|
||||
|
||||
func (c *Client) put(path string, payload interface{}, headers *map[string]string) (*http.Response, error) {
|
||||
|
||||
if payload != nil {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.do("PUT", path, bytes.NewBuffer(data), headers)
|
||||
}
|
||||
return c.do("PUT", path, nil, headers)
|
||||
}
|
||||
|
||||
func (c *Client) post(path string, payload interface{}, headers *map[string]string) (*http.Response, error) {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.do("POST", path, bytes.NewBuffer(data), headers)
|
||||
}
|
||||
|
||||
func (c *Client) get(path string) (*http.Response, error) {
|
||||
return c.do("GET", path, nil, nil)
|
||||
}
|
||||
|
||||
func (c *Client) do(method, path string, body io.Reader, headers *map[string]string) (*http.Response, error) {
|
||||
endpoint := apiEndpoint + path
|
||||
req, _ := http.NewRequest(method, endpoint, body)
|
||||
req.Header.Set("Accept", "application/vnd.pagerduty+json;version=2")
|
||||
if headers != nil {
|
||||
for k, v := range *headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Token token="+c.authToken)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
return c.checkResponse(resp, err)
|
||||
}
|
||||
|
||||
func (c *Client) decodeJSON(resp *http.Response, payload interface{}) error {
|
||||
defer resp.Body.Close()
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
return decoder.Decode(payload)
|
||||
}
|
||||
|
||||
func (c *Client) checkResponse(resp *http.Response, err error) (*http.Response, error) {
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("Error calling the API endpoint: %v", err)
|
||||
}
|
||||
if 199 >= resp.StatusCode || 300 <= resp.StatusCode {
|
||||
var eo *errorObject
|
||||
var getErr error
|
||||
if eo, getErr = c.getErrorFromResponse(resp); getErr != nil {
|
||||
return resp, fmt.Errorf("Response did not contain formatted error: %s. HTTP response code: %v. Raw response: %+v", getErr, resp.StatusCode, resp)
|
||||
}
|
||||
return resp, fmt.Errorf("Failed call API endpoint. HTTP response code: %v. Error: %v", resp.StatusCode, eo)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) getErrorFromResponse(resp *http.Response) (*errorObject, error) {
|
||||
var result map[string]errorObject
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", err)
|
||||
}
|
||||
s, ok := result["error"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have error field")
|
||||
}
|
||||
return &s, nil
|
||||
}
|
186
vendor/github.com/PagerDuty/go-pagerduty/escalation_policy.go
generated
vendored
Normal file
186
vendor/github.com/PagerDuty/go-pagerduty/escalation_policy.go
generated
vendored
Normal file
@ -0,0 +1,186 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
const (
|
||||
escPath = "/escalation_policies"
|
||||
)
|
||||
|
||||
// EscalationRule is a rule for an escalation policy to trigger.
|
||||
type EscalationRule struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Delay uint `json:"escalation_delay_in_minutes,omitempty"`
|
||||
Targets []APIObject `json:"targets"`
|
||||
}
|
||||
|
||||
// EscalationPolicy is a collection of escalation rules.
|
||||
type EscalationPolicy struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
EscalationRules []EscalationRule `json:"escalation_rules,omitempty"`
|
||||
Services []APIReference `json:"services,omitempty"`
|
||||
NumLoops uint `json:"num_loops,omitempty"`
|
||||
Teams []APIReference `json:"teams,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
RepeatEnabled bool `json:"repeat_enabled,omitempty"`
|
||||
}
|
||||
|
||||
// ListEscalationPoliciesResponse is the data structure returned from calling the ListEscalationPolicies API endpoint.
|
||||
type ListEscalationPoliciesResponse struct {
|
||||
APIListObject
|
||||
EscalationPolicies []EscalationPolicy `json:"escalation_policies"`
|
||||
}
|
||||
|
||||
type ListEscalationRulesResponse struct {
|
||||
APIListObject
|
||||
EscalationRules []EscalationRule `json:"escalation_rules"`
|
||||
}
|
||||
|
||||
// ListEscalationPoliciesOptions is the data structure used when calling the ListEscalationPolicies API endpoint.
|
||||
type ListEscalationPoliciesOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
UserIDs []string `url:"user_ids,omitempty,brackets"`
|
||||
TeamIDs []string `url:"team_ids,omitempty,brackets"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
SortBy string `url:"sort_by,omitempty"`
|
||||
}
|
||||
|
||||
// GetEscalationRuleOptions is the data structure used when calling the GetEscalationRule API endpoint.
|
||||
type GetEscalationRuleOptions struct {
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListEscalationPolicies lists all of the existing escalation policies.
|
||||
func (c *Client) ListEscalationPolicies(o ListEscalationPoliciesOptions) (*ListEscalationPoliciesResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get(escPath + "?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListEscalationPoliciesResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateEscalationPolicy creates a new escalation policy.
|
||||
func (c *Client) CreateEscalationPolicy(e EscalationPolicy) (*EscalationPolicy, error) {
|
||||
data := make(map[string]EscalationPolicy)
|
||||
data["escalation_policy"] = e
|
||||
resp, err := c.post(escPath, data, nil)
|
||||
return getEscalationPolicyFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteEscalationPolicy deletes an existing escalation policy and rules.
|
||||
func (c *Client) DeleteEscalationPolicy(id string) error {
|
||||
_, err := c.delete(escPath + "/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetEscalationPolicyOptions is the data structure used when calling the GetEscalationPolicy API endpoint.
|
||||
type GetEscalationPolicyOptions struct {
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// GetEscalationPolicy gets information about an existing escalation policy and its rules.
|
||||
func (c *Client) GetEscalationPolicy(id string, o *GetEscalationPolicyOptions) (*EscalationPolicy, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get(escPath + "/" + id + "?" + v.Encode())
|
||||
return getEscalationPolicyFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateEscalationPolicy updates an existing escalation policy and its rules.
|
||||
func (c *Client) UpdateEscalationPolicy(id string, e *EscalationPolicy) (*EscalationPolicy, error) {
|
||||
data := make(map[string]EscalationPolicy)
|
||||
data["escalation_policy"] = *e
|
||||
resp, err := c.put(escPath+"/"+id, data, nil)
|
||||
return getEscalationPolicyFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// CreateEscalationRule creates a new escalation rule for an escalation policy
|
||||
// and appends it to the end of the existing escalation rules.
|
||||
func (c *Client) CreateEscalationRule(escID string, e EscalationRule) (*EscalationRule, error) {
|
||||
data := make(map[string]EscalationRule)
|
||||
data["escalation_rule"] = e
|
||||
resp, err := c.post(escPath+"/"+escID+"/escalation_rules", data, nil)
|
||||
return getEscalationRuleFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// GetEscalationRule gets information about an existing escalation rule.
|
||||
func (c *Client) GetEscalationRule(escID string, id string, o *GetEscalationRuleOptions) (*EscalationRule, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get(escPath + "/" + escID + "/escalation_rules/" + id + "?" + v.Encode())
|
||||
return getEscalationRuleFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteEscalationRule deletes an existing escalation rule.
|
||||
func (c *Client) DeleteEscalationRule(escID string, id string) error {
|
||||
_, err := c.delete(escPath + "/" + escID + "/escalation_rules/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateEscalationRule updates an existing escalation rule.
|
||||
func (c *Client) UpdateEscalationRule(escID string, id string, e *EscalationRule) (*EscalationRule, error) {
|
||||
data := make(map[string]EscalationRule)
|
||||
data["escalation_rule"] = *e
|
||||
resp, err := c.put(escPath+"/"+escID+"/escalation_rules/"+id, data, nil)
|
||||
return getEscalationRuleFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// ListEscalationRules lists all of the escalation rules for an existing escalation policy.
|
||||
func (c *Client) ListEscalationRules(escID string) (*ListEscalationRulesResponse, error) {
|
||||
resp, err := c.get(escPath + "/" + escID + "/escalation_rules")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result ListEscalationRulesResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
func getEscalationRuleFromResponse(c *Client, resp *http.Response, err error) (*EscalationRule, error) {
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]EscalationRule
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "escalation_rule"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func getEscalationPolicyFromResponse(c *Client, resp *http.Response, err error) (*EscalationPolicy, error) {
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]EscalationPolicy
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "escalation_policy"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
62
vendor/github.com/PagerDuty/go-pagerduty/event.go
generated
vendored
Normal file
62
vendor/github.com/PagerDuty/go-pagerduty/event.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const eventEndPoint = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
|
||||
|
||||
// Event stores data for problem reporting, acknowledgement, and resolution.
|
||||
type Event struct {
|
||||
ServiceKey string `json:"service_key"`
|
||||
Type string `json:"event_type"`
|
||||
IncidentKey string `json:"incident_key,omitempty"`
|
||||
Description string `json:"description"`
|
||||
Client string `json:"client,omitempty"`
|
||||
ClientURL string `json:"client_url,omitempty"`
|
||||
Details interface{} `json:"details,omitempty"`
|
||||
Contexts []interface{} `json:"contexts,omitempty"`
|
||||
}
|
||||
|
||||
// EventResponse is the data returned from the CreateEvent API endpoint.
|
||||
type EventResponse struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
IncidentKey string `json:"incident_key"`
|
||||
}
|
||||
|
||||
// CreateEvent sends PagerDuty an event to trigger, acknowledge, or resolve a
|
||||
// problem. If you need to provide a custom HTTP client, please use
|
||||
// CreateEventWithHTTPClient.
|
||||
func CreateEvent(e Event) (*EventResponse, error) {
|
||||
return CreateEventWithHTTPClient(e, defaultHTTPClient)
|
||||
}
|
||||
|
||||
// CreateEventWithHTTPClient sends PagerDuty an event to trigger, acknowledge,
|
||||
// or resolve a problem. This function accepts a custom HTTP Client, if the
|
||||
// default one used by this package doesn't fit your needs. If you don't need a
|
||||
// custom HTTP client, please use CreateEvent instead.
|
||||
func CreateEventWithHTTPClient(e Event, client HTTPClient) (*EventResponse, error) {
|
||||
data, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, _ := http.NewRequest("POST", eventEndPoint, bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
|
||||
}
|
||||
var eventResponse EventResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &eventResponse, nil
|
||||
}
|
68
vendor/github.com/PagerDuty/go-pagerduty/event_v2.go
generated
vendored
Normal file
68
vendor/github.com/PagerDuty/go-pagerduty/event_v2.go
generated
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Event includes the incident/alert details
|
||||
type V2Event struct {
|
||||
RoutingKey string `json:"routing_key"`
|
||||
Action string `json:"event_action"`
|
||||
DedupKey string `json:"dedup_key,omitempty"`
|
||||
Images []interface{} `json:"images,omitempty"`
|
||||
Client string `json:"client,omitempty"`
|
||||
ClientURL string `json:"client_url,omitempty"`
|
||||
Payload *V2Payload `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
// Payload represents the individual event details for an event
|
||||
type V2Payload struct {
|
||||
Summary string `json:"summary"`
|
||||
Source string `json:"source"`
|
||||
Severity string `json:"severity"`
|
||||
Timestamp string `json:"timestamp,omitempty"`
|
||||
Component string `json:"component,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
Class string `json:"class,omitempty"`
|
||||
Details interface{} `json:"custom_details,omitempty"`
|
||||
}
|
||||
|
||||
// Response is the json response body for an event
|
||||
type V2EventResponse struct {
|
||||
RoutingKey string `json:"routing_key"`
|
||||
DedupKey string `json:"dedup_key"`
|
||||
EventAction string `json:"event_action"`
|
||||
}
|
||||
|
||||
const v2eventEndPoint = "https://events.pagerduty.com/v2/enqueue"
|
||||
|
||||
// ManageEvent handles the trigger, acknowledge, and resolve methods for an event
|
||||
func ManageEvent(e V2Event) (*V2EventResponse, error) {
|
||||
data, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, _ := http.NewRequest("POST", v2eventEndPoint, bytes.NewBuffer(data))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusAccepted {
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
|
||||
}
|
||||
return nil, fmt.Errorf("HTTP Status Code: %d, Message: %s", resp.StatusCode, string(bytes))
|
||||
}
|
||||
var eventResponse V2EventResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&eventResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &eventResponse, nil
|
||||
}
|
244
vendor/github.com/PagerDuty/go-pagerduty/incident.go
generated
vendored
Normal file
244
vendor/github.com/PagerDuty/go-pagerduty/incident.go
generated
vendored
Normal file
@ -0,0 +1,244 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Acknowledgement is the data structure of an acknoledgement of an incident.
|
||||
type Acknowledgement struct {
|
||||
At string
|
||||
Acknowledger APIObject
|
||||
}
|
||||
|
||||
// PendingAction is the data structure for any pending actions on an incident.
|
||||
type PendingAction struct {
|
||||
Type string
|
||||
At string
|
||||
}
|
||||
|
||||
// Assignment is the data structure for an assignment of an incident
|
||||
type Assignment struct {
|
||||
At string
|
||||
Assignee APIObject
|
||||
}
|
||||
|
||||
// Incident is a normalized, de-duplicated event generated by a PagerDuty integration.
|
||||
type Incident struct {
|
||||
APIObject
|
||||
IncidentNumber uint `json:"incident_number,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
PendingActions []PendingAction `json:"pending_actions,omitempty"`
|
||||
IncidentKey string `json:"incident_key,omitempty"`
|
||||
Service APIObject `json:"service,omitempty"`
|
||||
Assignments []Assignment `json:"assignments,omitempty"`
|
||||
Acknowledgements []Acknowledgement `json:"acknowledgements,omitempty"`
|
||||
LastStatusChangeAt string `json:"last_status_change_at,omitempty"`
|
||||
LastStatusChangeBy APIObject `json:"last_status_change_by,omitempty"`
|
||||
FirstTriggerLogEntry APIObject `json:"first_trigger_log_entry,omitempty"`
|
||||
EscalationPolicy APIObject `json:"escalation_policy,omitempty"`
|
||||
Teams []APIObject `json:"teams,omitempty"`
|
||||
Urgency string `json:"urgency,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Priority APIObject `json:"priority,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentsResponse is the response structure when calling the ListIncident API endpoint.
|
||||
type ListIncidentsResponse struct {
|
||||
APIListObject
|
||||
Incidents []Incident `json:"incidents,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentsOptions is the structure used when passing parameters to the ListIncident API endpoint.
|
||||
type ListIncidentsOptions struct {
|
||||
APIListObject
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
DateRange string `url:"date_range,omitempty"`
|
||||
Statuses []string `url:"statuses,omitempty,brackets"`
|
||||
IncidentKey string `url:"incident_key,omitempty"`
|
||||
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
|
||||
TeamIDs []string `url:"team_ids,omitempty,brackets"`
|
||||
UserIDs []string `url:"user_ids,omitempty,brackets"`
|
||||
Urgencies []string `url:"urgencies,omitempty,brackets"`
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
SortBy string `url:"sort_by,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListIncidents lists existing incidents.
|
||||
func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/incidents?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListIncidentsResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateIncident is the structure POST'd to the incidents endpoint. It wraps a CreateIncidentValue
|
||||
type CreateIncident struct {
|
||||
Incident CreateIncidentOptions `json:"incident"`
|
||||
}
|
||||
|
||||
// createIncidentResponse is returned from the API when creating a response.
|
||||
type createIncidentResponse struct {
|
||||
Incident Incident `json:incident`
|
||||
}
|
||||
|
||||
// CreateIncidentOptions is the structure used when POSTing to the CreateIncident API endpoint.
|
||||
type CreateIncidentOptions struct {
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Service APIReference `json:"service"`
|
||||
Priority APIReference `json:"priority"`
|
||||
IncidentKey string `json:"incident_key"`
|
||||
Body APIDetails `json:"body"`
|
||||
EscalationPolicy APIReference `json:"escalation_policy"`
|
||||
}
|
||||
|
||||
// CreateIncident creates an incident synchronously without a corresponding event from a monitoring service.
|
||||
func (c *Client) CreateIncident(from string, i *CreateIncident) (*Incident, error) {
|
||||
headers := make(map[string]string)
|
||||
headers["From"] = from
|
||||
resp, e := c.post("/incidents", i, &headers)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
var ii createIncidentResponse
|
||||
e = json.NewDecoder(resp.Body).Decode(&ii)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
return &ii.Incident, nil
|
||||
}
|
||||
|
||||
// ManageIncidents acknowledges, resolves, escalates, or reassigns one or more incidents.
|
||||
func (c *Client) ManageIncidents(from string, incidents []Incident) error {
|
||||
r := make(map[string][]Incident)
|
||||
headers := make(map[string]string)
|
||||
headers["From"] = from
|
||||
r["incidents"] = incidents
|
||||
_, e := c.put("/incidents", r, &headers)
|
||||
return e
|
||||
}
|
||||
|
||||
// GetIncident shows detailed information about an incident.
|
||||
func (c *Client) GetIncident(id string) (*Incident, error) {
|
||||
resp, err := c.get("/incidents/" + id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string]Incident
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i, ok := result["incident"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have incident field")
|
||||
}
|
||||
return &i, nil
|
||||
}
|
||||
|
||||
// IncidentNote is a note for the specified incident.
|
||||
type IncidentNote struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
User APIObject `json:"user,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentNotes lists existing notes for the specified incident.
|
||||
func (c *Client) ListIncidentNotes(id string) ([]IncidentNote, error) {
|
||||
resp, err := c.get("/incidents/" + id + "/notes")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string][]IncidentNote
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notes, ok := result["notes"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have notes field")
|
||||
}
|
||||
return notes, nil
|
||||
}
|
||||
|
||||
// IncidentAlert is a alert for the specified incident.
|
||||
type IncidentAlert struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
AlertKey string `json:"alert_key,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentAlerts lists existing alerts for the specified incident.
|
||||
func (c *Client) ListIncidentAlerts(id string) ([]IncidentAlert, error) {
|
||||
resp, err := c.get("/incidents/"+id+"/alerts")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string][]IncidentAlert
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
alerts, ok := result["alerts"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have alerts field")
|
||||
}
|
||||
return alerts, nil
|
||||
}
|
||||
|
||||
// CreateIncidentNote creates a new note for the specified incident.
|
||||
func (c *Client) CreateIncidentNote(id string, note IncidentNote) error {
|
||||
data := make(map[string]IncidentNote)
|
||||
data["note"] = note
|
||||
_, err := c.post("/incidents/"+id+"/notes", data, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// SnoozeIncident sets an incident to not alert for a specified period of time.
|
||||
func (c *Client) SnoozeIncident(id string, duration uint) error {
|
||||
data := make(map[string]uint)
|
||||
data["duration"] = duration
|
||||
_, err := c.post("/incidents/"+id+"/snooze", data, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListIncidentLogEntriesResponse is the response structure when calling the ListIncidentLogEntries API endpoint.
|
||||
type ListIncidentLogEntriesResponse struct {
|
||||
APIListObject
|
||||
LogEntries []LogEntry `json:"log_entries,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentLogEntriesOptions is the structure used when passing parameters to the ListIncidentLogEntries API endpoint.
|
||||
type ListIncidentLogEntriesOptions struct {
|
||||
APIListObject
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
IsOverview bool `url:"is_overview,omitempty"`
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
}
|
||||
|
||||
// ListIncidentLogEntries lists existing log entries for the specified incident.
|
||||
func (c *Client) ListIncidentLogEntries(id string, o ListIncidentLogEntriesOptions) (*ListIncidentLogEntriesResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/incidents/" + id + "/log_entries?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListIncidentLogEntriesResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
94
vendor/github.com/PagerDuty/go-pagerduty/log_entry.go
generated
vendored
Normal file
94
vendor/github.com/PagerDuty/go-pagerduty/log_entry.go
generated
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Agent is the actor who carried out the action.
|
||||
type Agent APIObject
|
||||
|
||||
// Channel is the means by which the action was carried out.
|
||||
type Channel struct {
|
||||
Type string
|
||||
}
|
||||
|
||||
// Context are to be included with the trigger such as links to graphs or images.
|
||||
type Context struct {
|
||||
Alt string
|
||||
Href string
|
||||
Src string
|
||||
Text string
|
||||
Type string
|
||||
}
|
||||
|
||||
// LogEntry is a list of all of the events that happened to an incident.
|
||||
type LogEntry struct {
|
||||
APIObject
|
||||
CreatedAt string `json:"created_at"`
|
||||
Agent Agent
|
||||
Channel Channel
|
||||
Incident Incident
|
||||
Teams []Team
|
||||
Contexts []Context
|
||||
AcknowledgementTimeout int `json:"acknowledgement_timeout"`
|
||||
EventDetails map[string]string
|
||||
}
|
||||
|
||||
// ListLogEntryResponse is the response data when calling the ListLogEntry API endpoint.
|
||||
type ListLogEntryResponse struct {
|
||||
APIListObject
|
||||
LogEntries []LogEntry `json:"log_entries"`
|
||||
}
|
||||
|
||||
// ListLogEntriesOptions is the data structure used when calling the ListLogEntry API endpoint.
|
||||
type ListLogEntriesOptions struct {
|
||||
APIListObject
|
||||
TimeZone string `url:"time_zone"`
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
IsOverview bool `url:"is_overview,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListLogEntries lists all of the incident log entries across the entire account.
|
||||
func (c *Client) ListLogEntries(o ListLogEntriesOptions) (*ListLogEntryResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/log_entries?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListLogEntryResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// GetLogEntryOptions is the data structure used when calling the GetLogEntry API endpoint.
|
||||
type GetLogEntryOptions struct {
|
||||
TimeZone string `url:"timezone,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// GetLogEntry list log entries for the specified incident.
|
||||
func (c *Client) GetLogEntry(id string, o GetLogEntryOptions) (*LogEntry, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/log_entries/" + id + "?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string]LogEntry
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
le, ok := result["log_entry"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have log_entry field")
|
||||
}
|
||||
return &le, nil
|
||||
}
|
101
vendor/github.com/PagerDuty/go-pagerduty/maintenance_window.go
generated
vendored
Normal file
101
vendor/github.com/PagerDuty/go-pagerduty/maintenance_window.go
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// MaintenanceWindow is used to temporarily disable one or more services for a set period of time.
|
||||
type MaintenanceWindow struct {
|
||||
APIObject
|
||||
SequenceNumber uint `json:"sequence_number,omitempty"`
|
||||
StartTime string `json:"start_time"`
|
||||
EndTime string `json:"end_time"`
|
||||
Description string `json:"description"`
|
||||
Services []APIObject `json:"services"`
|
||||
Teams []APIListObject `json:"teams"`
|
||||
CreatedBy APIListObject `json:"created_by"`
|
||||
}
|
||||
|
||||
// ListMaintenanceWindowsResponse is the data structur returned from calling the ListMaintenanceWindows API endpoint.
|
||||
type ListMaintenanceWindowsResponse struct {
|
||||
APIListObject
|
||||
MaintenanceWindows []MaintenanceWindow `json:"maintenance_windows"`
|
||||
}
|
||||
|
||||
// ListMaintenanceWindowsOptions is the data structure used when calling the ListMaintenanceWindows API endpoint.
|
||||
type ListMaintenanceWindowsOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
TeamIDs []string `url:"team_ids,omitempty,brackets"`
|
||||
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
|
||||
Filter string `url:"filter,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListMaintenanceWindows lists existing maintenance windows, optionally filtered by service and/or team, or whether they are from the past, present or future.
|
||||
func (c *Client) ListMaintenanceWindows(o ListMaintenanceWindowsOptions) (*ListMaintenanceWindowsResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/maintenance_windows?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListMaintenanceWindowsResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateMaintenanceWindows creates a new maintenance window for the specified services.
|
||||
func (c *Client) CreateMaintenanceWindows(m MaintenanceWindow) (*MaintenanceWindow, error) {
|
||||
data := make(map[string]MaintenanceWindow)
|
||||
data["maintenance_window"] = m
|
||||
resp, err := c.post("/maintenance_windows", data, nil)
|
||||
return getMaintenanceWindowFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteMaintenanceWindow deletes an existing maintenance window if it's in the future, or ends it if it's currently on-going.
|
||||
func (c *Client) DeleteMaintenanceWindow(id string) error {
|
||||
_, err := c.delete("/maintenance_windows/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetMaintenanceWindowOptions is the data structure used when calling the GetMaintenanceWindow API endpoint.
|
||||
type GetMaintenanceWindowOptions struct {
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// GetMaintenanceWindow gets an existing maintenance window.
|
||||
func (c *Client) GetMaintenanceWindow(id string, o GetMaintenanceWindowOptions) (*MaintenanceWindow, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/maintenance_windows/" + id + "?" + v.Encode())
|
||||
return getMaintenanceWindowFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateMaintenanceWindow updates an existing maintenance window.
|
||||
func (c *Client) UpdateMaintenanceWindow(m MaintenanceWindow) (*MaintenanceWindow, error) {
|
||||
resp, err := c.put("/maintenance_windows/"+m.ID, m, nil)
|
||||
return getMaintenanceWindowFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
func getMaintenanceWindowFromResponse(c *Client, resp *http.Response, err error) (*MaintenanceWindow, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]MaintenanceWindow
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "maintenance_window"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
44
vendor/github.com/PagerDuty/go-pagerduty/notification.go
generated
vendored
Normal file
44
vendor/github.com/PagerDuty/go-pagerduty/notification.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Notification is a message containing the details of the incident.
|
||||
type Notification struct {
|
||||
ID string `json:"id"`
|
||||
Type string
|
||||
StartedAt string `json:"started_at"`
|
||||
Address string
|
||||
User APIObject
|
||||
}
|
||||
|
||||
// ListNotificationOptions is the data structure used when calling the ListNotifications API endpoint.
|
||||
type ListNotificationOptions struct {
|
||||
APIListObject
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
Filter string `url:"filter,omitempty"`
|
||||
Includes []string `url:"include,omitempty"`
|
||||
}
|
||||
|
||||
// ListNotificationsResponse is the data structure returned from the ListNotifications API endpoint.
|
||||
type ListNotificationsResponse struct {
|
||||
APIListObject
|
||||
Notifications []Notification
|
||||
}
|
||||
|
||||
// ListNotifications lists notifications for a given time range, optionally filtered by type (sms_notification, email_notification, phone_notification, or push_notification).
|
||||
func (c *Client) ListNotifications(o ListNotificationOptions) (*ListNotificationsResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/notifications?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListNotificationsResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
48
vendor/github.com/PagerDuty/go-pagerduty/on_call.go
generated
vendored
Normal file
48
vendor/github.com/PagerDuty/go-pagerduty/on_call.go
generated
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// OnCall represents a contiguous unit of time for which a user will be on call for a given escalation policy and escalation rule.
|
||||
type OnCall struct {
|
||||
User APIObject `json:"user,omitempty"`
|
||||
Schedule APIObject `json:"schedule,omitempty"`
|
||||
EscalationPolicy APIObject `json:"escalation_policy,omitempty"`
|
||||
EscalationLevel uint `json:"escalation_level,omitempty"`
|
||||
Start string `json:"start,omitempty"`
|
||||
End string `json:"end,omitempty"`
|
||||
}
|
||||
|
||||
// ListOnCallsResponse is the data structure returned from calling the ListOnCalls API endpoint.
|
||||
type ListOnCallsResponse struct {
|
||||
APIListObject
|
||||
OnCalls []OnCall `json:"oncalls"`
|
||||
}
|
||||
|
||||
// ListOnCallOptions is the data structure used when calling the ListOnCalls API endpoint.
|
||||
type ListOnCallOptions struct {
|
||||
APIListObject
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
UserIDs []string `url:"user_ids,omitempty,brackets"`
|
||||
EscalationPolicyIDs []string `url:"escalation_policy_ids,omitempty,brackets"`
|
||||
ScheduleIDs []string `url:"schedule_ids,omitempty,brackets"`
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
Earliest bool `url:"earliest,omitempty"`
|
||||
}
|
||||
|
||||
// ListOnCalls list the on-call entries during a given time range.
|
||||
func (c *Client) ListOnCalls(o ListOnCallOptions) (*ListOnCallsResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/oncalls?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListOnCallsResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
33
vendor/github.com/PagerDuty/go-pagerduty/priorites.go
generated
vendored
Normal file
33
vendor/github.com/PagerDuty/go-pagerduty/priorites.go
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// PriorityProperty is a single priorty object returned from the Priorities endpoint
|
||||
type PriorityProperty struct {
|
||||
APIObject
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type Priorities struct {
|
||||
APIListObject
|
||||
Priorities []PriorityProperty `json:"priorities"`
|
||||
}
|
||||
|
||||
// ListPriorities lists existing priorities
|
||||
func (c *Client) ListPriorities() (*Priorities, error) {
|
||||
resp, e := c.get("/priorities")
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
var p Priorities
|
||||
e = json.NewDecoder(resp.Body).Decode(&p)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
}
|
264
vendor/github.com/PagerDuty/go-pagerduty/schedule.go
generated
vendored
Normal file
264
vendor/github.com/PagerDuty/go-pagerduty/schedule.go
generated
vendored
Normal file
@ -0,0 +1,264 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Restriction limits on-call responsibility for a layer to certain times of the day or week.
|
||||
type Restriction struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
StartTimeOfDay string `json:"start_time_of_day,omitempty"`
|
||||
StartDayOfWeek uint `json:"start_day_of_week,omitempty"`
|
||||
DurationSeconds uint `json:"duration_seconds,omitempty"`
|
||||
}
|
||||
|
||||
// RenderedScheduleEntry represents the computed set of schedule layer entries that put users on call for a schedule, and cannot be modified directly.
|
||||
type RenderedScheduleEntry struct {
|
||||
Start string `json:"start,omitempty"`
|
||||
End string `json:"end,omitempty"`
|
||||
User APIObject `json:"user,omitempty"`
|
||||
}
|
||||
|
||||
// ScheduleLayer is an entry that puts users on call for a schedule.
|
||||
type ScheduleLayer struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
Start string `json:"start,omitempty"`
|
||||
End string `json:"end,omitempty"`
|
||||
RotationVirtualStart string `json:"rotation_virtual_start,omitempty"`
|
||||
RotationTurnLengthSeconds uint `json:"rotation_turn_length_seconds,omitempty"`
|
||||
Users []UserReference `json:"users,omitempty"`
|
||||
Restrictions []Restriction `json:"restrictions,omitempty"`
|
||||
RenderedScheduleEntries []RenderedScheduleEntry `json:"rendered_schedule_entries,omitempty"`
|
||||
RenderedCoveragePercentage float64 `json:"rendered_coverage_percentage,omitempty"`
|
||||
}
|
||||
|
||||
// Schedule determines the time periods that users are on call.
|
||||
type Schedule struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
TimeZone string `json:"time_zone,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
EscalationPolicies []APIObject `json:"escalation_policies,omitempty"`
|
||||
Users []APIObject `json:"users,omitempty"`
|
||||
ScheduleLayers []ScheduleLayer `json:"schedule_layers,omitempty"`
|
||||
OverrideSubschedule ScheduleLayer `json:"override_subschedule,omitempty"`
|
||||
FinalSchedule ScheduleLayer `json:"final_schedule,omitempty"`
|
||||
}
|
||||
|
||||
// ListSchedulesOptions is the data structure used when calling the ListSchedules API endpoint.
|
||||
type ListSchedulesOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
}
|
||||
|
||||
// ListSchedulesResponse is the data structure returned from calling the ListSchedules API endpoint.
|
||||
type ListSchedulesResponse struct {
|
||||
APIListObject
|
||||
Schedules []Schedule `json:"schedules"`
|
||||
}
|
||||
|
||||
// UserReference is a reference to an authorized PagerDuty user.
|
||||
type UserReference struct {
|
||||
User APIObject `json:"user"`
|
||||
}
|
||||
|
||||
// ListSchedules lists the on-call schedules.
|
||||
func (c *Client) ListSchedules(o ListSchedulesOptions) (*ListSchedulesResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/schedules?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListSchedulesResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateSchedule creates a new on-call schedule.
|
||||
func (c *Client) CreateSchedule(s Schedule) (*Schedule, error) {
|
||||
data := make(map[string]Schedule)
|
||||
data["schedule"] = s
|
||||
resp, err := c.post("/schedules", data, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getScheduleFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// PreviewScheduleOptions is the data structure used when calling the PreviewSchedule API endpoint.
|
||||
type PreviewScheduleOptions struct {
|
||||
APIListObject
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
Overflow bool `url:"overflow,omitempty"`
|
||||
}
|
||||
|
||||
// PreviewSchedule previews what an on-call schedule would look like without saving it.
|
||||
func (c *Client) PreviewSchedule(s Schedule, o PreviewScheduleOptions) error {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var data map[string]Schedule
|
||||
data["schedule"] = s
|
||||
_, e := c.post("/schedules/preview?"+v.Encode(), data, nil)
|
||||
return e
|
||||
}
|
||||
|
||||
// DeleteSchedule deletes an on-call schedule.
|
||||
func (c *Client) DeleteSchedule(id string) error {
|
||||
_, err := c.delete("/schedules/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetScheduleOptions is the data structure used when calling the GetSchedule API endpoint.
|
||||
type GetScheduleOptions struct {
|
||||
APIListObject
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
}
|
||||
|
||||
// GetSchedule shows detailed information about a schedule, including entries for each layer and sub-schedule.
|
||||
func (c *Client) GetSchedule(id string, o GetScheduleOptions) (*Schedule, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not parse values for query: %v", err)
|
||||
}
|
||||
resp, err := c.get("/schedules/" + id + "?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getScheduleFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// UpdateScheduleOptions is the data structure used when calling the UpdateSchedule API endpoint.
|
||||
type UpdateScheduleOptions struct {
|
||||
Overflow bool `url:"overflow,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateSchedule updates an existing on-call schedule.
|
||||
func (c *Client) UpdateSchedule(id string, s Schedule) (*Schedule, error) {
|
||||
v := make(map[string]Schedule)
|
||||
v["schedule"] = s
|
||||
resp, err := c.put("/schedules/"+id, v, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getScheduleFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// ListOverridesOptions is the data structure used when calling the ListOverrides API endpoint.
|
||||
type ListOverridesOptions struct {
|
||||
APIListObject
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
Editable bool `url:"editable,omitempty"`
|
||||
Overflow bool `url:"overflow,omitempty"`
|
||||
}
|
||||
|
||||
// Overrides are any schedule layers from the override layer.
|
||||
type Override struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Start string `json:"start,omitempty"`
|
||||
End string `json:"end,omitempty"`
|
||||
User APIObject `json:"user,omitempty"`
|
||||
}
|
||||
|
||||
// ListOverrides lists overrides for a given time range.
|
||||
func (c *Client) ListOverrides(id string, o ListOverridesOptions) ([]Override, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/schedules/" + id + "/overrides?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string][]Override
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
overrides, ok := result["overrides"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have overrides field")
|
||||
}
|
||||
return overrides, nil
|
||||
}
|
||||
|
||||
// CreateOverride creates an override for a specific user covering the specified time range.
|
||||
func (c *Client) CreateOverride(id string, o Override) (*Override, error) {
|
||||
data := make(map[string]Override)
|
||||
data["override"] = o
|
||||
resp, err := c.post("/schedules/"+id+"/overrides", data, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return getOverrideFromResponse(c, resp)
|
||||
}
|
||||
|
||||
// DeleteOverride removes an override.
|
||||
func (c *Client) DeleteOverride(scheduleID, overrideID string) error {
|
||||
_, err := c.delete("/schedules/" + scheduleID + "/overrides/" + overrideID)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListOnCallUsersOptions is the data structure used when calling the ListOnCallUsers API endpoint.
|
||||
type ListOnCallUsersOptions struct {
|
||||
APIListObject
|
||||
Since string `url:"since,omitempty"`
|
||||
Until string `url:"until,omitempty"`
|
||||
}
|
||||
|
||||
// ListOnCallUsers lists all of the users on call in a given schedule for a given time range.
|
||||
func (c *Client) ListOnCallUsers(id string, o ListOnCallUsersOptions) ([]User, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/schedules/" + id + "/users?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string][]User
|
||||
if err := c.decodeJSON(resp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, ok := result["users"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("JSON response does not have users field")
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func getScheduleFromResponse(c *Client, resp *http.Response) (*Schedule, error) {
|
||||
var target map[string]Schedule
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "schedule"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func getOverrideFromResponse(c *Client, resp *http.Response) (*Override, error) {
|
||||
var target map[string]Override
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "override"
|
||||
o, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &o, nil
|
||||
}
|
204
vendor/github.com/PagerDuty/go-pagerduty/service.go
generated
vendored
Normal file
204
vendor/github.com/PagerDuty/go-pagerduty/service.go
generated
vendored
Normal file
@ -0,0 +1,204 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Integration is an endpoint (like Nagios, email, or an API call) that generates events, which are normalized and de-duplicated by PagerDuty to create incidents.
|
||||
type Integration struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
Service *APIObject `json:"service,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
Vendor *APIObject `json:"vendor,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
IntegrationKey string `json:"integration_key,omitempty"`
|
||||
IntegrationEmail string `json:"integration_email,omitempty"`
|
||||
}
|
||||
|
||||
// InlineModel represents when a scheduled action will occur.
|
||||
type InlineModel struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// ScheduledAction contains scheduled actions for the service.
|
||||
type ScheduledAction struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
At InlineModel `json:"at,omitempty"`
|
||||
ToUrgency string `json:"to_urgency"`
|
||||
}
|
||||
|
||||
// IncidentUrgencyType are the incidents urgency during or outside support hours.
|
||||
type IncidentUrgencyType struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Urgency string `json:"urgency,omitempty"`
|
||||
}
|
||||
|
||||
// SupportHours are the support hours for the service.
|
||||
type SupportHours struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Timezone string `json:"time_zone,omitempty"`
|
||||
StartTime string `json:"start_time,omitempty"`
|
||||
EndTime string `json:"end_time,omitempty"`
|
||||
DaysOfWeek []uint `json:"days_of_week,omitempty"`
|
||||
}
|
||||
|
||||
// IncidentUrgencyRule is the default urgency for new incidents.
|
||||
type IncidentUrgencyRule struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Urgency string `json:"urgency,omitempty"`
|
||||
DuringSupportHours *IncidentUrgencyType `json:"during_support_hours,omitempty"`
|
||||
OutsideSupportHours *IncidentUrgencyType `json:"outside_support_hours,omitempty"`
|
||||
}
|
||||
|
||||
// Service represents something you monitor (like a web service, email service, or database service).
|
||||
type Service struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
AutoResolveTimeout *uint `json:"auto_resolve_timeout"`
|
||||
AcknowledgementTimeout *uint `json:"acknowledgement_timeout"`
|
||||
CreateAt string `json:"created_at,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"`
|
||||
Integrations []Integration `json:"integrations,omitempty"`
|
||||
EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"`
|
||||
Teams []Team `json:"teams,omitempty"`
|
||||
IncidentUrgencyRule *IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"`
|
||||
SupportHours *SupportHours `json:"support_hours,omitempty"`
|
||||
ScheduledActions []ScheduledAction `json:"scheduled_actions,omitempty"`
|
||||
AlertCreation string `json:"alert_creation,omitempty"`
|
||||
}
|
||||
|
||||
// ListServiceOptions is the data structure used when calling the ListServices API endpoint.
|
||||
type ListServiceOptions struct {
|
||||
APIListObject
|
||||
TeamIDs []string `url:"team_ids,omitempty,brackets"`
|
||||
TimeZone string `url:"time_zone,omitempty"`
|
||||
SortBy string `url:"sort_by,omitempty"`
|
||||
Query string `url:"query,omitempty"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListServiceResponse is the data structure returned from calling the ListServices API endpoint.
|
||||
type ListServiceResponse struct {
|
||||
APIListObject
|
||||
Services []Service
|
||||
}
|
||||
|
||||
// ListServices lists existing services.
|
||||
func (c *Client) ListServices(o ListServiceOptions) (*ListServiceResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/services?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListServiceResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// GetServiceOptions is the data structure used when calling the GetService API endpoint.
|
||||
type GetServiceOptions struct {
|
||||
Includes []string `url:"include,brackets,omitempty"`
|
||||
}
|
||||
|
||||
// GetService gets details about an existing service.
|
||||
func (c *Client) GetService(id string, o *GetServiceOptions) (*Service, error) {
|
||||
v, err := query.Values(o)
|
||||
resp, err := c.get("/services/" + id + "?" + v.Encode())
|
||||
return getServiceFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// CreateService creates a new service.
|
||||
func (c *Client) CreateService(s Service) (*Service, error) {
|
||||
data := make(map[string]Service)
|
||||
data["service"] = s
|
||||
resp, err := c.post("/services", data, nil)
|
||||
return getServiceFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateService updates an existing service.
|
||||
func (c *Client) UpdateService(s Service) (*Service, error) {
|
||||
resp, err := c.put("/services/"+s.ID, s, nil)
|
||||
return getServiceFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteService deletes an existing service.
|
||||
func (c *Client) DeleteService(id string) error {
|
||||
_, err := c.delete("/services/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateIntegration creates a new integration belonging to a service.
|
||||
func (c *Client) CreateIntegration(id string, i Integration) (*Integration, error) {
|
||||
data := make(map[string]Integration)
|
||||
data["integration"] = i
|
||||
resp, err := c.post("/services/"+id+"/integrations", data, nil)
|
||||
return getIntegrationFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// GetIntegrationOptions is the data structure used when calling the GetIntegration API endpoint.
|
||||
type GetIntegrationOptions struct {
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// GetIntegration gets details about an integration belonging to a service.
|
||||
func (c *Client) GetIntegration(serviceID, integrationID string, o GetIntegrationOptions) (*Integration, error) {
|
||||
v, queryErr := query.Values(o)
|
||||
if queryErr != nil {
|
||||
return nil, queryErr
|
||||
}
|
||||
resp, err := c.get("/services/" + serviceID + "/integrations/" + integrationID + "?" + v.Encode())
|
||||
return getIntegrationFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateIntegration updates an integration belonging to a service.
|
||||
func (c *Client) UpdateIntegration(serviceID string, i Integration) (*Integration, error) {
|
||||
resp, err := c.put("/services/"+serviceID+"/integrations/"+i.ID, i, nil)
|
||||
return getIntegrationFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteIntegration deletes an existing integration.
|
||||
func (c *Client) DeleteIntegration(serviceID string, integrationID string) error {
|
||||
_, err := c.delete("/services/" + serviceID + "/integrations/" + integrationID)
|
||||
return err
|
||||
}
|
||||
|
||||
func getServiceFromResponse(c *Client, resp *http.Response, err error) (*Service, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]Service
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "service"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func getIntegrationFromResponse(c *Client, resp *http.Response, err error) (*Integration, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]Integration
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", err)
|
||||
}
|
||||
rootNode := "integration"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
106
vendor/github.com/PagerDuty/go-pagerduty/team.go
generated
vendored
Normal file
106
vendor/github.com/PagerDuty/go-pagerduty/team.go
generated
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Team is a collection of users and escalation policies that represent a group of people within an organization.
|
||||
type Team struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
// ListTeamResponse is the structure used when calling the ListTeams API endpoint.
|
||||
type ListTeamResponse struct {
|
||||
APIListObject
|
||||
Teams []Team
|
||||
}
|
||||
|
||||
// ListTeamOptions are the input parameters used when calling the ListTeams API endpoint.
|
||||
type ListTeamOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
}
|
||||
|
||||
// ListTeams lists teams of your PagerDuty account, optionally filtered by a search query.
|
||||
func (c *Client) ListTeams(o ListTeamOptions) (*ListTeamResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.get("/teams?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListTeamResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateTeam creates a new team.
|
||||
func (c *Client) CreateTeam(t *Team) (*Team, error) {
|
||||
resp, err := c.post("/teams", t, nil)
|
||||
return getTeamFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteTeam removes an existing team.
|
||||
func (c *Client) DeleteTeam(id string) error {
|
||||
_, err := c.delete("/teams/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetTeam gets details about an existing team.
|
||||
func (c *Client) GetTeam(id string) (*Team, error) {
|
||||
resp, err := c.get("/teams/" + id)
|
||||
return getTeamFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateTeam updates an existing team.
|
||||
func (c *Client) UpdateTeam(id string, t *Team) (*Team, error) {
|
||||
resp, err := c.put("/teams/"+id, t, nil)
|
||||
return getTeamFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// RemoveEscalationPolicyFromTeam removes an escalation policy from a team.
|
||||
func (c *Client) RemoveEscalationPolicyFromTeam(teamID, epID string) error {
|
||||
_, err := c.delete("/teams/" + teamID + "/escalation_policies/" + epID)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddEscalationPolicyToTeam adds an escalation policy to a team.
|
||||
func (c *Client) AddEscalationPolicyToTeam(teamID, epID string) error {
|
||||
_, err := c.put("/teams/"+teamID+"/escalation_policies/"+epID, nil, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveUserFromTeam removes a user from a team.
|
||||
func (c *Client) RemoveUserFromTeam(teamID, userID string) error {
|
||||
_, err := c.delete("/teams/" + teamID + "/users/" + userID)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddUserToTeam adds a user to a team.
|
||||
func (c *Client) AddUserToTeam(teamID, userID string) error {
|
||||
_, err := c.put("/teams/"+teamID+"/users/"+userID, nil, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func getTeamFromResponse(c *Client, resp *http.Response, err error) (*Team, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]Team
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "team"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
125
vendor/github.com/PagerDuty/go-pagerduty/user.go
generated
vendored
Normal file
125
vendor/github.com/PagerDuty/go-pagerduty/user.go
generated
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// ContactMethod is a way of contacting the user.
|
||||
type ContactMethod struct {
|
||||
ID string
|
||||
Label string
|
||||
Address string
|
||||
Type string
|
||||
SendShortEmail bool `json:"send_short_email"`
|
||||
}
|
||||
|
||||
// NotificationRule is a rule for notifying the user.
|
||||
type NotificationRule struct {
|
||||
ID string
|
||||
StartDelayInMinutes uint `json:"start_delay_in_minutes"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
ContactMethod ContactMethod `json:"contact_method"`
|
||||
Urgency string
|
||||
Type string
|
||||
}
|
||||
|
||||
// User is a member of a PagerDuty account that has the ability to interact with incidents and other data on the account.
|
||||
type User struct {
|
||||
APIObject
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Timezone string `json:"timezone,omitempty"`
|
||||
Color string `json:"color,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
AvatarURL string `json:"avatar_url,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
InvitationSent bool
|
||||
ContactMethods []ContactMethod `json:"contact_methods"`
|
||||
NotificationRules []NotificationRule `json:"notification_rules"`
|
||||
JobTitle string `json:"job_title,omitempty"`
|
||||
Teams []Team
|
||||
}
|
||||
|
||||
// ListUsersResponse is the data structure returned from calling the ListUsers API endpoint.
|
||||
type ListUsersResponse struct {
|
||||
APIListObject
|
||||
Users []User
|
||||
}
|
||||
|
||||
// ListUsersOptions is the data structure used when calling the ListUsers API endpoint.
|
||||
type ListUsersOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
TeamIDs []string `url:"team_ids,omitempty,brackets"`
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// GetUserOptions is the data structure used when calling the GetUser API endpoint.
|
||||
type GetUserOptions struct {
|
||||
Includes []string `url:"include,omitempty,brackets"`
|
||||
}
|
||||
|
||||
// ListUsers lists users of your PagerDuty account, optionally filtered by a search query.
|
||||
func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/users?" + v.Encode())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result ListUsersResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// CreateUser creates a new user.
|
||||
func (c *Client) CreateUser(u User) (*User, error) {
|
||||
data := make(map[string]User)
|
||||
data["user"] = u
|
||||
resp, err := c.post("/users", data, nil)
|
||||
return getUserFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// DeleteUser deletes a user.
|
||||
func (c *Client) DeleteUser(id string) error {
|
||||
_, err := c.delete("/users/" + id)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetUser gets details about an existing user.
|
||||
func (c *Client) GetUser(id string, o GetUserOptions) (*User, error) {
|
||||
v, err := query.Values(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.get("/users/" + id + "?" + v.Encode())
|
||||
return getUserFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
// UpdateUser updates an existing user.
|
||||
func (c *Client) UpdateUser(u User) (*User, error) {
|
||||
v := make(map[string]User)
|
||||
v["user"] = u
|
||||
resp, err := c.put("/users/"+u.ID, v, nil)
|
||||
return getUserFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
func getUserFromResponse(c *Client, resp *http.Response, err error) (*User, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]User
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "user"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
74
vendor/github.com/PagerDuty/go-pagerduty/vendor.go
generated
vendored
Normal file
74
vendor/github.com/PagerDuty/go-pagerduty/vendor.go
generated
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
)
|
||||
|
||||
// Vendor represents a specific type of integration. AWS Cloudwatch, Splunk, Datadog, etc are all examples of vendors that can be integrated in PagerDuty by making an integration.
|
||||
type Vendor struct {
|
||||
APIObject
|
||||
Name string `json:"name,omitempty"`
|
||||
LogoURL string `json:"logo_url,omitempty"`
|
||||
LongName string `json:"long_name,omitempty"`
|
||||
WebsiteURL string `json:"website_url,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Connectable bool `json:"connectable,omitempty"`
|
||||
ThumbnailURL string `json:"thumbnail_url,omitempty"`
|
||||
GenericServiceType string `json:"generic_service_type,omitempty"`
|
||||
IntegrationGuideURL string `json:"integration_guide_url,omitempty"`
|
||||
}
|
||||
|
||||
// ListVendorResponse is the data structure returned from calling the ListVendors API endpoint.
|
||||
type ListVendorResponse struct {
|
||||
APIListObject
|
||||
Vendors []Vendor
|
||||
}
|
||||
|
||||
// ListVendorOptions is the data structure used when calling the ListVendors API endpoint.
|
||||
type ListVendorOptions struct {
|
||||
APIListObject
|
||||
Query string `url:"query,omitempty"`
|
||||
}
|
||||
|
||||
// ListVendors lists existing vendors.
|
||||
func (c *Client) ListVendors(o ListVendorOptions) (*ListVendorResponse, error) {
|
||||
v, err := query.Values(o)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.get("/vendors?" + v.Encode())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result ListVendorResponse
|
||||
return &result, c.decodeJSON(resp, &result)
|
||||
}
|
||||
|
||||
// GetVendor gets details about an existing vendor.
|
||||
func (c *Client) GetVendor(id string) (*Vendor, error) {
|
||||
resp, err := c.get("/vendors/" + id)
|
||||
return getVendorFromResponse(c, resp, err)
|
||||
}
|
||||
|
||||
func getVendorFromResponse(c *Client, resp *http.Response, err error) (*Vendor, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var target map[string]Vendor
|
||||
if dErr := c.decodeJSON(resp, &target); dErr != nil {
|
||||
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
|
||||
}
|
||||
rootNode := "vendor"
|
||||
t, nodeOK := target[rootNode]
|
||||
if !nodeOK {
|
||||
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
|
||||
}
|
||||
return &t, nil
|
||||
}
|
37
vendor/github.com/PagerDuty/go-pagerduty/webhook.go
generated
vendored
Normal file
37
vendor/github.com/PagerDuty/go-pagerduty/webhook.go
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package pagerduty
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
// IncidentDetail contains a representation of the incident associated with the action that caused this webhook message.
|
||||
type IncidentDetail struct {
|
||||
ID string `json:"id"`
|
||||
IncidentNumber uint `json:"incident_number"`
|
||||
CreatedOn string `json:"created_on"`
|
||||
Status string `json:"status"`
|
||||
HTMLUrl string `json:"html_url"`
|
||||
Service string `json:"service"`
|
||||
AssignedToUser *json.RawMessage `json:"assigned_to_user"`
|
||||
AssignedTo []string `json:"assigned_to"`
|
||||
TriggerSummaryData *json.RawMessage `json:"trigger_summary_data"`
|
||||
TriggerDetailsHTMLUrl string `json:"trigger_details_html_url"`
|
||||
}
|
||||
|
||||
// WebhookPayload is a single message array for a webhook.
|
||||
type WebhookPayload struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
CreatedOn string `json:"created_on"`
|
||||
Data *json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// DecodeWebhook decodes a webhook from a response object.
|
||||
func DecodeWebhook(r io.Reader) (*WebhookPayload, error) {
|
||||
var payload WebhookPayload
|
||||
if err := json.NewDecoder(r).Decode(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &payload, nil
|
||||
}
|
1
vendor/github.com/PuerkitoBio/goquery/.gitattributes
generated
vendored
Normal file
1
vendor/github.com/PuerkitoBio/goquery/.gitattributes
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
testdata/* linguist-vendored
|
16
vendor/github.com/PuerkitoBio/goquery/.gitignore
generated
vendored
Normal file
16
vendor/github.com/PuerkitoBio/goquery/.gitignore
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
# editor temporary files
|
||||
*.sublime-*
|
||||
.DS_Store
|
||||
*.swp
|
||||
#*.*#
|
||||
tags
|
||||
|
||||
# direnv config
|
||||
.env*
|
||||
|
||||
# test binaries
|
||||
*.test
|
||||
|
||||
# coverage and profilte outputs
|
||||
*.out
|
||||
|
16
vendor/github.com/PuerkitoBio/goquery/.travis.yml
generated
vendored
Normal file
16
vendor/github.com/PuerkitoBio/goquery/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.1
|
||||
- 1.2.x
|
||||
- 1.3.x
|
||||
- 1.4.x
|
||||
- 1.5.x
|
||||
- 1.6.x
|
||||
- 1.7.x
|
||||
- 1.8.x
|
||||
- 1.9.x
|
||||
- "1.10.x"
|
||||
- 1.11.x
|
||||
- tip
|
||||
|
12
vendor/github.com/PuerkitoBio/goquery/LICENSE
generated
vendored
Normal file
12
vendor/github.com/PuerkitoBio/goquery/LICENSE
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
Copyright (c) 2012-2016, Martin Angers & Contributors
|
||||
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 the author 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 HOLDER 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.
|
179
vendor/github.com/PuerkitoBio/goquery/README.md
generated
vendored
Normal file
179
vendor/github.com/PuerkitoBio/goquery/README.md
generated
vendored
Normal file
@ -0,0 +1,179 @@
|
||||
# goquery - a little like that j-thing, only in Go
|
||||
[](http://travis-ci.org/PuerkitoBio/goquery) [](http://godoc.org/github.com/PuerkitoBio/goquery) [](https://sourcegraph.com/github.com/PuerkitoBio/goquery?badge)
|
||||
|
||||
goquery brings a syntax and a set of features similar to [jQuery][] to the [Go language][go]. It is based on Go's [net/html package][html] and the CSS Selector library [cascadia][]. Since the net/html parser returns nodes, and not a full-featured DOM tree, jQuery's stateful manipulation functions (like height(), css(), detach()) have been left off.
|
||||
|
||||
Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML. See the [wiki][] for various options to do this.
|
||||
|
||||
Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface. jQuery being the ultra-popular library that it is, I felt that writing a similar HTML-manipulating library was better to follow its API than to start anew (in the same spirit as Go's `fmt` package), even though some of its methods are less than intuitive (looking at you, [index()][index]...).
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Installation](#installation)
|
||||
* [Changelog](#changelog)
|
||||
* [API](#api)
|
||||
* [Examples](#examples)
|
||||
* [Related Projects](#related-projects)
|
||||
* [Support](#support)
|
||||
* [License](#license)
|
||||
|
||||
## Installation
|
||||
|
||||
Please note that because of the net/html dependency, goquery requires Go1.1+.
|
||||
|
||||
$ go get github.com/PuerkitoBio/goquery
|
||||
|
||||
(optional) To run unit tests:
|
||||
|
||||
$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
|
||||
$ go test
|
||||
|
||||
(optional) To run benchmarks (warning: it runs for a few minutes):
|
||||
|
||||
$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
|
||||
$ go test -bench=".*"
|
||||
|
||||
## Changelog
|
||||
|
||||
**Note that goquery's API is now stable, and will not break.**
|
||||
|
||||
* **2018-11-15 (v1.5.0)** : Go module support (thanks @Zaba505).
|
||||
* **2018-06-07 (v1.4.1)** : Add `NewDocumentFromReader` examples.
|
||||
* **2018-03-24 (v1.4.0)** : Deprecate `NewDocument(url)` and `NewDocumentFromResponse(response)`.
|
||||
* **2018-01-28 (v1.3.0)** : Add `ToEnd` constant to `Slice` until the end of the selection (thanks to @davidjwilkins for raising the issue).
|
||||
* **2018-01-11 (v1.2.0)** : Add `AddBack*` and deprecate `AndSelf` (thanks to @davidjwilkins).
|
||||
* **2017-02-12 (v1.1.0)** : Add `SetHtml` and `SetText` (thanks to @glebtv).
|
||||
* **2016-12-29 (v1.0.2)** : Optimize allocations for `Selection.Text` (thanks to @radovskyb).
|
||||
* **2016-08-28 (v1.0.1)** : Optimize performance for large documents.
|
||||
* **2016-07-27 (v1.0.0)** : Tag version 1.0.0.
|
||||
* **2016-06-15** : Invalid selector strings internally compile to a `Matcher` implementation that never matches any node (instead of a panic). So for example, `doc.Find("~")` returns an empty `*Selection` object.
|
||||
* **2016-02-02** : Add `NodeName` utility function similar to the DOM's `nodeName` property. It returns the tag name of the first element in a selection, and other relevant values of non-element nodes (see godoc for details). Add `OuterHtml` utility function similar to the DOM's `outerHTML` property (named `OuterHtml` in small caps for consistency with the existing `Html` method on the `Selection`).
|
||||
* **2015-04-20** : Add `AttrOr` helper method to return the attribute's value or a default value if absent. Thanks to [piotrkowalczuk][piotr].
|
||||
* **2015-02-04** : Add more manipulation functions - Prepend* - thanks again to [Andrew Stone][thatguystone].
|
||||
* **2014-11-28** : Add more manipulation functions - ReplaceWith*, Wrap* and Unwrap - thanks again to [Andrew Stone][thatguystone].
|
||||
* **2014-11-07** : Add manipulation functions (thanks to [Andrew Stone][thatguystone]) and `*Matcher` functions, that receive compiled cascadia selectors instead of selector strings, thus avoiding potential panics thrown by goquery via `cascadia.MustCompile` calls. This results in better performance (selectors can be compiled once and reused) and more idiomatic error handling (you can handle cascadia's compilation errors, instead of recovering from panics, which had been bugging me for a long time). Note that the actual type expected is a `Matcher` interface, that `cascadia.Selector` implements. Other matcher implementations could be used.
|
||||
* **2014-11-06** : Change import paths of net/html to golang.org/x/net/html (see https://groups.google.com/forum/#!topic/golang-nuts/eD8dh3T9yyA). Make sure to update your code to use the new import path too when you call goquery with `html.Node`s.
|
||||
* **v0.3.2** : Add `NewDocumentFromReader()` (thanks jweir) which allows creating a goquery document from an io.Reader.
|
||||
* **v0.3.1** : Add `NewDocumentFromResponse()` (thanks assassingj) which allows creating a goquery document from an http response.
|
||||
* **v0.3.0** : Add `EachWithBreak()` which allows to break out of an `Each()` loop by returning false. This function was added instead of changing the existing `Each()` to avoid breaking compatibility.
|
||||
* **v0.2.1** : Make go-getable, now that [go.net/html is Go1.0-compatible][gonet] (thanks to @matrixik for pointing this out).
|
||||
* **v0.2.0** : Add support for negative indices in Slice(). **BREAKING CHANGE** `Document.Root` is removed, `Document` is now a `Selection` itself (a selection of one, the root element, just like `Document.Root` was before). Add jQuery's Closest() method.
|
||||
* **v0.1.1** : Add benchmarks to use as baseline for refactorings, refactor Next...() and Prev...() methods to use the new html package's linked list features (Next/PrevSibling, FirstChild). Good performance boost (40+% in some cases).
|
||||
* **v0.1.0** : Initial release.
|
||||
|
||||
## API
|
||||
|
||||
goquery exposes two structs, `Document` and `Selection`, and the `Matcher` interface. Unlike jQuery, which is loaded as part of a DOM document, and thus acts on its containing document, goquery doesn't know which HTML document to act upon. So it needs to be told, and that's what the `Document` type is for. It holds the root document node as the initial Selection value to manipulate.
|
||||
|
||||
jQuery often has many variants for the same function (no argument, a selector string argument, a jQuery object argument, a DOM element argument, ...). Instead of exposing the same features in goquery as a single method with variadic empty interface arguments, statically-typed signatures are used following this naming convention:
|
||||
|
||||
* When the jQuery equivalent can be called with no argument, it has the same name as jQuery for the no argument signature (e.g.: `Prev()`), and the version with a selector string argument is called `XxxFiltered()` (e.g.: `PrevFiltered()`)
|
||||
* When the jQuery equivalent **requires** one argument, the same name as jQuery is used for the selector string version (e.g.: `Is()`)
|
||||
* The signatures accepting a jQuery object as argument are defined in goquery as `XxxSelection()` and take a `*Selection` object as argument (e.g.: `FilterSelection()`)
|
||||
* The signatures accepting a DOM element as argument in jQuery are defined in goquery as `XxxNodes()` and take a variadic argument of type `*html.Node` (e.g.: `FilterNodes()`)
|
||||
* The signatures accepting a function as argument in jQuery are defined in goquery as `XxxFunction()` and take a function as argument (e.g.: `FilterFunction()`)
|
||||
* The goquery methods that can be called with a selector string have a corresponding version that take a `Matcher` interface and are defined as `XxxMatcher()` (e.g.: `IsMatcher()`)
|
||||
|
||||
Utility functions that are not in jQuery but are useful in Go are implemented as functions (that take a `*Selection` as parameter), to avoid a potential naming clash on the `*Selection`'s methods (reserved for jQuery-equivalent behaviour).
|
||||
|
||||
The complete [godoc reference documentation can be found here][doc].
|
||||
|
||||
Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the [cascadia project][cascadia] for details. Invalid selector strings compile to a `Matcher` that fails to match any node. Behaviour of the various functions that take a selector string as argument follows from that fact, e.g. (where `~` is an invalid selector string):
|
||||
|
||||
* `Find("~")` returns an empty selection because the selector string doesn't match anything.
|
||||
* `Add("~")` returns a new selection that holds the same nodes as the original selection, because it didn't add any node (selector string didn't match anything).
|
||||
* `ParentsFiltered("~")` returns an empty selection because the selector string doesn't match anything.
|
||||
* `ParentsUntil("~")` returns all parents of the selection because the selector string didn't match any element to stop before the top element.
|
||||
|
||||
## Examples
|
||||
|
||||
See some tips and tricks in the [wiki][].
|
||||
|
||||
Adapted from example_test.go:
|
||||
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func ExampleScrape() {
|
||||
// Request the HTML page.
|
||||
res, err := http.Get("http://metalsucks.net")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
|
||||
}
|
||||
|
||||
// Load the HTML document
|
||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Find the review items
|
||||
doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
|
||||
// For each item found, get the band and title
|
||||
band := s.Find("a").Text()
|
||||
title := s.Find("i").Text()
|
||||
fmt.Printf("Review %d: %s - %s\n", i, band, title)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
ExampleScrape()
|
||||
}
|
||||
```
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [Goq][goq], an HTML deserialization and scraping library based on goquery and struct tags.
|
||||
- [andybalholm/cascadia][cascadia], the CSS selector library used by goquery.
|
||||
- [suntong/cascadia][cascadiacli], a command-line interface to the cascadia CSS selector library, useful to test selectors.
|
||||
- [asciimoo/colly](https://github.com/asciimoo/colly), a lightning fast and elegant Scraping Framework
|
||||
- [gnulnx/goperf](https://github.com/gnulnx/goperf), a website performance test tool that also fetches static assets.
|
||||
- [MontFerret/ferret](https://github.com/MontFerret/ferret), declarative web scraping.
|
||||
|
||||
## Support
|
||||
|
||||
There are a number of ways you can support the project:
|
||||
|
||||
* Use it, star it, build something with it, spread the word!
|
||||
- If you do build something open-source or otherwise publicly-visible, let me know so I can add it to the [Related Projects](#related-projects) section!
|
||||
* Raise issues to improve the project (note: doc typos and clarifications are issues too!)
|
||||
- Please search existing issues before opening a new one - it may have already been adressed.
|
||||
* Pull requests: please discuss new code in an issue first, unless the fix is really trivial.
|
||||
- Make sure new code is tested.
|
||||
- Be mindful of existing code - PRs that break existing code have a high probability of being declined, unless it fixes a serious issue.
|
||||
|
||||
If you desperately want to send money my way, I have a BuyMeACoffee.com page:
|
||||
|
||||
<a href="https://www.buymeacoffee.com/mna" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
||||
|
||||
## License
|
||||
|
||||
The [BSD 3-Clause license][bsd], the same as the [Go language][golic]. Cascadia's license is [here][caslic].
|
||||
|
||||
[jquery]: http://jquery.com/
|
||||
[go]: http://golang.org/
|
||||
[cascadia]: https://github.com/andybalholm/cascadia
|
||||
[cascadiacli]: https://github.com/suntong/cascadia
|
||||
[bsd]: http://opensource.org/licenses/BSD-3-Clause
|
||||
[golic]: http://golang.org/LICENSE
|
||||
[caslic]: https://github.com/andybalholm/cascadia/blob/master/LICENSE
|
||||
[doc]: http://godoc.org/github.com/PuerkitoBio/goquery
|
||||
[index]: http://api.jquery.com/index/
|
||||
[gonet]: https://github.com/golang/net/
|
||||
[html]: http://godoc.org/golang.org/x/net/html
|
||||
[wiki]: https://github.com/PuerkitoBio/goquery/wiki/Tips-and-tricks
|
||||
[thatguystone]: https://github.com/thatguystone
|
||||
[piotr]: https://github.com/piotrkowalczuk
|
||||
[goq]: https://github.com/andrewstuart/goq
|
124
vendor/github.com/PuerkitoBio/goquery/array.go
generated
vendored
Normal file
124
vendor/github.com/PuerkitoBio/goquery/array.go
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
const (
|
||||
maxUint = ^uint(0)
|
||||
maxInt = int(maxUint >> 1)
|
||||
|
||||
// ToEnd is a special index value that can be used as end index in a call
|
||||
// to Slice so that all elements are selected until the end of the Selection.
|
||||
// It is equivalent to passing (*Selection).Length().
|
||||
ToEnd = maxInt
|
||||
)
|
||||
|
||||
// First reduces the set of matched elements to the first in the set.
|
||||
// It returns a new Selection object, and an empty Selection object if the
|
||||
// the selection is empty.
|
||||
func (s *Selection) First() *Selection {
|
||||
return s.Eq(0)
|
||||
}
|
||||
|
||||
// Last reduces the set of matched elements to the last in the set.
|
||||
// It returns a new Selection object, and an empty Selection object if
|
||||
// the selection is empty.
|
||||
func (s *Selection) Last() *Selection {
|
||||
return s.Eq(-1)
|
||||
}
|
||||
|
||||
// Eq reduces the set of matched elements to the one at the specified index.
|
||||
// If a negative index is given, it counts backwards starting at the end of the
|
||||
// set. It returns a new Selection object, and an empty Selection object if the
|
||||
// index is invalid.
|
||||
func (s *Selection) Eq(index int) *Selection {
|
||||
if index < 0 {
|
||||
index += len(s.Nodes)
|
||||
}
|
||||
|
||||
if index >= len(s.Nodes) || index < 0 {
|
||||
return newEmptySelection(s.document)
|
||||
}
|
||||
|
||||
return s.Slice(index, index+1)
|
||||
}
|
||||
|
||||
// Slice reduces the set of matched elements to a subset specified by a range
|
||||
// of indices. The start index is 0-based and indicates the index of the first
|
||||
// element to select. The end index is 0-based and indicates the index at which
|
||||
// the elements stop being selected (the end index is not selected).
|
||||
//
|
||||
// The indices may be negative, in which case they represent an offset from the
|
||||
// end of the selection.
|
||||
//
|
||||
// The special value ToEnd may be specified as end index, in which case all elements
|
||||
// until the end are selected. This works both for a positive and negative start
|
||||
// index.
|
||||
func (s *Selection) Slice(start, end int) *Selection {
|
||||
if start < 0 {
|
||||
start += len(s.Nodes)
|
||||
}
|
||||
if end == ToEnd {
|
||||
end = len(s.Nodes)
|
||||
} else if end < 0 {
|
||||
end += len(s.Nodes)
|
||||
}
|
||||
return pushStack(s, s.Nodes[start:end])
|
||||
}
|
||||
|
||||
// Get retrieves the underlying node at the specified index.
|
||||
// Get without parameter is not implemented, since the node array is available
|
||||
// on the Selection object.
|
||||
func (s *Selection) Get(index int) *html.Node {
|
||||
if index < 0 {
|
||||
index += len(s.Nodes) // Negative index gets from the end
|
||||
}
|
||||
return s.Nodes[index]
|
||||
}
|
||||
|
||||
// Index returns the position of the first element within the Selection object
|
||||
// relative to its sibling elements.
|
||||
func (s *Selection) Index() int {
|
||||
if len(s.Nodes) > 0 {
|
||||
return newSingleSelection(s.Nodes[0], s.document).PrevAll().Length()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexSelector returns the position of the first element within the
|
||||
// Selection object relative to the elements matched by the selector, or -1 if
|
||||
// not found.
|
||||
func (s *Selection) IndexSelector(selector string) int {
|
||||
if len(s.Nodes) > 0 {
|
||||
sel := s.document.Find(selector)
|
||||
return indexInSlice(sel.Nodes, s.Nodes[0])
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexMatcher returns the position of the first element within the
|
||||
// Selection object relative to the elements matched by the matcher, or -1 if
|
||||
// not found.
|
||||
func (s *Selection) IndexMatcher(m Matcher) int {
|
||||
if len(s.Nodes) > 0 {
|
||||
sel := s.document.FindMatcher(m)
|
||||
return indexInSlice(sel.Nodes, s.Nodes[0])
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexOfNode returns the position of the specified node within the Selection
|
||||
// object, or -1 if not found.
|
||||
func (s *Selection) IndexOfNode(node *html.Node) int {
|
||||
return indexInSlice(s.Nodes, node)
|
||||
}
|
||||
|
||||
// IndexOfSelection returns the position of the first node in the specified
|
||||
// Selection object within this Selection object, or -1 if not found.
|
||||
func (s *Selection) IndexOfSelection(sel *Selection) int {
|
||||
if sel != nil && len(sel.Nodes) > 0 {
|
||||
return indexInSlice(s.Nodes, sel.Nodes[0])
|
||||
}
|
||||
return -1
|
||||
}
|
123
vendor/github.com/PuerkitoBio/goquery/doc.go
generated
vendored
Normal file
123
vendor/github.com/PuerkitoBio/goquery/doc.go
generated
vendored
Normal file
@ -0,0 +1,123 @@
|
||||
// Copyright (c) 2012-2016, Martin Angers & Contributors
|
||||
// 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 the author 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 HOLDER 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.
|
||||
|
||||
/*
|
||||
Package goquery implements features similar to jQuery, including the chainable
|
||||
syntax, to manipulate and query an HTML document.
|
||||
|
||||
It brings a syntax and a set of features similar to jQuery to the Go language.
|
||||
It is based on Go's net/html package and the CSS Selector library cascadia.
|
||||
Since the net/html parser returns nodes, and not a full-featured DOM
|
||||
tree, jQuery's stateful manipulation functions (like height(), css(), detach())
|
||||
have been left off.
|
||||
|
||||
Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is
|
||||
the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML.
|
||||
See the repository's wiki for various options on how to do this.
|
||||
|
||||
Syntax-wise, it is as close as possible to jQuery, with the same method names when
|
||||
possible, and that warm and fuzzy chainable interface. jQuery being the
|
||||
ultra-popular library that it is, writing a similar HTML-manipulating
|
||||
library was better to follow its API than to start anew (in the same spirit as
|
||||
Go's fmt package), even though some of its methods are less than intuitive (looking
|
||||
at you, index()...).
|
||||
|
||||
It is hosted on GitHub, along with additional documentation in the README.md
|
||||
file: https://github.com/puerkitobio/goquery
|
||||
|
||||
Please note that because of the net/html dependency, goquery requires Go1.1+.
|
||||
|
||||
The various methods are split into files based on the category of behavior.
|
||||
The three dots (...) indicate that various "overloads" are available.
|
||||
|
||||
* array.go : array-like positional manipulation of the selection.
|
||||
- Eq()
|
||||
- First()
|
||||
- Get()
|
||||
- Index...()
|
||||
- Last()
|
||||
- Slice()
|
||||
|
||||
* expand.go : methods that expand or augment the selection's set.
|
||||
- Add...()
|
||||
- AndSelf()
|
||||
- Union(), which is an alias for AddSelection()
|
||||
|
||||
* filter.go : filtering methods, that reduce the selection's set.
|
||||
- End()
|
||||
- Filter...()
|
||||
- Has...()
|
||||
- Intersection(), which is an alias of FilterSelection()
|
||||
- Not...()
|
||||
|
||||
* iteration.go : methods to loop over the selection's nodes.
|
||||
- Each()
|
||||
- EachWithBreak()
|
||||
- Map()
|
||||
|
||||
* manipulation.go : methods for modifying the document
|
||||
- After...()
|
||||
- Append...()
|
||||
- Before...()
|
||||
- Clone()
|
||||
- Empty()
|
||||
- Prepend...()
|
||||
- Remove...()
|
||||
- ReplaceWith...()
|
||||
- Unwrap()
|
||||
- Wrap...()
|
||||
- WrapAll...()
|
||||
- WrapInner...()
|
||||
|
||||
* property.go : methods that inspect and get the node's properties values.
|
||||
- Attr*(), RemoveAttr(), SetAttr()
|
||||
- AddClass(), HasClass(), RemoveClass(), ToggleClass()
|
||||
- Html()
|
||||
- Length()
|
||||
- Size(), which is an alias for Length()
|
||||
- Text()
|
||||
|
||||
* query.go : methods that query, or reflect, a node's identity.
|
||||
- Contains()
|
||||
- Is...()
|
||||
|
||||
* traversal.go : methods to traverse the HTML document tree.
|
||||
- Children...()
|
||||
- Contents()
|
||||
- Find...()
|
||||
- Next...()
|
||||
- Parent[s]...()
|
||||
- Prev...()
|
||||
- Siblings...()
|
||||
|
||||
* type.go : definition of the types exposed by goquery.
|
||||
- Document
|
||||
- Selection
|
||||
- Matcher
|
||||
|
||||
* utilities.go : definition of helper functions (and not methods on a *Selection)
|
||||
that are not part of jQuery, but are useful to goquery.
|
||||
- NodeName
|
||||
- OuterHtml
|
||||
*/
|
||||
package goquery
|
70
vendor/github.com/PuerkitoBio/goquery/expand.go
generated
vendored
Normal file
70
vendor/github.com/PuerkitoBio/goquery/expand.go
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
package goquery
|
||||
|
||||
import "golang.org/x/net/html"
|
||||
|
||||
// Add adds the selector string's matching nodes to those in the current
|
||||
// selection and returns a new Selection object.
|
||||
// The selector string is run in the context of the document of the current
|
||||
// Selection object.
|
||||
func (s *Selection) Add(selector string) *Selection {
|
||||
return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, compileMatcher(selector))...)
|
||||
}
|
||||
|
||||
// AddMatcher adds the matcher's matching nodes to those in the current
|
||||
// selection and returns a new Selection object.
|
||||
// The matcher is run in the context of the document of the current
|
||||
// Selection object.
|
||||
func (s *Selection) AddMatcher(m Matcher) *Selection {
|
||||
return s.AddNodes(findWithMatcher([]*html.Node{s.document.rootNode}, m)...)
|
||||
}
|
||||
|
||||
// AddSelection adds the specified Selection object's nodes to those in the
|
||||
// current selection and returns a new Selection object.
|
||||
func (s *Selection) AddSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.AddNodes()
|
||||
}
|
||||
return s.AddNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// Union is an alias for AddSelection.
|
||||
func (s *Selection) Union(sel *Selection) *Selection {
|
||||
return s.AddSelection(sel)
|
||||
}
|
||||
|
||||
// AddNodes adds the specified nodes to those in the
|
||||
// current selection and returns a new Selection object.
|
||||
func (s *Selection) AddNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, appendWithoutDuplicates(s.Nodes, nodes, nil))
|
||||
}
|
||||
|
||||
// AndSelf adds the previous set of elements on the stack to the current set.
|
||||
// It returns a new Selection object containing the current Selection combined
|
||||
// with the previous one.
|
||||
// Deprecated: This function has been deprecated and is now an alias for AddBack().
|
||||
func (s *Selection) AndSelf() *Selection {
|
||||
return s.AddBack()
|
||||
}
|
||||
|
||||
// AddBack adds the previous set of elements on the stack to the current set.
|
||||
// It returns a new Selection object containing the current Selection combined
|
||||
// with the previous one.
|
||||
func (s *Selection) AddBack() *Selection {
|
||||
return s.AddSelection(s.prevSel)
|
||||
}
|
||||
|
||||
// AddBackFiltered reduces the previous set of elements on the stack to those that
|
||||
// match the selector string, and adds them to the current set.
|
||||
// It returns a new Selection object containing the current Selection combined
|
||||
// with the filtered previous one
|
||||
func (s *Selection) AddBackFiltered(selector string) *Selection {
|
||||
return s.AddSelection(s.prevSel.Filter(selector))
|
||||
}
|
||||
|
||||
// AddBackMatcher reduces the previous set of elements on the stack to those that match
|
||||
// the mateher, and adds them to the curernt set.
|
||||
// It returns a new Selection object containing the current Selection combined
|
||||
// with the filtered previous one
|
||||
func (s *Selection) AddBackMatcher(m Matcher) *Selection {
|
||||
return s.AddSelection(s.prevSel.FilterMatcher(m))
|
||||
}
|
163
vendor/github.com/PuerkitoBio/goquery/filter.go
generated
vendored
Normal file
163
vendor/github.com/PuerkitoBio/goquery/filter.go
generated
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
package goquery
|
||||
|
||||
import "golang.org/x/net/html"
|
||||
|
||||
// Filter reduces the set of matched elements to those that match the selector string.
|
||||
// It returns a new Selection object for this subset of matching elements.
|
||||
func (s *Selection) Filter(selector string) *Selection {
|
||||
return s.FilterMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// FilterMatcher reduces the set of matched elements to those that match
|
||||
// the given matcher. It returns a new Selection object for this subset
|
||||
// of matching elements.
|
||||
func (s *Selection) FilterMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, winnow(s, m, true))
|
||||
}
|
||||
|
||||
// Not removes elements from the Selection that match the selector string.
|
||||
// It returns a new Selection object with the matching elements removed.
|
||||
func (s *Selection) Not(selector string) *Selection {
|
||||
return s.NotMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// NotMatcher removes elements from the Selection that match the given matcher.
|
||||
// It returns a new Selection object with the matching elements removed.
|
||||
func (s *Selection) NotMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, winnow(s, m, false))
|
||||
}
|
||||
|
||||
// FilterFunction reduces the set of matched elements to those that pass the function's test.
|
||||
// It returns a new Selection object for this subset of elements.
|
||||
func (s *Selection) FilterFunction(f func(int, *Selection) bool) *Selection {
|
||||
return pushStack(s, winnowFunction(s, f, true))
|
||||
}
|
||||
|
||||
// NotFunction removes elements from the Selection that pass the function's test.
|
||||
// It returns a new Selection object with the matching elements removed.
|
||||
func (s *Selection) NotFunction(f func(int, *Selection) bool) *Selection {
|
||||
return pushStack(s, winnowFunction(s, f, false))
|
||||
}
|
||||
|
||||
// FilterNodes reduces the set of matched elements to those that match the specified nodes.
|
||||
// It returns a new Selection object for this subset of elements.
|
||||
func (s *Selection) FilterNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, winnowNodes(s, nodes, true))
|
||||
}
|
||||
|
||||
// NotNodes removes elements from the Selection that match the specified nodes.
|
||||
// It returns a new Selection object with the matching elements removed.
|
||||
func (s *Selection) NotNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, winnowNodes(s, nodes, false))
|
||||
}
|
||||
|
||||
// FilterSelection reduces the set of matched elements to those that match a
|
||||
// node in the specified Selection object.
|
||||
// It returns a new Selection object for this subset of elements.
|
||||
func (s *Selection) FilterSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return pushStack(s, winnowNodes(s, nil, true))
|
||||
}
|
||||
return pushStack(s, winnowNodes(s, sel.Nodes, true))
|
||||
}
|
||||
|
||||
// NotSelection removes elements from the Selection that match a node in the specified
|
||||
// Selection object. It returns a new Selection object with the matching elements removed.
|
||||
func (s *Selection) NotSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return pushStack(s, winnowNodes(s, nil, false))
|
||||
}
|
||||
return pushStack(s, winnowNodes(s, sel.Nodes, false))
|
||||
}
|
||||
|
||||
// Intersection is an alias for FilterSelection.
|
||||
func (s *Selection) Intersection(sel *Selection) *Selection {
|
||||
return s.FilterSelection(sel)
|
||||
}
|
||||
|
||||
// Has reduces the set of matched elements to those that have a descendant
|
||||
// that matches the selector.
|
||||
// It returns a new Selection object with the matching elements.
|
||||
func (s *Selection) Has(selector string) *Selection {
|
||||
return s.HasSelection(s.document.Find(selector))
|
||||
}
|
||||
|
||||
// HasMatcher reduces the set of matched elements to those that have a descendant
|
||||
// that matches the matcher.
|
||||
// It returns a new Selection object with the matching elements.
|
||||
func (s *Selection) HasMatcher(m Matcher) *Selection {
|
||||
return s.HasSelection(s.document.FindMatcher(m))
|
||||
}
|
||||
|
||||
// HasNodes reduces the set of matched elements to those that have a
|
||||
// descendant that matches one of the nodes.
|
||||
// It returns a new Selection object with the matching elements.
|
||||
func (s *Selection) HasNodes(nodes ...*html.Node) *Selection {
|
||||
return s.FilterFunction(func(_ int, sel *Selection) bool {
|
||||
// Add all nodes that contain one of the specified nodes
|
||||
for _, n := range nodes {
|
||||
if sel.Contains(n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
// HasSelection reduces the set of matched elements to those that have a
|
||||
// descendant that matches one of the nodes of the specified Selection object.
|
||||
// It returns a new Selection object with the matching elements.
|
||||
func (s *Selection) HasSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.HasNodes()
|
||||
}
|
||||
return s.HasNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// End ends the most recent filtering operation in the current chain and
|
||||
// returns the set of matched elements to its previous state.
|
||||
func (s *Selection) End() *Selection {
|
||||
if s.prevSel != nil {
|
||||
return s.prevSel
|
||||
}
|
||||
return newEmptySelection(s.document)
|
||||
}
|
||||
|
||||
// Filter based on the matcher, and the indicator to keep (Filter) or
|
||||
// to get rid of (Not) the matching elements.
|
||||
func winnow(sel *Selection, m Matcher, keep bool) []*html.Node {
|
||||
// Optimize if keep is requested
|
||||
if keep {
|
||||
return m.Filter(sel.Nodes)
|
||||
}
|
||||
// Use grep
|
||||
return grep(sel, func(i int, s *Selection) bool {
|
||||
return !m.Match(s.Get(0))
|
||||
})
|
||||
}
|
||||
|
||||
// Filter based on an array of nodes, and the indicator to keep (Filter) or
|
||||
// to get rid of (Not) the matching elements.
|
||||
func winnowNodes(sel *Selection, nodes []*html.Node, keep bool) []*html.Node {
|
||||
if len(nodes)+len(sel.Nodes) < minNodesForSet {
|
||||
return grep(sel, func(i int, s *Selection) bool {
|
||||
return isInSlice(nodes, s.Get(0)) == keep
|
||||
})
|
||||
}
|
||||
|
||||
set := make(map[*html.Node]bool)
|
||||
for _, n := range nodes {
|
||||
set[n] = true
|
||||
}
|
||||
return grep(sel, func(i int, s *Selection) bool {
|
||||
return set[s.Get(0)] == keep
|
||||
})
|
||||
}
|
||||
|
||||
// Filter based on a function test, and the indicator to keep (Filter) or
|
||||
// to get rid of (Not) the matching elements.
|
||||
func winnowFunction(sel *Selection, f func(int, *Selection) bool, keep bool) []*html.Node {
|
||||
return grep(sel, func(i int, s *Selection) bool {
|
||||
return f(i, s) == keep
|
||||
})
|
||||
}
|
6
vendor/github.com/PuerkitoBio/goquery/go.mod
generated
vendored
Normal file
6
vendor/github.com/PuerkitoBio/goquery/go.mod
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
module github.com/PuerkitoBio/goquery
|
||||
|
||||
require (
|
||||
github.com/andybalholm/cascadia v1.0.0
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a
|
||||
)
|
5
vendor/github.com/PuerkitoBio/goquery/go.sum
generated
vendored
Normal file
5
vendor/github.com/PuerkitoBio/goquery/go.sum
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
39
vendor/github.com/PuerkitoBio/goquery/iteration.go
generated
vendored
Normal file
39
vendor/github.com/PuerkitoBio/goquery/iteration.go
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
package goquery
|
||||
|
||||
// Each iterates over a Selection object, executing a function for each
|
||||
// matched element. It returns the current Selection object. The function
|
||||
// f is called for each element in the selection with the index of the
|
||||
// element in that selection starting at 0, and a *Selection that contains
|
||||
// only that element.
|
||||
func (s *Selection) Each(f func(int, *Selection)) *Selection {
|
||||
for i, n := range s.Nodes {
|
||||
f(i, newSingleSelection(n, s.document))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// EachWithBreak iterates over a Selection object, executing a function for each
|
||||
// matched element. It is identical to Each except that it is possible to break
|
||||
// out of the loop by returning false in the callback function. It returns the
|
||||
// current Selection object.
|
||||
func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection {
|
||||
for i, n := range s.Nodes {
|
||||
if !f(i, newSingleSelection(n, s.document)) {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Map passes each element in the current matched set through a function,
|
||||
// producing a slice of string holding the returned values. The function
|
||||
// f is called for each element in the selection with the index of the
|
||||
// element in that selection starting at 0, and a *Selection that contains
|
||||
// only that element.
|
||||
func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
|
||||
for i, n := range s.Nodes {
|
||||
result = append(result, f(i, newSingleSelection(n, s.document)))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
574
vendor/github.com/PuerkitoBio/goquery/manipulation.go
generated
vendored
Normal file
574
vendor/github.com/PuerkitoBio/goquery/manipulation.go
generated
vendored
Normal file
@ -0,0 +1,574 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// After applies the selector from the root document and inserts the matched elements
|
||||
// after the elements in the set of matched elements.
|
||||
//
|
||||
// If one of the matched elements in the selection is not currently in the
|
||||
// document, it's impossible to insert nodes after it, so it will be ignored.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) After(selector string) *Selection {
|
||||
return s.AfterMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// AfterMatcher applies the matcher from the root document and inserts the matched elements
|
||||
// after the elements in the set of matched elements.
|
||||
//
|
||||
// If one of the matched elements in the selection is not currently in the
|
||||
// document, it's impossible to insert nodes after it, so it will be ignored.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AfterMatcher(m Matcher) *Selection {
|
||||
return s.AfterNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// AfterSelection inserts the elements in the selection after each element in the set of matched
|
||||
// elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AfterSelection(sel *Selection) *Selection {
|
||||
return s.AfterNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// AfterHtml parses the html and inserts it after the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AfterHtml(html string) *Selection {
|
||||
return s.AfterNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// AfterNodes inserts the nodes after each element in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AfterNodes(ns ...*html.Node) *Selection {
|
||||
return s.manipulateNodes(ns, true, func(sn *html.Node, n *html.Node) {
|
||||
if sn.Parent != nil {
|
||||
sn.Parent.InsertBefore(n, sn.NextSibling)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Append appends the elements specified by the selector to the end of each element
|
||||
// in the set of matched elements, following those rules:
|
||||
//
|
||||
// 1) The selector is applied to the root document.
|
||||
//
|
||||
// 2) Elements that are part of the document will be moved to the new location.
|
||||
//
|
||||
// 3) If there are multiple locations to append to, cloned nodes will be
|
||||
// appended to all target locations except the last one, which will be moved
|
||||
// as noted in (2).
|
||||
func (s *Selection) Append(selector string) *Selection {
|
||||
return s.AppendMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// AppendMatcher appends the elements specified by the matcher to the end of each element
|
||||
// in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AppendMatcher(m Matcher) *Selection {
|
||||
return s.AppendNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// AppendSelection appends the elements in the selection to the end of each element
|
||||
// in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AppendSelection(sel *Selection) *Selection {
|
||||
return s.AppendNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// AppendHtml parses the html and appends it to the set of matched elements.
|
||||
func (s *Selection) AppendHtml(html string) *Selection {
|
||||
return s.AppendNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// AppendNodes appends the specified nodes to each node in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) AppendNodes(ns ...*html.Node) *Selection {
|
||||
return s.manipulateNodes(ns, false, func(sn *html.Node, n *html.Node) {
|
||||
sn.AppendChild(n)
|
||||
})
|
||||
}
|
||||
|
||||
// Before inserts the matched elements before each element in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) Before(selector string) *Selection {
|
||||
return s.BeforeMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// BeforeMatcher inserts the matched elements before each element in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) BeforeMatcher(m Matcher) *Selection {
|
||||
return s.BeforeNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// BeforeSelection inserts the elements in the selection before each element in the set of matched
|
||||
// elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) BeforeSelection(sel *Selection) *Selection {
|
||||
return s.BeforeNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// BeforeHtml parses the html and inserts it before the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) BeforeHtml(html string) *Selection {
|
||||
return s.BeforeNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// BeforeNodes inserts the nodes before each element in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) BeforeNodes(ns ...*html.Node) *Selection {
|
||||
return s.manipulateNodes(ns, false, func(sn *html.Node, n *html.Node) {
|
||||
if sn.Parent != nil {
|
||||
sn.Parent.InsertBefore(n, sn)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Clone creates a deep copy of the set of matched nodes. The new nodes will not be
|
||||
// attached to the document.
|
||||
func (s *Selection) Clone() *Selection {
|
||||
ns := newEmptySelection(s.document)
|
||||
ns.Nodes = cloneNodes(s.Nodes)
|
||||
return ns
|
||||
}
|
||||
|
||||
// Empty removes all children nodes from the set of matched elements.
|
||||
// It returns the children nodes in a new Selection.
|
||||
func (s *Selection) Empty() *Selection {
|
||||
var nodes []*html.Node
|
||||
|
||||
for _, n := range s.Nodes {
|
||||
for c := n.FirstChild; c != nil; c = n.FirstChild {
|
||||
n.RemoveChild(c)
|
||||
nodes = append(nodes, c)
|
||||
}
|
||||
}
|
||||
|
||||
return pushStack(s, nodes)
|
||||
}
|
||||
|
||||
// Prepend prepends the elements specified by the selector to each element in
|
||||
// the set of matched elements, following the same rules as Append.
|
||||
func (s *Selection) Prepend(selector string) *Selection {
|
||||
return s.PrependMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// PrependMatcher prepends the elements specified by the matcher to each
|
||||
// element in the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) PrependMatcher(m Matcher) *Selection {
|
||||
return s.PrependNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// PrependSelection prepends the elements in the selection to each element in
|
||||
// the set of matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) PrependSelection(sel *Selection) *Selection {
|
||||
return s.PrependNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// PrependHtml parses the html and prepends it to the set of matched elements.
|
||||
func (s *Selection) PrependHtml(html string) *Selection {
|
||||
return s.PrependNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// PrependNodes prepends the specified nodes to each node in the set of
|
||||
// matched elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) PrependNodes(ns ...*html.Node) *Selection {
|
||||
return s.manipulateNodes(ns, true, func(sn *html.Node, n *html.Node) {
|
||||
// sn.FirstChild may be nil, in which case this functions like
|
||||
// sn.AppendChild()
|
||||
sn.InsertBefore(n, sn.FirstChild)
|
||||
})
|
||||
}
|
||||
|
||||
// Remove removes the set of matched elements from the document.
|
||||
// It returns the same selection, now consisting of nodes not in the document.
|
||||
func (s *Selection) Remove() *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
if n.Parent != nil {
|
||||
n.Parent.RemoveChild(n)
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// RemoveFiltered removes the set of matched elements by selector.
|
||||
// It returns the Selection of removed nodes.
|
||||
func (s *Selection) RemoveFiltered(selector string) *Selection {
|
||||
return s.RemoveMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// RemoveMatcher removes the set of matched elements.
|
||||
// It returns the Selection of removed nodes.
|
||||
func (s *Selection) RemoveMatcher(m Matcher) *Selection {
|
||||
return s.FilterMatcher(m).Remove()
|
||||
}
|
||||
|
||||
// ReplaceWith replaces each element in the set of matched elements with the
|
||||
// nodes matched by the given selector.
|
||||
// It returns the removed elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) ReplaceWith(selector string) *Selection {
|
||||
return s.ReplaceWithMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// ReplaceWithMatcher replaces each element in the set of matched elements with
|
||||
// the nodes matched by the given Matcher.
|
||||
// It returns the removed elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) ReplaceWithMatcher(m Matcher) *Selection {
|
||||
return s.ReplaceWithNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// ReplaceWithSelection replaces each element in the set of matched elements with
|
||||
// the nodes from the given Selection.
|
||||
// It returns the removed elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) ReplaceWithSelection(sel *Selection) *Selection {
|
||||
return s.ReplaceWithNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// ReplaceWithHtml replaces each element in the set of matched elements with
|
||||
// the parsed HTML.
|
||||
// It returns the removed elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) ReplaceWithHtml(html string) *Selection {
|
||||
return s.ReplaceWithNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// ReplaceWithNodes replaces each element in the set of matched elements with
|
||||
// the given nodes.
|
||||
// It returns the removed elements.
|
||||
//
|
||||
// This follows the same rules as Selection.Append.
|
||||
func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection {
|
||||
s.AfterNodes(ns...)
|
||||
return s.Remove()
|
||||
}
|
||||
|
||||
// SetHtml sets the html content of each element in the selection to
|
||||
// specified html string.
|
||||
func (s *Selection) SetHtml(html string) *Selection {
|
||||
return setHtmlNodes(s, parseHtml(html)...)
|
||||
}
|
||||
|
||||
// SetText sets the content of each element in the selection to specified content.
|
||||
// The provided text string is escaped.
|
||||
func (s *Selection) SetText(text string) *Selection {
|
||||
return s.SetHtml(html.EscapeString(text))
|
||||
}
|
||||
|
||||
// Unwrap removes the parents of the set of matched elements, leaving the matched
|
||||
// elements (and their siblings, if any) in their place.
|
||||
// It returns the original selection.
|
||||
func (s *Selection) Unwrap() *Selection {
|
||||
s.Parent().Each(func(i int, ss *Selection) {
|
||||
// For some reason, jquery allows unwrap to remove the <head> element, so
|
||||
// allowing it here too. Same for <html>. Why it allows those elements to
|
||||
// be unwrapped while not allowing body is a mystery to me.
|
||||
if ss.Nodes[0].Data != "body" {
|
||||
ss.ReplaceWithSelection(ss.Contents())
|
||||
}
|
||||
})
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Wrap wraps each element in the set of matched elements inside the first
|
||||
// element matched by the given selector. The matched child is cloned before
|
||||
// being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) Wrap(selector string) *Selection {
|
||||
return s.WrapMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// WrapMatcher wraps each element in the set of matched elements inside the
|
||||
// first element matched by the given matcher. The matched child is cloned
|
||||
// before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapMatcher(m Matcher) *Selection {
|
||||
return s.wrapNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// WrapSelection wraps each element in the set of matched elements inside the
|
||||
// first element in the given Selection. The element is cloned before being
|
||||
// inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapSelection(sel *Selection) *Selection {
|
||||
return s.wrapNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// WrapHtml wraps each element in the set of matched elements inside the inner-
|
||||
// most child of the given HTML.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapHtml(html string) *Selection {
|
||||
return s.wrapNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// WrapNode wraps each element in the set of matched elements inside the inner-
|
||||
// most child of the given node. The given node is copied before being inserted
|
||||
// into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapNode(n *html.Node) *Selection {
|
||||
return s.wrapNodes(n)
|
||||
}
|
||||
|
||||
func (s *Selection) wrapNodes(ns ...*html.Node) *Selection {
|
||||
s.Each(func(i int, ss *Selection) {
|
||||
ss.wrapAllNodes(ns...)
|
||||
})
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// WrapAll wraps a single HTML structure, matched by the given selector, around
|
||||
// all elements in the set of matched elements. The matched child is cloned
|
||||
// before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapAll(selector string) *Selection {
|
||||
return s.WrapAllMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// WrapAllMatcher wraps a single HTML structure, matched by the given Matcher,
|
||||
// around all elements in the set of matched elements. The matched child is
|
||||
// cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapAllMatcher(m Matcher) *Selection {
|
||||
return s.wrapAllNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// WrapAllSelection wraps a single HTML structure, the first node of the given
|
||||
// Selection, around all elements in the set of matched elements. The matched
|
||||
// child is cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapAllSelection(sel *Selection) *Selection {
|
||||
return s.wrapAllNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// WrapAllHtml wraps the given HTML structure around all elements in the set of
|
||||
// matched elements. The matched child is cloned before being inserted into the
|
||||
// document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapAllHtml(html string) *Selection {
|
||||
return s.wrapAllNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
func (s *Selection) wrapAllNodes(ns ...*html.Node) *Selection {
|
||||
if len(ns) > 0 {
|
||||
return s.WrapAllNode(ns[0])
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// WrapAllNode wraps the given node around the first element in the Selection,
|
||||
// making all other nodes in the Selection children of the given node. The node
|
||||
// is cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapAllNode(n *html.Node) *Selection {
|
||||
if s.Size() == 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
wrap := cloneNode(n)
|
||||
|
||||
first := s.Nodes[0]
|
||||
if first.Parent != nil {
|
||||
first.Parent.InsertBefore(wrap, first)
|
||||
first.Parent.RemoveChild(first)
|
||||
}
|
||||
|
||||
for c := getFirstChildEl(wrap); c != nil; c = getFirstChildEl(wrap) {
|
||||
wrap = c
|
||||
}
|
||||
|
||||
newSingleSelection(wrap, s.document).AppendSelection(s)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// WrapInner wraps an HTML structure, matched by the given selector, around the
|
||||
// content of element in the set of matched elements. The matched child is
|
||||
// cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapInner(selector string) *Selection {
|
||||
return s.WrapInnerMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// WrapInnerMatcher wraps an HTML structure, matched by the given selector,
|
||||
// around the content of element in the set of matched elements. The matched
|
||||
// child is cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapInnerMatcher(m Matcher) *Selection {
|
||||
return s.wrapInnerNodes(m.MatchAll(s.document.rootNode)...)
|
||||
}
|
||||
|
||||
// WrapInnerSelection wraps an HTML structure, matched by the given selector,
|
||||
// around the content of element in the set of matched elements. The matched
|
||||
// child is cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapInnerSelection(sel *Selection) *Selection {
|
||||
return s.wrapInnerNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// WrapInnerHtml wraps an HTML structure, matched by the given selector, around
|
||||
// the content of element in the set of matched elements. The matched child is
|
||||
// cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapInnerHtml(html string) *Selection {
|
||||
return s.wrapInnerNodes(parseHtml(html)...)
|
||||
}
|
||||
|
||||
// WrapInnerNode wraps an HTML structure, matched by the given selector, around
|
||||
// the content of element in the set of matched elements. The matched child is
|
||||
// cloned before being inserted into the document.
|
||||
//
|
||||
// It returns the original set of elements.
|
||||
func (s *Selection) WrapInnerNode(n *html.Node) *Selection {
|
||||
return s.wrapInnerNodes(n)
|
||||
}
|
||||
|
||||
func (s *Selection) wrapInnerNodes(ns ...*html.Node) *Selection {
|
||||
if len(ns) == 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
s.Each(func(i int, s *Selection) {
|
||||
contents := s.Contents()
|
||||
|
||||
if contents.Size() > 0 {
|
||||
contents.wrapAllNodes(ns...)
|
||||
} else {
|
||||
s.AppendNodes(cloneNode(ns[0]))
|
||||
}
|
||||
})
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func parseHtml(h string) []*html.Node {
|
||||
// Errors are only returned when the io.Reader returns any error besides
|
||||
// EOF, but strings.Reader never will
|
||||
nodes, err := html.ParseFragment(strings.NewReader(h), &html.Node{Type: html.ElementNode})
|
||||
if err != nil {
|
||||
panic("goquery: failed to parse HTML: " + err.Error())
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
func setHtmlNodes(s *Selection, ns ...*html.Node) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
for c := n.FirstChild; c != nil; c = n.FirstChild {
|
||||
n.RemoveChild(c)
|
||||
}
|
||||
for _, c := range ns {
|
||||
n.AppendChild(cloneNode(c))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Get the first child that is an ElementNode
|
||||
func getFirstChildEl(n *html.Node) *html.Node {
|
||||
c := n.FirstChild
|
||||
for c != nil && c.Type != html.ElementNode {
|
||||
c = c.NextSibling
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// Deep copy a slice of nodes.
|
||||
func cloneNodes(ns []*html.Node) []*html.Node {
|
||||
cns := make([]*html.Node, 0, len(ns))
|
||||
|
||||
for _, n := range ns {
|
||||
cns = append(cns, cloneNode(n))
|
||||
}
|
||||
|
||||
return cns
|
||||
}
|
||||
|
||||
// Deep copy a node. The new node has clones of all the original node's
|
||||
// children but none of its parents or siblings.
|
||||
func cloneNode(n *html.Node) *html.Node {
|
||||
nn := &html.Node{
|
||||
Type: n.Type,
|
||||
DataAtom: n.DataAtom,
|
||||
Data: n.Data,
|
||||
Attr: make([]html.Attribute, len(n.Attr)),
|
||||
}
|
||||
|
||||
copy(nn.Attr, n.Attr)
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
nn.AppendChild(cloneNode(c))
|
||||
}
|
||||
|
||||
return nn
|
||||
}
|
||||
|
||||
func (s *Selection) manipulateNodes(ns []*html.Node, reverse bool,
|
||||
f func(sn *html.Node, n *html.Node)) *Selection {
|
||||
|
||||
lasti := s.Size() - 1
|
||||
|
||||
// net.Html doesn't provide document fragments for insertion, so to get
|
||||
// things in the correct order with After() and Prepend(), the callback
|
||||
// needs to be called on the reverse of the nodes.
|
||||
if reverse {
|
||||
for i, j := 0, len(ns)-1; i < j; i, j = i+1, j-1 {
|
||||
ns[i], ns[j] = ns[j], ns[i]
|
||||
}
|
||||
}
|
||||
|
||||
for i, sn := range s.Nodes {
|
||||
for _, n := range ns {
|
||||
if i != lasti {
|
||||
f(sn, cloneNode(n))
|
||||
} else {
|
||||
if n.Parent != nil {
|
||||
n.Parent.RemoveChild(n)
|
||||
}
|
||||
f(sn, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
275
vendor/github.com/PuerkitoBio/goquery/property.go
generated
vendored
Normal file
275
vendor/github.com/PuerkitoBio/goquery/property.go
generated
vendored
Normal file
@ -0,0 +1,275 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
var rxClassTrim = regexp.MustCompile("[\t\r\n]")
|
||||
|
||||
// Attr gets the specified attribute's value for the first element in the
|
||||
// Selection. To get the value for each element individually, use a looping
|
||||
// construct such as Each or Map method.
|
||||
func (s *Selection) Attr(attrName string) (val string, exists bool) {
|
||||
if len(s.Nodes) == 0 {
|
||||
return
|
||||
}
|
||||
return getAttributeValue(attrName, s.Nodes[0])
|
||||
}
|
||||
|
||||
// AttrOr works like Attr but returns default value if attribute is not present.
|
||||
func (s *Selection) AttrOr(attrName, defaultValue string) string {
|
||||
if len(s.Nodes) == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
val, exists := getAttributeValue(attrName, s.Nodes[0])
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// RemoveAttr removes the named attribute from each element in the set of matched elements.
|
||||
func (s *Selection) RemoveAttr(attrName string) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
removeAttr(n, attrName)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// SetAttr sets the given attribute on each element in the set of matched elements.
|
||||
func (s *Selection) SetAttr(attrName, val string) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
attr := getAttributePtr(attrName, n)
|
||||
if attr == nil {
|
||||
n.Attr = append(n.Attr, html.Attribute{Key: attrName, Val: val})
|
||||
} else {
|
||||
attr.Val = val
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Text gets the combined text contents of each element in the set of matched
|
||||
// elements, including their descendants.
|
||||
func (s *Selection) Text() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
// Slightly optimized vs calling Each: no single selection object created
|
||||
var f func(*html.Node)
|
||||
f = func(n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
// Keep newlines and spaces, like jQuery
|
||||
buf.WriteString(n.Data)
|
||||
}
|
||||
if n.FirstChild != nil {
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
f(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, n := range s.Nodes {
|
||||
f(n)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Size is an alias for Length.
|
||||
func (s *Selection) Size() int {
|
||||
return s.Length()
|
||||
}
|
||||
|
||||
// Length returns the number of elements in the Selection object.
|
||||
func (s *Selection) Length() int {
|
||||
return len(s.Nodes)
|
||||
}
|
||||
|
||||
// Html gets the HTML contents of the first element in the set of matched
|
||||
// elements. It includes text and comment nodes.
|
||||
func (s *Selection) Html() (ret string, e error) {
|
||||
// Since there is no .innerHtml, the HTML content must be re-created from
|
||||
// the nodes using html.Render.
|
||||
var buf bytes.Buffer
|
||||
|
||||
if len(s.Nodes) > 0 {
|
||||
for c := s.Nodes[0].FirstChild; c != nil; c = c.NextSibling {
|
||||
e = html.Render(&buf, c)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
ret = buf.String()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AddClass adds the given class(es) to each element in the set of matched elements.
|
||||
// Multiple class names can be specified, separated by a space or via multiple arguments.
|
||||
func (s *Selection) AddClass(class ...string) *Selection {
|
||||
classStr := strings.TrimSpace(strings.Join(class, " "))
|
||||
|
||||
if classStr == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
tcls := getClassesSlice(classStr)
|
||||
for _, n := range s.Nodes {
|
||||
curClasses, attr := getClassesAndAttr(n, true)
|
||||
for _, newClass := range tcls {
|
||||
if !strings.Contains(curClasses, " "+newClass+" ") {
|
||||
curClasses += newClass + " "
|
||||
}
|
||||
}
|
||||
|
||||
setClasses(n, attr, curClasses)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// HasClass determines whether any of the matched elements are assigned the
|
||||
// given class.
|
||||
func (s *Selection) HasClass(class string) bool {
|
||||
class = " " + class + " "
|
||||
for _, n := range s.Nodes {
|
||||
classes, _ := getClassesAndAttr(n, false)
|
||||
if strings.Contains(classes, class) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveClass removes the given class(es) from each element in the set of matched elements.
|
||||
// Multiple class names can be specified, separated by a space or via multiple arguments.
|
||||
// If no class name is provided, all classes are removed.
|
||||
func (s *Selection) RemoveClass(class ...string) *Selection {
|
||||
var rclasses []string
|
||||
|
||||
classStr := strings.TrimSpace(strings.Join(class, " "))
|
||||
remove := classStr == ""
|
||||
|
||||
if !remove {
|
||||
rclasses = getClassesSlice(classStr)
|
||||
}
|
||||
|
||||
for _, n := range s.Nodes {
|
||||
if remove {
|
||||
removeAttr(n, "class")
|
||||
} else {
|
||||
classes, attr := getClassesAndAttr(n, true)
|
||||
for _, rcl := range rclasses {
|
||||
classes = strings.Replace(classes, " "+rcl+" ", " ", -1)
|
||||
}
|
||||
|
||||
setClasses(n, attr, classes)
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// ToggleClass adds or removes the given class(es) for each element in the set of matched elements.
|
||||
// Multiple class names can be specified, separated by a space or via multiple arguments.
|
||||
func (s *Selection) ToggleClass(class ...string) *Selection {
|
||||
classStr := strings.TrimSpace(strings.Join(class, " "))
|
||||
|
||||
if classStr == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
tcls := getClassesSlice(classStr)
|
||||
|
||||
for _, n := range s.Nodes {
|
||||
classes, attr := getClassesAndAttr(n, true)
|
||||
for _, tcl := range tcls {
|
||||
if strings.Contains(classes, " "+tcl+" ") {
|
||||
classes = strings.Replace(classes, " "+tcl+" ", " ", -1)
|
||||
} else {
|
||||
classes += tcl + " "
|
||||
}
|
||||
}
|
||||
|
||||
setClasses(n, attr, classes)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func getAttributePtr(attrName string, n *html.Node) *html.Attribute {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i, a := range n.Attr {
|
||||
if a.Key == attrName {
|
||||
return &n.Attr[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Private function to get the specified attribute's value from a node.
|
||||
func getAttributeValue(attrName string, n *html.Node) (val string, exists bool) {
|
||||
if a := getAttributePtr(attrName, n); a != nil {
|
||||
val = a.Val
|
||||
exists = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Get and normalize the "class" attribute from the node.
|
||||
func getClassesAndAttr(n *html.Node, create bool) (classes string, attr *html.Attribute) {
|
||||
// Applies only to element nodes
|
||||
if n.Type == html.ElementNode {
|
||||
attr = getAttributePtr("class", n)
|
||||
if attr == nil && create {
|
||||
n.Attr = append(n.Attr, html.Attribute{
|
||||
Key: "class",
|
||||
Val: "",
|
||||
})
|
||||
attr = &n.Attr[len(n.Attr)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if attr == nil {
|
||||
classes = " "
|
||||
} else {
|
||||
classes = rxClassTrim.ReplaceAllString(" "+attr.Val+" ", " ")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getClassesSlice(classes string) []string {
|
||||
return strings.Split(rxClassTrim.ReplaceAllString(" "+classes+" ", " "), " ")
|
||||
}
|
||||
|
||||
func removeAttr(n *html.Node, attrName string) {
|
||||
for i, a := range n.Attr {
|
||||
if a.Key == attrName {
|
||||
n.Attr[i], n.Attr[len(n.Attr)-1], n.Attr =
|
||||
n.Attr[len(n.Attr)-1], html.Attribute{}, n.Attr[:len(n.Attr)-1]
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setClasses(n *html.Node, attr *html.Attribute, classes string) {
|
||||
classes = strings.TrimSpace(classes)
|
||||
if classes == "" {
|
||||
removeAttr(n, "class")
|
||||
return
|
||||
}
|
||||
|
||||
attr.Val = classes
|
||||
}
|
49
vendor/github.com/PuerkitoBio/goquery/query.go
generated
vendored
Normal file
49
vendor/github.com/PuerkitoBio/goquery/query.go
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package goquery
|
||||
|
||||
import "golang.org/x/net/html"
|
||||
|
||||
// Is checks the current matched set of elements against a selector and
|
||||
// returns true if at least one of these elements matches.
|
||||
func (s *Selection) Is(selector string) bool {
|
||||
return s.IsMatcher(compileMatcher(selector))
|
||||
}
|
||||
|
||||
// IsMatcher checks the current matched set of elements against a matcher and
|
||||
// returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsMatcher(m Matcher) bool {
|
||||
if len(s.Nodes) > 0 {
|
||||
if len(s.Nodes) == 1 {
|
||||
return m.Match(s.Nodes[0])
|
||||
}
|
||||
return len(m.Filter(s.Nodes)) > 0
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsFunction checks the current matched set of elements against a predicate and
|
||||
// returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsFunction(f func(int, *Selection) bool) bool {
|
||||
return s.FilterFunction(f).Length() > 0
|
||||
}
|
||||
|
||||
// IsSelection checks the current matched set of elements against a Selection object
|
||||
// and returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsSelection(sel *Selection) bool {
|
||||
return s.FilterSelection(sel).Length() > 0
|
||||
}
|
||||
|
||||
// IsNodes checks the current matched set of elements against the specified nodes
|
||||
// and returns true if at least one of these elements matches.
|
||||
func (s *Selection) IsNodes(nodes ...*html.Node) bool {
|
||||
return s.FilterNodes(nodes...).Length() > 0
|
||||
}
|
||||
|
||||
// Contains returns true if the specified Node is within,
|
||||
// at any depth, one of the nodes in the Selection object.
|
||||
// It is NOT inclusive, to behave like jQuery's implementation, and
|
||||
// unlike Javascript's .contains, so if the contained
|
||||
// node is itself in the selection, it returns false.
|
||||
func (s *Selection) Contains(n *html.Node) bool {
|
||||
return sliceContains(s.Nodes, n)
|
||||
}
|
698
vendor/github.com/PuerkitoBio/goquery/traversal.go
generated
vendored
Normal file
698
vendor/github.com/PuerkitoBio/goquery/traversal.go
generated
vendored
Normal file
@ -0,0 +1,698 @@
|
||||
package goquery
|
||||
|
||||
import "golang.org/x/net/html"
|
||||
|
||||
type siblingType int
|
||||
|
||||
// Sibling type, used internally when iterating over children at the same
|
||||
// level (siblings) to specify which nodes are requested.
|
||||
const (
|
||||
siblingPrevUntil siblingType = iota - 3
|
||||
siblingPrevAll
|
||||
siblingPrev
|
||||
siblingAll
|
||||
siblingNext
|
||||
siblingNextAll
|
||||
siblingNextUntil
|
||||
siblingAllIncludingNonElements
|
||||
)
|
||||
|
||||
// Find gets the descendants of each element in the current set of matched
|
||||
// elements, filtered by a selector. It returns a new Selection object
|
||||
// containing these matched elements.
|
||||
func (s *Selection) Find(selector string) *Selection {
|
||||
return pushStack(s, findWithMatcher(s.Nodes, compileMatcher(selector)))
|
||||
}
|
||||
|
||||
// FindMatcher gets the descendants of each element in the current set of matched
|
||||
// elements, filtered by the matcher. It returns a new Selection object
|
||||
// containing these matched elements.
|
||||
func (s *Selection) FindMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, findWithMatcher(s.Nodes, m))
|
||||
}
|
||||
|
||||
// FindSelection gets the descendants of each element in the current
|
||||
// Selection, filtered by a Selection. It returns a new Selection object
|
||||
// containing these matched elements.
|
||||
func (s *Selection) FindSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return pushStack(s, nil)
|
||||
}
|
||||
return s.FindNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// FindNodes gets the descendants of each element in the current
|
||||
// Selection, filtered by some nodes. It returns a new Selection object
|
||||
// containing these matched elements.
|
||||
func (s *Selection) FindNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, mapNodes(nodes, func(i int, n *html.Node) []*html.Node {
|
||||
if sliceContains(s.Nodes, n) {
|
||||
return []*html.Node{n}
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
// Contents gets the children of each element in the Selection,
|
||||
// including text and comment nodes. It returns a new Selection object
|
||||
// containing these elements.
|
||||
func (s *Selection) Contents() *Selection {
|
||||
return pushStack(s, getChildrenNodes(s.Nodes, siblingAllIncludingNonElements))
|
||||
}
|
||||
|
||||
// ContentsFiltered gets the children of each element in the Selection,
|
||||
// filtered by the specified selector. It returns a new Selection
|
||||
// object containing these elements. Since selectors only act on Element nodes,
|
||||
// this function is an alias to ChildrenFiltered unless the selector is empty,
|
||||
// in which case it is an alias to Contents.
|
||||
func (s *Selection) ContentsFiltered(selector string) *Selection {
|
||||
if selector != "" {
|
||||
return s.ChildrenFiltered(selector)
|
||||
}
|
||||
return s.Contents()
|
||||
}
|
||||
|
||||
// ContentsMatcher gets the children of each element in the Selection,
|
||||
// filtered by the specified matcher. It returns a new Selection
|
||||
// object containing these elements. Since matchers only act on Element nodes,
|
||||
// this function is an alias to ChildrenMatcher.
|
||||
func (s *Selection) ContentsMatcher(m Matcher) *Selection {
|
||||
return s.ChildrenMatcher(m)
|
||||
}
|
||||
|
||||
// Children gets the child elements of each element in the Selection.
|
||||
// It returns a new Selection object containing these elements.
|
||||
func (s *Selection) Children() *Selection {
|
||||
return pushStack(s, getChildrenNodes(s.Nodes, siblingAll))
|
||||
}
|
||||
|
||||
// ChildrenFiltered gets the child elements of each element in the Selection,
|
||||
// filtered by the specified selector. It returns a new
|
||||
// Selection object containing these elements.
|
||||
func (s *Selection) ChildrenFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getChildrenNodes(s.Nodes, siblingAll), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// ChildrenMatcher gets the child elements of each element in the Selection,
|
||||
// filtered by the specified matcher. It returns a new
|
||||
// Selection object containing these elements.
|
||||
func (s *Selection) ChildrenMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getChildrenNodes(s.Nodes, siblingAll), m)
|
||||
}
|
||||
|
||||
// Parent gets the parent of each element in the Selection. It returns a
|
||||
// new Selection object containing the matched elements.
|
||||
func (s *Selection) Parent() *Selection {
|
||||
return pushStack(s, getParentNodes(s.Nodes))
|
||||
}
|
||||
|
||||
// ParentFiltered gets the parent of each element in the Selection filtered by a
|
||||
// selector. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) ParentFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getParentNodes(s.Nodes), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// ParentMatcher gets the parent of each element in the Selection filtered by a
|
||||
// matcher. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) ParentMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getParentNodes(s.Nodes), m)
|
||||
}
|
||||
|
||||
// Closest gets the first element that matches the selector by testing the
|
||||
// element itself and traversing up through its ancestors in the DOM tree.
|
||||
func (s *Selection) Closest(selector string) *Selection {
|
||||
cs := compileMatcher(selector)
|
||||
return s.ClosestMatcher(cs)
|
||||
}
|
||||
|
||||
// ClosestMatcher gets the first element that matches the matcher by testing the
|
||||
// element itself and traversing up through its ancestors in the DOM tree.
|
||||
func (s *Selection) ClosestMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, mapNodes(s.Nodes, func(i int, n *html.Node) []*html.Node {
|
||||
// For each node in the selection, test the node itself, then each parent
|
||||
// until a match is found.
|
||||
for ; n != nil; n = n.Parent {
|
||||
if m.Match(n) {
|
||||
return []*html.Node{n}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
// ClosestNodes gets the first element that matches one of the nodes by testing the
|
||||
// element itself and traversing up through its ancestors in the DOM tree.
|
||||
func (s *Selection) ClosestNodes(nodes ...*html.Node) *Selection {
|
||||
set := make(map[*html.Node]bool)
|
||||
for _, n := range nodes {
|
||||
set[n] = true
|
||||
}
|
||||
return pushStack(s, mapNodes(s.Nodes, func(i int, n *html.Node) []*html.Node {
|
||||
// For each node in the selection, test the node itself, then each parent
|
||||
// until a match is found.
|
||||
for ; n != nil; n = n.Parent {
|
||||
if set[n] {
|
||||
return []*html.Node{n}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
// ClosestSelection gets the first element that matches one of the nodes in the
|
||||
// Selection by testing the element itself and traversing up through its ancestors
|
||||
// in the DOM tree.
|
||||
func (s *Selection) ClosestSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return pushStack(s, nil)
|
||||
}
|
||||
return s.ClosestNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// Parents gets the ancestors of each element in the current Selection. It
|
||||
// returns a new Selection object with the matched elements.
|
||||
func (s *Selection) Parents() *Selection {
|
||||
return pushStack(s, getParentsNodes(s.Nodes, nil, nil))
|
||||
}
|
||||
|
||||
// ParentsFiltered gets the ancestors of each element in the current
|
||||
// Selection. It returns a new Selection object with the matched elements.
|
||||
func (s *Selection) ParentsFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// ParentsMatcher gets the ancestors of each element in the current
|
||||
// Selection. It returns a new Selection object with the matched elements.
|
||||
func (s *Selection) ParentsMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, nil, nil), m)
|
||||
}
|
||||
|
||||
// ParentsUntil gets the ancestors of each element in the Selection, up to but
|
||||
// not including the element matched by the selector. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) ParentsUntil(selector string) *Selection {
|
||||
return pushStack(s, getParentsNodes(s.Nodes, compileMatcher(selector), nil))
|
||||
}
|
||||
|
||||
// ParentsUntilMatcher gets the ancestors of each element in the Selection, up to but
|
||||
// not including the element matched by the matcher. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) ParentsUntilMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, getParentsNodes(s.Nodes, m, nil))
|
||||
}
|
||||
|
||||
// ParentsUntilSelection gets the ancestors of each element in the Selection,
|
||||
// up to but not including the elements in the specified Selection. It returns a
|
||||
// new Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsUntilSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.Parents()
|
||||
}
|
||||
return s.ParentsUntilNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// ParentsUntilNodes gets the ancestors of each element in the Selection,
|
||||
// up to but not including the specified nodes. It returns a
|
||||
// new Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsUntilNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, getParentsNodes(s.Nodes, nil, nodes))
|
||||
}
|
||||
|
||||
// ParentsFilteredUntil is like ParentsUntil, with the option to filter the
|
||||
// results based on a selector string. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, compileMatcher(untilSelector), nil), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// ParentsFilteredUntilMatcher is like ParentsUntilMatcher, with the option to filter the
|
||||
// results based on a matcher. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsFilteredUntilMatcher(filter, until Matcher) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, until, nil), filter)
|
||||
}
|
||||
|
||||
// ParentsFilteredUntilSelection is like ParentsUntilSelection, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
|
||||
return s.ParentsMatcherUntilSelection(compileMatcher(filterSelector), sel)
|
||||
}
|
||||
|
||||
// ParentsMatcherUntilSelection is like ParentsUntilSelection, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.ParentsMatcher(filter)
|
||||
}
|
||||
return s.ParentsMatcherUntilNodes(filter, sel.Nodes...)
|
||||
}
|
||||
|
||||
// ParentsFilteredUntilNodes is like ParentsUntilNodes, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// ParentsMatcherUntilNodes is like ParentsUntilNodes, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) ParentsMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), filter)
|
||||
}
|
||||
|
||||
// Siblings gets the siblings of each element in the Selection. It returns
|
||||
// a new Selection object containing the matched elements.
|
||||
func (s *Selection) Siblings() *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil))
|
||||
}
|
||||
|
||||
// SiblingsFiltered gets the siblings of each element in the Selection
|
||||
// filtered by a selector. It returns a new Selection object containing the
|
||||
// matched elements.
|
||||
func (s *Selection) SiblingsFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// SiblingsMatcher gets the siblings of each element in the Selection
|
||||
// filtered by a matcher. It returns a new Selection object containing the
|
||||
// matched elements.
|
||||
func (s *Selection) SiblingsMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil), m)
|
||||
}
|
||||
|
||||
// Next gets the immediately following sibling of each element in the
|
||||
// Selection. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) Next() *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil))
|
||||
}
|
||||
|
||||
// NextFiltered gets the immediately following sibling of each element in the
|
||||
// Selection filtered by a selector. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) NextFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// NextMatcher gets the immediately following sibling of each element in the
|
||||
// Selection filtered by a matcher. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) NextMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil), m)
|
||||
}
|
||||
|
||||
// NextAll gets all the following siblings of each element in the
|
||||
// Selection. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) NextAll() *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil))
|
||||
}
|
||||
|
||||
// NextAllFiltered gets all the following siblings of each element in the
|
||||
// Selection filtered by a selector. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) NextAllFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// NextAllMatcher gets all the following siblings of each element in the
|
||||
// Selection filtered by a matcher. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) NextAllMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil), m)
|
||||
}
|
||||
|
||||
// Prev gets the immediately preceding sibling of each element in the
|
||||
// Selection. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) Prev() *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil))
|
||||
}
|
||||
|
||||
// PrevFiltered gets the immediately preceding sibling of each element in the
|
||||
// Selection filtered by a selector. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) PrevFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// PrevMatcher gets the immediately preceding sibling of each element in the
|
||||
// Selection filtered by a matcher. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) PrevMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil), m)
|
||||
}
|
||||
|
||||
// PrevAll gets all the preceding siblings of each element in the
|
||||
// Selection. It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) PrevAll() *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil))
|
||||
}
|
||||
|
||||
// PrevAllFiltered gets all the preceding siblings of each element in the
|
||||
// Selection filtered by a selector. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) PrevAllFiltered(selector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil), compileMatcher(selector))
|
||||
}
|
||||
|
||||
// PrevAllMatcher gets all the preceding siblings of each element in the
|
||||
// Selection filtered by a matcher. It returns a new Selection object
|
||||
// containing the matched elements.
|
||||
func (s *Selection) PrevAllMatcher(m Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil), m)
|
||||
}
|
||||
|
||||
// NextUntil gets all following siblings of each element up to but not
|
||||
// including the element matched by the selector. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) NextUntil(selector string) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
compileMatcher(selector), nil))
|
||||
}
|
||||
|
||||
// NextUntilMatcher gets all following siblings of each element up to but not
|
||||
// including the element matched by the matcher. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) NextUntilMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
m, nil))
|
||||
}
|
||||
|
||||
// NextUntilSelection gets all following siblings of each element up to but not
|
||||
// including the element matched by the Selection. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) NextUntilSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.NextAll()
|
||||
}
|
||||
return s.NextUntilNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// NextUntilNodes gets all following siblings of each element up to but not
|
||||
// including the element matched by the nodes. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) NextUntilNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
nil, nodes))
|
||||
}
|
||||
|
||||
// PrevUntil gets all preceding siblings of each element up to but not
|
||||
// including the element matched by the selector. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) PrevUntil(selector string) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
compileMatcher(selector), nil))
|
||||
}
|
||||
|
||||
// PrevUntilMatcher gets all preceding siblings of each element up to but not
|
||||
// including the element matched by the matcher. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) PrevUntilMatcher(m Matcher) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
m, nil))
|
||||
}
|
||||
|
||||
// PrevUntilSelection gets all preceding siblings of each element up to but not
|
||||
// including the element matched by the Selection. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) PrevUntilSelection(sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.PrevAll()
|
||||
}
|
||||
return s.PrevUntilNodes(sel.Nodes...)
|
||||
}
|
||||
|
||||
// PrevUntilNodes gets all preceding siblings of each element up to but not
|
||||
// including the element matched by the nodes. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (s *Selection) PrevUntilNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
nil, nodes))
|
||||
}
|
||||
|
||||
// NextFilteredUntil is like NextUntil, with the option to filter
|
||||
// the results based on a selector string.
|
||||
// It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) NextFilteredUntil(filterSelector, untilSelector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
compileMatcher(untilSelector), nil), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// NextFilteredUntilMatcher is like NextUntilMatcher, with the option to filter
|
||||
// the results based on a matcher.
|
||||
// It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) NextFilteredUntilMatcher(filter, until Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
until, nil), filter)
|
||||
}
|
||||
|
||||
// NextFilteredUntilSelection is like NextUntilSelection, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) NextFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
|
||||
return s.NextMatcherUntilSelection(compileMatcher(filterSelector), sel)
|
||||
}
|
||||
|
||||
// NextMatcherUntilSelection is like NextUntilSelection, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) NextMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.NextMatcher(filter)
|
||||
}
|
||||
return s.NextMatcherUntilNodes(filter, sel.Nodes...)
|
||||
}
|
||||
|
||||
// NextFilteredUntilNodes is like NextUntilNodes, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
nil, nodes), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// NextMatcherUntilNodes is like NextUntilNodes, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) NextMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
|
||||
nil, nodes), filter)
|
||||
}
|
||||
|
||||
// PrevFilteredUntil is like PrevUntil, with the option to filter
|
||||
// the results based on a selector string.
|
||||
// It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) PrevFilteredUntil(filterSelector, untilSelector string) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
compileMatcher(untilSelector), nil), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// PrevFilteredUntilMatcher is like PrevUntilMatcher, with the option to filter
|
||||
// the results based on a matcher.
|
||||
// It returns a new Selection object containing the matched elements.
|
||||
func (s *Selection) PrevFilteredUntilMatcher(filter, until Matcher) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
until, nil), filter)
|
||||
}
|
||||
|
||||
// PrevFilteredUntilSelection is like PrevUntilSelection, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
|
||||
return s.PrevMatcherUntilSelection(compileMatcher(filterSelector), sel)
|
||||
}
|
||||
|
||||
// PrevMatcherUntilSelection is like PrevUntilSelection, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) PrevMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return s.PrevMatcher(filter)
|
||||
}
|
||||
return s.PrevMatcherUntilNodes(filter, sel.Nodes...)
|
||||
}
|
||||
|
||||
// PrevFilteredUntilNodes is like PrevUntilNodes, with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) PrevFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
nil, nodes), compileMatcher(filterSelector))
|
||||
}
|
||||
|
||||
// PrevMatcherUntilNodes is like PrevUntilNodes, with the
|
||||
// option to filter the results based on a matcher. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (s *Selection) PrevMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {
|
||||
return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
|
||||
nil, nodes), filter)
|
||||
}
|
||||
|
||||
// Filter and push filters the nodes based on a matcher, and pushes the results
|
||||
// on the stack, with the srcSel as previous selection.
|
||||
func filterAndPush(srcSel *Selection, nodes []*html.Node, m Matcher) *Selection {
|
||||
// Create a temporary Selection with the specified nodes to filter using winnow
|
||||
sel := &Selection{nodes, srcSel.document, nil}
|
||||
// Filter based on matcher and push on stack
|
||||
return pushStack(srcSel, winnow(sel, m, true))
|
||||
}
|
||||
|
||||
// Internal implementation of Find that return raw nodes.
|
||||
func findWithMatcher(nodes []*html.Node, m Matcher) []*html.Node {
|
||||
// Map nodes to find the matches within the children of each node
|
||||
return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
|
||||
// Go down one level, becausejQuery's Find selects only within descendants
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
if c.Type == html.ElementNode {
|
||||
result = append(result, m.MatchAll(c)...)
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// Internal implementation to get all parent nodes, stopping at the specified
|
||||
// node (or nil if no stop).
|
||||
func getParentsNodes(nodes []*html.Node, stopm Matcher, stopNodes []*html.Node) []*html.Node {
|
||||
return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
|
||||
for p := n.Parent; p != nil; p = p.Parent {
|
||||
sel := newSingleSelection(p, nil)
|
||||
if stopm != nil {
|
||||
if sel.IsMatcher(stopm) {
|
||||
break
|
||||
}
|
||||
} else if len(stopNodes) > 0 {
|
||||
if sel.IsNodes(stopNodes...) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if p.Type == html.ElementNode {
|
||||
result = append(result, p)
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// Internal implementation of sibling nodes that return a raw slice of matches.
|
||||
func getSiblingNodes(nodes []*html.Node, st siblingType, untilm Matcher, untilNodes []*html.Node) []*html.Node {
|
||||
var f func(*html.Node) bool
|
||||
|
||||
// If the requested siblings are ...Until, create the test function to
|
||||
// determine if the until condition is reached (returns true if it is)
|
||||
if st == siblingNextUntil || st == siblingPrevUntil {
|
||||
f = func(n *html.Node) bool {
|
||||
if untilm != nil {
|
||||
// Matcher-based condition
|
||||
sel := newSingleSelection(n, nil)
|
||||
return sel.IsMatcher(untilm)
|
||||
} else if len(untilNodes) > 0 {
|
||||
// Nodes-based condition
|
||||
sel := newSingleSelection(n, nil)
|
||||
return sel.IsNodes(untilNodes...)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {
|
||||
return getChildrenWithSiblingType(n.Parent, st, n, f)
|
||||
})
|
||||
}
|
||||
|
||||
// Gets the children nodes of each node in the specified slice of nodes,
|
||||
// based on the sibling type request.
|
||||
func getChildrenNodes(nodes []*html.Node, st siblingType) []*html.Node {
|
||||
return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {
|
||||
return getChildrenWithSiblingType(n, st, nil, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Gets the children of the specified parent, based on the requested sibling
|
||||
// type, skipping a specified node if required.
|
||||
func getChildrenWithSiblingType(parent *html.Node, st siblingType, skipNode *html.Node,
|
||||
untilFunc func(*html.Node) bool) (result []*html.Node) {
|
||||
|
||||
// Create the iterator function
|
||||
var iter = func(cur *html.Node) (ret *html.Node) {
|
||||
// Based on the sibling type requested, iterate the right way
|
||||
for {
|
||||
switch st {
|
||||
case siblingAll, siblingAllIncludingNonElements:
|
||||
if cur == nil {
|
||||
// First iteration, start with first child of parent
|
||||
// Skip node if required
|
||||
if ret = parent.FirstChild; ret == skipNode && skipNode != nil {
|
||||
ret = skipNode.NextSibling
|
||||
}
|
||||
} else {
|
||||
// Skip node if required
|
||||
if ret = cur.NextSibling; ret == skipNode && skipNode != nil {
|
||||
ret = skipNode.NextSibling
|
||||
}
|
||||
}
|
||||
case siblingPrev, siblingPrevAll, siblingPrevUntil:
|
||||
if cur == nil {
|
||||
// Start with previous sibling of the skip node
|
||||
ret = skipNode.PrevSibling
|
||||
} else {
|
||||
ret = cur.PrevSibling
|
||||
}
|
||||
case siblingNext, siblingNextAll, siblingNextUntil:
|
||||
if cur == nil {
|
||||
// Start with next sibling of the skip node
|
||||
ret = skipNode.NextSibling
|
||||
} else {
|
||||
ret = cur.NextSibling
|
||||
}
|
||||
default:
|
||||
panic("Invalid sibling type.")
|
||||
}
|
||||
if ret == nil || ret.Type == html.ElementNode || st == siblingAllIncludingNonElements {
|
||||
return
|
||||
}
|
||||
// Not a valid node, try again from this one
|
||||
cur = ret
|
||||
}
|
||||
}
|
||||
|
||||
for c := iter(nil); c != nil; c = iter(c) {
|
||||
// If this is an ...Until case, test before append (returns true
|
||||
// if the until condition is reached)
|
||||
if st == siblingNextUntil || st == siblingPrevUntil {
|
||||
if untilFunc(c) {
|
||||
return
|
||||
}
|
||||
}
|
||||
result = append(result, c)
|
||||
if st == siblingNext || st == siblingPrev {
|
||||
// Only one node was requested (immediate next or previous), so exit
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Internal implementation of parent nodes that return a raw slice of Nodes.
|
||||
func getParentNodes(nodes []*html.Node) []*html.Node {
|
||||
return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {
|
||||
if n.Parent != nil && n.Parent.Type == html.ElementNode {
|
||||
return []*html.Node{n.Parent}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Internal map function used by many traversing methods. Takes the source nodes
|
||||
// to iterate on and the mapping function that returns an array of nodes.
|
||||
// Returns an array of nodes mapped by calling the callback function once for
|
||||
// each node in the source nodes.
|
||||
func mapNodes(nodes []*html.Node, f func(int, *html.Node) []*html.Node) (result []*html.Node) {
|
||||
set := make(map[*html.Node]bool)
|
||||
for i, n := range nodes {
|
||||
if vals := f(i, n); len(vals) > 0 {
|
||||
result = appendWithoutDuplicates(result, vals, set)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
141
vendor/github.com/PuerkitoBio/goquery/type.go
generated
vendored
Normal file
141
vendor/github.com/PuerkitoBio/goquery/type.go
generated
vendored
Normal file
@ -0,0 +1,141 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/andybalholm/cascadia"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// Document represents an HTML document to be manipulated. Unlike jQuery, which
|
||||
// is loaded as part of a DOM document, and thus acts upon its containing
|
||||
// document, GoQuery doesn't know which HTML document to act upon. So it needs
|
||||
// to be told, and that's what the Document class is for. It holds the root
|
||||
// document node to manipulate, and can make selections on this document.
|
||||
type Document struct {
|
||||
*Selection
|
||||
Url *url.URL
|
||||
rootNode *html.Node
|
||||
}
|
||||
|
||||
// NewDocumentFromNode is a Document constructor that takes a root html Node
|
||||
// as argument.
|
||||
func NewDocumentFromNode(root *html.Node) *Document {
|
||||
return newDocument(root, nil)
|
||||
}
|
||||
|
||||
// NewDocument is a Document constructor that takes a string URL as argument.
|
||||
// It loads the specified document, parses it, and stores the root Document
|
||||
// node, ready to be manipulated.
|
||||
//
|
||||
// Deprecated: Use the net/http standard library package to make the request
|
||||
// and validate the response before calling goquery.NewDocumentFromReader
|
||||
// with the response's body.
|
||||
func NewDocument(url string) (*Document, error) {
|
||||
// Load the URL
|
||||
res, e := http.Get(url)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return NewDocumentFromResponse(res)
|
||||
}
|
||||
|
||||
// NewDocumentFromReader returns a Document from an io.Reader.
|
||||
// It returns an error as second value if the reader's data cannot be parsed
|
||||
// as html. It does not check if the reader is also an io.Closer, the
|
||||
// provided reader is never closed by this call. It is the responsibility
|
||||
// of the caller to close it if required.
|
||||
func NewDocumentFromReader(r io.Reader) (*Document, error) {
|
||||
root, e := html.Parse(r)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return newDocument(root, nil), nil
|
||||
}
|
||||
|
||||
// NewDocumentFromResponse is another Document constructor that takes an http response as argument.
|
||||
// It loads the specified response's document, parses it, and stores the root Document
|
||||
// node, ready to be manipulated. The response's body is closed on return.
|
||||
//
|
||||
// Deprecated: Use goquery.NewDocumentFromReader with the response's body.
|
||||
func NewDocumentFromResponse(res *http.Response) (*Document, error) {
|
||||
if res == nil {
|
||||
return nil, errors.New("Response is nil")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.Request == nil {
|
||||
return nil, errors.New("Response.Request is nil")
|
||||
}
|
||||
|
||||
// Parse the HTML into nodes
|
||||
root, e := html.Parse(res.Body)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
// Create and fill the document
|
||||
return newDocument(root, res.Request.URL), nil
|
||||
}
|
||||
|
||||
// CloneDocument creates a deep-clone of a document.
|
||||
func CloneDocument(doc *Document) *Document {
|
||||
return newDocument(cloneNode(doc.rootNode), doc.Url)
|
||||
}
|
||||
|
||||
// Private constructor, make sure all fields are correctly filled.
|
||||
func newDocument(root *html.Node, url *url.URL) *Document {
|
||||
// Create and fill the document
|
||||
d := &Document{nil, url, root}
|
||||
d.Selection = newSingleSelection(root, d)
|
||||
return d
|
||||
}
|
||||
|
||||
// Selection represents a collection of nodes matching some criteria. The
|
||||
// initial Selection can be created by using Document.Find, and then
|
||||
// manipulated using the jQuery-like chainable syntax and methods.
|
||||
type Selection struct {
|
||||
Nodes []*html.Node
|
||||
document *Document
|
||||
prevSel *Selection
|
||||
}
|
||||
|
||||
// Helper constructor to create an empty selection
|
||||
func newEmptySelection(doc *Document) *Selection {
|
||||
return &Selection{nil, doc, nil}
|
||||
}
|
||||
|
||||
// Helper constructor to create a selection of only one node
|
||||
func newSingleSelection(node *html.Node, doc *Document) *Selection {
|
||||
return &Selection{[]*html.Node{node}, doc, nil}
|
||||
}
|
||||
|
||||
// Matcher is an interface that defines the methods to match
|
||||
// HTML nodes against a compiled selector string. Cascadia's
|
||||
// Selector implements this interface.
|
||||
type Matcher interface {
|
||||
Match(*html.Node) bool
|
||||
MatchAll(*html.Node) []*html.Node
|
||||
Filter([]*html.Node) []*html.Node
|
||||
}
|
||||
|
||||
// compileMatcher compiles the selector string s and returns
|
||||
// the corresponding Matcher. If s is an invalid selector string,
|
||||
// it returns a Matcher that fails all matches.
|
||||
func compileMatcher(s string) Matcher {
|
||||
cs, err := cascadia.Compile(s)
|
||||
if err != nil {
|
||||
return invalidMatcher{}
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
// invalidMatcher is a Matcher that always fails to match.
|
||||
type invalidMatcher struct{}
|
||||
|
||||
func (invalidMatcher) Match(n *html.Node) bool { return false }
|
||||
func (invalidMatcher) MatchAll(n *html.Node) []*html.Node { return nil }
|
||||
func (invalidMatcher) Filter(ns []*html.Node) []*html.Node { return nil }
|
161
vendor/github.com/PuerkitoBio/goquery/utilities.go
generated
vendored
Normal file
161
vendor/github.com/PuerkitoBio/goquery/utilities.go
generated
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
package goquery
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// used to determine if a set (map[*html.Node]bool) should be used
|
||||
// instead of iterating over a slice. The set uses more memory and
|
||||
// is slower than slice iteration for small N.
|
||||
const minNodesForSet = 1000
|
||||
|
||||
var nodeNames = []string{
|
||||
html.ErrorNode: "#error",
|
||||
html.TextNode: "#text",
|
||||
html.DocumentNode: "#document",
|
||||
html.CommentNode: "#comment",
|
||||
}
|
||||
|
||||
// NodeName returns the node name of the first element in the selection.
|
||||
// It tries to behave in a similar way as the DOM's nodeName property
|
||||
// (https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName).
|
||||
//
|
||||
// Go's net/html package defines the following node types, listed with
|
||||
// the corresponding returned value from this function:
|
||||
//
|
||||
// ErrorNode : #error
|
||||
// TextNode : #text
|
||||
// DocumentNode : #document
|
||||
// ElementNode : the element's tag name
|
||||
// CommentNode : #comment
|
||||
// DoctypeNode : the name of the document type
|
||||
//
|
||||
func NodeName(s *Selection) string {
|
||||
if s.Length() == 0 {
|
||||
return ""
|
||||
}
|
||||
switch n := s.Get(0); n.Type {
|
||||
case html.ElementNode, html.DoctypeNode:
|
||||
return n.Data
|
||||
default:
|
||||
if n.Type >= 0 && int(n.Type) < len(nodeNames) {
|
||||
return nodeNames[n.Type]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// OuterHtml returns the outer HTML rendering of the first item in
|
||||
// the selection - that is, the HTML including the first element's
|
||||
// tag and attributes.
|
||||
//
|
||||
// Unlike InnerHtml, this is a function and not a method on the Selection,
|
||||
// because this is not a jQuery method (in javascript-land, this is
|
||||
// a property provided by the DOM).
|
||||
func OuterHtml(s *Selection) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if s.Length() == 0 {
|
||||
return "", nil
|
||||
}
|
||||
n := s.Get(0)
|
||||
if err := html.Render(&buf, n); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// Loop through all container nodes to search for the target node.
|
||||
func sliceContains(container []*html.Node, contained *html.Node) bool {
|
||||
for _, n := range container {
|
||||
if nodeContains(n, contained) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Checks if the contained node is within the container node.
|
||||
func nodeContains(container *html.Node, contained *html.Node) bool {
|
||||
// Check if the parent of the contained node is the container node, traversing
|
||||
// upward until the top is reached, or the container is found.
|
||||
for contained = contained.Parent; contained != nil; contained = contained.Parent {
|
||||
if container == contained {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Checks if the target node is in the slice of nodes.
|
||||
func isInSlice(slice []*html.Node, node *html.Node) bool {
|
||||
return indexInSlice(slice, node) > -1
|
||||
}
|
||||
|
||||
// Returns the index of the target node in the slice, or -1.
|
||||
func indexInSlice(slice []*html.Node, node *html.Node) int {
|
||||
if node != nil {
|
||||
for i, n := range slice {
|
||||
if n == node {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// Appends the new nodes to the target slice, making sure no duplicate is added.
|
||||
// There is no check to the original state of the target slice, so it may still
|
||||
// contain duplicates. The target slice is returned because append() may create
|
||||
// a new underlying array. If targetSet is nil, a local set is created with the
|
||||
// target if len(target) + len(nodes) is greater than minNodesForSet.
|
||||
func appendWithoutDuplicates(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool) []*html.Node {
|
||||
// if there are not that many nodes, don't use the map, faster to just use nested loops
|
||||
// (unless a non-nil targetSet is passed, in which case the caller knows better).
|
||||
if targetSet == nil && len(target)+len(nodes) < minNodesForSet {
|
||||
for _, n := range nodes {
|
||||
if !isInSlice(target, n) {
|
||||
target = append(target, n)
|
||||
}
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
// if a targetSet is passed, then assume it is reliable, otherwise create one
|
||||
// and initialize it with the current target contents.
|
||||
if targetSet == nil {
|
||||
targetSet = make(map[*html.Node]bool, len(target))
|
||||
for _, n := range target {
|
||||
targetSet[n] = true
|
||||
}
|
||||
}
|
||||
for _, n := range nodes {
|
||||
if !targetSet[n] {
|
||||
target = append(target, n)
|
||||
targetSet[n] = true
|
||||
}
|
||||
}
|
||||
|
||||
return target
|
||||
}
|
||||
|
||||
// Loop through a selection, returning only those nodes that pass the predicate
|
||||
// function.
|
||||
func grep(sel *Selection, predicate func(i int, s *Selection) bool) (result []*html.Node) {
|
||||
for i, n := range sel.Nodes {
|
||||
if predicate(i, newSingleSelection(n, sel.document)) {
|
||||
result = append(result, n)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Creates a new Selection object based on the specified nodes, and keeps the
|
||||
// source Selection object on the stack (linked list).
|
||||
func pushStack(fromSel *Selection, nodes []*html.Node) *Selection {
|
||||
result := &Selection{nodes, fromSel.document, fromSel}
|
||||
return result
|
||||
}
|
20
vendor/github.com/StackExchange/wmi/LICENSE
generated
vendored
Normal file
20
vendor/github.com/StackExchange/wmi/LICENSE
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Stack Exchange
|
||||
|
||||
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.
|
6
vendor/github.com/StackExchange/wmi/README.md
generated
vendored
Normal file
6
vendor/github.com/StackExchange/wmi/README.md
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
wmi
|
||||
===
|
||||
|
||||
Package wmi provides a WQL interface to Windows WMI.
|
||||
|
||||
Note: It interfaces with WMI on the local machine, therefore it only runs on Windows.
|
260
vendor/github.com/StackExchange/wmi/swbemservices.go
generated
vendored
Normal file
260
vendor/github.com/StackExchange/wmi/swbemservices.go
generated
vendored
Normal file
@ -0,0 +1,260 @@
|
||||
// +build windows
|
||||
|
||||
package wmi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/go-ole/go-ole"
|
||||
"github.com/go-ole/go-ole/oleutil"
|
||||
)
|
||||
|
||||
// SWbemServices is used to access wmi. See https://msdn.microsoft.com/en-us/library/aa393719(v=vs.85).aspx
|
||||
type SWbemServices struct {
|
||||
//TODO: track namespace. Not sure if we can re connect to a different namespace using the same instance
|
||||
cWMIClient *Client //This could also be an embedded struct, but then we would need to branch on Client vs SWbemServices in the Query method
|
||||
sWbemLocatorIUnknown *ole.IUnknown
|
||||
sWbemLocatorIDispatch *ole.IDispatch
|
||||
queries chan *queryRequest
|
||||
closeError chan error
|
||||
lQueryorClose sync.Mutex
|
||||
}
|
||||
|
||||
type queryRequest struct {
|
||||
query string
|
||||
dst interface{}
|
||||
args []interface{}
|
||||
finished chan error
|
||||
}
|
||||
|
||||
// InitializeSWbemServices will return a new SWbemServices object that can be used to query WMI
|
||||
func InitializeSWbemServices(c *Client, connectServerArgs ...interface{}) (*SWbemServices, error) {
|
||||
//fmt.Println("InitializeSWbemServices: Starting")
|
||||
//TODO: implement connectServerArgs as optional argument for init with connectServer call
|
||||
s := new(SWbemServices)
|
||||
s.cWMIClient = c
|
||||
s.queries = make(chan *queryRequest)
|
||||
initError := make(chan error)
|
||||
go s.process(initError)
|
||||
|
||||
err, ok := <-initError
|
||||
if ok {
|
||||
return nil, err //Send error to caller
|
||||
}
|
||||
//fmt.Println("InitializeSWbemServices: Finished")
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Close will clear and release all of the SWbemServices resources
|
||||
func (s *SWbemServices) Close() error {
|
||||
s.lQueryorClose.Lock()
|
||||
if s == nil || s.sWbemLocatorIDispatch == nil {
|
||||
s.lQueryorClose.Unlock()
|
||||
return fmt.Errorf("SWbemServices is not Initialized")
|
||||
}
|
||||
if s.queries == nil {
|
||||
s.lQueryorClose.Unlock()
|
||||
return fmt.Errorf("SWbemServices has been closed")
|
||||
}
|
||||
//fmt.Println("Close: sending close request")
|
||||
var result error
|
||||
ce := make(chan error)
|
||||
s.closeError = ce //Race condition if multiple callers to close. May need to lock here
|
||||
close(s.queries) //Tell background to shut things down
|
||||
s.lQueryorClose.Unlock()
|
||||
err, ok := <-ce
|
||||
if ok {
|
||||
result = err
|
||||
}
|
||||
//fmt.Println("Close: finished")
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *SWbemServices) process(initError chan error) {
|
||||
//fmt.Println("process: starting background thread initialization")
|
||||
//All OLE/WMI calls must happen on the same initialized thead, so lock this goroutine
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
|
||||
if err != nil {
|
||||
oleCode := err.(*ole.OleError).Code()
|
||||
if oleCode != ole.S_OK && oleCode != S_FALSE {
|
||||
initError <- fmt.Errorf("ole.CoInitializeEx error: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
defer ole.CoUninitialize()
|
||||
|
||||
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
|
||||
if err != nil {
|
||||
initError <- fmt.Errorf("CreateObject SWbemLocator error: %v", err)
|
||||
return
|
||||
} else if unknown == nil {
|
||||
initError <- ErrNilCreateObject
|
||||
return
|
||||
}
|
||||
defer unknown.Release()
|
||||
s.sWbemLocatorIUnknown = unknown
|
||||
|
||||
dispatch, err := s.sWbemLocatorIUnknown.QueryInterface(ole.IID_IDispatch)
|
||||
if err != nil {
|
||||
initError <- fmt.Errorf("SWbemLocator QueryInterface error: %v", err)
|
||||
return
|
||||
}
|
||||
defer dispatch.Release()
|
||||
s.sWbemLocatorIDispatch = dispatch
|
||||
|
||||
// we can't do the ConnectServer call outside the loop unless we find a way to track and re-init the connectServerArgs
|
||||
//fmt.Println("process: initialized. closing initError")
|
||||
close(initError)
|
||||
//fmt.Println("process: waiting for queries")
|
||||
for q := range s.queries {
|
||||
//fmt.Printf("process: new query: len(query)=%d\n", len(q.query))
|
||||
errQuery := s.queryBackground(q)
|
||||
//fmt.Println("process: s.queryBackground finished")
|
||||
if errQuery != nil {
|
||||
q.finished <- errQuery
|
||||
}
|
||||
close(q.finished)
|
||||
}
|
||||
//fmt.Println("process: queries channel closed")
|
||||
s.queries = nil //set channel to nil so we know it is closed
|
||||
//TODO: I think the Release/Clear calls can panic if things are in a bad state.
|
||||
//TODO: May need to recover from panics and send error to method caller instead.
|
||||
close(s.closeError)
|
||||
}
|
||||
|
||||
// Query runs the WQL query using a SWbemServices instance and appends the values to dst.
|
||||
//
|
||||
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
|
||||
// the query must have the same name in dst. Supported types are all signed and
|
||||
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
|
||||
// Array types are not supported.
|
||||
//
|
||||
// By default, the local machine and default namespace are used. These can be
|
||||
// changed using connectServerArgs. See
|
||||
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
|
||||
func (s *SWbemServices) Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
|
||||
s.lQueryorClose.Lock()
|
||||
if s == nil || s.sWbemLocatorIDispatch == nil {
|
||||
s.lQueryorClose.Unlock()
|
||||
return fmt.Errorf("SWbemServices is not Initialized")
|
||||
}
|
||||
if s.queries == nil {
|
||||
s.lQueryorClose.Unlock()
|
||||
return fmt.Errorf("SWbemServices has been closed")
|
||||
}
|
||||
|
||||
//fmt.Println("Query: Sending query request")
|
||||
qr := queryRequest{
|
||||
query: query,
|
||||
dst: dst,
|
||||
args: connectServerArgs,
|
||||
finished: make(chan error),
|
||||
}
|
||||
s.queries <- &qr
|
||||
s.lQueryorClose.Unlock()
|
||||
err, ok := <-qr.finished
|
||||
if ok {
|
||||
//fmt.Println("Query: Finished with error")
|
||||
return err //Send error to caller
|
||||
}
|
||||
//fmt.Println("Query: Finished")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SWbemServices) queryBackground(q *queryRequest) error {
|
||||
if s == nil || s.sWbemLocatorIDispatch == nil {
|
||||
return fmt.Errorf("SWbemServices is not Initialized")
|
||||
}
|
||||
wmi := s.sWbemLocatorIDispatch //Should just rename in the code, but this will help as we break things apart
|
||||
//fmt.Println("queryBackground: Starting")
|
||||
|
||||
dv := reflect.ValueOf(q.dst)
|
||||
if dv.Kind() != reflect.Ptr || dv.IsNil() {
|
||||
return ErrInvalidEntityType
|
||||
}
|
||||
dv = dv.Elem()
|
||||
mat, elemType := checkMultiArg(dv)
|
||||
if mat == multiArgTypeInvalid {
|
||||
return ErrInvalidEntityType
|
||||
}
|
||||
|
||||
// service is a SWbemServices
|
||||
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", q.args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
service := serviceRaw.ToIDispatch()
|
||||
defer serviceRaw.Clear()
|
||||
|
||||
// result is a SWBemObjectSet
|
||||
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", q.query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := resultRaw.ToIDispatch()
|
||||
defer resultRaw.Clear()
|
||||
|
||||
count, err := oleInt64(result, "Count")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enumProperty, err := result.GetProperty("_NewEnum")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer enumProperty.Clear()
|
||||
|
||||
enum, err := enumProperty.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if enum == nil {
|
||||
return fmt.Errorf("can't get IEnumVARIANT, enum is nil")
|
||||
}
|
||||
defer enum.Release()
|
||||
|
||||
// Initialize a slice with Count capacity
|
||||
dv.Set(reflect.MakeSlice(dv.Type(), 0, int(count)))
|
||||
|
||||
var errFieldMismatch error
|
||||
for itemRaw, length, err := enum.Next(1); length > 0; itemRaw, length, err = enum.Next(1) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := func() error {
|
||||
// item is a SWbemObject, but really a Win32_Process
|
||||
item := itemRaw.ToIDispatch()
|
||||
defer item.Release()
|
||||
|
||||
ev := reflect.New(elemType)
|
||||
if err = s.cWMIClient.loadEntity(ev.Interface(), item); err != nil {
|
||||
if _, ok := err.(*ErrFieldMismatch); ok {
|
||||
// We continue loading entities even in the face of field mismatch errors.
|
||||
// If we encounter any other error, that other error is returned. Otherwise,
|
||||
// an ErrFieldMismatch is returned.
|
||||
errFieldMismatch = err
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if mat != multiArgTypeStructPtr {
|
||||
ev = ev.Elem()
|
||||
}
|
||||
dv.Set(reflect.Append(dv, ev))
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//fmt.Println("queryBackground: Finished")
|
||||
return errFieldMismatch
|
||||
}
|
501
vendor/github.com/StackExchange/wmi/wmi.go
generated
vendored
Normal file
501
vendor/github.com/StackExchange/wmi/wmi.go
generated
vendored
Normal file
@ -0,0 +1,501 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
Package wmi provides a WQL interface for WMI on Windows.
|
||||
|
||||
Example code to print names of running processes:
|
||||
|
||||
type Win32_Process struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var dst []Win32_Process
|
||||
q := wmi.CreateQuery(&dst, "")
|
||||
err := wmi.Query(q, &dst)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for i, v := range dst {
|
||||
println(i, v.Name)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
package wmi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-ole/go-ole"
|
||||
"github.com/go-ole/go-ole/oleutil"
|
||||
)
|
||||
|
||||
var l = log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
||||
var (
|
||||
ErrInvalidEntityType = errors.New("wmi: invalid entity type")
|
||||
// ErrNilCreateObject is the error returned if CreateObject returns nil even
|
||||
// if the error was nil.
|
||||
ErrNilCreateObject = errors.New("wmi: create object returned nil")
|
||||
lock sync.Mutex
|
||||
)
|
||||
|
||||
// S_FALSE is returned by CoInitializeEx if it was already called on this thread.
|
||||
const S_FALSE = 0x00000001
|
||||
|
||||
// QueryNamespace invokes Query with the given namespace on the local machine.
|
||||
func QueryNamespace(query string, dst interface{}, namespace string) error {
|
||||
return Query(query, dst, nil, namespace)
|
||||
}
|
||||
|
||||
// Query runs the WQL query and appends the values to dst.
|
||||
//
|
||||
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
|
||||
// the query must have the same name in dst. Supported types are all signed and
|
||||
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
|
||||
// Array types are not supported.
|
||||
//
|
||||
// By default, the local machine and default namespace are used. These can be
|
||||
// changed using connectServerArgs. See
|
||||
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
|
||||
//
|
||||
// Query is a wrapper around DefaultClient.Query.
|
||||
func Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
|
||||
if DefaultClient.SWbemServicesClient == nil {
|
||||
return DefaultClient.Query(query, dst, connectServerArgs...)
|
||||
}
|
||||
return DefaultClient.SWbemServicesClient.Query(query, dst, connectServerArgs...)
|
||||
}
|
||||
|
||||
// A Client is an WMI query client.
|
||||
//
|
||||
// Its zero value (DefaultClient) is a usable client.
|
||||
type Client struct {
|
||||
// NonePtrZero specifies if nil values for fields which aren't pointers
|
||||
// should be returned as the field types zero value.
|
||||
//
|
||||
// Setting this to true allows stucts without pointer fields to be used
|
||||
// without the risk failure should a nil value returned from WMI.
|
||||
NonePtrZero bool
|
||||
|
||||
// PtrNil specifies if nil values for pointer fields should be returned
|
||||
// as nil.
|
||||
//
|
||||
// Setting this to true will set pointer fields to nil where WMI
|
||||
// returned nil, otherwise the types zero value will be returned.
|
||||
PtrNil bool
|
||||
|
||||
// AllowMissingFields specifies that struct fields not present in the
|
||||
// query result should not result in an error.
|
||||
//
|
||||
// Setting this to true allows custom queries to be used with full
|
||||
// struct definitions instead of having to define multiple structs.
|
||||
AllowMissingFields bool
|
||||
|
||||
// SWbemServiceClient is an optional SWbemServices object that can be
|
||||
// initialized and then reused across multiple queries. If it is null
|
||||
// then the method will initialize a new temporary client each time.
|
||||
SWbemServicesClient *SWbemServices
|
||||
}
|
||||
|
||||
// DefaultClient is the default Client and is used by Query, QueryNamespace
|
||||
var DefaultClient = &Client{}
|
||||
|
||||
// Query runs the WQL query and appends the values to dst.
|
||||
//
|
||||
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
|
||||
// the query must have the same name in dst. Supported types are all signed and
|
||||
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
|
||||
// Array types are not supported.
|
||||
//
|
||||
// By default, the local machine and default namespace are used. These can be
|
||||
// changed using connectServerArgs. See
|
||||
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
|
||||
func (c *Client) Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
|
||||
dv := reflect.ValueOf(dst)
|
||||
if dv.Kind() != reflect.Ptr || dv.IsNil() {
|
||||
return ErrInvalidEntityType
|
||||
}
|
||||
dv = dv.Elem()
|
||||
mat, elemType := checkMultiArg(dv)
|
||||
if mat == multiArgTypeInvalid {
|
||||
return ErrInvalidEntityType
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
|
||||
if err != nil {
|
||||
oleCode := err.(*ole.OleError).Code()
|
||||
if oleCode != ole.S_OK && oleCode != S_FALSE {
|
||||
return err
|
||||
}
|
||||
}
|
||||
defer ole.CoUninitialize()
|
||||
|
||||
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
|
||||
if err != nil {
|
||||
return err
|
||||
} else if unknown == nil {
|
||||
return ErrNilCreateObject
|
||||
}
|
||||
defer unknown.Release()
|
||||
|
||||
wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer wmi.Release()
|
||||
|
||||
// service is a SWbemServices
|
||||
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", connectServerArgs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
service := serviceRaw.ToIDispatch()
|
||||
defer serviceRaw.Clear()
|
||||
|
||||
// result is a SWBemObjectSet
|
||||
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := resultRaw.ToIDispatch()
|
||||
defer resultRaw.Clear()
|
||||
|
||||
count, err := oleInt64(result, "Count")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enumProperty, err := result.GetProperty("_NewEnum")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer enumProperty.Clear()
|
||||
|
||||
enum, err := enumProperty.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if enum == nil {
|
||||
return fmt.Errorf("can't get IEnumVARIANT, enum is nil")
|
||||
}
|
||||
defer enum.Release()
|
||||
|
||||
// Initialize a slice with Count capacity
|
||||
dv.Set(reflect.MakeSlice(dv.Type(), 0, int(count)))
|
||||
|
||||
var errFieldMismatch error
|
||||
for itemRaw, length, err := enum.Next(1); length > 0; itemRaw, length, err = enum.Next(1) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := func() error {
|
||||
// item is a SWbemObject, but really a Win32_Process
|
||||
item := itemRaw.ToIDispatch()
|
||||
defer item.Release()
|
||||
|
||||
ev := reflect.New(elemType)
|
||||
if err = c.loadEntity(ev.Interface(), item); err != nil {
|
||||
if _, ok := err.(*ErrFieldMismatch); ok {
|
||||
// We continue loading entities even in the face of field mismatch errors.
|
||||
// If we encounter any other error, that other error is returned. Otherwise,
|
||||
// an ErrFieldMismatch is returned.
|
||||
errFieldMismatch = err
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if mat != multiArgTypeStructPtr {
|
||||
ev = ev.Elem()
|
||||
}
|
||||
dv.Set(reflect.Append(dv, ev))
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return errFieldMismatch
|
||||
}
|
||||
|
||||
// ErrFieldMismatch is returned when a field is to be loaded into a different
|
||||
// type than the one it was stored from, or when a field is missing or
|
||||
// unexported in the destination struct.
|
||||
// StructType is the type of the struct pointed to by the destination argument.
|
||||
type ErrFieldMismatch struct {
|
||||
StructType reflect.Type
|
||||
FieldName string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (e *ErrFieldMismatch) Error() string {
|
||||
return fmt.Sprintf("wmi: cannot load field %q into a %q: %s",
|
||||
e.FieldName, e.StructType, e.Reason)
|
||||
}
|
||||
|
||||
var timeType = reflect.TypeOf(time.Time{})
|
||||
|
||||
// loadEntity loads a SWbemObject into a struct pointer.
|
||||
func (c *Client) loadEntity(dst interface{}, src *ole.IDispatch) (errFieldMismatch error) {
|
||||
v := reflect.ValueOf(dst).Elem()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
f := v.Field(i)
|
||||
of := f
|
||||
isPtr := f.Kind() == reflect.Ptr
|
||||
if isPtr {
|
||||
ptr := reflect.New(f.Type().Elem())
|
||||
f.Set(ptr)
|
||||
f = f.Elem()
|
||||
}
|
||||
n := v.Type().Field(i).Name
|
||||
if !f.CanSet() {
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "CanSet() is false",
|
||||
}
|
||||
}
|
||||
prop, err := oleutil.GetProperty(src, n)
|
||||
if err != nil {
|
||||
if !c.AllowMissingFields {
|
||||
errFieldMismatch = &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "no such struct field",
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
defer prop.Clear()
|
||||
|
||||
if prop.VT == 0x1 { //VT_NULL
|
||||
continue
|
||||
}
|
||||
|
||||
switch val := prop.Value().(type) {
|
||||
case int8, int16, int32, int64, int:
|
||||
v := reflect.ValueOf(val).Int()
|
||||
switch f.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
f.SetInt(v)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
f.SetUint(uint64(v))
|
||||
default:
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "not an integer class",
|
||||
}
|
||||
}
|
||||
case uint8, uint16, uint32, uint64:
|
||||
v := reflect.ValueOf(val).Uint()
|
||||
switch f.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
f.SetInt(int64(v))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
f.SetUint(v)
|
||||
default:
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "not an integer class",
|
||||
}
|
||||
}
|
||||
case string:
|
||||
switch f.Kind() {
|
||||
case reflect.String:
|
||||
f.SetString(val)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
iv, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.SetInt(iv)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
uv, err := strconv.ParseUint(val, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.SetUint(uv)
|
||||
case reflect.Struct:
|
||||
switch f.Type() {
|
||||
case timeType:
|
||||
if len(val) == 25 {
|
||||
mins, err := strconv.Atoi(val[22:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val = val[:22] + fmt.Sprintf("%02d%02d", mins/60, mins%60)
|
||||
}
|
||||
t, err := time.Parse("20060102150405.000000-0700", val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Set(reflect.ValueOf(t))
|
||||
}
|
||||
}
|
||||
case bool:
|
||||
switch f.Kind() {
|
||||
case reflect.Bool:
|
||||
f.SetBool(val)
|
||||
default:
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "not a bool",
|
||||
}
|
||||
}
|
||||
case float32:
|
||||
switch f.Kind() {
|
||||
case reflect.Float32:
|
||||
f.SetFloat(float64(val))
|
||||
default:
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: "not a Float32",
|
||||
}
|
||||
}
|
||||
default:
|
||||
if f.Kind() == reflect.Slice {
|
||||
switch f.Type().Elem().Kind() {
|
||||
case reflect.String:
|
||||
safeArray := prop.ToArray()
|
||||
if safeArray != nil {
|
||||
arr := safeArray.ToValueArray()
|
||||
fArr := reflect.MakeSlice(f.Type(), len(arr), len(arr))
|
||||
for i, v := range arr {
|
||||
s := fArr.Index(i)
|
||||
s.SetString(v.(string))
|
||||
}
|
||||
f.Set(fArr)
|
||||
}
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
||||
safeArray := prop.ToArray()
|
||||
if safeArray != nil {
|
||||
arr := safeArray.ToValueArray()
|
||||
fArr := reflect.MakeSlice(f.Type(), len(arr), len(arr))
|
||||
for i, v := range arr {
|
||||
s := fArr.Index(i)
|
||||
s.SetUint(reflect.ValueOf(v).Uint())
|
||||
}
|
||||
f.Set(fArr)
|
||||
}
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||
safeArray := prop.ToArray()
|
||||
if safeArray != nil {
|
||||
arr := safeArray.ToValueArray()
|
||||
fArr := reflect.MakeSlice(f.Type(), len(arr), len(arr))
|
||||
for i, v := range arr {
|
||||
s := fArr.Index(i)
|
||||
s.SetInt(reflect.ValueOf(v).Int())
|
||||
}
|
||||
f.Set(fArr)
|
||||
}
|
||||
default:
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: fmt.Sprintf("unsupported slice type (%T)", val),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
typeof := reflect.TypeOf(val)
|
||||
if typeof == nil && (isPtr || c.NonePtrZero) {
|
||||
if (isPtr && c.PtrNil) || (!isPtr && c.NonePtrZero) {
|
||||
of.Set(reflect.Zero(of.Type()))
|
||||
}
|
||||
break
|
||||
}
|
||||
return &ErrFieldMismatch{
|
||||
StructType: of.Type(),
|
||||
FieldName: n,
|
||||
Reason: fmt.Sprintf("unsupported type (%T)", val),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return errFieldMismatch
|
||||
}
|
||||
|
||||
type multiArgType int
|
||||
|
||||
const (
|
||||
multiArgTypeInvalid multiArgType = iota
|
||||
multiArgTypeStruct
|
||||
multiArgTypeStructPtr
|
||||
)
|
||||
|
||||
// checkMultiArg checks that v has type []S, []*S for some struct type S.
|
||||
//
|
||||
// It returns what category the slice's elements are, and the reflect.Type
|
||||
// that represents S.
|
||||
func checkMultiArg(v reflect.Value) (m multiArgType, elemType reflect.Type) {
|
||||
if v.Kind() != reflect.Slice {
|
||||
return multiArgTypeInvalid, nil
|
||||
}
|
||||
elemType = v.Type().Elem()
|
||||
switch elemType.Kind() {
|
||||
case reflect.Struct:
|
||||
return multiArgTypeStruct, elemType
|
||||
case reflect.Ptr:
|
||||
elemType = elemType.Elem()
|
||||
if elemType.Kind() == reflect.Struct {
|
||||
return multiArgTypeStructPtr, elemType
|
||||
}
|
||||
}
|
||||
return multiArgTypeInvalid, nil
|
||||
}
|
||||
|
||||
func oleInt64(item *ole.IDispatch, prop string) (int64, error) {
|
||||
v, err := oleutil.GetProperty(item, prop)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer v.Clear()
|
||||
|
||||
i := int64(v.Val)
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// CreateQuery returns a WQL query string that queries all columns of src. where
|
||||
// is an optional string that is appended to the query, to be used with WHERE
|
||||
// clauses. In such a case, the "WHERE" string should appear at the beginning.
|
||||
func CreateQuery(src interface{}, where string) string {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("SELECT ")
|
||||
s := reflect.Indirect(reflect.ValueOf(src))
|
||||
t := s.Type()
|
||||
if s.Kind() == reflect.Slice {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() != reflect.Struct {
|
||||
return ""
|
||||
}
|
||||
var fields []string
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
fields = append(fields, t.Field(i).Name)
|
||||
}
|
||||
b.WriteString(strings.Join(fields, ", "))
|
||||
b.WriteString(" FROM ")
|
||||
b.WriteString(t.Name())
|
||||
b.WriteString(" " + where)
|
||||
return b.String()
|
||||
}
|
28
vendor/github.com/adlio/trello/.gitignore
generated
vendored
Normal file
28
vendor/github.com/adlio/trello/.gitignore
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
# 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
|
||||
*.prof
|
||||
|
||||
coverage.sh
|
||||
coverage.out
|
||||
coverage.html
|
33
vendor/github.com/adlio/trello/.travis.yml
generated
vendored
Normal file
33
vendor/github.com/adlio/trello/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
language: go
|
||||
sudo: false
|
||||
go:
|
||||
- 1.7
|
||||
- 1.8
|
||||
- 1.9
|
||||
- '1.10'
|
||||
- 1.11.x
|
||||
- master
|
||||
|
||||
install:
|
||||
- go get github.com/pkg/errors
|
||||
# golint is no longer available for go versions 1.7 and 1.8
|
||||
- |
|
||||
INSTALLED_GO_VERSION=`go version | sed 's/go version go\(.*\) .*/\1/'`
|
||||
echo $INSTALLED_GO_VERSION
|
||||
if [ "$INSTALLED_GO_VERSION" != "1.7" ] && [ "$INSTALLED_GO_VERSION" != "1.8" ]; then
|
||||
go get golang.org/x/lint/golint
|
||||
fi
|
||||
|
||||
before_script:
|
||||
- |
|
||||
which golint;
|
||||
if [ $? == 0 ]; then
|
||||
golint ./...
|
||||
fi
|
||||
go vet
|
||||
|
||||
script:
|
||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
21
vendor/github.com/adlio/trello/LICENSE
generated
vendored
Normal file
21
vendor/github.com/adlio/trello/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2016 Aaron Longwell
|
||||
|
||||
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.
|
250
vendor/github.com/adlio/trello/README.md
generated
vendored
Normal file
250
vendor/github.com/adlio/trello/README.md
generated
vendored
Normal file
@ -0,0 +1,250 @@
|
||||
Go Trello API
|
||||
================
|
||||
|
||||
[](https://www.trello.com)
|
||||
|
||||
[](http://godoc.org/github.com/adlio/trello)
|
||||
[](https://travis-ci.org/adlio/trello)
|
||||
[](https://coveralls.io/github/adlio/trello?branch=master)
|
||||
|
||||
A #golang package to access the [Trello API](https://developers.trello.com/v1.0/reference). Nearly 100% of the
|
||||
read-only surface area of the API is covered, as is creation and modification of Cards.
|
||||
Low-level infrastructure for features to modify Lists and Boards are easy to add... just not
|
||||
done yet.
|
||||
|
||||
Pull requests are welcome for missing features.
|
||||
|
||||
My current development focus is documentation, especially enhancing this README.md with more
|
||||
example use cases.
|
||||
|
||||
## Installation
|
||||
|
||||
The Go Trello API has been Tested compatible with Go 1.7 on up. Its only dependency is
|
||||
the `github.com/pkg/errors` package. It otherwise relies only on the Go standard library.
|
||||
|
||||
```
|
||||
go get github.com/adlio/trello
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
|
||||
All interaction starts with a `trello.Client`. Create one with your appKey and token:
|
||||
|
||||
```Go
|
||||
client := trello.NewClient(appKey, token)
|
||||
```
|
||||
|
||||
All API requests accept a trello.Arguments object. This object is a simple
|
||||
`map[string]string`, converted to query string arguments in the API call.
|
||||
Trello has sane defaults on API calls. We have a `trello.Defaults()` utility function
|
||||
which can be used when you desire the default Trello arguments. Internally,
|
||||
`trello.Defaults()` is an empty map, which translates to an empty query string.
|
||||
|
||||
```Go
|
||||
board, err := client.GetBoard("bOaRdID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Client Longevity
|
||||
|
||||
When getting Lists from Boards or Cards from Lists, the original `trello.Client` pointer
|
||||
is carried along in returned child objects. It's important to realize that this enables
|
||||
you to make additional API calls via functions invoked on the objects you receive:
|
||||
|
||||
```Go
|
||||
client := trello.NewClient(appKey, token)
|
||||
board, err := client.GetBoard("ID", trello.Defaults())
|
||||
|
||||
// GetLists makes an API call to /boards/:id/lists using credentials from `client`
|
||||
lists, err := board.GetLists(trello.Defaults())
|
||||
|
||||
for _, list := range lists {
|
||||
// GetCards makes an API call to /lists/:id/cards using credentials from `client`
|
||||
cards, err := list.GetCards(trello.Defaults())
|
||||
}
|
||||
```
|
||||
|
||||
## Get Trello Boards for a User
|
||||
|
||||
Boards can be retrieved directly by their ID (see example above), or by asking
|
||||
for all boards for a member:
|
||||
|
||||
```Go
|
||||
member, err := client.GetMember("usernameOrId", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
boards, err := member.GetBoards(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Get Trello Lists on a Board
|
||||
|
||||
```Go
|
||||
board, err := client.GetBoard("bOaRdID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
lists, err := board.GetLists(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Get Trello Cards on a Board
|
||||
|
||||
```Go
|
||||
board, err := client.GetBoard("bOaRdID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
cards, err := board.GetCards(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Get Trello Cards on a List
|
||||
|
||||
```Go
|
||||
list, err := client.GetList("lIsTID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
cards, err := list.GetCards(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Creating and deleting a Board
|
||||
|
||||
A board can be created or deleted on the `Board` struct for the user whose credentials are being used.
|
||||
|
||||
```Go
|
||||
board := trello.NewBoard("My bucket list")
|
||||
|
||||
// POST
|
||||
err := client.CreateBoard(&board, trello.Defaults())
|
||||
|
||||
// DELETE
|
||||
err := board.Delete(trello.Defaults())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Creating a Card
|
||||
|
||||
The API provides several mechanisms for creating new cards.
|
||||
|
||||
### Creating Cards from Scratch on the Client
|
||||
|
||||
This approach requires the most input data on the card:
|
||||
|
||||
```Go
|
||||
card := trello.Card{
|
||||
Name: "Card Name",
|
||||
Desc: "Card description",
|
||||
Pos: 12345.678,
|
||||
IDList: "iDOfaLiSt",
|
||||
IDLabels: []string{"labelID1", "labelID2"},
|
||||
}
|
||||
err := client.CreateCard(card, trello.Defaults())
|
||||
```
|
||||
|
||||
|
||||
### Creating Cards On a List
|
||||
|
||||
```Go
|
||||
list, err := client.GetList("lIsTID", trello.Defaults())
|
||||
list.AddCard(&trello.Card{ Name: "Card Name", Desc: "Card description" }, trello.Defaults())
|
||||
```
|
||||
|
||||
### Creating a Card by Copying Another Card
|
||||
|
||||
```Go
|
||||
err := card.CopyToList("listIdNUmber", trello.Defaults())
|
||||
```
|
||||
|
||||
## Get Actions on a Board
|
||||
|
||||
```Go
|
||||
board, err := client.GetBoard("bOaRdID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
actions, err := board.GetActions(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Get Actions on a Card
|
||||
|
||||
```Go
|
||||
card, err := client.GetCard("cArDID", trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
actions, err := card.GetActions(trello.Defaults())
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
```
|
||||
|
||||
## Rearrange Cards Within a List
|
||||
|
||||
```Go
|
||||
err := card.MoveToTopOfList()
|
||||
err = card.MoveToBottomOfList()
|
||||
err = card.SetPos(12345.6789)
|
||||
```
|
||||
|
||||
|
||||
## Moving a Card to Another List
|
||||
|
||||
```Go
|
||||
err := card.MoveToList("listIdNUmber", trello.Defaults())
|
||||
```
|
||||
|
||||
|
||||
## Card Ancestry
|
||||
|
||||
Trello provides ancestry tracking when cards are created as copies of other cards. This package
|
||||
provides functions for working with this data:
|
||||
|
||||
```Go
|
||||
|
||||
// ancestors will hold a slice of *trello.Cards, with the first
|
||||
// being the card's parent, and the last being parent's parent's parent...
|
||||
ancestors, err := card.GetAncestorCards(trello.Defaults())
|
||||
|
||||
// GetOriginatingCard() is an alias for the last element in the slice
|
||||
// of ancestor cards.
|
||||
ultimateParentCard, err := card.GetOriginatingCard(trello.Defaults())
|
||||
|
||||
```
|
||||
|
||||
## Debug Logging
|
||||
|
||||
If you'd like to see all API calls logged, you can attach a `.Logger` (implementing `Debugf(string, ...interface{})`)
|
||||
to your client. The interface for the logger mimics logrus. Example usage:
|
||||
|
||||
```Go
|
||||
logger := logrus.New()
|
||||
logger.SetLevel(logrus.DebugLevel)
|
||||
client := trello.NewClient(appKey, token)
|
||||
client.Logger = logger
|
||||
```
|
5
vendor/github.com/adlio/trello/TODO.txt
generated
vendored
Normal file
5
vendor/github.com/adlio/trello/TODO.txt
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
- Create List
|
||||
- Delete Card
|
||||
- Archive Card
|
||||
- Reorder Cards in List
|
||||
|
62
vendor/github.com/adlio/trello/action-collection.go
generated
vendored
Normal file
62
vendor/github.com/adlio/trello/action-collection.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
package trello
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// ActionCollection is an alias of []*Action, which sorts by the Action's ID.
|
||||
// Which is the same as sorting by the Action's time of occurrence
|
||||
type ActionCollection []*Action
|
||||
|
||||
func (c ActionCollection) Len() int { return len(c) }
|
||||
func (c ActionCollection) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
func (c ActionCollection) Less(i, j int) bool { return c[i].ID < c[j].ID }
|
||||
|
||||
// FirstCardCreateAction returns first card-create action
|
||||
func (c ActionCollection) FirstCardCreateAction() *Action {
|
||||
sort.Sort(c)
|
||||
for _, action := range c {
|
||||
if action.DidCreateCard() {
|
||||
return action
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContainsCardCreation returns true if collection contains a card-create action
|
||||
func (c ActionCollection) ContainsCardCreation() bool {
|
||||
return c.FirstCardCreateAction() != nil
|
||||
}
|
||||
|
||||
// FilterToCardCreationActions returns this collection's card-create actions
|
||||
func (c ActionCollection) FilterToCardCreationActions() ActionCollection {
|
||||
newSlice := make(ActionCollection, 0, len(c))
|
||||
for _, action := range c {
|
||||
if action.DidCreateCard() {
|
||||
newSlice = append(newSlice, action)
|
||||
}
|
||||
}
|
||||
return newSlice
|
||||
}
|
||||
|
||||
// FilterToListChangeActions returns card-change-list actions
|
||||
func (c ActionCollection) FilterToListChangeActions() ActionCollection {
|
||||
newSlice := make(ActionCollection, 0, len(c))
|
||||
for _, action := range c {
|
||||
if action.DidChangeListForCard() {
|
||||
newSlice = append(newSlice, action)
|
||||
}
|
||||
}
|
||||
return newSlice
|
||||
}
|
||||
|
||||
// FilterToCardMembershipChangeActions returns the collection's card-change, archive and unarchive actions
|
||||
func (c ActionCollection) FilterToCardMembershipChangeActions() ActionCollection {
|
||||
newSlice := make(ActionCollection, 0, len(c))
|
||||
for _, action := range c {
|
||||
if action.DidChangeCardMembership() || action.DidArchiveCard() || action.DidUnarchiveCard() {
|
||||
newSlice = append(newSlice, action)
|
||||
}
|
||||
}
|
||||
return newSlice
|
||||
}
|
169
vendor/github.com/adlio/trello/action.go
generated
vendored
Normal file
169
vendor/github.com/adlio/trello/action.go
generated
vendored
Normal file
@ -0,0 +1,169 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Action represents Trello API actions
|
||||
// Actions are immutable event traces generated whenever an action occurs in Trello.
|
||||
// See https://developers.trello.com/reference/#actions.
|
||||
type Action struct {
|
||||
ID string `json:"id"`
|
||||
IDMemberCreator string `json:"idMemberCreator"`
|
||||
Type string `json:"type"`
|
||||
Date time.Time `json:"date"`
|
||||
Data *ActionData `json:"data,omitempty"`
|
||||
MemberCreator *Member `json:"memberCreator,omitempty"`
|
||||
Member *Member `json:"member,omitempty"`
|
||||
}
|
||||
|
||||
// ActionData represent the nested data of actions
|
||||
type ActionData struct {
|
||||
Text string `json:"text,omitempty"`
|
||||
List *List `json:"list,omitempty"`
|
||||
Card *ActionDataCard `json:"card,omitempty"`
|
||||
CardSource *ActionDataCard `json:"cardSource,omitempty"`
|
||||
Board *Board `json:"board,omitempty"`
|
||||
Old *ActionDataCard `json:"old,omitempty"`
|
||||
ListBefore *List `json:"listBefore,omitempty"`
|
||||
ListAfter *List `json:"listAfter,omitempty"`
|
||||
DateLastEdited time.Time `json:"dateLastEdited"`
|
||||
|
||||
CheckItem *CheckItem `json:"checkItem"`
|
||||
Checklist *Checklist `json:"checklist"`
|
||||
}
|
||||
|
||||
// ActionDataCard represent the nested 'card' data attribute of actions
|
||||
type ActionDataCard struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IDShort int `json:"idShort"`
|
||||
ShortLink string `json:"shortLink"`
|
||||
Pos float64 `json:"pos"`
|
||||
Closed bool `json:"closed"`
|
||||
}
|
||||
|
||||
// GetActions make a GET call for a board's actions
|
||||
func (b *Board) GetActions(args Arguments) (actions ActionCollection, err error) {
|
||||
path := fmt.Sprintf("boards/%s/actions", b.ID)
|
||||
err = b.client.Get(path, args, &actions)
|
||||
return
|
||||
}
|
||||
|
||||
// GetActions makes a GET call for a list's actions
|
||||
func (l *List) GetActions(args Arguments) (actions ActionCollection, err error) {
|
||||
path := fmt.Sprintf("lists/%s/actions", l.ID)
|
||||
err = l.client.Get(path, args, &actions)
|
||||
return
|
||||
}
|
||||
|
||||
// GetActions makes a GET for a card's actions
|
||||
func (c *Card) GetActions(args Arguments) (actions ActionCollection, err error) {
|
||||
path := fmt.Sprintf("cards/%s/actions", c.ID)
|
||||
err = c.client.Get(path, args, &actions)
|
||||
return
|
||||
}
|
||||
|
||||
// GetListChangeActions retrieves a slice of Actions which resulted in changes
|
||||
// to the card's active List. This includes the createCard and copyCard action (which
|
||||
// place the card in its first list), and the updateCard:closed action (which remove it
|
||||
// from its last list).
|
||||
//
|
||||
// This function is just an alias for:
|
||||
// card.GetActions(Arguments{"filter": "createCard,copyCard,updateCard:idList,updateCard:closed", "limit": "1000"})
|
||||
//
|
||||
func (c *Card) GetListChangeActions() (actions ActionCollection, err error) {
|
||||
return c.GetActions(Arguments{"filter": "createCard,copyCard,updateCard:idList,updateCard:closed"})
|
||||
}
|
||||
|
||||
// GetMembershipChangeActions makes a GET call for a card's membership-change actions
|
||||
func (c *Card) GetMembershipChangeActions() (actions ActionCollection, err error) {
|
||||
// We include updateCard:closed as if the member is implicitly removed from the card when it's closed.
|
||||
// This allows us to "close out" the duration length.
|
||||
return c.GetActions(Arguments{"filter": "addMemberToCard,removeMemberFromCard,updateCard:closed"})
|
||||
}
|
||||
|
||||
// DidCreateCard returns true if this action created a card, false otherwise.
|
||||
func (a *Action) DidCreateCard() bool {
|
||||
switch a.Type {
|
||||
case "createCard", "emailCard", "copyCard", "convertToCardFromCheckItem":
|
||||
return true
|
||||
case "moveCardToBoard":
|
||||
return true // Unsure about this one
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// DidArchiveCard returns true if the card was updated
|
||||
func (a *Action) DidArchiveCard() bool {
|
||||
return (a.Type == "updateCard") && a.Data != nil && a.Data.Card != nil && a.Data.Card.Closed
|
||||
}
|
||||
|
||||
// DidUnarchiveCard returns true if the card was unarchived
|
||||
func (a *Action) DidUnarchiveCard() bool {
|
||||
return (a.Type == "updateCard") && a.Data != nil && a.Data.Old != nil && a.Data.Old.Closed
|
||||
}
|
||||
|
||||
// DidChangeListForCard returns true if this action created the card (in which case it
|
||||
// caused it to enter its first list), archived the card (in which case it caused it to
|
||||
// leave its last List), or was an updateCard action involving a change to the list. This
|
||||
// is supporting functionality for ListDuration.
|
||||
//
|
||||
func (a *Action) DidChangeListForCard() bool {
|
||||
if a.DidCreateCard() {
|
||||
return true
|
||||
}
|
||||
if a.DidArchiveCard() {
|
||||
return true
|
||||
}
|
||||
if a.DidUnarchiveCard() {
|
||||
return true
|
||||
}
|
||||
if a.Type == "updateCard" {
|
||||
if a.Data != nil && a.Data.ListAfter != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DidChangeCardMembership returns true if card's membership was changed
|
||||
func (a *Action) DidChangeCardMembership() bool {
|
||||
switch a.Type {
|
||||
case "addMemberToCard":
|
||||
return true
|
||||
case "removeMemberFromCard":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ListAfterAction calculates which List the card ended up in after this action
|
||||
// completed. Returns nil when the action resulted in the card being archived (in
|
||||
// which case we consider it to not be in a list anymore), or when the action isn't
|
||||
// related to a list at all (in which case this is a nonsensical question to ask).
|
||||
//
|
||||
func ListAfterAction(a *Action) *List {
|
||||
switch a.Type {
|
||||
case "createCard", "copyCard", "emailCard", "convertToCardFromCheckItem":
|
||||
return a.Data.List
|
||||
case "updateCard":
|
||||
if a.DidArchiveCard() {
|
||||
return nil
|
||||
} else if a.DidUnarchiveCard() {
|
||||
return a.Data.List
|
||||
}
|
||||
if a.Data.ListAfter != nil {
|
||||
return a.Data.ListAfter
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
27
vendor/github.com/adlio/trello/arguments.go
generated
vendored
Normal file
27
vendor/github.com/adlio/trello/arguments.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Arguments are used for passing URL parameters to the client for making API calls.
|
||||
type Arguments map[string]string
|
||||
|
||||
// Defaults is a constructor for default Arguments.
|
||||
func Defaults() Arguments {
|
||||
return make(Arguments)
|
||||
}
|
||||
|
||||
// ToURLValues returns the argument's URL value representation.
|
||||
func (args Arguments) ToURLValues() url.Values {
|
||||
v := url.Values{}
|
||||
for key, value := range args {
|
||||
v.Set(key, value)
|
||||
}
|
||||
return v
|
||||
}
|
32
vendor/github.com/adlio/trello/attachment.go
generated
vendored
Normal file
32
vendor/github.com/adlio/trello/attachment.go
generated
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
// Attachment represent the attachments of cards. This is a nested resource of Card.
|
||||
// https://developers.trello.com/reference/#attachments
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Pos float32 `json:"pos"`
|
||||
Bytes int `json:"int"`
|
||||
Date string `json:"date"`
|
||||
EdgeColor string `json:"edgeColor"`
|
||||
IDMember string `json:"idMember"`
|
||||
IsUpload bool `json:"isUpload"`
|
||||
MimeType string `json:"mimeType"`
|
||||
Previews []AttachmentPreview `json:"previews"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// AttachmentPreview is a nested attribute of Attachment.
|
||||
type AttachmentPreview struct {
|
||||
ID string `json:"_id"`
|
||||
URL string `json:"url"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Bytes int `json:"bytes"`
|
||||
Scaled bool `json:"scaled"`
|
||||
}
|
167
vendor/github.com/adlio/trello/board.go
generated
vendored
Normal file
167
vendor/github.com/adlio/trello/board.go
generated
vendored
Normal file
@ -0,0 +1,167 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Board represents a Trello Board.
|
||||
// https://developers.trello.com/reference/#boardsid
|
||||
type Board struct {
|
||||
client *Client
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Desc string `json:"desc"`
|
||||
Closed bool `json:"closed"`
|
||||
IDOrganization string `json:"idOrganization"`
|
||||
Pinned bool `json:"pinned"`
|
||||
URL string `json:"url"`
|
||||
ShortURL string `json:"shortUrl"`
|
||||
Prefs struct {
|
||||
PermissionLevel string `json:"permissionLevel"`
|
||||
Voting string `json:"voting"`
|
||||
Comments string `json:"comments"`
|
||||
Invitations string `json:"invitations"`
|
||||
SelfJoin bool `json:"selfjoin"`
|
||||
CardCovers bool `json:"cardCovers"`
|
||||
CardAging string `json:"cardAging"`
|
||||
CalendarFeedEnabled bool `json:"calendarFeedEnabled"`
|
||||
Background string `json:"background"`
|
||||
BackgroundColor string `json:"backgroundColor"`
|
||||
BackgroundImage string `json:"backgroundImage"`
|
||||
BackgroundImageScaled []BackgroundImage `json:"backgroundImageScaled"`
|
||||
BackgroundTile bool `json:"backgroundTile"`
|
||||
BackgroundBrightness string `json:"backgroundBrightness"`
|
||||
CanBePublic bool `json:"canBePublic"`
|
||||
CanBeOrg bool `json:"canBeOrg"`
|
||||
CanBePrivate bool `json:"canBePrivate"`
|
||||
CanInvite bool `json:"canInvite"`
|
||||
} `json:"prefs"`
|
||||
LabelNames struct {
|
||||
Black string `json:"black,omitempty"`
|
||||
Blue string `json:"blue,omitempty"`
|
||||
Green string `json:"green,omitempty"`
|
||||
Lime string `json:"lime,omitempty"`
|
||||
Orange string `json:"orange,omitempty"`
|
||||
Pink string `json:"pink,omitempty"`
|
||||
Purple string `json:"purple,omitempty"`
|
||||
Red string `json:"red,omitempty"`
|
||||
Sky string `json:"sky,omitempty"`
|
||||
Yellow string `json:"yellow,omitempty"`
|
||||
} `json:"labelNames"`
|
||||
Lists []*List `json:"lists"`
|
||||
Actions []*Action `json:"actions"`
|
||||
Organization Organization `json:"organization"`
|
||||
}
|
||||
|
||||
// NewBoard is a constructor that sets the default values
|
||||
// for Prefs.SelfJoin and Prefs.CardCovers also set by the API.
|
||||
func NewBoard(name string) Board {
|
||||
b := Board{Name: name}
|
||||
|
||||
// default values in line with API POST
|
||||
b.Prefs.SelfJoin = true
|
||||
b.Prefs.CardCovers = true
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// BackgroundImage is a nested resource of Board.
|
||||
type BackgroundImage struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// CreatedAt returns a board's created-at attribute as time.Time.
|
||||
func (b *Board) CreatedAt() time.Time {
|
||||
t, _ := IDToTime(b.ID)
|
||||
return t
|
||||
}
|
||||
|
||||
// CreateBoard creates a board remote.
|
||||
// Attribute currently supported as exra argument: powerUps.
|
||||
// Attributes currently known to be unsupported: idBoardSource, keepFromSource.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference/#boardsid
|
||||
func (c *Client) CreateBoard(board *Board, extraArgs Arguments) error {
|
||||
path := "boards"
|
||||
args := Arguments{
|
||||
"desc": board.Desc,
|
||||
"name": board.Name,
|
||||
"prefs_selfJoin": fmt.Sprintf("%t", board.Prefs.SelfJoin),
|
||||
"prefs_cardCovers": fmt.Sprintf("%t", board.Prefs.CardCovers),
|
||||
"idOrganization": board.IDOrganization,
|
||||
}
|
||||
|
||||
if board.Prefs.Voting != "" {
|
||||
args["prefs_voting"] = board.Prefs.Voting
|
||||
}
|
||||
if board.Prefs.PermissionLevel != "" {
|
||||
args["prefs_permissionLevel"] = board.Prefs.PermissionLevel
|
||||
}
|
||||
if board.Prefs.Comments != "" {
|
||||
args["prefs_comments"] = board.Prefs.Comments
|
||||
}
|
||||
if board.Prefs.Invitations != "" {
|
||||
args["prefs_invitations"] = board.Prefs.Invitations
|
||||
}
|
||||
if board.Prefs.Background != "" {
|
||||
args["prefs_background"] = board.Prefs.Background
|
||||
}
|
||||
if board.Prefs.CardAging != "" {
|
||||
args["prefs_cardAging"] = board.Prefs.CardAging
|
||||
}
|
||||
|
||||
// Expects one of "all", "calendar", "cardAging", "recap", or "voting".
|
||||
if powerUps, ok := extraArgs["powerUps"]; ok {
|
||||
args["powerUps"] = powerUps
|
||||
}
|
||||
|
||||
err := c.Post(path, args, &board)
|
||||
if err == nil {
|
||||
board.client = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete makes a DELETE call for the receiver Board.
|
||||
func (b *Board) Delete(extraArgs Arguments) error {
|
||||
path := fmt.Sprintf("boards/%s", b.ID)
|
||||
return b.client.Delete(path, Arguments{}, b)
|
||||
}
|
||||
|
||||
// GetBoard retrieves a Trello board by its ID.
|
||||
func (c *Client) GetBoard(boardID string, args Arguments) (board *Board, err error) {
|
||||
path := fmt.Sprintf("boards/%s", boardID)
|
||||
err = c.Get(path, args, &board)
|
||||
if board != nil {
|
||||
board.client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetMyBoards returns a slice of all boards associated with the credentials set on the client.
|
||||
func (c *Client) GetMyBoards(args Arguments) (boards []*Board, err error) {
|
||||
path := "members/me/boards"
|
||||
err = c.Get(path, args, &boards)
|
||||
for i := range boards {
|
||||
boards[i].client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetBoards returns a slice of all public boards of the receiver Member.
|
||||
func (m *Member) GetBoards(args Arguments) (boards []*Board, err error) {
|
||||
path := fmt.Sprintf("members/%s/boards", m.ID)
|
||||
err = m.client.Get(path, args, &boards)
|
||||
for i := range boards {
|
||||
boards[i].client = m.client
|
||||
}
|
||||
return
|
||||
}
|
487
vendor/github.com/adlio/trello/card.go
generated
vendored
Normal file
487
vendor/github.com/adlio/trello/card.go
generated
vendored
Normal file
@ -0,0 +1,487 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Card represents the card resource.
|
||||
// https://developers.trello.com/reference/#card-object
|
||||
type Card struct {
|
||||
client *Client
|
||||
|
||||
// Key metadata
|
||||
ID string `json:"id"`
|
||||
IDShort int `json:"idShort"`
|
||||
Name string `json:"name"`
|
||||
Pos float64 `json:"pos"`
|
||||
Email string `json:"email"`
|
||||
ShortLink string `json:"shortLink"`
|
||||
ShortURL string `json:"shortUrl"`
|
||||
URL string `json:"url"`
|
||||
Desc string `json:"desc"`
|
||||
Due *time.Time `json:"due"`
|
||||
DueComplete bool `json:"dueComplete"`
|
||||
Closed bool `json:"closed"`
|
||||
Subscribed bool `json:"subscribed"`
|
||||
DateLastActivity *time.Time `json:"dateLastActivity"`
|
||||
|
||||
// Board
|
||||
Board *Board
|
||||
IDBoard string `json:"idBoard"`
|
||||
|
||||
// List
|
||||
List *List
|
||||
IDList string `json:"idList"`
|
||||
|
||||
// Badges
|
||||
Badges struct {
|
||||
Votes int `json:"votes"`
|
||||
ViewingMemberVoted bool `json:"viewingMemberVoted"`
|
||||
Subscribed bool `json:"subscribed"`
|
||||
Fogbugz string `json:"fogbugz,omitempty"`
|
||||
CheckItems int `json:"checkItems"`
|
||||
CheckItemsChecked int `json:"checkItemsChecked"`
|
||||
Comments int `json:"comments"`
|
||||
Attachments int `json:"attachments"`
|
||||
Description bool `json:"description"`
|
||||
Due *time.Time `json:"due,omitempty"`
|
||||
} `json:"badges"`
|
||||
|
||||
// Actions
|
||||
Actions ActionCollection `json:"actions,omitempty"`
|
||||
|
||||
// Checklists
|
||||
IDCheckLists []string `json:"idCheckLists"`
|
||||
Checklists []*Checklist `json:"checklists,omitempty"`
|
||||
CheckItemStates []*CheckItemState `json:"checkItemStates,omitempty"`
|
||||
|
||||
// Members
|
||||
IDMembers []string `json:"idMembers,omitempty"`
|
||||
IDMembersVoted []string `json:"idMembersVoted,omitempty"`
|
||||
Members []*Member `json:"members,omitempty"`
|
||||
|
||||
// Attachments
|
||||
IDAttachmentCover string `json:"idAttachmentCover"`
|
||||
ManualCoverAttachment bool `json:"manualCoverAttachment"`
|
||||
Attachments []*Attachment `json:"attachments,omitempty"`
|
||||
|
||||
// Labels
|
||||
IDLabels []string `json:"idLabels,omitempty"`
|
||||
Labels []*Label `json:"labels,omitempty"`
|
||||
|
||||
// Custom Fields
|
||||
CustomFieldItems []*CustomFieldItem `json:"customFieldItems,omitempty"`
|
||||
|
||||
customFieldMap *map[string]interface{}
|
||||
}
|
||||
|
||||
// CreatedAt returns the receiver card's created-at attribute as time.Time.
|
||||
func (c *Card) CreatedAt() time.Time {
|
||||
t, _ := IDToTime(c.ID)
|
||||
return t
|
||||
}
|
||||
|
||||
// CustomFields returns the card's custom fields.
|
||||
func (c *Card) CustomFields(boardCustomFields []*CustomField) map[string]interface{} {
|
||||
|
||||
cfm := c.customFieldMap
|
||||
|
||||
if cfm == nil {
|
||||
cfm = &(map[string]interface{}{})
|
||||
|
||||
// bcfOptionNames[CustomField ID] = Custom Field Name
|
||||
bcfOptionNames := map[string]string{}
|
||||
|
||||
// bcfOptionsMap[CustomField ID][ID of the option] = Value of the option
|
||||
bcfOptionsMap := map[string]map[string]interface{}{}
|
||||
|
||||
for _, bcf := range boardCustomFields {
|
||||
bcfOptionNames[bcf.ID] = bcf.Name
|
||||
for _, cf := range bcf.Options {
|
||||
// create 2nd level map when not available yet
|
||||
map2, ok := bcfOptionsMap[cf.IDCustomField]
|
||||
if !ok {
|
||||
map2 = map[string]interface{}{}
|
||||
bcfOptionsMap[bcf.ID] = map2
|
||||
}
|
||||
|
||||
bcfOptionsMap[bcf.ID][cf.ID] = cf.Value.Text
|
||||
}
|
||||
}
|
||||
|
||||
for _, cf := range c.CustomFieldItems {
|
||||
name := bcfOptionNames[cf.IDCustomField]
|
||||
|
||||
// create 2nd level map when not available yet
|
||||
map2, ok := bcfOptionsMap[cf.IDCustomField]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
value, ok := map2[cf.IDValue]
|
||||
|
||||
if ok {
|
||||
(*cfm)[name] = value
|
||||
}
|
||||
}
|
||||
c.customFieldMap = cfm
|
||||
}
|
||||
return *cfm
|
||||
}
|
||||
|
||||
// MoveToList moves a card to a list given by listID.
|
||||
func (c *Card) MoveToList(listID string, args Arguments) error {
|
||||
path := fmt.Sprintf("cards/%s", c.ID)
|
||||
args["idList"] = listID
|
||||
return c.client.Put(path, args, &c)
|
||||
}
|
||||
|
||||
// SetPos sets a card's new position.
|
||||
func (c *Card) SetPos(newPos float64) error {
|
||||
path := fmt.Sprintf("cards/%s", c.ID)
|
||||
return c.client.Put(path, Arguments{"pos": fmt.Sprintf("%f", newPos)}, c)
|
||||
}
|
||||
|
||||
// RemoveMember receives the id of a member and removes the corresponding member from the card.
|
||||
func (c *Card) RemoveMember(memberID string) error {
|
||||
path := fmt.Sprintf("cards/%s/idMembers/%s", c.ID, memberID)
|
||||
return c.client.Delete(path, Defaults(), nil)
|
||||
}
|
||||
|
||||
// AddMemberID receives a member id and adds the corresponding member to the card.
|
||||
// Returns a list of the card's members or an error.
|
||||
func (c *Card) AddMemberID(memberID string) (member []*Member, err error) {
|
||||
path := fmt.Sprintf("cards/%s/idMembers", c.ID)
|
||||
err = c.client.Post(path, Arguments{"value": memberID}, &member)
|
||||
return member, err
|
||||
}
|
||||
|
||||
// RemoveIDLabel removes a label id from the card.
|
||||
func (c *Card) RemoveIDLabel(labelID string, label *Label) error {
|
||||
path := fmt.Sprintf("cards/%s/idLabels/%s", c.ID, labelID)
|
||||
return c.client.Delete(path, Defaults(), label)
|
||||
|
||||
}
|
||||
|
||||
// AddIDLabel receives a label id and adds the corresponding label or returns an error.
|
||||
func (c *Card) AddIDLabel(labelID string) error {
|
||||
path := fmt.Sprintf("cards/%s/idLabels", c.ID)
|
||||
err := c.client.Post(path, Arguments{"value": labelID}, &c.IDLabels)
|
||||
return err
|
||||
}
|
||||
|
||||
// MoveToTopOfList moves the card to the top of it's list.
|
||||
func (c *Card) MoveToTopOfList() error {
|
||||
path := fmt.Sprintf("cards/%s", c.ID)
|
||||
return c.client.Put(path, Arguments{"pos": "top"}, c)
|
||||
}
|
||||
|
||||
// MoveToBottomOfList moves the card to the bottom of its list.
|
||||
func (c *Card) MoveToBottomOfList() error {
|
||||
path := fmt.Sprintf("cards/%s", c.ID)
|
||||
return c.client.Put(path, Arguments{"pos": "bottom"}, c)
|
||||
}
|
||||
|
||||
// Update UPDATEs the card's attributes.
|
||||
func (c *Card) Update(args Arguments) error {
|
||||
path := fmt.Sprintf("cards/%s", c.ID)
|
||||
return c.client.Put(path, args, c)
|
||||
}
|
||||
|
||||
// CreateCard takes a Card and Arguments and POSTs the card.
|
||||
func (c *Client) CreateCard(card *Card, extraArgs Arguments) error {
|
||||
path := "cards"
|
||||
args := Arguments{
|
||||
"name": card.Name,
|
||||
"desc": card.Desc,
|
||||
"pos": strconv.FormatFloat(card.Pos, 'g', -1, 64),
|
||||
"idList": card.IDList,
|
||||
"idMembers": strings.Join(card.IDMembers, ","),
|
||||
"idLabels": strings.Join(card.IDLabels, ","),
|
||||
}
|
||||
if card.Due != nil {
|
||||
args["due"] = card.Due.Format(time.RFC3339)
|
||||
}
|
||||
// Allow overriding the creation position with 'top' or 'botttom'
|
||||
if pos, ok := extraArgs["pos"]; ok {
|
||||
args["pos"] = pos
|
||||
}
|
||||
err := c.Post(path, args, &card)
|
||||
if err == nil {
|
||||
card.client = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// AddCard takes a Card and Arguments and adds the card to the receiver list.
|
||||
func (l *List) AddCard(card *Card, extraArgs Arguments) error {
|
||||
path := fmt.Sprintf("lists/%s/cards", l.ID)
|
||||
args := Arguments{
|
||||
"name": card.Name,
|
||||
"desc": card.Desc,
|
||||
"idMembers": strings.Join(card.IDMembers, ","),
|
||||
"idLabels": strings.Join(card.IDLabels, ","),
|
||||
}
|
||||
if card.Due != nil {
|
||||
args["due"] = card.Due.Format(time.RFC3339)
|
||||
}
|
||||
// Allow overwriting the creation position with 'top' or 'bottom'
|
||||
if pos, ok := extraArgs["pos"]; ok {
|
||||
args["pos"] = pos
|
||||
}
|
||||
err := l.client.Post(path, args, &card)
|
||||
if err == nil {
|
||||
card.client = l.client
|
||||
} else {
|
||||
err = errors.Wrapf(err, "Error adding card to list %s", l.ID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// CopyToList takes a list id and Arguments and returns the matching Card.
|
||||
// The following Arguments are supported.
|
||||
//
|
||||
// Arguments["keepFromSource"] = "all"
|
||||
// Arguments["keepFromSource"] = "none"
|
||||
// Arguments["keepFromSource"] = "attachments,checklists,comments"
|
||||
func (c *Card) CopyToList(listID string, args Arguments) (*Card, error) {
|
||||
path := "cards"
|
||||
args["idList"] = listID
|
||||
args["idCardSource"] = c.ID
|
||||
newCard := Card{}
|
||||
err := c.client.Post(path, args, &newCard)
|
||||
if err == nil {
|
||||
newCard.client = c.client
|
||||
} else {
|
||||
err = errors.Wrapf(err, "Error copying card '%s' to list '%s'.", c.ID, listID)
|
||||
}
|
||||
return &newCard, err
|
||||
}
|
||||
|
||||
// AddComment takes a comment string and Arguments and adds the comment to the card.
|
||||
func (c *Card) AddComment(comment string, args Arguments) (*Action, error) {
|
||||
path := fmt.Sprintf("cards/%s/actions/comments", c.ID)
|
||||
args["text"] = comment
|
||||
action := Action{}
|
||||
err := c.client.Post(path, args, &action)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "Error commenting on card %s", c.ID)
|
||||
}
|
||||
return &action, err
|
||||
}
|
||||
|
||||
// AddURLAttachment takes an Attachment and adds it to the card.
|
||||
func (c *Card) AddURLAttachment(attachment *Attachment) error {
|
||||
path := fmt.Sprintf("cards/%s/attachments", c.ID)
|
||||
args := Arguments{
|
||||
"url": attachment.URL,
|
||||
"name": attachment.Name,
|
||||
}
|
||||
err := c.client.Post(path, args, &attachment)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "Error adding attachment to card %s", c.ID)
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
// GetParentCard retrieves the originating Card if the Card was created
|
||||
// from a copy of another Card. Returns an error only when a low-level failure occurred.
|
||||
// If this Card has no parent, a nil card and nil error are returned. In other words, the
|
||||
// non-existence of a parent is not treated as an error.
|
||||
func (c *Card) GetParentCard(args Arguments) (*Card, error) {
|
||||
|
||||
// Hopefully the card came pre-loaded with Actions including the card creation
|
||||
action := c.Actions.FirstCardCreateAction()
|
||||
|
||||
if action == nil {
|
||||
// No luck. Go get copyCard actions for this card.
|
||||
c.client.log("Creation action wasn't supplied before GetParentCard() on '%s'. Getting copyCard actions.", c.ID)
|
||||
actions, err := c.GetActions(Arguments{"filter": "copyCard"})
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetParentCard() failed to GetActions() for card '%s'", c.ID)
|
||||
return nil, err
|
||||
}
|
||||
action = actions.FirstCardCreateAction()
|
||||
}
|
||||
|
||||
if action != nil && action.Data != nil && action.Data.CardSource != nil {
|
||||
card, err := c.client.GetCard(action.Data.CardSource.ID, args)
|
||||
return card, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetAncestorCards takes Arguments, GETs the card's ancestors and returns them as a slice.
|
||||
func (c *Card) GetAncestorCards(args Arguments) (ancestors []*Card, err error) {
|
||||
|
||||
// Get the first parent
|
||||
parent, err := c.GetParentCard(args)
|
||||
if IsNotFound(err) || IsPermissionDenied(err) {
|
||||
c.client.log("[trello] Can't get details about the parent of card '%s' due to lack of permissions or card deleted.", c.ID)
|
||||
return ancestors, nil
|
||||
}
|
||||
|
||||
for parent != nil {
|
||||
ancestors = append(ancestors, parent)
|
||||
parent, err = parent.GetParentCard(args)
|
||||
if IsNotFound(err) || IsPermissionDenied(err) {
|
||||
c.client.log("[trello] Can't get details about the parent of card '%s' due to lack of permissions or card deleted.", c.ID)
|
||||
return ancestors, nil
|
||||
} else if err != nil {
|
||||
return ancestors, err
|
||||
}
|
||||
}
|
||||
|
||||
return ancestors, err
|
||||
}
|
||||
|
||||
// GetOriginatingCard takes Arguments, GETs ancestors and returns most recent ancestor card of the Card.
|
||||
func (c *Card) GetOriginatingCard(args Arguments) (*Card, error) {
|
||||
ancestors, err := c.GetAncestorCards(args)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
if len(ancestors) > 0 {
|
||||
return ancestors[len(ancestors)-1], nil
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// CreatorMember returns the member of the card who created it or and error.
|
||||
// The creator is the member who is associated with the card's first action.
|
||||
func (c *Card) CreatorMember() (*Member, error) {
|
||||
var actions ActionCollection
|
||||
var err error
|
||||
|
||||
if len(c.Actions) == 0 {
|
||||
c.Actions, err = c.GetActions(Arguments{"filter": "all", "limit": "1000", "memberCreator_fields": "all"})
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetActions() call failed.")
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
actions = c.Actions.FilterToCardCreationActions()
|
||||
|
||||
if len(actions) > 0 {
|
||||
return actions[0].MemberCreator, nil
|
||||
}
|
||||
return nil, errors.Errorf("No card creation actions on Card %s with a .MemberCreator", c.ID)
|
||||
}
|
||||
|
||||
// CreatorMemberID returns as string the id of the member who created the card or an error.
|
||||
// The creator is the member who is associated with the card's first action.
|
||||
func (c *Card) CreatorMemberID() (string, error) {
|
||||
|
||||
var actions ActionCollection
|
||||
var err error
|
||||
|
||||
if len(c.Actions) == 0 {
|
||||
c.client.log("[trello] CreatorMemberID() called on card '%s' without any Card.Actions. Fetching fresh.", c.ID)
|
||||
c.Actions, err = c.GetActions(Defaults())
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetActions() call failed.")
|
||||
}
|
||||
}
|
||||
actions = c.Actions.FilterToCardCreationActions()
|
||||
|
||||
if len(actions) > 0 {
|
||||
if actions[0].IDMemberCreator != "" {
|
||||
return actions[0].IDMemberCreator, err
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.Wrapf(err, "No Actions on card '%s' could be used to find its creator.", c.ID)
|
||||
}
|
||||
|
||||
// ContainsCopyOfCard accepts a card id and Arguments and returns true
|
||||
// if the receiver Board contains a Card with the id.
|
||||
func (b *Board) ContainsCopyOfCard(cardID string, args Arguments) (bool, error) {
|
||||
args["filter"] = "copyCard"
|
||||
actions, err := b.GetActions(args)
|
||||
if err != nil {
|
||||
err := errors.Wrapf(err, "GetCards() failed inside ContainsCopyOf() for board '%s' and card '%s'.", b.ID, cardID)
|
||||
return false, err
|
||||
}
|
||||
for _, action := range actions {
|
||||
if action.Data != nil && action.Data.CardSource != nil && action.Data.CardSource.ID == cardID {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// GetCard receives a card id and Arguments and returns the card if found
|
||||
// with the credentials given for the receiver Client. Returns an error
|
||||
// otherwise.
|
||||
func (c *Client) GetCard(cardID string, args Arguments) (card *Card, err error) {
|
||||
path := fmt.Sprintf("cards/%s", cardID)
|
||||
err = c.Get(path, args, &card)
|
||||
if card != nil {
|
||||
card.client = c
|
||||
}
|
||||
return card, err
|
||||
}
|
||||
|
||||
// GetCards takes Arguments and retrieves all Cards on a Board as slice or returns error.
|
||||
func (b *Board) GetCards(args Arguments) (cards []*Card, err error) {
|
||||
path := fmt.Sprintf("boards/%s/cards", b.ID)
|
||||
|
||||
err = b.client.Get(path, args, &cards)
|
||||
|
||||
// Naive implementation would return here. To make sure we get all
|
||||
// cards, we begin
|
||||
if len(cards) > 0 {
|
||||
moreCards := true
|
||||
for moreCards == true {
|
||||
nextCardBatch := make([]*Card, 0)
|
||||
args["before"] = earliestCardID(cards)
|
||||
err = b.client.Get(path, args, &nextCardBatch)
|
||||
if len(nextCardBatch) > 0 {
|
||||
cards = append(cards, nextCardBatch...)
|
||||
} else {
|
||||
moreCards = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i := range cards {
|
||||
cards[i].client = b.client
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetCards retrieves all Cards in a List or an error if something goes wrong.
|
||||
func (l *List) GetCards(args Arguments) (cards []*Card, err error) {
|
||||
path := fmt.Sprintf("lists/%s/cards", l.ID)
|
||||
err = l.client.Get(path, args, &cards)
|
||||
for i := range cards {
|
||||
cards[i].client = l.client
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func earliestCardID(cards []*Card) string {
|
||||
if len(cards) == 0 {
|
||||
return ""
|
||||
}
|
||||
earliest := cards[0].ID
|
||||
for _, card := range cards {
|
||||
if card.ID < earliest {
|
||||
earliest = card.ID
|
||||
}
|
||||
}
|
||||
return earliest
|
||||
}
|
97
vendor/github.com/adlio/trello/checklist.go
generated
vendored
Normal file
97
vendor/github.com/adlio/trello/checklist.go
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
// Checklist represents Trello card's checklists.
|
||||
// A card can have one zero or more checklists.
|
||||
// https://developers.trello.com/reference/#checklist-object
|
||||
type Checklist struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IDBoard string `json:"idBoard,omitempty"`
|
||||
IDCard string `json:"idCard,omitempty"`
|
||||
Pos float64 `json:"pos,omitempty"`
|
||||
CheckItems []CheckItem `json:"checkItems,omitempty"`
|
||||
client *Client
|
||||
}
|
||||
|
||||
// CheckItem is a nested resource representing an item in Checklist.
|
||||
type CheckItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
IDChecklist string `json:"idChecklist,omitempty"`
|
||||
Pos float64 `json:"pos,omitempty"`
|
||||
}
|
||||
|
||||
// CheckItemState represents a CheckItem when it appears in CheckItemStates on a Card.
|
||||
type CheckItemState struct {
|
||||
IDCheckItem string `json:"idCheckItem"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
// CreateChecklist creates a checklist.
|
||||
// Attribute currently supported as extra argument: pos.
|
||||
// Attributes currently known to be unsupported: idChecklistSource.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference#cardsidchecklists-1
|
||||
func (c *Client) CreateChecklist(card *Card, name string, extraArgs Arguments) (checklist *Checklist, err error) {
|
||||
path := "cards/" + card.ID + "/checklists"
|
||||
args := Arguments{
|
||||
"name": name,
|
||||
"pos": "bottom",
|
||||
}
|
||||
|
||||
if pos, ok := extraArgs["pos"]; ok{
|
||||
args["pos"] = pos
|
||||
}
|
||||
|
||||
checklist = &Checklist{}
|
||||
err = c.Post(path, args, &checklist)
|
||||
if err == nil {
|
||||
checklist.client = c
|
||||
checklist.IDCard = card.ID
|
||||
card.Checklists = append(card.Checklists, checklist)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreateCheckItem creates a checkitem inside the checklist.
|
||||
// Attribute currently supported as extra argument: pos.
|
||||
// Attributes currently known to be unsupported: checked.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference#checklistsidcheckitems
|
||||
func (cl *Checklist) CreateCheckItem(name string, extraArgs Arguments) (item *CheckItem, err error) {
|
||||
return cl.client.CreateCheckItem(cl, name, extraArgs)
|
||||
}
|
||||
|
||||
// CreateCheckItem creates a checkitem inside the given checklist.
|
||||
// Attribute currently supported as extra argument: pos.
|
||||
// Attributes currently known to be unsupported: checked.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference#checklistsidcheckitems
|
||||
func (c *Client) CreateCheckItem(checklist *Checklist, name string, extraArgs Arguments) (item *CheckItem, err error) {
|
||||
path := "checklists/" + checklist.ID + "/checkItems"
|
||||
args := Arguments {
|
||||
"name": name,
|
||||
"pos": "bottom",
|
||||
"checked": "false",
|
||||
}
|
||||
|
||||
if pos, ok := extraArgs["pos"]; ok{
|
||||
args["pos"] = pos
|
||||
}
|
||||
if checked, ok := extraArgs["checked"]; ok {
|
||||
args["checked"] = checked
|
||||
}
|
||||
|
||||
item = &CheckItem{}
|
||||
err = c.Post(path, args, item)
|
||||
if err == nil {
|
||||
checklist.CheckItems = append(checklist.CheckItems, *item)
|
||||
}
|
||||
return
|
||||
}
|
263
vendor/github.com/adlio/trello/client.go
generated
vendored
Normal file
263
vendor/github.com/adlio/trello/client.go
generated
vendored
Normal file
@ -0,0 +1,263 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// DefaultBaseURL is the default API base url used by Client to send requests to Trello.
|
||||
const DefaultBaseURL = "https://api.trello.com/1"
|
||||
|
||||
// Client is the central object for making API calls. It wraps a http client,
|
||||
// context, logger and identity configuration (Key and Token) of the Trello member.
|
||||
type Client struct {
|
||||
Client *http.Client
|
||||
Logger logger
|
||||
BaseURL string
|
||||
Key string
|
||||
Token string
|
||||
throttle <-chan time.Time
|
||||
testMode bool
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type logger interface {
|
||||
Debugf(string, ...interface{})
|
||||
}
|
||||
|
||||
// NewClient is a constructor for the Client. It takes the key and token credentials
|
||||
// of a Trello member to authenticate and authorise requests with.
|
||||
func NewClient(key, token string) *Client {
|
||||
return &Client{
|
||||
Client: http.DefaultClient,
|
||||
BaseURL: DefaultBaseURL,
|
||||
Key: key,
|
||||
Token: token,
|
||||
throttle: time.Tick(time.Second / 8), // Actually 10/second, but we're extra cautious
|
||||
testMode: false,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
// WithContext takes a context.Context, sets it as context on the client and returns
|
||||
// a Client pointer.
|
||||
func (c *Client) WithContext(ctx context.Context) *Client {
|
||||
newC := *c
|
||||
newC.ctx = ctx
|
||||
return &newC
|
||||
}
|
||||
|
||||
// Throttle starts receiving throttles from throttle channel each ticker period.
|
||||
func (c *Client) Throttle() {
|
||||
if !c.testMode {
|
||||
<-c.throttle
|
||||
}
|
||||
}
|
||||
|
||||
// Get takes a path, Arguments, and a target interface (e.g. Board or Card).
|
||||
// It runs a GET request on the Trello API endpoint and the path and uses the
|
||||
// Arguments as URL parameters. Then it returns either the target interface
|
||||
// updated from the response or an error.
|
||||
func (c *Client) Get(path string, args Arguments, target interface{}) error {
|
||||
|
||||
// Trello prohibits more than 10 seconds/second per token
|
||||
c.Throttle()
|
||||
|
||||
params := args.ToURLValues()
|
||||
c.log("[trello] GET %s?%s", path, params.Encode())
|
||||
|
||||
if c.Key != "" {
|
||||
params.Set("key", c.Key)
|
||||
}
|
||||
|
||||
if c.Token != "" {
|
||||
params.Set("token", c.Token)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s", c.BaseURL, path)
|
||||
urlWithParams := fmt.Sprintf("%s?%s", url, params.Encode())
|
||||
|
||||
req, err := http.NewRequest("GET", urlWithParams, nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Invalid GET request %s", url)
|
||||
}
|
||||
req = req.WithContext(c.ctx)
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP request failure on %s", url)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return makeHTTPClientError(url, resp)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(target)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "JSON decode failed on %s", url)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Put takes a path, Arguments, and a target interface (e.g. Board or Card).
|
||||
// It runs a PUT request on the Trello API endpoint with the path and uses
|
||||
// the Arguments as URL parameters. Then it returns either the target interface
|
||||
// updated from the response or an error.
|
||||
func (c *Client) Put(path string, args Arguments, target interface{}) error {
|
||||
|
||||
// Trello prohibits more than 10 seconds/second per token
|
||||
c.Throttle()
|
||||
|
||||
params := args.ToURLValues()
|
||||
c.log("[trello] PUT %s?%s", path, params.Encode())
|
||||
|
||||
if c.Key != "" {
|
||||
params.Set("key", c.Key)
|
||||
}
|
||||
|
||||
if c.Token != "" {
|
||||
params.Set("token", c.Token)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s", c.BaseURL, path)
|
||||
urlWithParams := fmt.Sprintf("%s?%s", url, params.Encode())
|
||||
|
||||
req, err := http.NewRequest("PUT", urlWithParams, nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Invalid PUT request %s", url)
|
||||
}
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP request failure on %s", url)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return makeHTTPClientError(url, resp)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(target)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "JSON decode failed on %s", url)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Post takes a path, Arguments, and a target interface (e.g. Board or Card).
|
||||
// It runs a POST request on the Trello API endpoint with the path and uses
|
||||
// the Arguments as URL parameters. Then it returns either the target interface
|
||||
// updated from the response or an error.
|
||||
func (c *Client) Post(path string, args Arguments, target interface{}) error {
|
||||
|
||||
// Trello prohibits more than 10 seconds/second per token
|
||||
c.Throttle()
|
||||
|
||||
params := args.ToURLValues()
|
||||
c.log("[trello] POST %s?%s", path, params.Encode())
|
||||
|
||||
if c.Key != "" {
|
||||
params.Set("key", c.Key)
|
||||
}
|
||||
|
||||
if c.Token != "" {
|
||||
params.Set("token", c.Token)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s", c.BaseURL, path)
|
||||
urlWithParams := fmt.Sprintf("%s?%s", url, params.Encode())
|
||||
|
||||
req, err := http.NewRequest("POST", urlWithParams, nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Invalid POST request %s", url)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP request failure on %s", url)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP Read error on response for %s", url)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewBuffer(b))
|
||||
err = decoder.Decode(target)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "JSON decode failed on %s:\n%s", url, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete takes a path, Arguments, and a target interface (e.g. Board or Card).
|
||||
// It runs a DELETE request on the Trello API endpoint with the path and uses
|
||||
// the Arguments as URL parameters. Then it returns either the target interface
|
||||
// updated from the response or an error.
|
||||
func (c *Client) Delete(path string, args Arguments, target interface{}) error {
|
||||
|
||||
c.Throttle()
|
||||
|
||||
params := args.ToURLValues()
|
||||
c.log("[trello] DELETE %s?%s", path, params.Encode())
|
||||
|
||||
if c.Key != "" {
|
||||
params.Set("key", c.Key)
|
||||
}
|
||||
|
||||
if c.Token != "" {
|
||||
params.Set("token", c.Token)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s", c.BaseURL, path)
|
||||
urlWithParams := fmt.Sprintf("%s?%s", url, params.Encode())
|
||||
|
||||
req, err := http.NewRequest("DELETE", urlWithParams, nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Invalid DELETE request %s", url)
|
||||
}
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP request failure on %s", url)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "HTTP Read error on response for %s", url)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewBuffer(b))
|
||||
err = decoder.Decode(target)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "JSON decode failed on %s:\n%s", url, string(b))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) log(format string, args ...interface{}) {
|
||||
if c.Logger != nil {
|
||||
c.Logger.Debugf(format, args...)
|
||||
}
|
||||
}
|
54
vendor/github.com/adlio/trello/custom-fields.go
generated
vendored
Normal file
54
vendor/github.com/adlio/trello/custom-fields.go
generated
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
package trello
|
||||
|
||||
import "fmt"
|
||||
|
||||
// CustomFieldItem represents the custom field items of Trello a trello card.
|
||||
type CustomFieldItem struct {
|
||||
ID string `json:"id"`
|
||||
IDValue string `json:"idValue"`
|
||||
IDCustomField string `json:"idCustomField"`
|
||||
IDModel string `json:"idModel"`
|
||||
IDModelType string `json:"modelType,omitempty"`
|
||||
}
|
||||
|
||||
// CustomField represents Trello's custom fields: "extra bits of structured data
|
||||
// attached to cards when our users need a bit more than what Trello provides."
|
||||
// https://developers.trello.com/reference/#custom-fields
|
||||
type CustomField struct {
|
||||
ID string `json:"id"`
|
||||
IDModel string `json:"idModel"`
|
||||
IDModelType string `json:"modelType,omitempty"`
|
||||
FieldGroup string `json:"fieldGroup"`
|
||||
Name string `json:"name"`
|
||||
Pos int `json:"pos"`
|
||||
Display struct {
|
||||
CardFront bool `json:"cardfront"`
|
||||
} `json:"display"`
|
||||
Type string `json:"type"`
|
||||
Options []*CustomFieldOption `json:"options"`
|
||||
}
|
||||
|
||||
// CustomFieldOption are nested resources of CustomFields
|
||||
type CustomFieldOption struct {
|
||||
ID string `json:"id"`
|
||||
IDCustomField string `json:"idCustomField"`
|
||||
Value struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"value"`
|
||||
Color string `json:"color,omitempty"`
|
||||
Pos int `json:"pos"`
|
||||
}
|
||||
|
||||
// GetCustomField takes a field id string and Arguments and returns the matching custom Field.
|
||||
func (c *Client) GetCustomField(fieldID string, args Arguments) (customField *CustomField, err error) {
|
||||
path := fmt.Sprintf("customFields/%s", fieldID)
|
||||
err = c.Get(path, args, &customField)
|
||||
return
|
||||
}
|
||||
|
||||
// GetCustomFields returns a slice of all receiver board's custom fields.
|
||||
func (b *Board) GetCustomFields(args Arguments) (customFields []*CustomField, err error) {
|
||||
path := fmt.Sprintf("boards/%s/customFields", b.ID)
|
||||
err = b.client.Get(path, args, &customFields)
|
||||
return
|
||||
}
|
59
vendor/github.com/adlio/trello/errors.go
generated
vendored
Normal file
59
vendor/github.com/adlio/trello/errors.go
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type notFoundError interface {
|
||||
IsNotFound() bool
|
||||
}
|
||||
|
||||
type rateLimitError interface {
|
||||
IsRateLimit() bool
|
||||
}
|
||||
|
||||
type permissionDeniedError interface {
|
||||
IsPermissionDenied() bool
|
||||
}
|
||||
|
||||
type httpClientError struct {
|
||||
msg string
|
||||
code int
|
||||
}
|
||||
|
||||
func makeHTTPClientError(url string, resp *http.Response) error {
|
||||
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
msg := fmt.Sprintf("HTTP request failure on %s:\n%d: %s", url, resp.StatusCode, string(body))
|
||||
|
||||
return &httpClientError{
|
||||
msg: msg,
|
||||
code: resp.StatusCode,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *httpClientError) Error() string { return e.msg }
|
||||
func (e *httpClientError) IsRateLimit() bool { return e.code == 429 }
|
||||
func (e *httpClientError) IsNotFound() bool { return e.code == 404 }
|
||||
func (e *httpClientError) IsPermissionDenied() bool { return e.code == 401 }
|
||||
|
||||
// IsRateLimit takes an error and returns true exactly if the error is a rate-limit error.
|
||||
func IsRateLimit(err error) bool {
|
||||
re, ok := err.(rateLimitError)
|
||||
return ok && re.IsRateLimit()
|
||||
}
|
||||
|
||||
// IsNotFound takes an error and returns true exactly if the error is a not-found error.
|
||||
func IsNotFound(err error) bool {
|
||||
nf, ok := err.(notFoundError)
|
||||
return ok && nf.IsNotFound()
|
||||
}
|
||||
|
||||
// IsPermissionDenied takes an error and returns true exactly if the error is a
|
||||
// permission-denied error.
|
||||
func IsPermissionDenied(err error) bool {
|
||||
pd, ok := err.(permissionDeniedError)
|
||||
return ok && pd.IsPermissionDenied()
|
||||
}
|
5
vendor/github.com/adlio/trello/go.mod
generated
vendored
Normal file
5
vendor/github.com/adlio/trello/go.mod
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
module github.com/adlio/trello
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/pkg/errors v0.8.1
|
2
vendor/github.com/adlio/trello/go.sum
generated
vendored
Normal file
2
vendor/github.com/adlio/trello/go.sum
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
34
vendor/github.com/adlio/trello/label.go
generated
vendored
Normal file
34
vendor/github.com/adlio/trello/label.go
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Label represents a Trello label.
|
||||
// Labels are defined per board, and can be applied to the cards on that board.
|
||||
// https://developers.trello.com/reference/#label-object
|
||||
type Label struct {
|
||||
ID string `json:"id"`
|
||||
IDBoard string `json:"idBoard"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Uses int `json:"uses"`
|
||||
}
|
||||
|
||||
// GetLabel takes a label id and Arguments and returns the matching label (per Trello member)
|
||||
// or an error.
|
||||
func (c *Client) GetLabel(labelID string, args Arguments) (label *Label, err error) {
|
||||
path := fmt.Sprintf("labels/%s", labelID)
|
||||
err = c.Get(path, args, &label)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLabels takes Arguments and returns a slice containing all labels of the receiver board or an error.
|
||||
func (b *Board) GetLabels(args Arguments) (labels []*Label, err error) {
|
||||
path := fmt.Sprintf("boards/%s/labels", b.ID)
|
||||
err = b.client.Get(path, args, &labels)
|
||||
return
|
||||
}
|
16
vendor/github.com/adlio/trello/list-duration-sort.go
generated
vendored
Normal file
16
vendor/github.com/adlio/trello/list-duration-sort.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
package trello
|
||||
|
||||
// ByFirstEntered is a slice of ListDurations
|
||||
type ByFirstEntered []*ListDuration
|
||||
|
||||
// ByFirstEntered returns the length of the receiver.
|
||||
func (durs ByFirstEntered) Len() int { return len(durs) }
|
||||
|
||||
// Less takes two indexes i and j and returns true exactly if the ListDuration
|
||||
// at i was entered before j.
|
||||
func (durs ByFirstEntered) Less(i, j int) bool {
|
||||
return durs[i].FirstEntered.Before(durs[j].FirstEntered)
|
||||
}
|
||||
|
||||
// Swap takes two indexes i and j and swaps the ListDurations at the indexes.
|
||||
func (durs ByFirstEntered) Swap(i, j int) { durs[i], durs[j] = durs[j], durs[i] }
|
87
vendor/github.com/adlio/trello/list-duration.go
generated
vendored
Normal file
87
vendor/github.com/adlio/trello/list-duration.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
package trello
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ListDuration represents the time a Card has been or was in list.
|
||||
type ListDuration struct {
|
||||
ListID string
|
||||
ListName string
|
||||
Duration time.Duration
|
||||
FirstEntered time.Time
|
||||
TimesInList int
|
||||
}
|
||||
|
||||
// AddDuration takes a duration and adds it to the ListDuration's Duration.
|
||||
// Also increments TimesInList.
|
||||
func (l *ListDuration) AddDuration(d time.Duration) {
|
||||
l.Duration = l.Duration + d
|
||||
l.TimesInList++
|
||||
}
|
||||
|
||||
// GetListDurations analyses a Card's actions to figure out how long it was in each List.
|
||||
// It returns a slice of the ListDurations, one Duration per list, or an error.
|
||||
func (c *Card) GetListDurations() (durations []*ListDuration, err error) {
|
||||
|
||||
var actions ActionCollection
|
||||
if len(c.Actions) == 0 {
|
||||
// Get all actions which affected the Card's List
|
||||
c.client.log("[trello] GetListDurations() called on card '%s' without any Card.Actions. Fetching fresh.", c.ID)
|
||||
actions, err = c.GetListChangeActions()
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "GetListChangeActions() call failed.")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
actions = c.Actions.FilterToListChangeActions()
|
||||
}
|
||||
|
||||
return actions.GetListDurations()
|
||||
}
|
||||
|
||||
// GetListDurations returns a slice of ListDurations based on the receiver Actions.
|
||||
func (actions ActionCollection) GetListDurations() (durations []*ListDuration, err error) {
|
||||
sort.Sort(actions)
|
||||
|
||||
var prevTime time.Time
|
||||
var prevList *List
|
||||
|
||||
durs := make(map[string]*ListDuration)
|
||||
for _, action := range actions {
|
||||
if action.DidChangeListForCard() {
|
||||
if prevList != nil {
|
||||
duration := action.Date.Sub(prevTime)
|
||||
_, durExists := durs[prevList.ID]
|
||||
if !durExists {
|
||||
durs[prevList.ID] = &ListDuration{ListID: prevList.ID, ListName: prevList.Name, Duration: duration, TimesInList: 1, FirstEntered: prevTime}
|
||||
} else {
|
||||
durs[prevList.ID].AddDuration(duration)
|
||||
}
|
||||
}
|
||||
prevList = ListAfterAction(action)
|
||||
prevTime = action.Date
|
||||
}
|
||||
}
|
||||
|
||||
if prevList != nil {
|
||||
duration := time.Now().Sub(prevTime)
|
||||
_, durExists := durs[prevList.ID]
|
||||
if !durExists {
|
||||
durs[prevList.ID] = &ListDuration{ListID: prevList.ID, ListName: prevList.Name, Duration: duration, TimesInList: 1, FirstEntered: prevTime}
|
||||
} else {
|
||||
durs[prevList.ID].AddDuration(duration)
|
||||
}
|
||||
}
|
||||
|
||||
durations = make([]*ListDuration, 0, len(durs))
|
||||
for _, ld := range durs {
|
||||
durations = append(durations, ld)
|
||||
}
|
||||
sort.Sort(ByFirstEntered(durations))
|
||||
|
||||
return durations, nil
|
||||
}
|
91
vendor/github.com/adlio/trello/list.go
generated
vendored
Normal file
91
vendor/github.com/adlio/trello/list.go
generated
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// List represents Trello lists.
|
||||
// https://developers.trello.com/reference/#list-object
|
||||
type List struct {
|
||||
client *Client
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IDBoard string `json:"idBoard,omitempty"`
|
||||
Closed bool `json:"closed"`
|
||||
Pos float32 `json:"pos,omitempty"`
|
||||
Board *Board `json:"board,omitempty"`
|
||||
Cards []*Card `json:"cards,omitempty"`
|
||||
}
|
||||
|
||||
// CreatedAt returns the time.Time from the list's id.
|
||||
func (l *List) CreatedAt() time.Time {
|
||||
t, _ := IDToTime(l.ID)
|
||||
return t
|
||||
}
|
||||
|
||||
// GetList takes a list's id and Arguments and returns the matching list.
|
||||
func (c *Client) GetList(listID string, args Arguments) (list *List, err error) {
|
||||
path := fmt.Sprintf("lists/%s", listID)
|
||||
err = c.Get(path, args, &list)
|
||||
if list != nil {
|
||||
list.client = c
|
||||
for i := range list.Cards {
|
||||
list.Cards[i].client = c
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetLists takes Arguments and returns the lists of the receiver Board.
|
||||
func (b *Board) GetLists(args Arguments) (lists []*List, err error) {
|
||||
path := fmt.Sprintf("boards/%s/lists", b.ID)
|
||||
err = b.client.Get(path, args, &lists)
|
||||
for i := range lists {
|
||||
lists[i].client = b.client
|
||||
for j := range lists[i].Cards {
|
||||
lists[i].Cards[j].client = b.client
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// CreateList creates a list.
|
||||
// Attribute currently supported as extra argument: pos.
|
||||
// Attributes currently known to be unsupported: idListSource.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference/#lists-1
|
||||
func (c *Client) CreateList(onBoard *Board, name string, extraArgs Arguments) (list *List, err error) {
|
||||
path := "lists"
|
||||
args := Arguments{
|
||||
"name": name,
|
||||
"pos": "top",
|
||||
"idBoard": onBoard.ID,
|
||||
}
|
||||
|
||||
if pos, ok := extraArgs["pos"]; ok{
|
||||
args["pos"] = pos
|
||||
}
|
||||
|
||||
list = &List{}
|
||||
err = c.Post(path, args, &list)
|
||||
if err == nil {
|
||||
list.client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreateList creates a list.
|
||||
// Attribute currently supported as extra argument: pos.
|
||||
// Attributes currently known to be unsupported: idListSource.
|
||||
//
|
||||
// API Docs: https://developers.trello.com/reference/#lists-1
|
||||
func (b *Board) CreateList(name string, extraArgs Arguments) (list *List, err error) {
|
||||
return b.client.CreateList(b, name, extraArgs)
|
||||
}
|
122
vendor/github.com/adlio/trello/member-duration.go
generated
vendored
Normal file
122
vendor/github.com/adlio/trello/member-duration.go
generated
vendored
Normal file
@ -0,0 +1,122 @@
|
||||
package trello
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// MemberDuration is used to track the periods of time which a user (member) is attached to a card.
|
||||
type MemberDuration struct {
|
||||
MemberID string
|
||||
MemberName string
|
||||
FirstAdded time.Time
|
||||
Duration time.Duration
|
||||
active bool
|
||||
lastAdded time.Time
|
||||
}
|
||||
|
||||
// ByLongestDuration is a slice of *MemberDuration
|
||||
type ByLongestDuration []*MemberDuration
|
||||
|
||||
// Len returns the length of the ByLongestDuration slice.
|
||||
func (d ByLongestDuration) Len() int { return len(d) }
|
||||
|
||||
// Less takes two indexes i and j and returns true exactly if the Duration
|
||||
// at i is larger than the Duration at j.
|
||||
func (d ByLongestDuration) Less(i, j int) bool { return d[i].Duration > d[j].Duration }
|
||||
func (d ByLongestDuration) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
||||
|
||||
func (d *MemberDuration) addAsOf(t time.Time) {
|
||||
d.active = true
|
||||
if d.FirstAdded.IsZero() {
|
||||
d.FirstAdded = t
|
||||
}
|
||||
d.startTimerAsOf(t)
|
||||
}
|
||||
|
||||
func (d *MemberDuration) startTimerAsOf(t time.Time) {
|
||||
if d.active {
|
||||
d.lastAdded = t
|
||||
}
|
||||
}
|
||||
|
||||
func (d *MemberDuration) removeAsOf(t time.Time) {
|
||||
d.stopTimerAsOf(t)
|
||||
d.active = false
|
||||
d.lastAdded = time.Time{}
|
||||
}
|
||||
|
||||
func (d *MemberDuration) stopTimerAsOf(t time.Time) {
|
||||
if d.active {
|
||||
d.Duration = d.Duration + t.Sub(d.lastAdded)
|
||||
}
|
||||
}
|
||||
|
||||
// GetMemberDurations returns a slice containing all durations of a card.
|
||||
func (c *Card) GetMemberDurations() (durations []*MemberDuration, err error) {
|
||||
var actions ActionCollection
|
||||
if len(c.Actions) == 0 {
|
||||
c.client.log("[trello] GetMemberDurations() called on card '%s' without any Card.Actions. Fetching fresh.", c.ID)
|
||||
actions, err = c.GetMembershipChangeActions()
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "GetMembershipChangeActions() call failed.")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
actions = c.Actions.FilterToCardMembershipChangeActions()
|
||||
}
|
||||
|
||||
return actions.GetMemberDurations()
|
||||
}
|
||||
|
||||
// GetMemberDurations is similar to GetListDurations. It returns a slice of MemberDuration objects,
|
||||
// which describes the length of time each member was attached to this card. Durations are
|
||||
// calculated such that being added to a card starts a timer for that member, and being removed
|
||||
// starts it again (so that if a person is added and removed multiple times, the duration
|
||||
// captures only the times which they were attached). Archiving the card also stops the timer.
|
||||
func (actions ActionCollection) GetMemberDurations() (durations []*MemberDuration, err error) {
|
||||
sort.Sort(actions)
|
||||
durs := make(map[string]*MemberDuration)
|
||||
for _, action := range actions {
|
||||
if action.DidChangeCardMembership() {
|
||||
_, durExists := durs[action.Member.ID]
|
||||
if !durExists {
|
||||
switch action.Type {
|
||||
case "addMemberToCard":
|
||||
durs[action.Member.ID] = &MemberDuration{MemberID: action.Member.ID, MemberName: action.Member.FullName}
|
||||
durs[action.Member.ID].addAsOf(action.Date)
|
||||
case "removeMemberFromCard":
|
||||
// Surprisingly, this is possible. If a card was copied, and members were preserved, those
|
||||
// members exist on the card without a corresponding addMemberToCard action.
|
||||
t, _ := IDToTime(action.Data.Card.ID)
|
||||
durs[action.Member.ID] = &MemberDuration{MemberID: action.Member.ID, MemberName: action.Member.FullName, lastAdded: t}
|
||||
durs[action.Member.ID].removeAsOf(action.Date)
|
||||
}
|
||||
} else {
|
||||
switch action.Type {
|
||||
case "addMemberToCard":
|
||||
durs[action.Member.ID].addAsOf(action.Date)
|
||||
case "removeMemberFromCard":
|
||||
durs[action.Member.ID].removeAsOf(action.Date)
|
||||
}
|
||||
}
|
||||
} else if action.DidArchiveCard() {
|
||||
for id := range durs {
|
||||
durs[id].stopTimerAsOf(action.Date)
|
||||
}
|
||||
} else if action.DidUnarchiveCard() {
|
||||
for id := range durs {
|
||||
durs[id].startTimerAsOf(action.Date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
durations = make([]*MemberDuration, 0, len(durs))
|
||||
for _, md := range durs {
|
||||
durations = append(durations, md)
|
||||
}
|
||||
// sort.Sort(ByLongestDuration(durations))
|
||||
return durations, nil
|
||||
}
|
62
vendor/github.com/adlio/trello/member.go
generated
vendored
Normal file
62
vendor/github.com/adlio/trello/member.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Member represents a Trello member.
|
||||
// https://developers.trello.com/reference/#member-object
|
||||
type Member struct {
|
||||
client *Client
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
FullName string `json:"fullName"`
|
||||
Initials string `json:"initials"`
|
||||
AvatarHash string `json:"avatarHash"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// GetMember takes a member id and Arguments and returns a Member or an error.
|
||||
func (c *Client) GetMember(memberID string, args Arguments) (member *Member, err error) {
|
||||
path := fmt.Sprintf("members/%s", memberID)
|
||||
err = c.Get(path, args, &member)
|
||||
if err == nil {
|
||||
member.client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetMembers takes Arguments and returns a slice of all members of the organization or an error.
|
||||
func (o *Organization) GetMembers(args Arguments) (members []*Member, err error) {
|
||||
path := fmt.Sprintf("organizations/%s/members", o.ID)
|
||||
err = o.client.Get(path, args, &members)
|
||||
for i := range members {
|
||||
members[i].client = o.client
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetMembers takes Arguments and returns a slice of all members of the Board or an error.
|
||||
func (b *Board) GetMembers(args Arguments) (members []*Member, err error) {
|
||||
path := fmt.Sprintf("boards/%s/members", b.ID)
|
||||
err = b.client.Get(path, args, &members)
|
||||
for i := range members {
|
||||
members[i].client = b.client
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetMembers takes Arguments and returns a slice of all members of the Card or an error.
|
||||
func (c *Card) GetMembers(args Arguments) (members []*Member, err error) {
|
||||
path := fmt.Sprintf("cards/%s/members", c.ID)
|
||||
err = c.client.Get(path, args, &members)
|
||||
for i := range members {
|
||||
members[i].client = c.client
|
||||
}
|
||||
return
|
||||
}
|
53
vendor/github.com/adlio/trello/notification.go
generated
vendored
Normal file
53
vendor/github.com/adlio/trello/notification.go
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
package trello
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Notification represents a Trello Notification.
|
||||
// https://developers.trello.com/reference/#notifications
|
||||
type Notification struct {
|
||||
client *Client
|
||||
|
||||
ID string `json:"id"`
|
||||
IDAction string `json:"idAction"`
|
||||
Unread bool `json:"unread"`
|
||||
Type string `json:"type"`
|
||||
IDMemberCreator string `json:"idMemberCreator"`
|
||||
Date time.Time `json:"date"`
|
||||
DateRead time.Time `json:"dataRead"`
|
||||
Data NotificationData `json:"data,omitempty"`
|
||||
MemberCreator *Member `json:"memberCreator,omitempty"`
|
||||
}
|
||||
|
||||
// NotificationData represents the 'notificaiton.data'
|
||||
type NotificationData struct {
|
||||
Text string `json:"text"`
|
||||
Card *NotificationDataCard `json:"card,omitempty"`
|
||||
Board *NotificationDataBoard `json:"board,omitempty"`
|
||||
}
|
||||
|
||||
// NotificationDataBoard represents the 'notification.data.board'
|
||||
type NotificationDataBoard struct {
|
||||
ID string `json:"id"`
|
||||
ShortLink string `json:"shortLink"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// NotificationDataCard represents the 'notification.data.card'
|
||||
type NotificationDataCard struct {
|
||||
ID string `json:"id"`
|
||||
IDShort int `json:"idShort"`
|
||||
Name string `json:"name"`
|
||||
ShortLink string `json:"shortLink"`
|
||||
}
|
||||
|
||||
// GetMyNotifications returns the notifications of the authenticated user
|
||||
func (c *Client) GetMyNotifications(args Arguments) (notifications []*Notification, err error) {
|
||||
path := "members/me/notifications"
|
||||
err = c.Get(path, args, ¬ifications)
|
||||
for i := range notifications {
|
||||
notifications[i].client = c
|
||||
}
|
||||
return
|
||||
}
|
35
vendor/github.com/adlio/trello/organization.go
generated
vendored
Normal file
35
vendor/github.com/adlio/trello/organization.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Organization represents a Trello organization or team, i.e. a collection of members and boards.
|
||||
// https://developers.trello.com/reference/#organizations
|
||||
type Organization struct {
|
||||
client *Client
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Desc string `json:"desc"`
|
||||
URL string `json:"url"`
|
||||
Website string `json:"website"`
|
||||
Products []string `json:"products"`
|
||||
PowerUps []string `json:"powerUps"`
|
||||
}
|
||||
|
||||
// GetOrganization takes an organization id and Arguments and either
|
||||
// GETs returns an Organization, or an error.
|
||||
func (c *Client) GetOrganization(orgID string, args Arguments) (organization *Organization, err error) {
|
||||
path := fmt.Sprintf("organizations/%s", orgID)
|
||||
err = c.Get(path, args, &organization)
|
||||
if organization != nil {
|
||||
organization.client = c
|
||||
}
|
||||
return
|
||||
}
|
65
vendor/github.com/adlio/trello/search.go
generated
vendored
Normal file
65
vendor/github.com/adlio/trello/search.go
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
// SearchResult represents a search result as collections of various
|
||||
// types returned by a search, e.g. Cards or Boards.
|
||||
type SearchResult struct {
|
||||
Options SearchOptions `json:"options"`
|
||||
Actions []*Action `json:"actions,omitempty"`
|
||||
Cards []*Card `json:"cards,omitempty"`
|
||||
Boards []*Board `json:"boards,omitempty"`
|
||||
Members []*Member `json:"members,omitempty"`
|
||||
}
|
||||
|
||||
// SearchOptions contains options for search requests.
|
||||
type SearchOptions struct {
|
||||
Terms []SearchTerm `json:"terms"`
|
||||
Modifiers []SearchModifier `json:"modifiers,omitempty"`
|
||||
ModelTypes []string `json:"modelTypes,omitempty"`
|
||||
Partial bool `json:"partial"`
|
||||
}
|
||||
|
||||
// SearchModifier is wrapper for a search string.
|
||||
type SearchModifier struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// SearchTerm is a string that may be negated in a search query.
|
||||
type SearchTerm struct {
|
||||
Text string `json:"text"`
|
||||
Negated bool `json:"negated,omitempty"`
|
||||
}
|
||||
|
||||
// SearchCards takes a query string and Arguments and returns a slice of Cards or an error.
|
||||
func (c *Client) SearchCards(query string, args Arguments) (cards []*Card, err error) {
|
||||
args["query"] = query
|
||||
args["modelTypes"] = "cards"
|
||||
res := SearchResult{}
|
||||
err = c.Get("search", args, &res)
|
||||
cards = res.Cards
|
||||
return
|
||||
}
|
||||
|
||||
// SearchBoards takes a query string and Arguments and returns a slice of Boards or an error.
|
||||
func (c *Client) SearchBoards(query string, args Arguments) (boards []*Board, err error) {
|
||||
args["query"] = query
|
||||
args["modelTypes"] = "boards"
|
||||
res := SearchResult{}
|
||||
err = c.Get("search", args, &res)
|
||||
boards = res.Boards
|
||||
for _, board := range boards {
|
||||
board.client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SearchMembers takes a query string and Arguments and returns a slice of Members or an error.
|
||||
func (c *Client) SearchMembers(query string, args Arguments) (members []*Member, err error) {
|
||||
args["query"] = query
|
||||
err = c.Get("search/members", args, &members)
|
||||
return
|
||||
}
|
41
vendor/github.com/adlio/trello/token.go
generated
vendored
Normal file
41
vendor/github.com/adlio/trello/token.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Token represents Trello tokens. Tokens can be used for setting up Webhooks among other things.
|
||||
// https://developers.trello.com/reference/#tokens
|
||||
type Token struct {
|
||||
client *Client
|
||||
ID string `json:"id"`
|
||||
DateCreated time.Time `json:"dateCreated"`
|
||||
DateExpires *time.Time `json:"dateExpires"`
|
||||
IDMember string `json:"idMember"`
|
||||
Identifier string `json:"identifier"`
|
||||
Permissions []Permission `json:"permissions"`
|
||||
}
|
||||
|
||||
// Permission represent a Token's permissions.
|
||||
type Permission struct {
|
||||
IDModel string `json:"idModel"`
|
||||
ModelType string `json:"modelType"`
|
||||
Read bool `json:"read"`
|
||||
Write bool `json:"write"`
|
||||
}
|
||||
|
||||
// GetToken takes a token id and Arguments and GETs and returns the Token or an error.
|
||||
func (c *Client) GetToken(tokenID string, args Arguments) (token *Token, err error) {
|
||||
path := fmt.Sprintf("tokens/%s", tokenID)
|
||||
err = c.Get(path, args, &token)
|
||||
if token != nil {
|
||||
token.client = c
|
||||
}
|
||||
return
|
||||
}
|
BIN
vendor/github.com/adlio/trello/trello-logo.png
generated
vendored
Normal file
BIN
vendor/github.com/adlio/trello/trello-logo.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 76 KiB |
29
vendor/github.com/adlio/trello/trello.go
generated
vendored
Normal file
29
vendor/github.com/adlio/trello/trello.go
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// IDToTime is a convenience function. It takes a Trello ID string and
|
||||
// extracts the encoded create time as time.Time or an error.
|
||||
func IDToTime(id string) (t time.Time, err error) {
|
||||
if id == "" {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
// The first 8 characters in the object ID are a Unix timestamp
|
||||
ts, err := strconv.ParseUint(id[:8], 16, 64)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "ID '%s' failed to convert to timestamp.", id)
|
||||
} else {
|
||||
t = time.Unix(int64(ts), 0)
|
||||
}
|
||||
return
|
||||
}
|
129
vendor/github.com/adlio/trello/webhook.go
generated
vendored
Normal file
129
vendor/github.com/adlio/trello/webhook.go
generated
vendored
Normal file
@ -0,0 +1,129 @@
|
||||
// Copyright © 2016 Aaron Longwell
|
||||
//
|
||||
// Use of this source code is governed by an MIT licese.
|
||||
// Details in the LICENSE file.
|
||||
|
||||
package trello
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Webhook is the Go representation of a webhook registered in Trello's systems.
|
||||
// Used when creating, modifying or deleting webhooks.
|
||||
// https://developers.trello.com/reference/#webhook-object
|
||||
//
|
||||
type Webhook struct {
|
||||
client *Client
|
||||
ID string `json:"id,omitempty"`
|
||||
IDModel string `json:"idModel"`
|
||||
Description string `json:"description"`
|
||||
CallbackURL string `json:"callbackURL"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
// BoardWebhookRequest is the object sent by Trello to a Webhook for Board-triggered
|
||||
// webhooks.
|
||||
//
|
||||
type BoardWebhookRequest struct {
|
||||
Model *Board
|
||||
Action *Action
|
||||
}
|
||||
|
||||
// ListWebhookRequest is the object sent by Trello to a Webhook for List-triggered
|
||||
// webhooks.
|
||||
//
|
||||
type ListWebhookRequest struct {
|
||||
Model *List
|
||||
Action *Action
|
||||
}
|
||||
|
||||
// CardWebhookRequest is the object sent by Trello to a Webhook for Card-triggered
|
||||
// webhooks.
|
||||
//
|
||||
type CardWebhookRequest struct {
|
||||
Model *Card
|
||||
Action *Action
|
||||
}
|
||||
|
||||
// CreateWebhook takes a Webhook, POSTs it and returns an error object.
|
||||
func (c *Client) CreateWebhook(webhook *Webhook) error {
|
||||
path := "webhooks"
|
||||
args := Arguments{"idModel": webhook.IDModel, "description": webhook.Description, "callbackURL": webhook.CallbackURL}
|
||||
err := c.Post(path, args, webhook)
|
||||
if err == nil {
|
||||
webhook.client = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteWebhook takes a webhook and deletes it
|
||||
func (w *Webhook) Delete(args Arguments) error {
|
||||
path := fmt.Sprintf("webhooks/%s", w.ID)
|
||||
return w.client.Delete(path, Arguments{}, w)
|
||||
}
|
||||
|
||||
// GetWebhook takes a webhook id and Arguments, GETs the matching Webhook and returns it or an error.
|
||||
func (c *Client) GetWebhook(webhookID string, args Arguments) (webhook *Webhook, err error) {
|
||||
path := fmt.Sprintf("webhooks/%s", webhookID)
|
||||
err = c.Get(path, args, &webhook)
|
||||
if webhook != nil {
|
||||
webhook.client = c
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetWebhooks takes Arguments and returns a list of all Webhooks for the receiver Token or an error.
|
||||
func (t *Token) GetWebhooks(args Arguments) (webhooks []*Webhook, err error) {
|
||||
path := fmt.Sprintf("tokens/%s/webhooks", t.client.Token)
|
||||
err = t.client.Get(path, args, &webhooks)
|
||||
if err == nil {
|
||||
for _, webhook := range webhooks {
|
||||
webhook.client = t.client
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetBoardWebhookRequest takes a http.Request and returns the decoded body as BoardWebhookRequest or an error.
|
||||
func GetBoardWebhookRequest(r *http.Request) (whr *BoardWebhookRequest, err error) {
|
||||
if r.Method == "HEAD" {
|
||||
return &BoardWebhookRequest{}, nil
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err = decoder.Decode(&whr)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetBoardWebhookRequest() failed to decode '%s'.", r.URL)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetListWebhookRequest takes a http.Request and returns the decoded Body as ListWebhookRequest or an error.
|
||||
func GetListWebhookRequest(r *http.Request) (whr *ListWebhookRequest, err error) {
|
||||
if r.Method == "HEAD" {
|
||||
return &ListWebhookRequest{}, nil
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err = decoder.Decode(&whr)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetListWebhookRequest() failed to decode '%s'.", r.URL)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetCardWebhookRequest takes a http.Request and returns the decoded Body as CardWebhookRequest or an error.
|
||||
func GetCardWebhookRequest(r *http.Request) (whr *CardWebhookRequest, err error) {
|
||||
if r.Method == "HEAD" {
|
||||
return &CardWebhookRequest{}, nil
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err = decoder.Decode(&whr)
|
||||
if err != nil {
|
||||
err = errors.Wrapf(err, "GetCardWebhookRequest() failed to decode '%s'.", r.URL)
|
||||
}
|
||||
return
|
||||
}
|
19
vendor/github.com/alecthomas/chroma/.gitignore
generated
vendored
Normal file
19
vendor/github.com/alecthomas/chroma/.gitignore
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
/cmd/chroma/chroma
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
|
||||
_models/
|
||||
|
||||
_examples/
|
44
vendor/github.com/alecthomas/chroma/.golangci.yml
generated
vendored
Normal file
44
vendor/github.com/alecthomas/chroma/.golangci.yml
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
run:
|
||||
tests: true
|
||||
skip-dirs:
|
||||
- _examples
|
||||
|
||||
output:
|
||||
print-issued-lines: false
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- maligned
|
||||
- megacheck
|
||||
- lll
|
||||
- gocyclo
|
||||
- dupl
|
||||
|
||||
linters-settings:
|
||||
govet:
|
||||
check-shadowing: true
|
||||
gocyclo:
|
||||
min-complexity: 10
|
||||
dupl:
|
||||
threshold: 100
|
||||
goconst:
|
||||
min-len: 8
|
||||
min-occurrences: 3
|
||||
|
||||
issues:
|
||||
max-per-linter: 0
|
||||
max-same: 0
|
||||
exclude-use-default: false
|
||||
exclude:
|
||||
# Captured by errcheck.
|
||||
- '^(G104|G204):'
|
||||
# Very commonly not checked.
|
||||
- 'Error return value of .(.*\.Help|.*\.MarkFlagRequired|(os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked'
|
||||
- 'exported method (.*\.MarshalJSON|.*\.UnmarshalJSON|.*\.EntityURN|.*\.GoString|.*\.Pos) should have comment or be unexported'
|
||||
- 'composite literal uses unkeyed fields'
|
||||
- 'declaration of "err" shadows declaration'
|
||||
- 'should not use dot imports'
|
||||
- 'Potential file inclusion via variable'
|
||||
- 'should have comment or be unexported'
|
||||
- 'comment on exported var .* should be of the form'
|
31
vendor/github.com/alecthomas/chroma/.goreleaser.yml
generated
vendored
Normal file
31
vendor/github.com/alecthomas/chroma/.goreleaser.yml
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
project_name: chroma
|
||||
release:
|
||||
github:
|
||||
owner: alecthomas
|
||||
name: chroma
|
||||
brew:
|
||||
install: bin.install "chroma"
|
||||
builds:
|
||||
- goos:
|
||||
- linux
|
||||
- darwin
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
- "386"
|
||||
goarm:
|
||||
- "6"
|
||||
main: ./cmd/chroma/main.go
|
||||
ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
|
||||
binary: chroma
|
||||
archive:
|
||||
format: tar.gz
|
||||
name_template: '{{ .Binary }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{
|
||||
.Arm }}{{ end }}'
|
||||
files:
|
||||
- COPYING
|
||||
- README*
|
||||
snapshot:
|
||||
name_template: SNAPSHOT-{{ .Commit }}
|
||||
checksum:
|
||||
name_template: '{{ .ProjectName }}-{{ .Version }}-checksums.txt'
|
10
vendor/github.com/alecthomas/chroma/.travis.yml
generated
vendored
Normal file
10
vendor/github.com/alecthomas/chroma/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
sudo: false
|
||||
language: go
|
||||
script:
|
||||
- go test -v ./...
|
||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.10.2
|
||||
- ./bin/golangci-lint run
|
||||
- git clean -fdx .
|
||||
after_success:
|
||||
go get github.com/goreleaser/goreleaser && goreleaser
|
||||
|
19
vendor/github.com/alecthomas/chroma/COPYING
generated
vendored
Normal file
19
vendor/github.com/alecthomas/chroma/COPYING
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (C) 2017 Alec Thomas
|
||||
|
||||
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.
|
245
vendor/github.com/alecthomas/chroma/README.md
generated
vendored
Normal file
245
vendor/github.com/alecthomas/chroma/README.md
generated
vendored
Normal file
@ -0,0 +1,245 @@
|
||||
# Chroma — A general purpose syntax highlighter in pure Go [](https://godoc.org/github.com/alecthomas/chroma) [](https://travis-ci.org/alecthomas/chroma) [](https://gitter.im/alecthomas/Lobby)
|
||||
|
||||
> **NOTE:** As Chroma has just been released, its API is still in flux. That said, the high-level interface should not change significantly.
|
||||
|
||||
Chroma takes source code and other structured text and converts it into syntax
|
||||
highlighted HTML, ANSI-coloured text, etc.
|
||||
|
||||
Chroma is based heavily on [Pygments](http://pygments.org/), and includes
|
||||
translators for Pygments lexers and styles.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
<!-- MarkdownTOC -->
|
||||
|
||||
1. [Supported languages](#supported-languages)
|
||||
1. [Using the library](#using-the-library)
|
||||
1. [Quick start](#quick-start)
|
||||
1. [Identifying the language](#identifying-the-language)
|
||||
1. [Formatting the output](#formatting-the-output)
|
||||
1. [The HTML formatter](#the-html-formatter)
|
||||
1. [More detail](#more-detail)
|
||||
1. [Lexers](#lexers)
|
||||
1. [Formatters](#formatters)
|
||||
1. [Styles](#styles)
|
||||
1. [Command-line interface](#command-line-interface)
|
||||
1. [What's missing compared to Pygments?](#whats-missing-compared-to-pygments)
|
||||
|
||||
<!-- /MarkdownTOC -->
|
||||
|
||||
## Supported languages
|
||||
|
||||
Prefix | Language
|
||||
:----: | --------
|
||||
A | ABNF, ActionScript, ActionScript 3, Ada, Angular2, ANTLR, ApacheConf, APL, AppleScript, Arduino, Awk
|
||||
B | Ballerina, Base Makefile, Bash, Batchfile, BlitzBasic, BNF, Brainfuck
|
||||
C | C, C#, C++, Cassandra CQL, CFEngine3, cfstatement/ColdFusion, CMake, COBOL, CSS, Cap'n Proto, Ceylon, ChaiScript, Cheetah, Clojure, CoffeeScript, Common Lisp, Coq, Crystal, Cython
|
||||
D | Dart, Diff, Django/Jinja, Docker, DTD
|
||||
E | EBNF, Elixir, Elm, EmacsLisp, Erlang
|
||||
F | Factor, Fish, Forth, Fortran, FSharp
|
||||
G | GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groovy
|
||||
H | Handlebars, Haskell, Haxe, Hexdump, HTML, HTTP, Hy
|
||||
I | Idris, INI, Io
|
||||
J | Java, JavaScript, JSON, Jsx, Julia, Jungle
|
||||
K | Kotlin
|
||||
L | Lighttpd configuration file, LLVM, Lua
|
||||
M | Mako, Markdown, Mason, Mathematica, MiniZinc, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL
|
||||
N | NASM, Newspeak, Nginx configuration file, Nim, Nix
|
||||
O | Objective-C, OCaml, Octave, OpenSCAD, Org Mode
|
||||
P | PacmanConf, Perl, PHP, Pig, PkgConfig, Plaintext, PL/pgSQL, PostgreSQL SQL dialect, PostScript, POVRay, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3
|
||||
Q | QBasic
|
||||
R | R, Racket, Ragel, reg, reStructuredText, Rexx, Ruby, Rust
|
||||
S | Sass, Scala, Scheme, Scilab, SCSS, Smalltalk, Smarty, Snobol, Solidity, SPARQL, SQL, SquidConf, Swift, systemd, Systemverilog
|
||||
T | TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, TeX, Thrift, TOML, TradingView, Transact-SQL, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData
|
||||
V | verilog, VHDL, VimL
|
||||
W | WDTE
|
||||
X | XML, Xorg
|
||||
Y | YAML
|
||||
|
||||
_I will attempt to keep this section up to date, but an authoritative list can be
|
||||
displayed with `chroma --list`._
|
||||
|
||||
## Using the library
|
||||
|
||||
Chroma, like Pygments, has the concepts of
|
||||
[lexers](https://github.com/alecthomas/chroma/tree/master/lexers),
|
||||
[formatters](https://github.com/alecthomas/chroma/tree/master/formatters) and
|
||||
[styles](https://github.com/alecthomas/chroma/tree/master/styles).
|
||||
|
||||
Lexers convert source text into a stream of tokens, styles specify how token
|
||||
types are mapped to colours, and formatters convert tokens and styles into
|
||||
formatted output.
|
||||
|
||||
A package exists for each of these, containing a global `Registry` variable
|
||||
with all of the registered implementations. There are also helper functions
|
||||
for using the registry in each package, such as looking up lexers by name or
|
||||
matching filenames, etc.
|
||||
|
||||
In all cases, if a lexer, formatter or style can not be determined, `nil` will
|
||||
be returned. In this situation you may want to default to the `Fallback`
|
||||
value in each respective package, which provides sane defaults.
|
||||
|
||||
### Quick start
|
||||
|
||||
A convenience function exists that can be used to simply format some source
|
||||
text, without any effort:
|
||||
|
||||
```go
|
||||
err := quick.Highlight(os.Stdout, someSourceCode, "go", "html", "monokai")
|
||||
```
|
||||
|
||||
### Identifying the language
|
||||
|
||||
To highlight code, you'll first have to identify what language the code is
|
||||
written in. There are three primary ways to do that:
|
||||
|
||||
1. Detect the language from its filename.
|
||||
|
||||
```go
|
||||
lexer := lexers.Match("foo.go")
|
||||
```
|
||||
|
||||
3. Explicitly specify the language by its Chroma syntax ID (a full list is available from `lexers.Names()`).
|
||||
|
||||
```go
|
||||
lexer := lexers.Get("go")
|
||||
```
|
||||
|
||||
3. Detect the language from its content.
|
||||
|
||||
```go
|
||||
lexer := lexers.Analyse("package main\n\nfunc main()\n{\n}\n")
|
||||
```
|
||||
|
||||
In all cases, `nil` will be returned if the language can not be identified.
|
||||
|
||||
```go
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
```
|
||||
|
||||
At this point, it should be noted that some lexers can be extremely chatty. To
|
||||
mitigate this, you can use the coalescing lexer to coalesce runs of identical
|
||||
token types into a single token:
|
||||
|
||||
```go
|
||||
lexer = chroma.Coalesce(lexer)
|
||||
```
|
||||
|
||||
### Formatting the output
|
||||
|
||||
Once a language is identified you will need to pick a formatter and a style (theme).
|
||||
|
||||
```go
|
||||
style := styles.Get("swapoff")
|
||||
if style == nil {
|
||||
style = styles.Fallback
|
||||
}
|
||||
formatter := formatters.Get("html")
|
||||
if formatter == nil {
|
||||
formatter = formatters.Fallback
|
||||
}
|
||||
```
|
||||
|
||||
Then obtain an iterator over the tokens:
|
||||
|
||||
```go
|
||||
contents, err := ioutil.ReadAll(r)
|
||||
iterator, err := lexer.Tokenise(nil, string(contents))
|
||||
```
|
||||
|
||||
And finally, format the tokens from the iterator:
|
||||
|
||||
```go
|
||||
err := formatter.Format(w, style, iterator)
|
||||
```
|
||||
|
||||
### The HTML formatter
|
||||
|
||||
By default the `html` registered formatter generates standalone HTML with
|
||||
embedded CSS. More flexibility is available through the `formatters/html` package.
|
||||
|
||||
Firstly, the output generated by the formatter can be customised with the
|
||||
following constructor options:
|
||||
|
||||
- `Standalone()` - generate standalone HTML with embedded CSS.
|
||||
- `WithClasses()` - use classes rather than inlined style attributes.
|
||||
- `ClassPrefix(prefix)` - prefix each generated CSS class.
|
||||
- `TabWidth(width)` - Set the rendered tab width, in characters.
|
||||
- `WithLineNumbers()` - Render line numbers (style with `LineNumbers`).
|
||||
- `HighlightLines(ranges)` - Highlight lines in these ranges (style with `LineHighlight`).
|
||||
- `LineNumbersInTable()` - Use a table for formatting line numbers and code, rather than spans.
|
||||
|
||||
If `WithClasses()` is used, the corresponding CSS can be obtained from the formatter with:
|
||||
|
||||
```go
|
||||
formatter := html.New(html.WithClasses())
|
||||
err := formatter.WriteCSS(w, style)
|
||||
```
|
||||
|
||||
## More detail
|
||||
|
||||
### Lexers
|
||||
|
||||
See the [Pygments documentation](http://pygments.org/docs/lexerdevelopment/)
|
||||
for details on implementing lexers. Most concepts apply directly to Chroma,
|
||||
but see existing lexer implementations for real examples.
|
||||
|
||||
In many cases lexers can be automatically converted directly from Pygments by
|
||||
using the included Python 3 script `pygments2chroma.py`. I use something like
|
||||
the following:
|
||||
|
||||
```sh
|
||||
python3 ~/Projects/chroma/_tools/pygments2chroma.py \
|
||||
pygments.lexers.jvm.KotlinLexer \
|
||||
> ~/Projects/chroma/lexers/kotlin.go \
|
||||
&& gofmt -s -w ~/Projects/chroma/lexers/*.go
|
||||
```
|
||||
|
||||
See notes in [pygments-lexers.go](https://github.com/alecthomas/chroma/blob/master/pygments-lexers.txt)
|
||||
for a list of lexers, and notes on some of the issues importing them.
|
||||
|
||||
### Formatters
|
||||
|
||||
Chroma supports HTML output, as well as terminal output in 8 colour, 256 colour, and true-colour.
|
||||
|
||||
A `noop` formatter is included that outputs the token text only, and a `tokens`
|
||||
formatter outputs raw tokens. The latter is useful for debugging lexers.
|
||||
|
||||
### Styles
|
||||
|
||||
Chroma styles use the [same syntax](http://pygments.org/docs/styles/) as Pygments.
|
||||
|
||||
All Pygments styles have been converted to Chroma using the `_tools/style.py` script.
|
||||
|
||||
When you work with one of [Chroma's styles](https://github.com/alecthomas/chroma/tree/master/styles), know that the `chroma.Background` token type provides the default style for tokens. It does so by defining a foreground color and background color.
|
||||
|
||||
For example, this gives each token name not defined in the style a default color of `#f8f8f8` and uses `#000000` for the highlighted code block's background:
|
||||
|
||||
~~~go
|
||||
chroma.Background: "#f8f8f2 bg:#000000",
|
||||
~~~
|
||||
|
||||
Also, token types in a style file are hierarchical. For instance, when `CommentSpecial` is not defined, Chroma uses the token style from `Comment`. So when several comment tokens use the same color, you'll only need to define `Comment` and override the one that has a different color.
|
||||
|
||||
For a quick overview of the available styles and how they look, check out the [Chroma Style Gallery](https://xyproto.github.io/splash/docs/).
|
||||
|
||||
## Command-line interface
|
||||
|
||||
A command-line interface to Chroma is included. It can be installed with:
|
||||
|
||||
```sh
|
||||
go get -u github.com/alecthomas/chroma/cmd/chroma
|
||||
```
|
||||
|
||||
## What's missing compared to Pygments?
|
||||
|
||||
- Quite a few lexers, for various reasons (pull-requests welcome):
|
||||
- Pygments lexers for complex languages often include custom code to
|
||||
handle certain aspects, such as Perl6's ability to nest code inside
|
||||
regular expressions. These require time and effort to convert.
|
||||
- I mostly only converted languages I had heard of, to reduce the porting cost.
|
||||
- Some more esoteric features of Pygments are omitted for simplicity.
|
||||
- Though the Chroma API supports content detection, very few languages support them.
|
||||
I have plans to implement a statistical analyser at some point, but not enough time.
|
35
vendor/github.com/alecthomas/chroma/coalesce.go
generated
vendored
Normal file
35
vendor/github.com/alecthomas/chroma/coalesce.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package chroma
|
||||
|
||||
// Coalesce is a Lexer interceptor that collapses runs of common types into a single token.
|
||||
func Coalesce(lexer Lexer) Lexer { return &coalescer{lexer} }
|
||||
|
||||
type coalescer struct{ Lexer }
|
||||
|
||||
func (d *coalescer) Tokenise(options *TokeniseOptions, text string) (Iterator, error) {
|
||||
var prev Token
|
||||
it, err := d.Lexer.Tokenise(options, text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func() Token {
|
||||
for token := it(); token != (EOF); token = it() {
|
||||
if len(token.Value) == 0 {
|
||||
continue
|
||||
}
|
||||
if prev == EOF {
|
||||
prev = token
|
||||
} else {
|
||||
if prev.Type == token.Type && len(prev.Value) < 8192 {
|
||||
prev.Value += token.Value
|
||||
} else {
|
||||
out := prev
|
||||
prev = token
|
||||
return out
|
||||
}
|
||||
}
|
||||
}
|
||||
out := prev
|
||||
prev = EOF
|
||||
return out
|
||||
}, nil
|
||||
}
|
164
vendor/github.com/alecthomas/chroma/colour.go
generated
vendored
Normal file
164
vendor/github.com/alecthomas/chroma/colour.go
generated
vendored
Normal file
@ -0,0 +1,164 @@
|
||||
package chroma
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ANSI2RGB maps ANSI colour names, as supported by Chroma, to hex RGB values.
|
||||
var ANSI2RGB = map[string]string{
|
||||
"#ansiblack": "000000",
|
||||
"#ansidarkred": "7f0000",
|
||||
"#ansidarkgreen": "007f00",
|
||||
"#ansibrown": "7f7fe0",
|
||||
"#ansidarkblue": "00007f",
|
||||
"#ansipurple": "7f007f",
|
||||
"#ansiteal": "007f7f",
|
||||
"#ansilightgray": "e5e5e5",
|
||||
// Normal
|
||||
"#ansidarkgray": "555555",
|
||||
"#ansired": "ff0000",
|
||||
"#ansigreen": "00ff00",
|
||||
"#ansiyellow": "ffff00",
|
||||
"#ansiblue": "0000ff",
|
||||
"#ansifuchsia": "ff00ff",
|
||||
"#ansiturquoise": "00ffff",
|
||||
"#ansiwhite": "ffffff",
|
||||
|
||||
// Aliases without the "ansi" prefix, because...why?
|
||||
"#black": "000000",
|
||||
"#darkred": "7f0000",
|
||||
"#darkgreen": "007f00",
|
||||
"#brown": "7f7fe0",
|
||||
"#darkblue": "00007f",
|
||||
"#purple": "7f007f",
|
||||
"#teal": "007f7f",
|
||||
"#lightgray": "e5e5e5",
|
||||
// Normal
|
||||
"#darkgray": "555555",
|
||||
"#red": "ff0000",
|
||||
"#green": "00ff00",
|
||||
"#yellow": "ffff00",
|
||||
"#blue": "0000ff",
|
||||
"#fuchsia": "ff00ff",
|
||||
"#turquoise": "00ffff",
|
||||
"#white": "ffffff",
|
||||
}
|
||||
|
||||
// Colour represents an RGB colour.
|
||||
type Colour int32
|
||||
|
||||
// NewColour creates a Colour directly from RGB values.
|
||||
func NewColour(r, g, b uint8) Colour {
|
||||
return ParseColour(fmt.Sprintf("%02x%02x%02x", r, g, b))
|
||||
}
|
||||
|
||||
// Distance between this colour and another.
|
||||
//
|
||||
// This uses the approach described here (https://www.compuphase.com/cmetric.htm).
|
||||
// This is not as accurate as LAB, et. al. but is *vastly* simpler and sufficient for our needs.
|
||||
func (c Colour) Distance(e2 Colour) float64 {
|
||||
ar, ag, ab := int64(c.Red()), int64(c.Green()), int64(c.Blue())
|
||||
br, bg, bb := int64(e2.Red()), int64(e2.Green()), int64(e2.Blue())
|
||||
rmean := (ar + br) / 2
|
||||
r := ar - br
|
||||
g := ag - bg
|
||||
b := ab - bb
|
||||
return math.Sqrt(float64((((512 + rmean) * r * r) >> 8) + 4*g*g + (((767 - rmean) * b * b) >> 8)))
|
||||
}
|
||||
|
||||
// Brighten returns a copy of this colour with its brightness adjusted.
|
||||
//
|
||||
// If factor is negative, the colour is darkened.
|
||||
//
|
||||
// Uses approach described here (http://www.pvladov.com/2012/09/make-color-lighter-or-darker.html).
|
||||
func (c Colour) Brighten(factor float64) Colour {
|
||||
r := float64(c.Red())
|
||||
g := float64(c.Green())
|
||||
b := float64(c.Blue())
|
||||
|
||||
if factor < 0 {
|
||||
factor++
|
||||
r *= factor
|
||||
g *= factor
|
||||
b *= factor
|
||||
} else {
|
||||
r = (255-r)*factor + r
|
||||
g = (255-g)*factor + g
|
||||
b = (255-b)*factor + b
|
||||
}
|
||||
return NewColour(uint8(r), uint8(g), uint8(b))
|
||||
}
|
||||
|
||||
// BrightenOrDarken brightens a colour if it is < 0.5 brighteness or darkens if > 0.5 brightness.
|
||||
func (c Colour) BrightenOrDarken(factor float64) Colour {
|
||||
if c.Brightness() < 0.5 {
|
||||
return c.Brighten(factor)
|
||||
}
|
||||
return c.Brighten(-factor)
|
||||
}
|
||||
|
||||
// Brightness of the colour (roughly) in the range 0.0 to 1.0
|
||||
func (c Colour) Brightness() float64 {
|
||||
return (float64(c.Red()) + float64(c.Green()) + float64(c.Blue())) / 255.0 / 3.0
|
||||
}
|
||||
|
||||
// ParseColour in the forms #rgb, #rrggbb, #ansi<colour>, or #<colour>.
|
||||
// Will return an "unset" colour if invalid.
|
||||
func ParseColour(colour string) Colour {
|
||||
colour = normaliseColour(colour)
|
||||
n, err := strconv.ParseUint(colour, 16, 32)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return Colour(n + 1)
|
||||
}
|
||||
|
||||
// MustParseColour is like ParseColour except it panics if the colour is invalid.
|
||||
//
|
||||
// Will panic if colour is in an invalid format.
|
||||
func MustParseColour(colour string) Colour {
|
||||
parsed := ParseColour(colour)
|
||||
if !parsed.IsSet() {
|
||||
panic(fmt.Errorf("invalid colour %q", colour))
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
// IsSet returns true if the colour is set.
|
||||
func (c Colour) IsSet() bool { return c != 0 }
|
||||
|
||||
func (c Colour) String() string { return fmt.Sprintf("#%06x", int(c-1)) }
|
||||
func (c Colour) GoString() string { return fmt.Sprintf("Colour(0x%06x)", int(c-1)) }
|
||||
|
||||
// Red component of colour.
|
||||
func (c Colour) Red() uint8 { return uint8(((c - 1) >> 16) & 0xff) }
|
||||
|
||||
// Green component of colour.
|
||||
func (c Colour) Green() uint8 { return uint8(((c - 1) >> 8) & 0xff) }
|
||||
|
||||
// Blue component of colour.
|
||||
func (c Colour) Blue() uint8 { return uint8((c - 1) & 0xff) }
|
||||
|
||||
// Colours is an orderable set of colours.
|
||||
type Colours []Colour
|
||||
|
||||
func (c Colours) Len() int { return len(c) }
|
||||
func (c Colours) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
func (c Colours) Less(i, j int) bool { return c[i] < c[j] }
|
||||
|
||||
// Convert colours to #rrggbb.
|
||||
func normaliseColour(colour string) string {
|
||||
if ansi, ok := ANSI2RGB[colour]; ok {
|
||||
return ansi
|
||||
}
|
||||
if strings.HasPrefix(colour, "#") {
|
||||
colour = colour[1:]
|
||||
if len(colour) == 3 {
|
||||
return colour[0:1] + colour[0:1] + colour[1:2] + colour[1:2] + colour[2:3] + colour[2:3]
|
||||
}
|
||||
}
|
||||
return colour
|
||||
}
|
137
vendor/github.com/alecthomas/chroma/delegate.go
generated
vendored
Normal file
137
vendor/github.com/alecthomas/chroma/delegate.go
generated
vendored
Normal file
@ -0,0 +1,137 @@
|
||||
package chroma
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type delegatingLexer struct {
|
||||
root Lexer
|
||||
language Lexer
|
||||
}
|
||||
|
||||
// DelegatingLexer combines two lexers to handle the common case of a language embedded inside another, such as PHP
|
||||
// inside HTML or PHP inside plain text.
|
||||
//
|
||||
// It takes two lexer as arguments: a root lexer and a language lexer. First everything is scanned using the language
|
||||
// lexer, which must return "Other" for unrecognised tokens. Then all "Other" tokens are lexed using the root lexer.
|
||||
// Finally, these two sets of tokens are merged.
|
||||
//
|
||||
// The lexers from the template lexer package use this base lexer.
|
||||
func DelegatingLexer(root Lexer, language Lexer) Lexer {
|
||||
return &delegatingLexer{
|
||||
root: root,
|
||||
language: language,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *delegatingLexer) Config() *Config {
|
||||
return d.language.Config()
|
||||
}
|
||||
|
||||
// An insertion is the character range where language tokens should be inserted.
|
||||
type insertion struct {
|
||||
start, end int
|
||||
tokens []Token
|
||||
}
|
||||
|
||||
func (d *delegatingLexer) Tokenise(options *TokeniseOptions, text string) (Iterator, error) {
|
||||
tokens, err := Tokenise(Coalesce(d.language), options, text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Compute insertions and gather "Other" tokens.
|
||||
others := &bytes.Buffer{}
|
||||
insertions := []*insertion{}
|
||||
var insert *insertion
|
||||
offset := 0
|
||||
var last Token
|
||||
for _, t := range tokens {
|
||||
if t.Type == Other {
|
||||
if last != EOF && insert != nil && last.Type != Other {
|
||||
insert.end = offset
|
||||
}
|
||||
others.WriteString(t.Value)
|
||||
} else {
|
||||
if last == EOF || last.Type == Other {
|
||||
insert = &insertion{start: offset}
|
||||
insertions = append(insertions, insert)
|
||||
}
|
||||
insert.tokens = append(insert.tokens, t)
|
||||
}
|
||||
last = t
|
||||
offset += len(t.Value)
|
||||
}
|
||||
|
||||
if len(insertions) == 0 {
|
||||
return d.root.Tokenise(options, text)
|
||||
}
|
||||
|
||||
// Lex the other tokens.
|
||||
rootTokens, err := Tokenise(Coalesce(d.root), options, others.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Interleave the two sets of tokens.
|
||||
var out []Token
|
||||
offset = 0 // Offset into text.
|
||||
tokenIndex := 0
|
||||
nextToken := func() Token {
|
||||
if tokenIndex >= len(rootTokens) {
|
||||
return EOF
|
||||
}
|
||||
t := rootTokens[tokenIndex]
|
||||
tokenIndex++
|
||||
return t
|
||||
}
|
||||
insertionIndex := 0
|
||||
nextInsertion := func() *insertion {
|
||||
if insertionIndex >= len(insertions) {
|
||||
return nil
|
||||
}
|
||||
i := insertions[insertionIndex]
|
||||
insertionIndex++
|
||||
return i
|
||||
}
|
||||
t := nextToken()
|
||||
i := nextInsertion()
|
||||
for t != EOF || i != nil {
|
||||
// fmt.Printf("%d->%d:%q %d->%d:%q\n", offset, offset+len(t.Value), t.Value, i.start, i.end, Stringify(i.tokens...))
|
||||
if t == EOF || (i != nil && i.start < offset+len(t.Value)) {
|
||||
var l Token
|
||||
l, t = splitToken(t, i.start-offset)
|
||||
if l != EOF {
|
||||
out = append(out, l)
|
||||
offset += len(l.Value)
|
||||
}
|
||||
out = append(out, i.tokens...)
|
||||
offset += i.end - i.start
|
||||
if t == EOF {
|
||||
t = nextToken()
|
||||
}
|
||||
i = nextInsertion()
|
||||
} else {
|
||||
out = append(out, t)
|
||||
offset += len(t.Value)
|
||||
t = nextToken()
|
||||
}
|
||||
}
|
||||
return Literator(out...), nil
|
||||
}
|
||||
|
||||
func splitToken(t Token, offset int) (l Token, r Token) {
|
||||
if t == EOF {
|
||||
return EOF, EOF
|
||||
}
|
||||
if offset == 0 {
|
||||
return EOF, t
|
||||
}
|
||||
if offset == len(t.Value) {
|
||||
return t, EOF
|
||||
}
|
||||
l = t.Clone()
|
||||
r = t.Clone()
|
||||
l.Value = l.Value[:offset]
|
||||
r.Value = r.Value[offset:]
|
||||
return
|
||||
}
|
7
vendor/github.com/alecthomas/chroma/doc.go
generated
vendored
Normal file
7
vendor/github.com/alecthomas/chroma/doc.go
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
// Package chroma takes source code and other structured text and converts it into syntax highlighted HTML, ANSI-
|
||||
// coloured text, etc.
|
||||
//
|
||||
// Chroma is based heavily on Pygments, and includes translators for Pygments lexers and styles.
|
||||
//
|
||||
// For more information, go here: https://github.com/alecthomas/chroma
|
||||
package chroma
|
43
vendor/github.com/alecthomas/chroma/formatter.go
generated
vendored
Normal file
43
vendor/github.com/alecthomas/chroma/formatter.go
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
package chroma
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// A Formatter for Chroma lexers.
|
||||
type Formatter interface {
|
||||
// Format returns a formatting function for tokens.
|
||||
//
|
||||
// If the iterator panics, the Formatter should recover.
|
||||
Format(w io.Writer, style *Style, iterator Iterator) error
|
||||
}
|
||||
|
||||
// A FormatterFunc is a Formatter implemented as a function.
|
||||
//
|
||||
// Guards against iterator panics.
|
||||
type FormatterFunc func(w io.Writer, style *Style, iterator Iterator) error
|
||||
|
||||
func (f FormatterFunc) Format(w io.Writer, s *Style, it Iterator) (err error) { // nolint
|
||||
defer func() {
|
||||
if perr := recover(); perr != nil {
|
||||
err = perr.(error)
|
||||
}
|
||||
}()
|
||||
return f(w, s, it)
|
||||
}
|
||||
|
||||
type recoveringFormatter struct {
|
||||
Formatter
|
||||
}
|
||||
|
||||
func (r recoveringFormatter) Format(w io.Writer, s *Style, it Iterator) (err error) {
|
||||
defer func() {
|
||||
if perr := recover(); perr != nil {
|
||||
err = perr.(error)
|
||||
}
|
||||
}()
|
||||
return r.Formatter.Format(w, s, it)
|
||||
}
|
||||
|
||||
// RecoveringFormatter wraps a formatter with panic recovery.
|
||||
func RecoveringFormatter(formatter Formatter) Formatter { return recoveringFormatter{formatter} }
|
55
vendor/github.com/alecthomas/chroma/formatters/api.go
generated
vendored
Normal file
55
vendor/github.com/alecthomas/chroma/formatters/api.go
generated
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
package formatters
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/formatters/html"
|
||||
)
|
||||
|
||||
var (
|
||||
// NoOp formatter.
|
||||
NoOp = Register("noop", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, iterator chroma.Iterator) error {
|
||||
for t := iterator(); t != chroma.EOF; t = iterator() {
|
||||
if _, err := io.WriteString(w, t.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
// Default HTML formatter outputs self-contained HTML.
|
||||
htmlFull = Register("html", html.New(html.Standalone(), html.WithClasses())) // nolint
|
||||
)
|
||||
|
||||
// Fallback formatter.
|
||||
var Fallback = NoOp
|
||||
|
||||
// Registry of Formatters.
|
||||
var Registry = map[string]chroma.Formatter{}
|
||||
|
||||
// Names of registered formatters.
|
||||
func Names() []string {
|
||||
out := []string{}
|
||||
for name := range Registry {
|
||||
out = append(out, name)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// Get formatter by name.
|
||||
//
|
||||
// If the given formatter is not found, the Fallback formatter will be returned.
|
||||
func Get(name string) chroma.Formatter {
|
||||
if f, ok := Registry[name]; ok {
|
||||
return f
|
||||
}
|
||||
return Fallback
|
||||
}
|
||||
|
||||
// Register a named formatter.
|
||||
func Register(name string, formatter chroma.Formatter) chroma.Formatter {
|
||||
Registry[name] = formatter
|
||||
return formatter
|
||||
}
|
364
vendor/github.com/alecthomas/chroma/formatters/html/html.go
generated
vendored
Normal file
364
vendor/github.com/alecthomas/chroma/formatters/html/html.go
generated
vendored
Normal file
@ -0,0 +1,364 @@
|
||||
package html
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
)
|
||||
|
||||
// Option sets an option of the HTML formatter.
|
||||
type Option func(f *Formatter)
|
||||
|
||||
// Standalone configures the HTML formatter for generating a standalone HTML document.
|
||||
func Standalone() Option { return func(f *Formatter) { f.standalone = true } }
|
||||
|
||||
// ClassPrefix sets the CSS class prefix.
|
||||
func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } }
|
||||
|
||||
// WithClasses emits HTML using CSS classes, rather than inline styles.
|
||||
func WithClasses() Option { return func(f *Formatter) { f.Classes = true } }
|
||||
|
||||
// TabWidth sets the number of characters for a tab. Defaults to 8.
|
||||
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
|
||||
|
||||
// PreventSurroundingPre prevents the surrounding pre tags around the generated code
|
||||
func PreventSurroundingPre() Option { return func(f *Formatter) { f.preventSurroundingPre = true } }
|
||||
|
||||
// WithLineNumbers formats output with line numbers.
|
||||
func WithLineNumbers() Option {
|
||||
return func(f *Formatter) {
|
||||
f.lineNumbers = true
|
||||
}
|
||||
}
|
||||
|
||||
// LineNumbersInTable will, when combined with WithLineNumbers, separate the line numbers
|
||||
// and code in table td's, which make them copy-and-paste friendly.
|
||||
func LineNumbersInTable() Option {
|
||||
return func(f *Formatter) {
|
||||
f.lineNumbersInTable = true
|
||||
}
|
||||
}
|
||||
|
||||
// HighlightLines higlights the given line ranges with the Highlight style.
|
||||
//
|
||||
// A range is the beginning and ending of a range as 1-based line numbers, inclusive.
|
||||
func HighlightLines(ranges [][2]int) Option {
|
||||
return func(f *Formatter) {
|
||||
f.highlightRanges = ranges
|
||||
sort.Sort(f.highlightRanges)
|
||||
}
|
||||
}
|
||||
|
||||
// BaseLineNumber sets the initial number to start line numbering at. Defaults to 1.
|
||||
func BaseLineNumber(n int) Option {
|
||||
return func(f *Formatter) {
|
||||
f.baseLineNumber = n
|
||||
}
|
||||
}
|
||||
|
||||
// New HTML formatter.
|
||||
func New(options ...Option) *Formatter {
|
||||
f := &Formatter{
|
||||
baseLineNumber: 1,
|
||||
}
|
||||
for _, option := range options {
|
||||
option(f)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// Formatter that generates HTML.
|
||||
type Formatter struct {
|
||||
standalone bool
|
||||
prefix string
|
||||
Classes bool // Exported field to detect when classes are being used
|
||||
preventSurroundingPre bool
|
||||
tabWidth int
|
||||
lineNumbers bool
|
||||
lineNumbersInTable bool
|
||||
highlightRanges highlightRanges
|
||||
baseLineNumber int
|
||||
}
|
||||
|
||||
type highlightRanges [][2]int
|
||||
|
||||
func (h highlightRanges) Len() int { return len(h) }
|
||||
func (h highlightRanges) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
||||
func (h highlightRanges) Less(i, j int) bool { return h[i][0] < h[j][0] }
|
||||
|
||||
func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Iterator) (err error) {
|
||||
defer func() {
|
||||
if perr := recover(); perr != nil {
|
||||
err = perr.(error)
|
||||
}
|
||||
}()
|
||||
return f.writeHTML(w, style, iterator.Tokens())
|
||||
}
|
||||
|
||||
// We deliberately don't use html/template here because it is two orders of magnitude slower (benchmarked).
|
||||
//
|
||||
// OTOH we need to be super careful about correct escaping...
|
||||
func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.Token) (err error) { // nolint: gocyclo
|
||||
css := f.styleToCSS(style)
|
||||
if !f.Classes {
|
||||
for t, style := range css {
|
||||
css[t] = compressStyle(style)
|
||||
}
|
||||
}
|
||||
if f.standalone {
|
||||
fmt.Fprint(w, "<html>\n")
|
||||
if f.Classes {
|
||||
fmt.Fprint(w, "<style type=\"text/css\">\n")
|
||||
err = f.WriteCSS(w, style)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(w, "body { %s; }\n", css[chroma.Background])
|
||||
fmt.Fprint(w, "</style>")
|
||||
}
|
||||
fmt.Fprintf(w, "<body%s>\n", f.styleAttr(css, chroma.Background))
|
||||
}
|
||||
|
||||
wrapInTable := f.lineNumbers && f.lineNumbersInTable
|
||||
|
||||
lines := chroma.SplitTokensIntoLines(tokens)
|
||||
lineDigits := len(fmt.Sprintf("%d", len(lines)))
|
||||
highlightIndex := 0
|
||||
|
||||
if wrapInTable {
|
||||
// List line numbers in its own <td>
|
||||
fmt.Fprintf(w, "<div%s>\n", f.styleAttr(css, chroma.Background))
|
||||
fmt.Fprintf(w, "<table%s><tr>", f.styleAttr(css, chroma.LineTable))
|
||||
fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD))
|
||||
if !f.preventSurroundingPre {
|
||||
fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background))
|
||||
}
|
||||
for index := range lines {
|
||||
line := f.baseLineNumber + index
|
||||
highlight, next := f.shouldHighlight(highlightIndex, line)
|
||||
if next {
|
||||
highlightIndex++
|
||||
}
|
||||
if highlight {
|
||||
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "<span%s>%*d\n</span>", f.styleAttr(css, chroma.LineNumbersTable), lineDigits, line)
|
||||
|
||||
if highlight {
|
||||
fmt.Fprintf(w, "</span>")
|
||||
}
|
||||
}
|
||||
if !f.preventSurroundingPre {
|
||||
fmt.Fprint(w, "</pre>")
|
||||
}
|
||||
fmt.Fprint(w, "</td>\n")
|
||||
fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD))
|
||||
}
|
||||
|
||||
if !f.preventSurroundingPre {
|
||||
fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background))
|
||||
}
|
||||
highlightIndex = 0
|
||||
for index, tokens := range lines {
|
||||
// 1-based line number.
|
||||
line := f.baseLineNumber + index
|
||||
highlight, next := f.shouldHighlight(highlightIndex, line)
|
||||
if next {
|
||||
highlightIndex++
|
||||
}
|
||||
if highlight {
|
||||
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
|
||||
}
|
||||
|
||||
if f.lineNumbers && !wrapInTable {
|
||||
fmt.Fprintf(w, "<span%s>%*d</span>", f.styleAttr(css, chroma.LineNumbers), lineDigits, line)
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
html := html.EscapeString(token.String())
|
||||
attr := f.styleAttr(css, token.Type)
|
||||
if attr != "" {
|
||||
html = fmt.Sprintf("<span%s>%s</span>", attr, html)
|
||||
}
|
||||
fmt.Fprint(w, html)
|
||||
}
|
||||
if highlight {
|
||||
fmt.Fprintf(w, "</span>")
|
||||
}
|
||||
}
|
||||
|
||||
if !f.preventSurroundingPre {
|
||||
fmt.Fprint(w, "</pre>")
|
||||
}
|
||||
|
||||
if wrapInTable {
|
||||
fmt.Fprint(w, "</td></tr></table>\n")
|
||||
fmt.Fprint(w, "</div>\n")
|
||||
}
|
||||
|
||||
if f.standalone {
|
||||
fmt.Fprint(w, "\n</body>\n")
|
||||
fmt.Fprint(w, "</html>\n")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Formatter) shouldHighlight(highlightIndex, line int) (bool, bool) {
|
||||
next := false
|
||||
for highlightIndex < len(f.highlightRanges) && line > f.highlightRanges[highlightIndex][1] {
|
||||
highlightIndex++
|
||||
next = true
|
||||
}
|
||||
if highlightIndex < len(f.highlightRanges) {
|
||||
hrange := f.highlightRanges[highlightIndex]
|
||||
if line >= hrange[0] && line <= hrange[1] {
|
||||
return true, next
|
||||
}
|
||||
}
|
||||
return false, next
|
||||
}
|
||||
|
||||
func (f *Formatter) class(t chroma.TokenType) string {
|
||||
for t != 0 {
|
||||
if cls, ok := chroma.StandardTypes[t]; ok {
|
||||
if cls != "" {
|
||||
return f.prefix + cls
|
||||
}
|
||||
return ""
|
||||
}
|
||||
t = t.Parent()
|
||||
}
|
||||
if cls := chroma.StandardTypes[t]; cls != "" {
|
||||
return f.prefix + cls
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f *Formatter) styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
|
||||
if f.Classes {
|
||||
cls := f.class(tt)
|
||||
if cls == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(` class="%s"`, cls)
|
||||
}
|
||||
if _, ok := styles[tt]; !ok {
|
||||
tt = tt.SubCategory()
|
||||
if _, ok := styles[tt]; !ok {
|
||||
tt = tt.Category()
|
||||
if _, ok := styles[tt]; !ok {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf(` style="%s"`, styles[tt])
|
||||
}
|
||||
|
||||
func (f *Formatter) tabWidthStyle() string {
|
||||
if f.tabWidth != 0 && f.tabWidth != 8 {
|
||||
return fmt.Sprintf("; -moz-tab-size: %[1]d; -o-tab-size: %[1]d; tab-size: %[1]d", f.tabWidth)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// WriteCSS writes CSS style definitions (without any surrounding HTML).
|
||||
func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error {
|
||||
css := f.styleToCSS(style)
|
||||
// Special-case background as it is mapped to the outer ".chroma" class.
|
||||
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma { %s }\n", chroma.Background, f.prefix, css[chroma.Background]); err != nil {
|
||||
return err
|
||||
}
|
||||
// Special-case code column of table to expand width.
|
||||
if f.lineNumbers && f.lineNumbersInTable {
|
||||
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s:last-child { width: 100%%; }",
|
||||
chroma.LineTableTD, f.prefix, f.class(chroma.LineTableTD)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
tts := []int{}
|
||||
for tt := range css {
|
||||
tts = append(tts, int(tt))
|
||||
}
|
||||
sort.Ints(tts)
|
||||
for _, ti := range tts {
|
||||
tt := chroma.TokenType(ti)
|
||||
if tt == chroma.Background {
|
||||
continue
|
||||
}
|
||||
styles := css[tt]
|
||||
if _, err := fmt.Fprintf(w, "/* %s */ .%schroma .%s { %s }\n", tt, f.prefix, f.class(tt), styles); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string {
|
||||
classes := map[chroma.TokenType]string{}
|
||||
bg := style.Get(chroma.Background)
|
||||
// Convert the style.
|
||||
for t := range chroma.StandardTypes {
|
||||
entry := style.Get(t)
|
||||
if t != chroma.Background {
|
||||
entry = entry.Sub(bg)
|
||||
}
|
||||
if entry.IsZero() {
|
||||
continue
|
||||
}
|
||||
classes[t] = StyleEntryToCSS(entry)
|
||||
}
|
||||
classes[chroma.Background] += f.tabWidthStyle()
|
||||
lineNumbersStyle := "margin-right: 0.4em; padding: 0 0.4em 0 0.4em;"
|
||||
// All rules begin with default rules followed by user provided rules
|
||||
classes[chroma.LineNumbers] = lineNumbersStyle + classes[chroma.LineNumbers]
|
||||
classes[chroma.LineNumbersTable] = lineNumbersStyle + classes[chroma.LineNumbersTable]
|
||||
classes[chroma.LineHighlight] = "display: block; width: 100%;" + classes[chroma.LineHighlight]
|
||||
classes[chroma.LineTable] = "border-spacing: 0; padding: 0; margin: 0; border: 0; width: auto; overflow: auto; display: block;" + classes[chroma.LineTable]
|
||||
classes[chroma.LineTableTD] = "vertical-align: top; padding: 0; margin: 0; border: 0;" + classes[chroma.LineTableTD]
|
||||
return classes
|
||||
}
|
||||
|
||||
// StyleEntryToCSS converts a chroma.StyleEntry to CSS attributes.
|
||||
func StyleEntryToCSS(e chroma.StyleEntry) string {
|
||||
styles := []string{}
|
||||
if e.Colour.IsSet() {
|
||||
styles = append(styles, "color: "+e.Colour.String())
|
||||
}
|
||||
if e.Background.IsSet() {
|
||||
styles = append(styles, "background-color: "+e.Background.String())
|
||||
}
|
||||
if e.Bold == chroma.Yes {
|
||||
styles = append(styles, "font-weight: bold")
|
||||
}
|
||||
if e.Italic == chroma.Yes {
|
||||
styles = append(styles, "font-style: italic")
|
||||
}
|
||||
if e.Underline == chroma.Yes {
|
||||
styles = append(styles, "text-decoration: underline")
|
||||
}
|
||||
return strings.Join(styles, "; ")
|
||||
}
|
||||
|
||||
// Compress CSS attributes - remove spaces, transform 6-digit colours to 3.
|
||||
func compressStyle(s string) string {
|
||||
parts := strings.Split(s, ";")
|
||||
out := []string{}
|
||||
for _, p := range parts {
|
||||
p = strings.Join(strings.Fields(p), " ")
|
||||
p = strings.Replace(p, ": ", ":", 1)
|
||||
if strings.Contains(p, "#") {
|
||||
c := p[len(p)-6:]
|
||||
if c[0] == c[1] && c[2] == c[3] && c[4] == c[5] {
|
||||
p = p[:len(p)-6] + c[0:1] + c[2:3] + c[4:5]
|
||||
}
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return strings.Join(out, ";")
|
||||
}
|
31
vendor/github.com/alecthomas/chroma/formatters/json.go
generated
vendored
Normal file
31
vendor/github.com/alecthomas/chroma/formatters/json.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
package formatters
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
)
|
||||
|
||||
// JSON formatter outputs the raw token structures as JSON.
|
||||
var JSON = Register("json", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, it chroma.Iterator) error {
|
||||
fmt.Fprintln(w, "[")
|
||||
i := 0
|
||||
for t := it(); t != chroma.EOF; t = it() {
|
||||
if i > 0 {
|
||||
fmt.Fprintln(w, ",")
|
||||
}
|
||||
i++
|
||||
bytes, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprint(w, " "+string(bytes)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintln(w, "]")
|
||||
return nil
|
||||
}))
|
18
vendor/github.com/alecthomas/chroma/formatters/tokens.go
generated
vendored
Normal file
18
vendor/github.com/alecthomas/chroma/formatters/tokens.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package formatters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
)
|
||||
|
||||
// Tokens formatter outputs the raw token structures.
|
||||
var Tokens = Register("tokens", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, it chroma.Iterator) error {
|
||||
for t := it(); t != chroma.EOF; t = it() {
|
||||
if _, err := fmt.Fprintln(w, t.GoString()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}))
|
250
vendor/github.com/alecthomas/chroma/formatters/tty_indexed.go
generated
vendored
Normal file
250
vendor/github.com/alecthomas/chroma/formatters/tty_indexed.go
generated
vendored
Normal file
@ -0,0 +1,250 @@
|
||||
package formatters
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
)
|
||||
|
||||
type ttyTable struct {
|
||||
foreground map[chroma.Colour]string
|
||||
background map[chroma.Colour]string
|
||||
}
|
||||
|
||||
var c = chroma.MustParseColour
|
||||
|
||||
var ttyTables = map[int]*ttyTable{
|
||||
8: {
|
||||
foreground: map[chroma.Colour]string{
|
||||
c("#000000"): "\033[30m", c("#7f0000"): "\033[31m", c("#007f00"): "\033[32m", c("#7f7fe0"): "\033[33m",
|
||||
c("#00007f"): "\033[34m", c("#7f007f"): "\033[35m", c("#007f7f"): "\033[36m", c("#e5e5e5"): "\033[37m",
|
||||
c("#555555"): "\033[90m", c("#ff0000"): "\033[91m", c("#00ff00"): "\033[92m", c("#ffff00"): "\033[93m",
|
||||
c("#0000ff"): "\033[94m", c("#ff00ff"): "\033[95m", c("#00ffff"): "\033[96m", c("#ffffff"): "\033[97m",
|
||||
},
|
||||
background: map[chroma.Colour]string{
|
||||
c("#000000"): "\033[40m", c("#7f0000"): "\033[41m", c("#007f00"): "\033[42m", c("#7f7fe0"): "\033[43m",
|
||||
c("#00007f"): "\033[44m", c("#7f007f"): "\033[45m", c("#007f7f"): "\033[46m", c("#e5e5e5"): "\033[47m",
|
||||
c("#555555"): "\033[100m", c("#ff0000"): "\033[101m", c("#00ff00"): "\033[102m", c("#ffff00"): "\033[103m",
|
||||
c("#0000ff"): "\033[104m", c("#ff00ff"): "\033[105m", c("#00ffff"): "\033[106m", c("#ffffff"): "\033[107m",
|
||||
},
|
||||
},
|
||||
256: {
|
||||
foreground: map[chroma.Colour]string{
|
||||
c("#000000"): "\033[38;5;0m", c("#800000"): "\033[38;5;1m", c("#008000"): "\033[38;5;2m", c("#808000"): "\033[38;5;3m",
|
||||
c("#000080"): "\033[38;5;4m", c("#800080"): "\033[38;5;5m", c("#008080"): "\033[38;5;6m", c("#c0c0c0"): "\033[38;5;7m",
|
||||
c("#808080"): "\033[38;5;8m", c("#ff0000"): "\033[38;5;9m", c("#00ff00"): "\033[38;5;10m", c("#ffff00"): "\033[38;5;11m",
|
||||
c("#0000ff"): "\033[38;5;12m", c("#ff00ff"): "\033[38;5;13m", c("#00ffff"): "\033[38;5;14m", c("#ffffff"): "\033[38;5;15m",
|
||||
c("#000000"): "\033[38;5;16m", c("#00005f"): "\033[38;5;17m", c("#000087"): "\033[38;5;18m", c("#0000af"): "\033[38;5;19m",
|
||||
c("#0000d7"): "\033[38;5;20m", c("#0000ff"): "\033[38;5;21m", c("#005f00"): "\033[38;5;22m", c("#005f5f"): "\033[38;5;23m",
|
||||
c("#005f87"): "\033[38;5;24m", c("#005faf"): "\033[38;5;25m", c("#005fd7"): "\033[38;5;26m", c("#005fff"): "\033[38;5;27m",
|
||||
c("#008700"): "\033[38;5;28m", c("#00875f"): "\033[38;5;29m", c("#008787"): "\033[38;5;30m", c("#0087af"): "\033[38;5;31m",
|
||||
c("#0087d7"): "\033[38;5;32m", c("#0087ff"): "\033[38;5;33m", c("#00af00"): "\033[38;5;34m", c("#00af5f"): "\033[38;5;35m",
|
||||
c("#00af87"): "\033[38;5;36m", c("#00afaf"): "\033[38;5;37m", c("#00afd7"): "\033[38;5;38m", c("#00afff"): "\033[38;5;39m",
|
||||
c("#00d700"): "\033[38;5;40m", c("#00d75f"): "\033[38;5;41m", c("#00d787"): "\033[38;5;42m", c("#00d7af"): "\033[38;5;43m",
|
||||
c("#00d7d7"): "\033[38;5;44m", c("#00d7ff"): "\033[38;5;45m", c("#00ff00"): "\033[38;5;46m", c("#00ff5f"): "\033[38;5;47m",
|
||||
c("#00ff87"): "\033[38;5;48m", c("#00ffaf"): "\033[38;5;49m", c("#00ffd7"): "\033[38;5;50m", c("#00ffff"): "\033[38;5;51m",
|
||||
c("#5f0000"): "\033[38;5;52m", c("#5f005f"): "\033[38;5;53m", c("#5f0087"): "\033[38;5;54m", c("#5f00af"): "\033[38;5;55m",
|
||||
c("#5f00d7"): "\033[38;5;56m", c("#5f00ff"): "\033[38;5;57m", c("#5f5f00"): "\033[38;5;58m", c("#5f5f5f"): "\033[38;5;59m",
|
||||
c("#5f5f87"): "\033[38;5;60m", c("#5f5faf"): "\033[38;5;61m", c("#5f5fd7"): "\033[38;5;62m", c("#5f5fff"): "\033[38;5;63m",
|
||||
c("#5f8700"): "\033[38;5;64m", c("#5f875f"): "\033[38;5;65m", c("#5f8787"): "\033[38;5;66m", c("#5f87af"): "\033[38;5;67m",
|
||||
c("#5f87d7"): "\033[38;5;68m", c("#5f87ff"): "\033[38;5;69m", c("#5faf00"): "\033[38;5;70m", c("#5faf5f"): "\033[38;5;71m",
|
||||
c("#5faf87"): "\033[38;5;72m", c("#5fafaf"): "\033[38;5;73m", c("#5fafd7"): "\033[38;5;74m", c("#5fafff"): "\033[38;5;75m",
|
||||
c("#5fd700"): "\033[38;5;76m", c("#5fd75f"): "\033[38;5;77m", c("#5fd787"): "\033[38;5;78m", c("#5fd7af"): "\033[38;5;79m",
|
||||
c("#5fd7d7"): "\033[38;5;80m", c("#5fd7ff"): "\033[38;5;81m", c("#5fff00"): "\033[38;5;82m", c("#5fff5f"): "\033[38;5;83m",
|
||||
c("#5fff87"): "\033[38;5;84m", c("#5fffaf"): "\033[38;5;85m", c("#5fffd7"): "\033[38;5;86m", c("#5fffff"): "\033[38;5;87m",
|
||||
c("#870000"): "\033[38;5;88m", c("#87005f"): "\033[38;5;89m", c("#870087"): "\033[38;5;90m", c("#8700af"): "\033[38;5;91m",
|
||||
c("#8700d7"): "\033[38;5;92m", c("#8700ff"): "\033[38;5;93m", c("#875f00"): "\033[38;5;94m", c("#875f5f"): "\033[38;5;95m",
|
||||
c("#875f87"): "\033[38;5;96m", c("#875faf"): "\033[38;5;97m", c("#875fd7"): "\033[38;5;98m", c("#875fff"): "\033[38;5;99m",
|
||||
c("#878700"): "\033[38;5;100m", c("#87875f"): "\033[38;5;101m", c("#878787"): "\033[38;5;102m", c("#8787af"): "\033[38;5;103m",
|
||||
c("#8787d7"): "\033[38;5;104m", c("#8787ff"): "\033[38;5;105m", c("#87af00"): "\033[38;5;106m", c("#87af5f"): "\033[38;5;107m",
|
||||
c("#87af87"): "\033[38;5;108m", c("#87afaf"): "\033[38;5;109m", c("#87afd7"): "\033[38;5;110m", c("#87afff"): "\033[38;5;111m",
|
||||
c("#87d700"): "\033[38;5;112m", c("#87d75f"): "\033[38;5;113m", c("#87d787"): "\033[38;5;114m", c("#87d7af"): "\033[38;5;115m",
|
||||
c("#87d7d7"): "\033[38;5;116m", c("#87d7ff"): "\033[38;5;117m", c("#87ff00"): "\033[38;5;118m", c("#87ff5f"): "\033[38;5;119m",
|
||||
c("#87ff87"): "\033[38;5;120m", c("#87ffaf"): "\033[38;5;121m", c("#87ffd7"): "\033[38;5;122m", c("#87ffff"): "\033[38;5;123m",
|
||||
c("#af0000"): "\033[38;5;124m", c("#af005f"): "\033[38;5;125m", c("#af0087"): "\033[38;5;126m", c("#af00af"): "\033[38;5;127m",
|
||||
c("#af00d7"): "\033[38;5;128m", c("#af00ff"): "\033[38;5;129m", c("#af5f00"): "\033[38;5;130m", c("#af5f5f"): "\033[38;5;131m",
|
||||
c("#af5f87"): "\033[38;5;132m", c("#af5faf"): "\033[38;5;133m", c("#af5fd7"): "\033[38;5;134m", c("#af5fff"): "\033[38;5;135m",
|
||||
c("#af8700"): "\033[38;5;136m", c("#af875f"): "\033[38;5;137m", c("#af8787"): "\033[38;5;138m", c("#af87af"): "\033[38;5;139m",
|
||||
c("#af87d7"): "\033[38;5;140m", c("#af87ff"): "\033[38;5;141m", c("#afaf00"): "\033[38;5;142m", c("#afaf5f"): "\033[38;5;143m",
|
||||
c("#afaf87"): "\033[38;5;144m", c("#afafaf"): "\033[38;5;145m", c("#afafd7"): "\033[38;5;146m", c("#afafff"): "\033[38;5;147m",
|
||||
c("#afd700"): "\033[38;5;148m", c("#afd75f"): "\033[38;5;149m", c("#afd787"): "\033[38;5;150m", c("#afd7af"): "\033[38;5;151m",
|
||||
c("#afd7d7"): "\033[38;5;152m", c("#afd7ff"): "\033[38;5;153m", c("#afff00"): "\033[38;5;154m", c("#afff5f"): "\033[38;5;155m",
|
||||
c("#afff87"): "\033[38;5;156m", c("#afffaf"): "\033[38;5;157m", c("#afffd7"): "\033[38;5;158m", c("#afffff"): "\033[38;5;159m",
|
||||
c("#d70000"): "\033[38;5;160m", c("#d7005f"): "\033[38;5;161m", c("#d70087"): "\033[38;5;162m", c("#d700af"): "\033[38;5;163m",
|
||||
c("#d700d7"): "\033[38;5;164m", c("#d700ff"): "\033[38;5;165m", c("#d75f00"): "\033[38;5;166m", c("#d75f5f"): "\033[38;5;167m",
|
||||
c("#d75f87"): "\033[38;5;168m", c("#d75faf"): "\033[38;5;169m", c("#d75fd7"): "\033[38;5;170m", c("#d75fff"): "\033[38;5;171m",
|
||||
c("#d78700"): "\033[38;5;172m", c("#d7875f"): "\033[38;5;173m", c("#d78787"): "\033[38;5;174m", c("#d787af"): "\033[38;5;175m",
|
||||
c("#d787d7"): "\033[38;5;176m", c("#d787ff"): "\033[38;5;177m", c("#d7af00"): "\033[38;5;178m", c("#d7af5f"): "\033[38;5;179m",
|
||||
c("#d7af87"): "\033[38;5;180m", c("#d7afaf"): "\033[38;5;181m", c("#d7afd7"): "\033[38;5;182m", c("#d7afff"): "\033[38;5;183m",
|
||||
c("#d7d700"): "\033[38;5;184m", c("#d7d75f"): "\033[38;5;185m", c("#d7d787"): "\033[38;5;186m", c("#d7d7af"): "\033[38;5;187m",
|
||||
c("#d7d7d7"): "\033[38;5;188m", c("#d7d7ff"): "\033[38;5;189m", c("#d7ff00"): "\033[38;5;190m", c("#d7ff5f"): "\033[38;5;191m",
|
||||
c("#d7ff87"): "\033[38;5;192m", c("#d7ffaf"): "\033[38;5;193m", c("#d7ffd7"): "\033[38;5;194m", c("#d7ffff"): "\033[38;5;195m",
|
||||
c("#ff0000"): "\033[38;5;196m", c("#ff005f"): "\033[38;5;197m", c("#ff0087"): "\033[38;5;198m", c("#ff00af"): "\033[38;5;199m",
|
||||
c("#ff00d7"): "\033[38;5;200m", c("#ff00ff"): "\033[38;5;201m", c("#ff5f00"): "\033[38;5;202m", c("#ff5f5f"): "\033[38;5;203m",
|
||||
c("#ff5f87"): "\033[38;5;204m", c("#ff5faf"): "\033[38;5;205m", c("#ff5fd7"): "\033[38;5;206m", c("#ff5fff"): "\033[38;5;207m",
|
||||
c("#ff8700"): "\033[38;5;208m", c("#ff875f"): "\033[38;5;209m", c("#ff8787"): "\033[38;5;210m", c("#ff87af"): "\033[38;5;211m",
|
||||
c("#ff87d7"): "\033[38;5;212m", c("#ff87ff"): "\033[38;5;213m", c("#ffaf00"): "\033[38;5;214m", c("#ffaf5f"): "\033[38;5;215m",
|
||||
c("#ffaf87"): "\033[38;5;216m", c("#ffafaf"): "\033[38;5;217m", c("#ffafd7"): "\033[38;5;218m", c("#ffafff"): "\033[38;5;219m",
|
||||
c("#ffd700"): "\033[38;5;220m", c("#ffd75f"): "\033[38;5;221m", c("#ffd787"): "\033[38;5;222m", c("#ffd7af"): "\033[38;5;223m",
|
||||
c("#ffd7d7"): "\033[38;5;224m", c("#ffd7ff"): "\033[38;5;225m", c("#ffff00"): "\033[38;5;226m", c("#ffff5f"): "\033[38;5;227m",
|
||||
c("#ffff87"): "\033[38;5;228m", c("#ffffaf"): "\033[38;5;229m", c("#ffffd7"): "\033[38;5;230m", c("#ffffff"): "\033[38;5;231m",
|
||||
c("#080808"): "\033[38;5;232m", c("#121212"): "\033[38;5;233m", c("#1c1c1c"): "\033[38;5;234m", c("#262626"): "\033[38;5;235m",
|
||||
c("#303030"): "\033[38;5;236m", c("#3a3a3a"): "\033[38;5;237m", c("#444444"): "\033[38;5;238m", c("#4e4e4e"): "\033[38;5;239m",
|
||||
c("#585858"): "\033[38;5;240m", c("#626262"): "\033[38;5;241m", c("#6c6c6c"): "\033[38;5;242m", c("#767676"): "\033[38;5;243m",
|
||||
c("#808080"): "\033[38;5;244m", c("#8a8a8a"): "\033[38;5;245m", c("#949494"): "\033[38;5;246m", c("#9e9e9e"): "\033[38;5;247m",
|
||||
c("#a8a8a8"): "\033[38;5;248m", c("#b2b2b2"): "\033[38;5;249m", c("#bcbcbc"): "\033[38;5;250m", c("#c6c6c6"): "\033[38;5;251m",
|
||||
c("#d0d0d0"): "\033[38;5;252m", c("#dadada"): "\033[38;5;253m", c("#e4e4e4"): "\033[38;5;254m", c("#eeeeee"): "\033[38;5;255m",
|
||||
},
|
||||
background: map[chroma.Colour]string{
|
||||
c("#000000"): "\033[48;5;0m", c("#800000"): "\033[48;5;1m", c("#008000"): "\033[48;5;2m", c("#808000"): "\033[48;5;3m",
|
||||
c("#000080"): "\033[48;5;4m", c("#800080"): "\033[48;5;5m", c("#008080"): "\033[48;5;6m", c("#c0c0c0"): "\033[48;5;7m",
|
||||
c("#808080"): "\033[48;5;8m", c("#ff0000"): "\033[48;5;9m", c("#00ff00"): "\033[48;5;10m", c("#ffff00"): "\033[48;5;11m",
|
||||
c("#0000ff"): "\033[48;5;12m", c("#ff00ff"): "\033[48;5;13m", c("#00ffff"): "\033[48;5;14m", c("#ffffff"): "\033[48;5;15m",
|
||||
c("#000000"): "\033[48;5;16m", c("#00005f"): "\033[48;5;17m", c("#000087"): "\033[48;5;18m", c("#0000af"): "\033[48;5;19m",
|
||||
c("#0000d7"): "\033[48;5;20m", c("#0000ff"): "\033[48;5;21m", c("#005f00"): "\033[48;5;22m", c("#005f5f"): "\033[48;5;23m",
|
||||
c("#005f87"): "\033[48;5;24m", c("#005faf"): "\033[48;5;25m", c("#005fd7"): "\033[48;5;26m", c("#005fff"): "\033[48;5;27m",
|
||||
c("#008700"): "\033[48;5;28m", c("#00875f"): "\033[48;5;29m", c("#008787"): "\033[48;5;30m", c("#0087af"): "\033[48;5;31m",
|
||||
c("#0087d7"): "\033[48;5;32m", c("#0087ff"): "\033[48;5;33m", c("#00af00"): "\033[48;5;34m", c("#00af5f"): "\033[48;5;35m",
|
||||
c("#00af87"): "\033[48;5;36m", c("#00afaf"): "\033[48;5;37m", c("#00afd7"): "\033[48;5;38m", c("#00afff"): "\033[48;5;39m",
|
||||
c("#00d700"): "\033[48;5;40m", c("#00d75f"): "\033[48;5;41m", c("#00d787"): "\033[48;5;42m", c("#00d7af"): "\033[48;5;43m",
|
||||
c("#00d7d7"): "\033[48;5;44m", c("#00d7ff"): "\033[48;5;45m", c("#00ff00"): "\033[48;5;46m", c("#00ff5f"): "\033[48;5;47m",
|
||||
c("#00ff87"): "\033[48;5;48m", c("#00ffaf"): "\033[48;5;49m", c("#00ffd7"): "\033[48;5;50m", c("#00ffff"): "\033[48;5;51m",
|
||||
c("#5f0000"): "\033[48;5;52m", c("#5f005f"): "\033[48;5;53m", c("#5f0087"): "\033[48;5;54m", c("#5f00af"): "\033[48;5;55m",
|
||||
c("#5f00d7"): "\033[48;5;56m", c("#5f00ff"): "\033[48;5;57m", c("#5f5f00"): "\033[48;5;58m", c("#5f5f5f"): "\033[48;5;59m",
|
||||
c("#5f5f87"): "\033[48;5;60m", c("#5f5faf"): "\033[48;5;61m", c("#5f5fd7"): "\033[48;5;62m", c("#5f5fff"): "\033[48;5;63m",
|
||||
c("#5f8700"): "\033[48;5;64m", c("#5f875f"): "\033[48;5;65m", c("#5f8787"): "\033[48;5;66m", c("#5f87af"): "\033[48;5;67m",
|
||||
c("#5f87d7"): "\033[48;5;68m", c("#5f87ff"): "\033[48;5;69m", c("#5faf00"): "\033[48;5;70m", c("#5faf5f"): "\033[48;5;71m",
|
||||
c("#5faf87"): "\033[48;5;72m", c("#5fafaf"): "\033[48;5;73m", c("#5fafd7"): "\033[48;5;74m", c("#5fafff"): "\033[48;5;75m",
|
||||
c("#5fd700"): "\033[48;5;76m", c("#5fd75f"): "\033[48;5;77m", c("#5fd787"): "\033[48;5;78m", c("#5fd7af"): "\033[48;5;79m",
|
||||
c("#5fd7d7"): "\033[48;5;80m", c("#5fd7ff"): "\033[48;5;81m", c("#5fff00"): "\033[48;5;82m", c("#5fff5f"): "\033[48;5;83m",
|
||||
c("#5fff87"): "\033[48;5;84m", c("#5fffaf"): "\033[48;5;85m", c("#5fffd7"): "\033[48;5;86m", c("#5fffff"): "\033[48;5;87m",
|
||||
c("#870000"): "\033[48;5;88m", c("#87005f"): "\033[48;5;89m", c("#870087"): "\033[48;5;90m", c("#8700af"): "\033[48;5;91m",
|
||||
c("#8700d7"): "\033[48;5;92m", c("#8700ff"): "\033[48;5;93m", c("#875f00"): "\033[48;5;94m", c("#875f5f"): "\033[48;5;95m",
|
||||
c("#875f87"): "\033[48;5;96m", c("#875faf"): "\033[48;5;97m", c("#875fd7"): "\033[48;5;98m", c("#875fff"): "\033[48;5;99m",
|
||||
c("#878700"): "\033[48;5;100m", c("#87875f"): "\033[48;5;101m", c("#878787"): "\033[48;5;102m", c("#8787af"): "\033[48;5;103m",
|
||||
c("#8787d7"): "\033[48;5;104m", c("#8787ff"): "\033[48;5;105m", c("#87af00"): "\033[48;5;106m", c("#87af5f"): "\033[48;5;107m",
|
||||
c("#87af87"): "\033[48;5;108m", c("#87afaf"): "\033[48;5;109m", c("#87afd7"): "\033[48;5;110m", c("#87afff"): "\033[48;5;111m",
|
||||
c("#87d700"): "\033[48;5;112m", c("#87d75f"): "\033[48;5;113m", c("#87d787"): "\033[48;5;114m", c("#87d7af"): "\033[48;5;115m",
|
||||
c("#87d7d7"): "\033[48;5;116m", c("#87d7ff"): "\033[48;5;117m", c("#87ff00"): "\033[48;5;118m", c("#87ff5f"): "\033[48;5;119m",
|
||||
c("#87ff87"): "\033[48;5;120m", c("#87ffaf"): "\033[48;5;121m", c("#87ffd7"): "\033[48;5;122m", c("#87ffff"): "\033[48;5;123m",
|
||||
c("#af0000"): "\033[48;5;124m", c("#af005f"): "\033[48;5;125m", c("#af0087"): "\033[48;5;126m", c("#af00af"): "\033[48;5;127m",
|
||||
c("#af00d7"): "\033[48;5;128m", c("#af00ff"): "\033[48;5;129m", c("#af5f00"): "\033[48;5;130m", c("#af5f5f"): "\033[48;5;131m",
|
||||
c("#af5f87"): "\033[48;5;132m", c("#af5faf"): "\033[48;5;133m", c("#af5fd7"): "\033[48;5;134m", c("#af5fff"): "\033[48;5;135m",
|
||||
c("#af8700"): "\033[48;5;136m", c("#af875f"): "\033[48;5;137m", c("#af8787"): "\033[48;5;138m", c("#af87af"): "\033[48;5;139m",
|
||||
c("#af87d7"): "\033[48;5;140m", c("#af87ff"): "\033[48;5;141m", c("#afaf00"): "\033[48;5;142m", c("#afaf5f"): "\033[48;5;143m",
|
||||
c("#afaf87"): "\033[48;5;144m", c("#afafaf"): "\033[48;5;145m", c("#afafd7"): "\033[48;5;146m", c("#afafff"): "\033[48;5;147m",
|
||||
c("#afd700"): "\033[48;5;148m", c("#afd75f"): "\033[48;5;149m", c("#afd787"): "\033[48;5;150m", c("#afd7af"): "\033[48;5;151m",
|
||||
c("#afd7d7"): "\033[48;5;152m", c("#afd7ff"): "\033[48;5;153m", c("#afff00"): "\033[48;5;154m", c("#afff5f"): "\033[48;5;155m",
|
||||
c("#afff87"): "\033[48;5;156m", c("#afffaf"): "\033[48;5;157m", c("#afffd7"): "\033[48;5;158m", c("#afffff"): "\033[48;5;159m",
|
||||
c("#d70000"): "\033[48;5;160m", c("#d7005f"): "\033[48;5;161m", c("#d70087"): "\033[48;5;162m", c("#d700af"): "\033[48;5;163m",
|
||||
c("#d700d7"): "\033[48;5;164m", c("#d700ff"): "\033[48;5;165m", c("#d75f00"): "\033[48;5;166m", c("#d75f5f"): "\033[48;5;167m",
|
||||
c("#d75f87"): "\033[48;5;168m", c("#d75faf"): "\033[48;5;169m", c("#d75fd7"): "\033[48;5;170m", c("#d75fff"): "\033[48;5;171m",
|
||||
c("#d78700"): "\033[48;5;172m", c("#d7875f"): "\033[48;5;173m", c("#d78787"): "\033[48;5;174m", c("#d787af"): "\033[48;5;175m",
|
||||
c("#d787d7"): "\033[48;5;176m", c("#d787ff"): "\033[48;5;177m", c("#d7af00"): "\033[48;5;178m", c("#d7af5f"): "\033[48;5;179m",
|
||||
c("#d7af87"): "\033[48;5;180m", c("#d7afaf"): "\033[48;5;181m", c("#d7afd7"): "\033[48;5;182m", c("#d7afff"): "\033[48;5;183m",
|
||||
c("#d7d700"): "\033[48;5;184m", c("#d7d75f"): "\033[48;5;185m", c("#d7d787"): "\033[48;5;186m", c("#d7d7af"): "\033[48;5;187m",
|
||||
c("#d7d7d7"): "\033[48;5;188m", c("#d7d7ff"): "\033[48;5;189m", c("#d7ff00"): "\033[48;5;190m", c("#d7ff5f"): "\033[48;5;191m",
|
||||
c("#d7ff87"): "\033[48;5;192m", c("#d7ffaf"): "\033[48;5;193m", c("#d7ffd7"): "\033[48;5;194m", c("#d7ffff"): "\033[48;5;195m",
|
||||
c("#ff0000"): "\033[48;5;196m", c("#ff005f"): "\033[48;5;197m", c("#ff0087"): "\033[48;5;198m", c("#ff00af"): "\033[48;5;199m",
|
||||
c("#ff00d7"): "\033[48;5;200m", c("#ff00ff"): "\033[48;5;201m", c("#ff5f00"): "\033[48;5;202m", c("#ff5f5f"): "\033[48;5;203m",
|
||||
c("#ff5f87"): "\033[48;5;204m", c("#ff5faf"): "\033[48;5;205m", c("#ff5fd7"): "\033[48;5;206m", c("#ff5fff"): "\033[48;5;207m",
|
||||
c("#ff8700"): "\033[48;5;208m", c("#ff875f"): "\033[48;5;209m", c("#ff8787"): "\033[48;5;210m", c("#ff87af"): "\033[48;5;211m",
|
||||
c("#ff87d7"): "\033[48;5;212m", c("#ff87ff"): "\033[48;5;213m", c("#ffaf00"): "\033[48;5;214m", c("#ffaf5f"): "\033[48;5;215m",
|
||||
c("#ffaf87"): "\033[48;5;216m", c("#ffafaf"): "\033[48;5;217m", c("#ffafd7"): "\033[48;5;218m", c("#ffafff"): "\033[48;5;219m",
|
||||
c("#ffd700"): "\033[48;5;220m", c("#ffd75f"): "\033[48;5;221m", c("#ffd787"): "\033[48;5;222m", c("#ffd7af"): "\033[48;5;223m",
|
||||
c("#ffd7d7"): "\033[48;5;224m", c("#ffd7ff"): "\033[48;5;225m", c("#ffff00"): "\033[48;5;226m", c("#ffff5f"): "\033[48;5;227m",
|
||||
c("#ffff87"): "\033[48;5;228m", c("#ffffaf"): "\033[48;5;229m", c("#ffffd7"): "\033[48;5;230m", c("#ffffff"): "\033[48;5;231m",
|
||||
c("#080808"): "\033[48;5;232m", c("#121212"): "\033[48;5;233m", c("#1c1c1c"): "\033[48;5;234m", c("#262626"): "\033[48;5;235m",
|
||||
c("#303030"): "\033[48;5;236m", c("#3a3a3a"): "\033[48;5;237m", c("#444444"): "\033[48;5;238m", c("#4e4e4e"): "\033[48;5;239m",
|
||||
c("#585858"): "\033[48;5;240m", c("#626262"): "\033[48;5;241m", c("#6c6c6c"): "\033[48;5;242m", c("#767676"): "\033[48;5;243m",
|
||||
c("#808080"): "\033[48;5;244m", c("#8a8a8a"): "\033[48;5;245m", c("#949494"): "\033[48;5;246m", c("#9e9e9e"): "\033[48;5;247m",
|
||||
c("#a8a8a8"): "\033[48;5;248m", c("#b2b2b2"): "\033[48;5;249m", c("#bcbcbc"): "\033[48;5;250m", c("#c6c6c6"): "\033[48;5;251m",
|
||||
c("#d0d0d0"): "\033[48;5;252m", c("#dadada"): "\033[48;5;253m", c("#e4e4e4"): "\033[48;5;254m", c("#eeeeee"): "\033[48;5;255m",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func entryToEscapeSequence(table *ttyTable, entry chroma.StyleEntry) string {
|
||||
out := ""
|
||||
if entry.Bold == chroma.Yes {
|
||||
out += "\033[1m"
|
||||
}
|
||||
if entry.Underline == chroma.Yes {
|
||||
out += "\033[4m"
|
||||
}
|
||||
if entry.Colour.IsSet() {
|
||||
out += table.foreground[findClosest(table, entry.Colour)]
|
||||
}
|
||||
if entry.Background.IsSet() {
|
||||
out += table.background[findClosest(table, entry.Background)]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func findClosest(table *ttyTable, seeking chroma.Colour) chroma.Colour {
|
||||
closestColour := chroma.Colour(0)
|
||||
closest := float64(math.MaxFloat64)
|
||||
for colour := range table.foreground {
|
||||
distance := colour.Distance(seeking)
|
||||
if distance < closest {
|
||||
closest = distance
|
||||
closestColour = colour
|
||||
}
|
||||
}
|
||||
return closestColour
|
||||
}
|
||||
|
||||
func styleToEscapeSequence(table *ttyTable, style *chroma.Style) map[chroma.TokenType]string {
|
||||
out := map[chroma.TokenType]string{}
|
||||
for _, ttype := range style.Types() {
|
||||
entry := style.Get(ttype)
|
||||
out[ttype] = entryToEscapeSequence(table, entry)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type indexedTTYFormatter struct {
|
||||
table *ttyTable
|
||||
}
|
||||
|
||||
func (c *indexedTTYFormatter) Format(w io.Writer, style *chroma.Style, it chroma.Iterator) (err error) {
|
||||
defer func() {
|
||||
if perr := recover(); perr != nil {
|
||||
err = perr.(error)
|
||||
}
|
||||
}()
|
||||
theme := styleToEscapeSequence(c.table, style)
|
||||
for token := it(); token != chroma.EOF; token = it() {
|
||||
// TODO: Cache token lookups?
|
||||
clr, ok := theme[token.Type]
|
||||
if !ok {
|
||||
clr, ok = theme[token.Type.SubCategory()]
|
||||
if !ok {
|
||||
clr = theme[token.Type.Category()]
|
||||
// if !ok {
|
||||
// clr = theme[chroma.InheritStyle]
|
||||
// }
|
||||
}
|
||||
}
|
||||
if clr != "" {
|
||||
fmt.Fprint(w, clr)
|
||||
}
|
||||
fmt.Fprint(w, token.Value)
|
||||
if clr != "" {
|
||||
fmt.Fprintf(w, "\033[0m")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TTY8 is an 8-colour terminal formatter.
|
||||
//
|
||||
// The Lab colour space is used to map RGB values to the most appropriate index colour.
|
||||
var TTY8 = Register("terminal", &indexedTTYFormatter{ttyTables[8]})
|
||||
|
||||
// TTY256 is a 256-colour terminal formatter.
|
||||
//
|
||||
// The Lab colour space is used to map RGB values to the most appropriate index colour.
|
||||
var TTY256 = Register("terminal256", &indexedTTYFormatter{ttyTables[256]})
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user