Adds vendor data
This commit is contained in:
354
vendor/github.com/hashicorp/go-version/LICENSE
generated
vendored
Normal file
354
vendor/github.com/hashicorp/go-version/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,354 @@
|
||||
Mozilla Public License, version 2.0
|
||||
|
||||
1. Definitions
|
||||
|
||||
1.1. “Contributor”
|
||||
|
||||
means each individual or legal entity that creates, contributes to the
|
||||
creation of, or owns Covered Software.
|
||||
|
||||
1.2. “Contributor Version”
|
||||
|
||||
means the combination of the Contributions of others (if any) used by a
|
||||
Contributor and that particular Contributor’s Contribution.
|
||||
|
||||
1.3. “Contribution”
|
||||
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. “Covered Software”
|
||||
|
||||
means Source Code Form to which the initial Contributor has attached the
|
||||
notice in Exhibit A, the Executable Form of such Source Code Form, and
|
||||
Modifications of such Source Code Form, in each case including portions
|
||||
thereof.
|
||||
|
||||
1.5. “Incompatible With Secondary Licenses”
|
||||
means
|
||||
|
||||
a. that the initial Contributor has attached the notice described in
|
||||
Exhibit B to the Covered Software; or
|
||||
|
||||
b. that the Covered Software was made available under the terms of version
|
||||
1.1 or earlier of the License, but not also under the terms of a
|
||||
Secondary License.
|
||||
|
||||
1.6. “Executable Form”
|
||||
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. “Larger Work”
|
||||
|
||||
means a work that combines Covered Software with other material, in a separate
|
||||
file or files, that is not Covered Software.
|
||||
|
||||
1.8. “License”
|
||||
|
||||
means this document.
|
||||
|
||||
1.9. “Licensable”
|
||||
|
||||
means having the right to grant, to the maximum extent possible, whether at the
|
||||
time of the initial grant or subsequently, any and all of the rights conveyed by
|
||||
this License.
|
||||
|
||||
1.10. “Modifications”
|
||||
|
||||
means any of the following:
|
||||
|
||||
a. any file in Source Code Form that results from an addition to, deletion
|
||||
from, or modification of the contents of Covered Software; or
|
||||
|
||||
b. any new file in Source Code Form that contains any Covered Software.
|
||||
|
||||
1.11. “Patent Claims” of a Contributor
|
||||
|
||||
means any patent claim(s), including without limitation, method, process,
|
||||
and apparatus claims, in any patent Licensable by such Contributor that
|
||||
would be infringed, but for the grant of the License, by the making,
|
||||
using, selling, offering for sale, having made, import, or transfer of
|
||||
either its Contributions or its Contributor Version.
|
||||
|
||||
1.12. “Secondary License”
|
||||
|
||||
means either the GNU General Public License, Version 2.0, the GNU Lesser
|
||||
General Public License, Version 2.1, the GNU Affero General Public
|
||||
License, Version 3.0, or any later versions of those licenses.
|
||||
|
||||
1.13. “Source Code Form”
|
||||
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. “You” (or “Your”)
|
||||
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, “You” includes any entity that controls, is
|
||||
controlled by, or is under common control with You. For purposes of this
|
||||
definition, “control” means (a) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (b) ownership of more than fifty percent (50%) of the
|
||||
outstanding shares or beneficial ownership of such entity.
|
||||
|
||||
|
||||
2. License Grants and Conditions
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
a. under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or as
|
||||
part of a Larger Work; and
|
||||
|
||||
b. under Patent Claims of such Contributor to make, use, sell, offer for
|
||||
sale, have made, import, and otherwise transfer either its Contributions
|
||||
or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution become
|
||||
effective for each Contribution on the date the Contributor first distributes
|
||||
such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under this
|
||||
License. No additional rights or licenses will be implied from the distribution
|
||||
or licensing of Covered Software under this License. Notwithstanding Section
|
||||
2.1(b) above, no patent license is granted by a Contributor:
|
||||
|
||||
a. for any code that a Contributor has removed from Covered Software; or
|
||||
|
||||
b. for infringements caused by: (i) Your and any other third party’s
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
c. under Patent Claims infringed by Covered Software in the absence of its
|
||||
Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks, or
|
||||
logos of any Contributor (except as may be necessary to comply with the
|
||||
notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this License
|
||||
(see Section 10.2) or under the terms of a Secondary License (if permitted
|
||||
under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its Contributions
|
||||
are its original creation(s) or it has sufficient rights to grant the
|
||||
rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under applicable
|
||||
copyright doctrines of fair use, fair dealing, or other equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
||||
Section 2.1.
|
||||
|
||||
|
||||
3. Responsibilities
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under the
|
||||
terms of this License. You must inform recipients that the Source Code Form
|
||||
of the Covered Software is governed by the terms of this License, and how
|
||||
they can obtain a copy of this License. You may not attempt to alter or
|
||||
restrict the recipients’ rights in the Source Code Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
a. such Covered Software must also be made available in Source Code Form,
|
||||
as described in Section 3.1, and You must inform recipients of the
|
||||
Executable Form how they can obtain a copy of such Source Code Form by
|
||||
reasonable means in a timely manner, at a charge no more than the cost
|
||||
of distribution to the recipient; and
|
||||
|
||||
b. You may distribute such Executable Form under the terms of this License,
|
||||
or sublicense it under different terms, provided that the license for
|
||||
the Executable Form does not attempt to limit or alter the recipients’
|
||||
rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for the
|
||||
Covered Software. If the Larger Work is a combination of Covered Software
|
||||
with a work governed by one or more Secondary Licenses, and the Covered
|
||||
Software is not Incompatible With Secondary Licenses, this License permits
|
||||
You to additionally distribute such Covered Software under the terms of
|
||||
such Secondary License(s), so that the recipient of the Larger Work may, at
|
||||
their option, further distribute the Covered Software under the terms of
|
||||
either this License or such Secondary License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices (including
|
||||
copyright notices, patent notices, disclaimers of warranty, or limitations
|
||||
of liability) contained within the Source Code Form of the Covered
|
||||
Software, except that You may alter any license notices to the extent
|
||||
required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on behalf
|
||||
of any Contributor. You must make it absolutely clear that any such
|
||||
warranty, support, indemnity, or liability obligation is offered by You
|
||||
alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this License
|
||||
with respect to some or all of the Covered Software due to statute, judicial
|
||||
order, or regulation then You must: (a) comply with the terms of this License
|
||||
to the maximum extent possible; and (b) describe the limitations and the code
|
||||
they affect. Such description must be placed in a text file included with all
|
||||
distributions of the Covered Software under this License. Except to the
|
||||
extent prohibited by statute or regulation, such description must be
|
||||
sufficiently detailed for a recipient of ordinary skill to be able to
|
||||
understand it.
|
||||
|
||||
5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically if You
|
||||
fail to comply with any of its terms. However, if You become compliant,
|
||||
then the rights granted under this License from a particular Contributor
|
||||
are reinstated (a) provisionally, unless and until such Contributor
|
||||
explicitly and finally terminates Your grants, and (b) on an ongoing basis,
|
||||
if such Contributor fails to notify You of the non-compliance by some
|
||||
reasonable means prior to 60 days after You have come back into compliance.
|
||||
Moreover, Your grants from a particular Contributor are reinstated on an
|
||||
ongoing basis if such Contributor notifies You of the non-compliance by
|
||||
some reasonable means, this is the first time You have received notice of
|
||||
non-compliance with this License from such Contributor, and You become
|
||||
compliant prior to 30 days after Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions, counter-claims,
|
||||
and cross-claims) alleging that a Contributor Version directly or
|
||||
indirectly infringes any patent, then the rights granted to You by any and
|
||||
all Contributors for the Covered Software under Section 2.1 of this License
|
||||
shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
|
||||
license agreements (excluding distributors and resellers) which have been
|
||||
validly granted by You or Your distributors under this License prior to
|
||||
termination shall survive termination.
|
||||
|
||||
6. Disclaimer of Warranty
|
||||
|
||||
Covered Software is provided under this License on an “as is” basis, without
|
||||
warranty of any kind, either expressed, implied, or statutory, including,
|
||||
without limitation, warranties that the Covered Software is free of defects,
|
||||
merchantable, fit for a particular purpose or non-infringing. The entire
|
||||
risk as to the quality and performance of the Covered Software is with You.
|
||||
Should any Covered Software prove defective in any respect, You (not any
|
||||
Contributor) assume the cost of any necessary servicing, repair, or
|
||||
correction. This disclaimer of warranty constitutes an essential part of this
|
||||
License. No use of any Covered Software is authorized under this License
|
||||
except under this disclaimer.
|
||||
|
||||
7. Limitation of Liability
|
||||
|
||||
Under no circumstances and under no legal theory, whether tort (including
|
||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
||||
distributes Covered Software as permitted above, be liable to You for any
|
||||
direct, indirect, special, incidental, or consequential damages of any
|
||||
character including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses, even if such party shall have been
|
||||
informed of the possibility of such damages. This limitation of liability
|
||||
shall not apply to liability for death or personal injury resulting from such
|
||||
party’s negligence to the extent applicable law prohibits such limitation.
|
||||
Some jurisdictions do not allow the exclusion or limitation of incidental or
|
||||
consequential damages, so this exclusion and limitation may not apply to You.
|
||||
|
||||
8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the courts of
|
||||
a jurisdiction where the defendant maintains its principal place of business
|
||||
and such litigation shall be governed by laws of that jurisdiction, without
|
||||
reference to its conflict-of-law provisions. Nothing in this Section shall
|
||||
prevent a party’s ability to bring cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject matter
|
||||
hereof. If any provision of this License is held to be unenforceable, such
|
||||
provision shall be reformed only to the extent necessary to make it
|
||||
enforceable. Any law or regulation which provides that the language of a
|
||||
contract shall be construed against the drafter shall not be used to construe
|
||||
this License against a Contributor.
|
||||
|
||||
|
||||
10. Versions of the License
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version of
|
||||
the License under which You originally received the Covered Software, or
|
||||
under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a modified
|
||||
version of this License if you rename the license and remove any
|
||||
references to the name of the license steward (except to note that such
|
||||
modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular file, then
|
||||
You may include the notice in a location (such as a LICENSE file in a relevant
|
||||
directory) where a recipient would be likely to look for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - “Incompatible With Secondary Licenses” Notice
|
||||
|
||||
This Source Code Form is “Incompatible
|
||||
With Secondary Licenses”, as defined by
|
||||
the Mozilla Public License, v. 2.0.
|
||||
|
||||
66
vendor/github.com/hashicorp/go-version/README.md
generated
vendored
Normal file
66
vendor/github.com/hashicorp/go-version/README.md
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
# Versioning Library for Go
|
||||
[](https://circleci.com/gh/hashicorp/go-version/tree/master)
|
||||
[](https://godoc.org/github.com/hashicorp/go-version)
|
||||
|
||||
go-version is a library for parsing versions and version constraints,
|
||||
and verifying versions against a set of constraints. go-version
|
||||
can sort a collection of versions properly, handles prerelease/beta
|
||||
versions, can increment versions, etc.
|
||||
|
||||
Versions used with go-version must follow [SemVer](http://semver.org/).
|
||||
|
||||
## Installation and Usage
|
||||
|
||||
Package documentation can be found on
|
||||
[GoDoc](http://godoc.org/github.com/hashicorp/go-version).
|
||||
|
||||
Installation can be done with a normal `go get`:
|
||||
|
||||
```
|
||||
$ go get github.com/hashicorp/go-version
|
||||
```
|
||||
|
||||
#### Version Parsing and Comparison
|
||||
|
||||
```go
|
||||
v1, err := version.NewVersion("1.2")
|
||||
v2, err := version.NewVersion("1.5+metadata")
|
||||
|
||||
// Comparison example. There is also GreaterThan, Equal, and just
|
||||
// a simple Compare that returns an int allowing easy >=, <=, etc.
|
||||
if v1.LessThan(v2) {
|
||||
fmt.Printf("%s is less than %s", v1, v2)
|
||||
}
|
||||
```
|
||||
|
||||
#### Version Constraints
|
||||
|
||||
```go
|
||||
v1, err := version.NewVersion("1.2")
|
||||
|
||||
// Constraints example.
|
||||
constraints, err := version.NewConstraint(">= 1.0, < 1.4")
|
||||
if constraints.Check(v1) {
|
||||
fmt.Printf("%s satisfies constraints %s", v1, constraints)
|
||||
}
|
||||
```
|
||||
|
||||
#### Version Sorting
|
||||
|
||||
```go
|
||||
versionsRaw := []string{"1.1", "0.7.1", "1.4-beta", "1.4", "2"}
|
||||
versions := make([]*version.Version, len(versionsRaw))
|
||||
for i, raw := range versionsRaw {
|
||||
v, _ := version.NewVersion(raw)
|
||||
versions[i] = v
|
||||
}
|
||||
|
||||
// After this, the versions are properly sorted
|
||||
sort.Sort(version.Collection(versions))
|
||||
```
|
||||
|
||||
## Issues and Contributing
|
||||
|
||||
If you find an issue with this library, please report an issue. If you'd
|
||||
like, we welcome any contributions. Fork this library and submit a pull
|
||||
request.
|
||||
204
vendor/github.com/hashicorp/go-version/constraint.go
generated
vendored
Normal file
204
vendor/github.com/hashicorp/go-version/constraint.go
generated
vendored
Normal file
@@ -0,0 +1,204 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Constraint represents a single constraint for a version, such as
|
||||
// ">= 1.0".
|
||||
type Constraint struct {
|
||||
f constraintFunc
|
||||
check *Version
|
||||
original string
|
||||
}
|
||||
|
||||
// Constraints is a slice of constraints. We make a custom type so that
|
||||
// we can add methods to it.
|
||||
type Constraints []*Constraint
|
||||
|
||||
type constraintFunc func(v, c *Version) bool
|
||||
|
||||
var constraintOperators map[string]constraintFunc
|
||||
|
||||
var constraintRegexp *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
constraintOperators = map[string]constraintFunc{
|
||||
"": constraintEqual,
|
||||
"=": constraintEqual,
|
||||
"!=": constraintNotEqual,
|
||||
">": constraintGreaterThan,
|
||||
"<": constraintLessThan,
|
||||
">=": constraintGreaterThanEqual,
|
||||
"<=": constraintLessThanEqual,
|
||||
"~>": constraintPessimistic,
|
||||
}
|
||||
|
||||
ops := make([]string, 0, len(constraintOperators))
|
||||
for k := range constraintOperators {
|
||||
ops = append(ops, regexp.QuoteMeta(k))
|
||||
}
|
||||
|
||||
constraintRegexp = regexp.MustCompile(fmt.Sprintf(
|
||||
`^\s*(%s)\s*(%s)\s*$`,
|
||||
strings.Join(ops, "|"),
|
||||
VersionRegexpRaw))
|
||||
}
|
||||
|
||||
// NewConstraint will parse one or more constraints from the given
|
||||
// constraint string. The string must be a comma-separated list of
|
||||
// constraints.
|
||||
func NewConstraint(v string) (Constraints, error) {
|
||||
vs := strings.Split(v, ",")
|
||||
result := make([]*Constraint, len(vs))
|
||||
for i, single := range vs {
|
||||
c, err := parseSingle(single)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result[i] = c
|
||||
}
|
||||
|
||||
return Constraints(result), nil
|
||||
}
|
||||
|
||||
// Check tests if a version satisfies all the constraints.
|
||||
func (cs Constraints) Check(v *Version) bool {
|
||||
for _, c := range cs {
|
||||
if !c.Check(v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Returns the string format of the constraints
|
||||
func (cs Constraints) String() string {
|
||||
csStr := make([]string, len(cs))
|
||||
for i, c := range cs {
|
||||
csStr[i] = c.String()
|
||||
}
|
||||
|
||||
return strings.Join(csStr, ",")
|
||||
}
|
||||
|
||||
// Check tests if a constraint is validated by the given version.
|
||||
func (c *Constraint) Check(v *Version) bool {
|
||||
return c.f(v, c.check)
|
||||
}
|
||||
|
||||
func (c *Constraint) String() string {
|
||||
return c.original
|
||||
}
|
||||
|
||||
func parseSingle(v string) (*Constraint, error) {
|
||||
matches := constraintRegexp.FindStringSubmatch(v)
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("Malformed constraint: %s", v)
|
||||
}
|
||||
|
||||
check, err := NewVersion(matches[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Constraint{
|
||||
f: constraintOperators[matches[1]],
|
||||
check: check,
|
||||
original: v,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func prereleaseCheck(v, c *Version) bool {
|
||||
switch vPre, cPre := v.Prerelease() != "", c.Prerelease() != ""; {
|
||||
case cPre && vPre:
|
||||
// A constraint with a pre-release can only match a pre-release version
|
||||
// with the same base segments.
|
||||
return reflect.DeepEqual(c.Segments64(), v.Segments64())
|
||||
|
||||
case !cPre && vPre:
|
||||
// A constraint without a pre-release can only match a version without a
|
||||
// pre-release.
|
||||
return false
|
||||
|
||||
case cPre && !vPre:
|
||||
// OK, except with the pessimistic operator
|
||||
case !cPre && !vPre:
|
||||
// OK
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Constraint functions
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
func constraintEqual(v, c *Version) bool {
|
||||
return v.Equal(c)
|
||||
}
|
||||
|
||||
func constraintNotEqual(v, c *Version) bool {
|
||||
return !v.Equal(c)
|
||||
}
|
||||
|
||||
func constraintGreaterThan(v, c *Version) bool {
|
||||
return prereleaseCheck(v, c) && v.Compare(c) == 1
|
||||
}
|
||||
|
||||
func constraintLessThan(v, c *Version) bool {
|
||||
return prereleaseCheck(v, c) && v.Compare(c) == -1
|
||||
}
|
||||
|
||||
func constraintGreaterThanEqual(v, c *Version) bool {
|
||||
return prereleaseCheck(v, c) && v.Compare(c) >= 0
|
||||
}
|
||||
|
||||
func constraintLessThanEqual(v, c *Version) bool {
|
||||
return prereleaseCheck(v, c) && v.Compare(c) <= 0
|
||||
}
|
||||
|
||||
func constraintPessimistic(v, c *Version) bool {
|
||||
// Using a pessimistic constraint with a pre-release, restricts versions to pre-releases
|
||||
if !prereleaseCheck(v, c) || (c.Prerelease() != "" && v.Prerelease() == "") {
|
||||
return false
|
||||
}
|
||||
|
||||
// If the version being checked is naturally less than the constraint, then there
|
||||
// is no way for the version to be valid against the constraint
|
||||
if v.LessThan(c) {
|
||||
return false
|
||||
}
|
||||
// We'll use this more than once, so grab the length now so it's a little cleaner
|
||||
// to write the later checks
|
||||
cs := len(c.segments)
|
||||
|
||||
// If the version being checked has less specificity than the constraint, then there
|
||||
// is no way for the version to be valid against the constraint
|
||||
if cs > len(v.segments) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check the segments in the constraint against those in the version. If the version
|
||||
// being checked, at any point, does not have the same values in each index of the
|
||||
// constraints segments, then it cannot be valid against the constraint.
|
||||
for i := 0; i < c.si-1; i++ {
|
||||
if v.segments[i] != c.segments[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Check the last part of the segment in the constraint. If the version segment at
|
||||
// this index is less than the constraints segment at this index, then it cannot
|
||||
// be valid against the constraint
|
||||
if c.segments[cs-1] > v.segments[cs-1] {
|
||||
return false
|
||||
}
|
||||
|
||||
// If nothing has rejected the version by now, it's valid
|
||||
return true
|
||||
}
|
||||
384
vendor/github.com/hashicorp/go-version/version.go
generated
vendored
Normal file
384
vendor/github.com/hashicorp/go-version/version.go
generated
vendored
Normal file
@@ -0,0 +1,384 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The compiled regular expression used to test the validity of a version.
|
||||
var (
|
||||
versionRegexp *regexp.Regexp
|
||||
semverRegexp *regexp.Regexp
|
||||
)
|
||||
|
||||
// The raw regular expression string used for testing the validity
|
||||
// of a version.
|
||||
const (
|
||||
VersionRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
|
||||
`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-?([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
|
||||
`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
|
||||
`?`
|
||||
|
||||
// SemverRegexpRaw requires a separator between version and prerelease
|
||||
SemverRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
|
||||
`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
|
||||
`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
|
||||
`?`
|
||||
)
|
||||
|
||||
// Version represents a single version.
|
||||
type Version struct {
|
||||
metadata string
|
||||
pre string
|
||||
segments []int64
|
||||
si int
|
||||
original string
|
||||
}
|
||||
|
||||
func init() {
|
||||
versionRegexp = regexp.MustCompile("^" + VersionRegexpRaw + "$")
|
||||
semverRegexp = regexp.MustCompile("^" + SemverRegexpRaw + "$")
|
||||
}
|
||||
|
||||
// NewVersion parses the given version and returns a new
|
||||
// Version.
|
||||
func NewVersion(v string) (*Version, error) {
|
||||
return newVersion(v, versionRegexp)
|
||||
}
|
||||
|
||||
// NewSemver parses the given version and returns a new
|
||||
// Version that adheres strictly to SemVer specs
|
||||
// https://semver.org/
|
||||
func NewSemver(v string) (*Version, error) {
|
||||
return newVersion(v, semverRegexp)
|
||||
}
|
||||
|
||||
func newVersion(v string, pattern *regexp.Regexp) (*Version, error) {
|
||||
matches := pattern.FindStringSubmatch(v)
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("Malformed version: %s", v)
|
||||
}
|
||||
segmentsStr := strings.Split(matches[1], ".")
|
||||
segments := make([]int64, len(segmentsStr))
|
||||
si := 0
|
||||
for i, str := range segmentsStr {
|
||||
val, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"Error parsing version: %s", err)
|
||||
}
|
||||
|
||||
segments[i] = int64(val)
|
||||
si++
|
||||
}
|
||||
|
||||
// Even though we could support more than three segments, if we
|
||||
// got less than three, pad it with 0s. This is to cover the basic
|
||||
// default usecase of semver, which is MAJOR.MINOR.PATCH at the minimum
|
||||
for i := len(segments); i < 3; i++ {
|
||||
segments = append(segments, 0)
|
||||
}
|
||||
|
||||
pre := matches[7]
|
||||
if pre == "" {
|
||||
pre = matches[4]
|
||||
}
|
||||
|
||||
return &Version{
|
||||
metadata: matches[10],
|
||||
pre: pre,
|
||||
segments: segments,
|
||||
si: si,
|
||||
original: v,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Must is a helper that wraps a call to a function returning (*Version, error)
|
||||
// and panics if error is non-nil.
|
||||
func Must(v *Version, err error) *Version {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// Compare compares this version to another version. This
|
||||
// returns -1, 0, or 1 if this version is smaller, equal,
|
||||
// or larger than the other version, respectively.
|
||||
//
|
||||
// If you want boolean results, use the LessThan, Equal,
|
||||
// GreaterThan, GreaterThanOrEqual or LessThanOrEqual methods.
|
||||
func (v *Version) Compare(other *Version) int {
|
||||
// A quick, efficient equality check
|
||||
if v.String() == other.String() {
|
||||
return 0
|
||||
}
|
||||
|
||||
segmentsSelf := v.Segments64()
|
||||
segmentsOther := other.Segments64()
|
||||
|
||||
// If the segments are the same, we must compare on prerelease info
|
||||
if reflect.DeepEqual(segmentsSelf, segmentsOther) {
|
||||
preSelf := v.Prerelease()
|
||||
preOther := other.Prerelease()
|
||||
if preSelf == "" && preOther == "" {
|
||||
return 0
|
||||
}
|
||||
if preSelf == "" {
|
||||
return 1
|
||||
}
|
||||
if preOther == "" {
|
||||
return -1
|
||||
}
|
||||
|
||||
return comparePrereleases(preSelf, preOther)
|
||||
}
|
||||
|
||||
// Get the highest specificity (hS), or if they're equal, just use segmentSelf length
|
||||
lenSelf := len(segmentsSelf)
|
||||
lenOther := len(segmentsOther)
|
||||
hS := lenSelf
|
||||
if lenSelf < lenOther {
|
||||
hS = lenOther
|
||||
}
|
||||
// Compare the segments
|
||||
// Because a constraint could have more/less specificity than the version it's
|
||||
// checking, we need to account for a lopsided or jagged comparison
|
||||
for i := 0; i < hS; i++ {
|
||||
if i > lenSelf-1 {
|
||||
// This means Self had the lower specificity
|
||||
// Check to see if the remaining segments in Other are all zeros
|
||||
if !allZero(segmentsOther[i:]) {
|
||||
// if not, it means that Other has to be greater than Self
|
||||
return -1
|
||||
}
|
||||
break
|
||||
} else if i > lenOther-1 {
|
||||
// this means Other had the lower specificity
|
||||
// Check to see if the remaining segments in Self are all zeros -
|
||||
if !allZero(segmentsSelf[i:]) {
|
||||
//if not, it means that Self has to be greater than Other
|
||||
return 1
|
||||
}
|
||||
break
|
||||
}
|
||||
lhs := segmentsSelf[i]
|
||||
rhs := segmentsOther[i]
|
||||
if lhs == rhs {
|
||||
continue
|
||||
} else if lhs < rhs {
|
||||
return -1
|
||||
}
|
||||
// Otherwis, rhs was > lhs, they're not equal
|
||||
return 1
|
||||
}
|
||||
|
||||
// if we got this far, they're equal
|
||||
return 0
|
||||
}
|
||||
|
||||
func allZero(segs []int64) bool {
|
||||
for _, s := range segs {
|
||||
if s != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func comparePart(preSelf string, preOther string) int {
|
||||
if preSelf == preOther {
|
||||
return 0
|
||||
}
|
||||
|
||||
var selfInt int64
|
||||
selfNumeric := true
|
||||
selfInt, err := strconv.ParseInt(preSelf, 10, 64)
|
||||
if err != nil {
|
||||
selfNumeric = false
|
||||
}
|
||||
|
||||
var otherInt int64
|
||||
otherNumeric := true
|
||||
otherInt, err = strconv.ParseInt(preOther, 10, 64)
|
||||
if err != nil {
|
||||
otherNumeric = false
|
||||
}
|
||||
|
||||
// if a part is empty, we use the other to decide
|
||||
if preSelf == "" {
|
||||
if otherNumeric {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
if preOther == "" {
|
||||
if selfNumeric {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
if selfNumeric && !otherNumeric {
|
||||
return -1
|
||||
} else if !selfNumeric && otherNumeric {
|
||||
return 1
|
||||
} else if !selfNumeric && !otherNumeric && preSelf > preOther {
|
||||
return 1
|
||||
} else if selfInt > otherInt {
|
||||
return 1
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
func comparePrereleases(v string, other string) int {
|
||||
// the same pre release!
|
||||
if v == other {
|
||||
return 0
|
||||
}
|
||||
|
||||
// split both pre releases for analyse their parts
|
||||
selfPreReleaseMeta := strings.Split(v, ".")
|
||||
otherPreReleaseMeta := strings.Split(other, ".")
|
||||
|
||||
selfPreReleaseLen := len(selfPreReleaseMeta)
|
||||
otherPreReleaseLen := len(otherPreReleaseMeta)
|
||||
|
||||
biggestLen := otherPreReleaseLen
|
||||
if selfPreReleaseLen > otherPreReleaseLen {
|
||||
biggestLen = selfPreReleaseLen
|
||||
}
|
||||
|
||||
// loop for parts to find the first difference
|
||||
for i := 0; i < biggestLen; i = i + 1 {
|
||||
partSelfPre := ""
|
||||
if i < selfPreReleaseLen {
|
||||
partSelfPre = selfPreReleaseMeta[i]
|
||||
}
|
||||
|
||||
partOtherPre := ""
|
||||
if i < otherPreReleaseLen {
|
||||
partOtherPre = otherPreReleaseMeta[i]
|
||||
}
|
||||
|
||||
compare := comparePart(partSelfPre, partOtherPre)
|
||||
// if parts are equals, continue the loop
|
||||
if compare != 0 {
|
||||
return compare
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// Equal tests if two versions are equal.
|
||||
func (v *Version) Equal(o *Version) bool {
|
||||
if v == nil || o == nil {
|
||||
return v == o
|
||||
}
|
||||
|
||||
return v.Compare(o) == 0
|
||||
}
|
||||
|
||||
// GreaterThan tests if this version is greater than another version.
|
||||
func (v *Version) GreaterThan(o *Version) bool {
|
||||
return v.Compare(o) > 0
|
||||
}
|
||||
|
||||
// GreaterThanOrEqual tests if this version is greater than or equal to another version.
|
||||
func (v *Version) GreaterThanOrEqual(o *Version) bool {
|
||||
return v.Compare(o) >= 0
|
||||
}
|
||||
|
||||
// LessThan tests if this version is less than another version.
|
||||
func (v *Version) LessThan(o *Version) bool {
|
||||
return v.Compare(o) < 0
|
||||
}
|
||||
|
||||
// LessThanOrEqual tests if this version is less than or equal to another version.
|
||||
func (v *Version) LessThanOrEqual(o *Version) bool {
|
||||
return v.Compare(o) <= 0
|
||||
}
|
||||
|
||||
// Metadata returns any metadata that was part of the version
|
||||
// string.
|
||||
//
|
||||
// Metadata is anything that comes after the "+" in the version.
|
||||
// For example, with "1.2.3+beta", the metadata is "beta".
|
||||
func (v *Version) Metadata() string {
|
||||
return v.metadata
|
||||
}
|
||||
|
||||
// Prerelease returns any prerelease data that is part of the version,
|
||||
// or blank if there is no prerelease data.
|
||||
//
|
||||
// Prerelease information is anything that comes after the "-" in the
|
||||
// version (but before any metadata). For example, with "1.2.3-beta",
|
||||
// the prerelease information is "beta".
|
||||
func (v *Version) Prerelease() string {
|
||||
return v.pre
|
||||
}
|
||||
|
||||
// Segments returns the numeric segments of the version as a slice of ints.
|
||||
//
|
||||
// This excludes any metadata or pre-release information. For example,
|
||||
// for a version "1.2.3-beta", segments will return a slice of
|
||||
// 1, 2, 3.
|
||||
func (v *Version) Segments() []int {
|
||||
segmentSlice := make([]int, len(v.segments))
|
||||
for i, v := range v.segments {
|
||||
segmentSlice[i] = int(v)
|
||||
}
|
||||
return segmentSlice
|
||||
}
|
||||
|
||||
// Segments64 returns the numeric segments of the version as a slice of int64s.
|
||||
//
|
||||
// This excludes any metadata or pre-release information. For example,
|
||||
// for a version "1.2.3-beta", segments will return a slice of
|
||||
// 1, 2, 3.
|
||||
func (v *Version) Segments64() []int64 {
|
||||
result := make([]int64, len(v.segments))
|
||||
copy(result, v.segments)
|
||||
return result
|
||||
}
|
||||
|
||||
// String returns the full version string included pre-release
|
||||
// and metadata information.
|
||||
//
|
||||
// This value is rebuilt according to the parsed segments and other
|
||||
// information. Therefore, ambiguities in the version string such as
|
||||
// prefixed zeroes (1.04.0 => 1.4.0), `v` prefix (v1.0.0 => 1.0.0), and
|
||||
// missing parts (1.0 => 1.0.0) will be made into a canonicalized form
|
||||
// as shown in the parenthesized examples.
|
||||
func (v *Version) String() string {
|
||||
var buf bytes.Buffer
|
||||
fmtParts := make([]string, len(v.segments))
|
||||
for i, s := range v.segments {
|
||||
// We can ignore err here since we've pre-parsed the values in segments
|
||||
str := strconv.FormatInt(s, 10)
|
||||
fmtParts[i] = str
|
||||
}
|
||||
fmt.Fprintf(&buf, strings.Join(fmtParts, "."))
|
||||
if v.pre != "" {
|
||||
fmt.Fprintf(&buf, "-%s", v.pre)
|
||||
}
|
||||
if v.metadata != "" {
|
||||
fmt.Fprintf(&buf, "+%s", v.metadata)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Original returns the original parsed version as-is, including any
|
||||
// potential whitespace, `v` prefix, etc.
|
||||
func (v *Version) Original() string {
|
||||
return v.original
|
||||
}
|
||||
17
vendor/github.com/hashicorp/go-version/version_collection.go
generated
vendored
Normal file
17
vendor/github.com/hashicorp/go-version/version_collection.go
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
package version
|
||||
|
||||
// Collection is a type that implements the sort.Interface interface
|
||||
// so that versions can be sorted.
|
||||
type Collection []*Version
|
||||
|
||||
func (v Collection) Len() int {
|
||||
return len(v)
|
||||
}
|
||||
|
||||
func (v Collection) Less(i, j int) bool {
|
||||
return v[i].LessThan(v[j])
|
||||
}
|
||||
|
||||
func (v Collection) Swap(i, j int) {
|
||||
v[i], v[j] = v[j], v[i]
|
||||
}
|
||||
8
vendor/github.com/sethvargo/go-envconfig/AUTHORS
generated
vendored
Normal file
8
vendor/github.com/sethvargo/go-envconfig/AUTHORS
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# This is the list of envconfig authors for copyright purposes.
|
||||
#
|
||||
# This does not necessarily list everyone who has contributed code, since in
|
||||
# some cases, their employer may be the copyright holder. To see the full list
|
||||
# of contributors, see the revision history in source control.
|
||||
|
||||
Google LLC
|
||||
Seth Vargo
|
||||
202
vendor/github.com/sethvargo/go-envconfig/LICENSE
generated
vendored
Normal file
202
vendor/github.com/sethvargo/go-envconfig/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.
|
||||
32
vendor/github.com/sethvargo/go-envconfig/Makefile
generated
vendored
Normal file
32
vendor/github.com/sethvargo/go-envconfig/Makefile
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
# Copyright The envconfig Authors
|
||||
#
|
||||
# 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.
|
||||
|
||||
test:
|
||||
@go test \
|
||||
-count=1 \
|
||||
-short \
|
||||
-timeout=5m \
|
||||
-vet="${VETTERS}" \
|
||||
./...
|
||||
.PHONY: test
|
||||
|
||||
test-acc:
|
||||
@go test \
|
||||
-count=1 \
|
||||
-race \
|
||||
-shuffle=on \
|
||||
-timeout=10m \
|
||||
-vet="${VETTERS}" \
|
||||
./...
|
||||
.PHONY: test-acc
|
||||
405
vendor/github.com/sethvargo/go-envconfig/README.md
generated
vendored
Normal file
405
vendor/github.com/sethvargo/go-envconfig/README.md
generated
vendored
Normal file
@@ -0,0 +1,405 @@
|
||||
# Envconfig
|
||||
|
||||
[](https://pkg.go.dev/mod/github.com/sethvargo/go-envconfig)
|
||||
[](https://github.com/sethvargo/go-envconfig/actions?query=branch%3Amain+-event%3Aschedule)
|
||||
|
||||
Envconfig populates struct field values based on environment variables or
|
||||
arbitrary lookup functions. It supports pre-setting mutations, which is useful
|
||||
for things like converting values to uppercase, trimming whitespace, or looking
|
||||
up secrets.
|
||||
|
||||
**Note:** Versions prior to v0.2 used a different import path. This README and
|
||||
examples are for v0.2+.
|
||||
|
||||
## Usage
|
||||
|
||||
Define a struct with fields using the `env` tag:
|
||||
|
||||
```go
|
||||
type MyConfig struct {
|
||||
Port int `env:"PORT"`
|
||||
Username string `env:"USERNAME"`
|
||||
}
|
||||
```
|
||||
|
||||
Set some environment variables:
|
||||
|
||||
```sh
|
||||
export PORT=5555
|
||||
export USERNAME=yoyo
|
||||
```
|
||||
|
||||
Process it using envconfig:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/sethvargo/go-envconfig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
var c MyConfig
|
||||
if err := envconfig.Process(ctx, &c); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// c.Port = 5555
|
||||
// c.Username = "yoyo"
|
||||
}
|
||||
```
|
||||
|
||||
You can also use nested structs, just remember that any fields you want to
|
||||
process must be public:
|
||||
|
||||
```go
|
||||
type MyConfig struct {
|
||||
Database *DatabaseConfig
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Port int `env:"PORT"`
|
||||
Username string `env:"USERNAME"`
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Use the `env` struct tag to define configuration.
|
||||
|
||||
### Overwrite
|
||||
|
||||
If overwrite is set, the value will be overwritten if there is an
|
||||
environment variable match regardless if the value is non-zero.
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
Port int `env:"PORT,overwrite"`
|
||||
}
|
||||
```
|
||||
|
||||
### Required
|
||||
|
||||
If a field is required, processing will error if the environment variable is
|
||||
unset.
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
Port int `env:"PORT,required"`
|
||||
}
|
||||
```
|
||||
|
||||
It is invalid to have a field as both `required` and `default`.
|
||||
|
||||
### Default
|
||||
|
||||
If an environment variable is not set, the field will be set to the default
|
||||
value. Note that the environment variable must not be set (e.g. `unset PORT`).
|
||||
If the environment variable is the empty string, that counts as a "value" and
|
||||
the default will not be used.
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
Port int `env:"PORT,default=5555"`
|
||||
}
|
||||
```
|
||||
|
||||
You can also set the default value to another field or value from the
|
||||
environment, for example:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
DefaultPort int `env:"DEFAULT_PORT,default=5555"`
|
||||
Port int `env:"OVERRIDE_PORT,default=$DEFAULT_PORT"`
|
||||
}
|
||||
```
|
||||
|
||||
The value for `Port` will default to the value of `DEFAULT_PORT`.
|
||||
|
||||
It is invalid to have a field as both `required` and `default`.
|
||||
|
||||
### Prefix
|
||||
|
||||
For shared, embedded structs, you can define a prefix to use when processing
|
||||
struct values for that embed.
|
||||
|
||||
```go
|
||||
type SharedConfig struct {
|
||||
Port int `env:"PORT,default=5555"`
|
||||
}
|
||||
|
||||
type Server1 struct {
|
||||
// This processes Port from $FOO_PORT.
|
||||
*SharedConfig `env:",prefix=FOO_"`
|
||||
}
|
||||
|
||||
type Server2 struct {
|
||||
// This processes Port from $BAR_PORT.
|
||||
*SharedConfig `env:",prefix=BAR_"`
|
||||
}
|
||||
```
|
||||
|
||||
It is invalid to specify a prefix on non-struct fields.
|
||||
|
||||
### Delimiter
|
||||
|
||||
When parsing maps and slices, a comma (`,`) is the default element delimiter.
|
||||
Define a custom delimiter with `delimiter`:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar map[string]string `env:"MYVAR,delimiter=;"`
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYVAR="a:1;b:2"
|
||||
# map[string]string{"a":"1", "b":"2"}
|
||||
```
|
||||
|
||||
This is especially helpful when your values include the default delimiter
|
||||
character.
|
||||
|
||||
```bash
|
||||
export MYVAR="a:1,2,3;b:4,5"
|
||||
# map[string]string{"a":"1,2,3", "b":"4,5"}
|
||||
```
|
||||
|
||||
### Separator
|
||||
|
||||
When parsing maps, a colon (`:`) is the default key-value separator. Define a
|
||||
separator with `separator`:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar map[string]string `env:"MYVAR,separator=="`
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYVAR="a=b,c=d"
|
||||
# map[string]string{"a":"b", "c":"d"}
|
||||
```
|
||||
|
||||
This is especially helpful when your keys or values include the default
|
||||
separator character.
|
||||
|
||||
```bash
|
||||
export MYVAR="client=abcd::1/128,backend=abcd::2/128"
|
||||
# map[string]string{"client":"abcd::1/128", "backend":"abcd::2/128"}
|
||||
```
|
||||
|
||||
## Complex Types
|
||||
|
||||
### Durations
|
||||
|
||||
In the environment, `time.Duration` values are specified as a parsable Go
|
||||
duration:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar time.Duration `env:"MYVAR"`
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYVAR="10m" # 10 * time.Minute
|
||||
```
|
||||
|
||||
### TextUnmarshaler / BinaryUnmarshaler
|
||||
|
||||
Types that implement `TextUnmarshaler` or `BinaryUnmarshaler` are processed as such.
|
||||
|
||||
### json.Unmarshaler
|
||||
|
||||
Types that implement `json.Unmarshaler` are processed as such.
|
||||
|
||||
### gob.Decoder
|
||||
|
||||
Types that implement `gob.Decoder` are processed as such.
|
||||
|
||||
|
||||
### Slices
|
||||
|
||||
Slices are specified as comma-separated values:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar []string `env:"MYVAR"`
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYVAR="a,b,c,d" # []string{"a", "b", "c", "d"}
|
||||
```
|
||||
|
||||
Note that byte slices are special cased and interpreted as strings from the
|
||||
environment.
|
||||
|
||||
### Maps
|
||||
|
||||
Maps are specified as comma-separated key:value pairs:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar map[string]string `env:"MYVAR"`
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYVAR="a:b,c:d" # map[string]string{"a":"b", "c":"d"}
|
||||
```
|
||||
|
||||
### Structs
|
||||
|
||||
Envconfig walks the entire struct, including nested structs, so deeply-nested
|
||||
fields are also supported.
|
||||
|
||||
If a nested struct is a pointer type, it will automatically be instantianted to
|
||||
the non-nil value. To change this behavior, see
|
||||
(Initialization)[#Initialization].
|
||||
|
||||
|
||||
### Custom
|
||||
|
||||
You can also define your own decoder for structs (see below).
|
||||
|
||||
|
||||
## Prefixing
|
||||
|
||||
You can define a custom prefix using the `PrefixLookuper`. This will lookup
|
||||
values in the environment by prefixing the keys with the provided value:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
MyVar string `env:"MYVAR"`
|
||||
}
|
||||
```
|
||||
|
||||
```go
|
||||
// Process variables, but look for the "APP_" prefix.
|
||||
l := envconfig.PrefixLookuper("APP_", envconfig.OsLookuper())
|
||||
if err := envconfig.ProcessWith(ctx, &c, l); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
export APP_MYVAR="foo"
|
||||
```
|
||||
|
||||
## Initialization
|
||||
|
||||
By default, all pointer fields are initialized (allocated) so they are not
|
||||
`nil`. To disable this behavior, use the tag the field as `noinit`:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
// Without `noinit`, DeleteUser would be initialized to the default boolean
|
||||
// value. With `noinit`, if the environment variable is not given, the value
|
||||
// is kept as uninitialized (nil).
|
||||
DeleteUser *bool `env:"DELETE_USER, noinit"`
|
||||
}
|
||||
```
|
||||
|
||||
This also applies to nested fields in a struct:
|
||||
|
||||
```go
|
||||
type ParentConfig struct {
|
||||
// Without `noinit` tag, `Child` would be set to `&ChildConfig{}` whether
|
||||
// or not `FIELD` is set in the env var.
|
||||
// With `noinit`, `Child` would stay nil if `FIELD` is not set in the env var.
|
||||
Child *ChildConfig `env:",noinit"`
|
||||
}
|
||||
|
||||
type ChildConfig struct {
|
||||
Field string `env:"FIELD"`
|
||||
}
|
||||
```
|
||||
|
||||
The `noinit` tag is only applicable for pointer fields. Putting the tag on a
|
||||
non-struct-pointer will return an error.
|
||||
|
||||
|
||||
## Extension
|
||||
|
||||
All built-in types are supported except Func and Chan. If you need to define a
|
||||
custom decoder, implement the `Decoder` interface:
|
||||
|
||||
```go
|
||||
type MyStruct struct {
|
||||
field string
|
||||
}
|
||||
|
||||
func (v *MyStruct) EnvDecode(val string) error {
|
||||
v.field = fmt.Sprintf("PREFIX-%s", val)
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ envconfig.Decoder = (*MyStruct)(nil) // interface check
|
||||
```
|
||||
|
||||
If you need to modify environment variable values before processing, you can
|
||||
specify a custom `Mutator`:
|
||||
|
||||
```go
|
||||
type Config struct {
|
||||
Password `env:"PASSWORD"`
|
||||
}
|
||||
|
||||
func resolveSecretFunc(ctx context.Context, key, value string) (string, error) {
|
||||
if strings.HasPrefix(value, "secret://") {
|
||||
return secretmanager.Resolve(ctx, value) // example
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
var config Config
|
||||
envconfig.ProcessWith(ctx, &config, envconfig.OsLookuper(), resolveSecretFunc)
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Relying on the environment in tests can be troublesome because environment
|
||||
variables are global, which makes it difficult to parallelize the tests.
|
||||
Envconfig supports extracting data from anything that returns a value:
|
||||
|
||||
```go
|
||||
lookuper := envconfig.MapLookuper(map[string]string{
|
||||
"FOO": "bar",
|
||||
"ZIP": "zap",
|
||||
})
|
||||
|
||||
var config Config
|
||||
envconfig.ProcessWith(ctx, &config, lookuper)
|
||||
```
|
||||
|
||||
Now you can parallelize all your tests by providing a map for the lookup
|
||||
function. In fact, that's how the tests in this repo work, so check there for an
|
||||
example.
|
||||
|
||||
You can also combine multiple lookupers with `MultiLookuper`. See the GoDoc for
|
||||
more information and examples.
|
||||
|
||||
|
||||
## Inspiration
|
||||
|
||||
This library is conceptually similar to [kelseyhightower/envconfig](https://github.com/kelseyhightower/envconfig), with the following
|
||||
major behavioral differences:
|
||||
|
||||
- Adds support for specifying a custom lookup function (such as a map), which
|
||||
is useful for testing.
|
||||
|
||||
- Only populates fields if they contain zero or nil values if `overwrite` is unset.
|
||||
This means you can pre-initialize a struct and any pre-populated fields will not
|
||||
be overwritten during processing.
|
||||
|
||||
- Support for interpolation. The default value for a field can be the value of
|
||||
another field.
|
||||
|
||||
- Support for arbitrary mutators that change/resolve data before type
|
||||
conversion.
|
||||
57
vendor/github.com/sethvargo/go-envconfig/decoding.go
generated
vendored
Normal file
57
vendor/github.com/sethvargo/go-envconfig/decoding.go
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright The envconfig Authors
|
||||
//
|
||||
// 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 envconfig
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Base64Bytes is a slice of bytes where the information is base64-encoded in
|
||||
// the environment variable.
|
||||
type Base64Bytes []byte
|
||||
|
||||
// EnvDecode implements env.Decoder.
|
||||
func (b *Base64Bytes) EnvDecode(val string) error {
|
||||
val = strings.ReplaceAll(val, "+", "-")
|
||||
val = strings.ReplaceAll(val, "/", "_")
|
||||
val = strings.TrimRight(val, "=")
|
||||
|
||||
var err error
|
||||
*b, err = base64.RawURLEncoding.DecodeString(val)
|
||||
return err
|
||||
}
|
||||
|
||||
// Bytes returns the underlying bytes.
|
||||
func (b Base64Bytes) Bytes() []byte {
|
||||
return []byte(b)
|
||||
}
|
||||
|
||||
// HexBytes is a slice of bytes where the information is hex-encoded in the
|
||||
// environment variable.
|
||||
type HexBytes []byte
|
||||
|
||||
// EnvDecode implements env.Decoder.
|
||||
func (b *HexBytes) EnvDecode(val string) error {
|
||||
var err error
|
||||
*b, err = hex.DecodeString(val)
|
||||
return err
|
||||
}
|
||||
|
||||
// Bytes returns the underlying bytes.
|
||||
func (b HexBytes) Bytes() []byte {
|
||||
return []byte(b)
|
||||
}
|
||||
691
vendor/github.com/sethvargo/go-envconfig/envconfig.go
generated
vendored
Normal file
691
vendor/github.com/sethvargo/go-envconfig/envconfig.go
generated
vendored
Normal file
@@ -0,0 +1,691 @@
|
||||
// Copyright The envconfig Authors
|
||||
//
|
||||
// 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 envconfig populates struct fields based on environment variable
|
||||
// values (or anything that responds to "Lookup"). Structs declare their
|
||||
// environment dependencies using the `env` tag with the key being the name of
|
||||
// the environment variable, case sensitive.
|
||||
//
|
||||
// type MyStruct struct {
|
||||
// A string `env:"A"` // resolves A to $A
|
||||
// B string `env:"B,required"` // resolves B to $B, errors if $B is unset
|
||||
// C string `env:"C,default=foo"` // resolves C to $C, defaults to "foo"
|
||||
//
|
||||
// D string `env:"D,required,default=foo"` // error, cannot be required and default
|
||||
// E string `env:""` // error, must specify key
|
||||
// }
|
||||
//
|
||||
// All built-in types are supported except Func and Chan. If you need to define
|
||||
// a custom decoder, implement Decoder:
|
||||
//
|
||||
// type MyStruct struct {
|
||||
// field string
|
||||
// }
|
||||
//
|
||||
// func (v *MyStruct) EnvDecode(val string) error {
|
||||
// v.field = fmt.Sprintf("PREFIX-%s", val)
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// In the environment, slices are specified as comma-separated values:
|
||||
//
|
||||
// export MYVAR="a,b,c,d" // []string{"a", "b", "c", "d"}
|
||||
//
|
||||
// In the environment, maps are specified as comma-separated key:value pairs:
|
||||
//
|
||||
// export MYVAR="a:b,c:d" // map[string]string{"a":"b", "c":"d"}
|
||||
//
|
||||
// If you need to modify environment variable values before processing, you can
|
||||
// specify a custom mutator:
|
||||
//
|
||||
// type Config struct {
|
||||
// Password `env:"PASSWORD_SECRET"`
|
||||
// }
|
||||
//
|
||||
// func resolveSecretFunc(ctx context.Context, key, value string) (string, error) {
|
||||
// if strings.HasPrefix(value, "secret://") {
|
||||
// return secretmanager.Resolve(ctx, value) // example
|
||||
// }
|
||||
// return value, nil
|
||||
// }
|
||||
//
|
||||
// var config Config
|
||||
// ProcessWith(&config, OsLookuper(), resolveSecretFunc)
|
||||
//
|
||||
package envconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
envTag = "env"
|
||||
|
||||
optDefault = "default="
|
||||
optDelimiter = "delimiter="
|
||||
optNoInit = "noinit"
|
||||
optOverwrite = "overwrite"
|
||||
optPrefix = "prefix="
|
||||
optRequired = "required"
|
||||
optSeparator = "separator="
|
||||
|
||||
defaultDelimiter = ","
|
||||
defaultSeparator = ":"
|
||||
)
|
||||
|
||||
var envvarNameRe = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`)
|
||||
|
||||
// Error is a custom error type for errors returned by envconfig.
|
||||
type Error string
|
||||
|
||||
// Error implements error.
|
||||
func (e Error) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
const (
|
||||
ErrInvalidEnvvarName = Error("invalid environment variable name")
|
||||
ErrInvalidMapItem = Error("invalid map item")
|
||||
ErrLookuperNil = Error("lookuper cannot be nil")
|
||||
ErrMissingKey = Error("missing key")
|
||||
ErrMissingRequired = Error("missing required value")
|
||||
ErrNoInitNotPtr = Error("field must be a pointer to have noinit")
|
||||
ErrNotPtr = Error("input must be a pointer")
|
||||
ErrNotStruct = Error("input must be a struct")
|
||||
ErrPrefixNotStruct = Error("prefix is only valid on struct types")
|
||||
ErrPrivateField = Error("cannot parse private fields")
|
||||
ErrRequiredAndDefault = Error("field cannot be required and have a default value")
|
||||
ErrUnknownOption = Error("unknown option")
|
||||
)
|
||||
|
||||
// Lookuper is an interface that provides a lookup for a string-based key.
|
||||
type Lookuper interface {
|
||||
// Lookup searches for the given key and returns the corresponding string
|
||||
// value. If a value is found, it returns the value and true. If a value is
|
||||
// not found, it returns the empty string and false.
|
||||
Lookup(key string) (string, bool)
|
||||
}
|
||||
|
||||
// osLookuper looks up environment configuration from the local environment.
|
||||
type osLookuper struct{}
|
||||
|
||||
// Verify implements interface.
|
||||
var _ Lookuper = (*osLookuper)(nil)
|
||||
|
||||
func (o *osLookuper) Lookup(key string) (string, bool) {
|
||||
return os.LookupEnv(key)
|
||||
}
|
||||
|
||||
// OsLookuper returns a lookuper that uses the environment (os.LookupEnv) to
|
||||
// resolve values.
|
||||
func OsLookuper() Lookuper {
|
||||
return new(osLookuper)
|
||||
}
|
||||
|
||||
type mapLookuper map[string]string
|
||||
|
||||
var _ Lookuper = (*mapLookuper)(nil)
|
||||
|
||||
func (m mapLookuper) Lookup(key string) (string, bool) {
|
||||
v, ok := m[key]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// MapLookuper looks up environment configuration from a provided map. This is
|
||||
// useful for testing, especially in parallel, since it does not require you to
|
||||
// mutate the parent environment (which is stateful).
|
||||
func MapLookuper(m map[string]string) Lookuper {
|
||||
return mapLookuper(m)
|
||||
}
|
||||
|
||||
type multiLookuper struct {
|
||||
ls []Lookuper
|
||||
}
|
||||
|
||||
var _ Lookuper = (*multiLookuper)(nil)
|
||||
|
||||
func (m *multiLookuper) Lookup(key string) (string, bool) {
|
||||
for _, l := range m.ls {
|
||||
if v, ok := l.Lookup(key); ok {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// PrefixLookuper looks up environment configuration using the specified prefix.
|
||||
// This is useful if you want all your variables to start with a particular
|
||||
// prefix like "MY_APP_".
|
||||
func PrefixLookuper(prefix string, l Lookuper) Lookuper {
|
||||
if typ, ok := l.(*prefixLookuper); ok {
|
||||
return &prefixLookuper{prefix: typ.prefix + prefix, l: typ.l}
|
||||
}
|
||||
return &prefixLookuper{prefix: prefix, l: l}
|
||||
}
|
||||
|
||||
type prefixLookuper struct {
|
||||
l Lookuper
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (p *prefixLookuper) Lookup(key string) (string, bool) {
|
||||
return p.l.Lookup(p.prefix + key)
|
||||
}
|
||||
|
||||
// MultiLookuper wraps a collection of lookupers. It does not combine them, and
|
||||
// lookups appear in the order in which they are provided to the initializer.
|
||||
func MultiLookuper(lookupers ...Lookuper) Lookuper {
|
||||
return &multiLookuper{ls: lookupers}
|
||||
}
|
||||
|
||||
// Decoder is an interface that custom types/fields can implement to control how
|
||||
// decoding takes place. For example:
|
||||
//
|
||||
// type MyType string
|
||||
//
|
||||
// func (mt MyType) EnvDecode(val string) error {
|
||||
// return "CUSTOM-"+val
|
||||
// }
|
||||
//
|
||||
type Decoder interface {
|
||||
EnvDecode(val string) error
|
||||
}
|
||||
|
||||
// MutatorFunc is a function that mutates a given value before it is passed
|
||||
// along for processing. This is useful if you want to mutate the environment
|
||||
// variable value before it's converted to the proper type.
|
||||
type MutatorFunc func(ctx context.Context, k, v string) (string, error)
|
||||
|
||||
// options are internal options for decoding.
|
||||
type options struct {
|
||||
Default string
|
||||
Delimiter string
|
||||
Prefix string
|
||||
Separator string
|
||||
NoInit bool
|
||||
Overwrite bool
|
||||
Required bool
|
||||
}
|
||||
|
||||
// Process processes the struct using the environment. See ProcessWith for a
|
||||
// more customizable version.
|
||||
func Process(ctx context.Context, i interface{}) error {
|
||||
return ProcessWith(ctx, i, OsLookuper())
|
||||
}
|
||||
|
||||
// ProcessWith processes the given interface with the given lookuper. See the
|
||||
// package-level documentation for specific examples and behaviors.
|
||||
func ProcessWith(ctx context.Context, i interface{}, l Lookuper, fns ...MutatorFunc) error {
|
||||
return processWith(ctx, i, l, false, fns...)
|
||||
}
|
||||
|
||||
// processWith is a helper that captures whether the parent wanted
|
||||
// initialization.
|
||||
func processWith(ctx context.Context, i interface{}, l Lookuper, parentNoInit bool, fns ...MutatorFunc) error {
|
||||
if l == nil {
|
||||
return ErrLookuperNil
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(i)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
return ErrNotPtr
|
||||
}
|
||||
|
||||
e := v.Elem()
|
||||
if e.Kind() != reflect.Struct {
|
||||
return ErrNotStruct
|
||||
}
|
||||
|
||||
t := e.Type()
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
ef := e.Field(i)
|
||||
tf := t.Field(i)
|
||||
tag := tf.Tag.Get(envTag)
|
||||
|
||||
if !ef.CanSet() {
|
||||
if tag != "" {
|
||||
// There's an "env" tag on a private field, we can't alter it, and it's
|
||||
// likely a mistake. Return an error so the user can handle.
|
||||
return fmt.Errorf("%s: %w", tf.Name, ErrPrivateField)
|
||||
}
|
||||
|
||||
// Otherwise continue to the next field.
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse the key and options.
|
||||
key, opts, err := keyAndOpts(tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", tf.Name, err)
|
||||
}
|
||||
|
||||
// NoInit is only permitted on pointers.
|
||||
if opts.NoInit && ef.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("%s: %w", tf.Name, ErrNoInitNotPtr)
|
||||
}
|
||||
|
||||
isNilStructPtr := false
|
||||
setNilStruct := func(v reflect.Value) {
|
||||
origin := e.Field(i)
|
||||
if isNilStructPtr {
|
||||
empty := reflect.New(origin.Type().Elem()).Interface()
|
||||
// If a struct (after traversal) equals to the empty value, it means
|
||||
// nothing was changed in any sub-fields. With the noinit opt, we skip
|
||||
// setting the empty value to the original struct pointer (aka. keep it
|
||||
// nil).
|
||||
if !reflect.DeepEqual(v.Interface(), empty) || (!opts.NoInit && !parentNoInit) {
|
||||
origin.Set(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize pointer structs.
|
||||
for ef.Kind() == reflect.Ptr {
|
||||
if ef.IsNil() {
|
||||
if ef.Type().Elem().Kind() != reflect.Struct {
|
||||
// This is a nil pointer to something that isn't a struct, like
|
||||
// *string. Move along.
|
||||
break
|
||||
}
|
||||
|
||||
isNilStructPtr = true
|
||||
// Use an empty struct of the type so we can traverse.
|
||||
ef = reflect.New(ef.Type().Elem()).Elem()
|
||||
|
||||
} else {
|
||||
ef = ef.Elem()
|
||||
}
|
||||
}
|
||||
|
||||
// Special case handle structs. This has to come after the value resolution in
|
||||
// case the struct has a custom decoder.
|
||||
if ef.Kind() == reflect.Struct {
|
||||
for ef.CanAddr() {
|
||||
ef = ef.Addr()
|
||||
}
|
||||
|
||||
// Lookup the value, ignoring an error if the key isn't defined. This is
|
||||
// required for nested structs that don't declare their own `env` keys,
|
||||
// but have internal fields with an `env` defined.
|
||||
val, found, usedDefault, err := lookup(key, opts, l)
|
||||
if err != nil && !errors.Is(err, ErrMissingKey) {
|
||||
return fmt.Errorf("%s: %w", tf.Name, err)
|
||||
}
|
||||
|
||||
if found || usedDefault {
|
||||
if ok, err := processAsDecoder(val, ef); ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
setNilStruct(ef)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
plu := l
|
||||
if opts.Prefix != "" {
|
||||
plu = PrefixLookuper(opts.Prefix, l)
|
||||
}
|
||||
|
||||
if err := processWith(ctx, ef.Interface(), plu, opts.NoInit, fns...); err != nil {
|
||||
return fmt.Errorf("%s: %w", tf.Name, err)
|
||||
}
|
||||
|
||||
setNilStruct(ef)
|
||||
continue
|
||||
}
|
||||
|
||||
// It's invalid to have a prefix on a non-struct field.
|
||||
if opts.Prefix != "" {
|
||||
return ErrPrefixNotStruct
|
||||
}
|
||||
|
||||
// Stop processing if there's no env tag (this comes after nested parsing),
|
||||
// in case there's an env tag in an embedded struct.
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// The field already has a non-zero value and overwrite is false, do not
|
||||
// overwrite.
|
||||
if !ef.IsZero() && !opts.Overwrite {
|
||||
continue
|
||||
}
|
||||
|
||||
val, found, usedDefault, err := lookup(key, opts, l)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", tf.Name, err)
|
||||
}
|
||||
|
||||
// If the field already has a non-zero value and there was no value directly
|
||||
// specified, do not overwrite the existing field. We only want to overwrite
|
||||
// when the envvar was provided directly.
|
||||
if !ef.IsZero() && !found {
|
||||
continue
|
||||
}
|
||||
|
||||
// Apply any mutators. Mutators are applied after the lookup, but before any
|
||||
// type conversions. They always resolve to a string (or error), so we don't
|
||||
// call mutators when the environment variable was not set.
|
||||
if found || usedDefault {
|
||||
for _, fn := range fns {
|
||||
if fn != nil {
|
||||
val, err = fn(ctx, key, val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", tf.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If Delimiter is not defined set it to ","
|
||||
if opts.Delimiter == "" {
|
||||
opts.Delimiter = defaultDelimiter
|
||||
}
|
||||
|
||||
// If Separator is not defined set it to ":"
|
||||
if opts.Separator == "" {
|
||||
opts.Separator = defaultSeparator
|
||||
}
|
||||
|
||||
// Set value.
|
||||
if err := processField(val, ef, opts.Delimiter, opts.Separator, opts.NoInit); err != nil {
|
||||
return fmt.Errorf("%s(%q): %w", tf.Name, val, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// keyAndOpts parses the given tag value (e.g. env:"foo,required") and
|
||||
// returns the key name and options as a list.
|
||||
func keyAndOpts(tag string) (string, *options, error) {
|
||||
parts := strings.Split(tag, ",")
|
||||
key, tagOpts := strings.TrimSpace(parts[0]), parts[1:]
|
||||
|
||||
if key != "" && !envvarNameRe.MatchString(key) {
|
||||
return "", nil, fmt.Errorf("%q: %w ", key, ErrInvalidEnvvarName)
|
||||
}
|
||||
|
||||
var opts options
|
||||
|
||||
LOOP:
|
||||
for i, o := range tagOpts {
|
||||
o = strings.TrimSpace(o)
|
||||
switch {
|
||||
case o == optOverwrite:
|
||||
opts.Overwrite = true
|
||||
case o == optRequired:
|
||||
opts.Required = true
|
||||
case o == optNoInit:
|
||||
opts.NoInit = true
|
||||
case strings.HasPrefix(o, optPrefix):
|
||||
opts.Prefix = strings.TrimPrefix(o, optPrefix)
|
||||
case strings.HasPrefix(o, optDelimiter):
|
||||
opts.Delimiter = strings.TrimPrefix(o, optDelimiter)
|
||||
case strings.HasPrefix(o, optSeparator):
|
||||
opts.Separator = strings.TrimPrefix(o, optSeparator)
|
||||
case strings.HasPrefix(o, optDefault):
|
||||
// If a default value was given, assume everything after is the provided
|
||||
// value, including comma-seprated items.
|
||||
o = strings.TrimLeft(strings.Join(tagOpts[i:], ","), " ")
|
||||
opts.Default = strings.TrimPrefix(o, optDefault)
|
||||
break LOOP
|
||||
default:
|
||||
return "", nil, fmt.Errorf("%q: %w", o, ErrUnknownOption)
|
||||
}
|
||||
}
|
||||
|
||||
return key, &opts, nil
|
||||
}
|
||||
|
||||
// lookup looks up the given key using the provided Lookuper and options. The
|
||||
// first boolean parameter indicates whether the value was found in the
|
||||
// lookuper. The second boolean parameter indicates whether the default value
|
||||
// was used.
|
||||
func lookup(key string, opts *options, l Lookuper) (string, bool, bool, error) {
|
||||
if key == "" {
|
||||
// The struct has something like `env:",required"`, which is likely a
|
||||
// mistake. We could try to infer the envvar from the field name, but that
|
||||
// feels too magical.
|
||||
return "", false, false, ErrMissingKey
|
||||
}
|
||||
|
||||
if opts.Required && opts.Default != "" {
|
||||
// Having a default value on a required value doesn't make sense.
|
||||
return "", false, false, ErrRequiredAndDefault
|
||||
}
|
||||
|
||||
// Lookup value.
|
||||
val, found := l.Lookup(key)
|
||||
if !found {
|
||||
if opts.Required {
|
||||
if pl, ok := l.(*prefixLookuper); ok {
|
||||
key = pl.prefix + key
|
||||
}
|
||||
|
||||
return "", false, false, fmt.Errorf("%w: %s", ErrMissingRequired, key)
|
||||
}
|
||||
|
||||
if opts.Default != "" {
|
||||
// Expand the default value. This allows for a default value that maps to
|
||||
// a different variable.
|
||||
val = os.Expand(opts.Default, func(i string) string {
|
||||
s, ok := l.Lookup(i)
|
||||
if ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
})
|
||||
|
||||
return val, false, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return val, found, false, nil
|
||||
}
|
||||
|
||||
// processAsDecoder processes the given value as a decoder or custom
|
||||
// unmarshaller.
|
||||
func processAsDecoder(v string, ef reflect.Value) (bool, error) {
|
||||
// Keep a running error. It's possible that a property might implement
|
||||
// multiple decoders, and we don't know *which* decoder will succeed. If we
|
||||
// get through all of them, we'll return the most recent error.
|
||||
var imp bool
|
||||
var err error
|
||||
|
||||
// Resolve any pointers.
|
||||
for ef.CanAddr() {
|
||||
ef = ef.Addr()
|
||||
}
|
||||
|
||||
if ef.CanInterface() {
|
||||
iface := ef.Interface()
|
||||
|
||||
// If a developer chooses to implement the Decoder interface on a type,
|
||||
// never attempt to use other decoders in case of failure. EnvDecode's
|
||||
// decoding logic is "the right one", and the error returned (if any)
|
||||
// is the most specific we can get.
|
||||
if dec, ok := iface.(Decoder); ok {
|
||||
imp = true
|
||||
err = dec.EnvDecode(v)
|
||||
return imp, err
|
||||
}
|
||||
|
||||
if tu, ok := iface.(encoding.TextUnmarshaler); ok {
|
||||
imp = true
|
||||
if err = tu.UnmarshalText([]byte(v)); err == nil {
|
||||
return imp, nil
|
||||
}
|
||||
}
|
||||
|
||||
if tu, ok := iface.(json.Unmarshaler); ok {
|
||||
imp = true
|
||||
if err = tu.UnmarshalJSON([]byte(v)); err == nil {
|
||||
return imp, nil
|
||||
}
|
||||
}
|
||||
|
||||
if tu, ok := iface.(encoding.BinaryUnmarshaler); ok {
|
||||
imp = true
|
||||
if err = tu.UnmarshalBinary([]byte(v)); err == nil {
|
||||
return imp, nil
|
||||
}
|
||||
}
|
||||
|
||||
if tu, ok := iface.(gob.GobDecoder); ok {
|
||||
imp = true
|
||||
if err = tu.GobDecode([]byte(v)); err == nil {
|
||||
return imp, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return imp, err
|
||||
}
|
||||
|
||||
func processField(v string, ef reflect.Value, delimiter, separator string, noInit bool) error {
|
||||
// If the input value is empty and initialization is skipped, do nothing.
|
||||
if v == "" && noInit {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle pointers and uninitialized pointers.
|
||||
for ef.Type().Kind() == reflect.Ptr {
|
||||
if ef.IsNil() {
|
||||
ef.Set(reflect.New(ef.Type().Elem()))
|
||||
}
|
||||
ef = ef.Elem()
|
||||
}
|
||||
|
||||
tf := ef.Type()
|
||||
tk := tf.Kind()
|
||||
|
||||
// Handle existing decoders.
|
||||
if ok, err := processAsDecoder(v, ef); ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// We don't check if the value is empty earlier, because the user might want
|
||||
// to define a custom decoder and treat the empty variable as a special case.
|
||||
// However, if we got this far, none of the remaining parsers will succeed, so
|
||||
// bail out now.
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tk {
|
||||
case reflect.Bool:
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetBool(b)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
f, err := strconv.ParseFloat(v, tf.Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetFloat(f)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||
i, err := strconv.ParseInt(v, 0, tf.Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetInt(i)
|
||||
case reflect.Int64:
|
||||
// Special case time.Duration values.
|
||||
if tf.PkgPath() == "time" && tf.Name() == "Duration" {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetInt(int64(d))
|
||||
} else {
|
||||
i, err := strconv.ParseInt(v, 0, tf.Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetInt(i)
|
||||
}
|
||||
case reflect.String:
|
||||
ef.SetString(v)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
i, err := strconv.ParseUint(v, 0, tf.Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ef.SetUint(i)
|
||||
|
||||
case reflect.Interface:
|
||||
return fmt.Errorf("cannot decode into interfaces")
|
||||
|
||||
// Maps
|
||||
case reflect.Map:
|
||||
vals := strings.Split(v, delimiter)
|
||||
mp := reflect.MakeMapWithSize(tf, len(vals))
|
||||
for _, val := range vals {
|
||||
pair := strings.SplitN(val, separator, 2)
|
||||
if len(pair) < 2 {
|
||||
return fmt.Errorf("%s: %w", val, ErrInvalidMapItem)
|
||||
}
|
||||
mKey, mVal := strings.TrimSpace(pair[0]), strings.TrimSpace(pair[1])
|
||||
|
||||
k := reflect.New(tf.Key()).Elem()
|
||||
if err := processField(mKey, k, delimiter, separator, noInit); err != nil {
|
||||
return fmt.Errorf("%s: %w", mKey, err)
|
||||
}
|
||||
|
||||
v := reflect.New(tf.Elem()).Elem()
|
||||
if err := processField(mVal, v, delimiter, separator, noInit); err != nil {
|
||||
return fmt.Errorf("%s: %w", mVal, err)
|
||||
}
|
||||
|
||||
mp.SetMapIndex(k, v)
|
||||
}
|
||||
ef.Set(mp)
|
||||
|
||||
// Slices
|
||||
case reflect.Slice:
|
||||
// Special case: []byte
|
||||
if tf.Elem().Kind() == reflect.Uint8 {
|
||||
ef.Set(reflect.ValueOf([]byte(v)))
|
||||
} else {
|
||||
vals := strings.Split(v, delimiter)
|
||||
s := reflect.MakeSlice(tf, len(vals), len(vals))
|
||||
for i, val := range vals {
|
||||
val = strings.TrimSpace(val)
|
||||
if err := processField(val, s.Index(i), delimiter, separator, noInit); err != nil {
|
||||
return fmt.Errorf("%s: %w", val, err)
|
||||
}
|
||||
}
|
||||
ef.Set(s)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
1
vendor/github.com/sethvargo/go-githubactions/.gitignore
generated
vendored
Normal file
1
vendor/github.com/sethvargo/go-githubactions/.gitignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
coverage.out
|
||||
7
vendor/github.com/sethvargo/go-githubactions/AUTHORS
generated
vendored
Normal file
7
vendor/github.com/sethvargo/go-githubactions/AUTHORS
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
# This is the list of authors for copyright purposes.
|
||||
#
|
||||
# This does not necessarily list everyone who has contributed code, since in
|
||||
# some cases, their employer may be the copyright holder. To see the full list
|
||||
# of contributors, see the revision history in source control.
|
||||
|
||||
Seth Vargo
|
||||
202
vendor/github.com/sethvargo/go-githubactions/LICENSE
generated
vendored
Normal file
202
vendor/github.com/sethvargo/go-githubactions/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.
|
||||
29
vendor/github.com/sethvargo/go-githubactions/Makefile
generated
vendored
Normal file
29
vendor/github.com/sethvargo/go-githubactions/Makefile
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# Copyright 2020 The Authors
|
||||
#
|
||||
# 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.
|
||||
|
||||
test:
|
||||
@go test \
|
||||
-count=1 \
|
||||
-short \
|
||||
-timeout=5m \
|
||||
./...
|
||||
.PHONY: test
|
||||
|
||||
test-acc:
|
||||
@go test \
|
||||
-count=1 \
|
||||
-race \
|
||||
-timeout=10m \
|
||||
./...
|
||||
.PHONY: test-acc
|
||||
150
vendor/github.com/sethvargo/go-githubactions/README.md
generated
vendored
Normal file
150
vendor/github.com/sethvargo/go-githubactions/README.md
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
# GitHub Actions SDK (Go)
|
||||
|
||||
[](https://pkg.go.dev/github.com/sethvargo/go-githubactions)
|
||||
[](https://github.com/sethvargo/go-githubactions/actions?query=workflow%3ATest)
|
||||
|
||||
This library provides an SDK for authoring [GitHub Actions][gh-actions] in Go. It has no external dependencies and provides a Go-like interface for interacting with GitHub Actions' build system.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Download the library:
|
||||
|
||||
```text
|
||||
$ go get -u github.com/sethvargo/go-githubactions/...
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
The easiest way to use the library is by importing it and invoking the functions
|
||||
at the root:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/sethvargo/go-githubactions"
|
||||
)
|
||||
|
||||
func main() {
|
||||
val := githubactions.GetInput("val")
|
||||
if val == "" {
|
||||
githubactions.Fatalf("missing 'val'")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can also create an instance with custom fields that will be included in log messages:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/sethvargo/go-githubactions"
|
||||
)
|
||||
|
||||
func main() {
|
||||
actions := githubactions.WithFieldsMap(map[string]string{
|
||||
"file": "myfile.js",
|
||||
"line": "100",
|
||||
})
|
||||
|
||||
val := actions.GetInput("val")
|
||||
if val == "" {
|
||||
actions.Fatalf("missing 'val'")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For more examples and API documentation, please see the [Go docs][godoc].
|
||||
|
||||
|
||||
## Publishing
|
||||
|
||||
There are multiple ways to publish GitHub Actions written in Go:
|
||||
|
||||
- [Composite actions](https://github.com/FerretDB/github-actions/blob/2ae30fd2cdb635d8aefdaf9f770257e156c9f77b/extract-docker-tag/action.yml)
|
||||
- [Pre-compiled binaries with a shim](https://full-stack.blend.com/how-we-write-github-actions-in-go.html)
|
||||
- Docker containers (see below)
|
||||
|
||||
By default, GitHub Actions expects actions to be written in Node.js. For other languages like Go, you need to provide a `Dockerfile` and entrypoint instructions in an `action.yml` file:
|
||||
|
||||
```dockerfile
|
||||
# your-repo/Dockerfile
|
||||
FROM golang:1.18
|
||||
RUN go build -o /bin/app .
|
||||
ENTRYPOINT ["/bin/app"]
|
||||
```
|
||||
|
||||
```yaml
|
||||
# your-repo/action.yml
|
||||
name: My action
|
||||
author: My name
|
||||
description: My description
|
||||
|
||||
runs:
|
||||
using: docker
|
||||
image: Dockerfile
|
||||
```
|
||||
|
||||
And then users can import your action by the repository name:
|
||||
|
||||
```yaml
|
||||
# their-repo/.github/workflows/thing.yml
|
||||
steps:
|
||||
- name: My action
|
||||
uses: username/repo@latest
|
||||
```
|
||||
|
||||
However, this will clone the entire repo and compile the Go code each time the action runs. Worse, it uses the Go base container which is a few hundred MBs and includes a ton of unnecessary things.
|
||||
|
||||
Fortunately, GitHub Actions can also source from a Docker container directly from Docker Hub:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- name: My action
|
||||
uses: docker://username/repo:latest
|
||||
```
|
||||
|
||||
Now we can precompile and publish our Go Action as a Docker container, but we need to make it much, much smaller first. This can be achieved using multi-stage Docker builds:
|
||||
|
||||
```dockerfile
|
||||
FROM golang:1.18 AS builder
|
||||
|
||||
ENV GO111MODULE=on \
|
||||
CGO_ENABLED=0 \
|
||||
GOOS=linux \
|
||||
GOARCH=amd64
|
||||
|
||||
RUN apt-get -qq update && \
|
||||
apt-get -yqq install upx
|
||||
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
|
||||
RUN go build \
|
||||
-ldflags "-s -w -extldflags '-static'" \
|
||||
-o /bin/app \
|
||||
. \
|
||||
&& strip /bin/app \
|
||||
&& upx -q -9 /bin/app
|
||||
|
||||
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_passwd
|
||||
|
||||
|
||||
|
||||
FROM scratch
|
||||
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
COPY --from=builder /etc_passwd /etc/passwd
|
||||
COPY --from=builder --chown=65534:0 /bin/app /app
|
||||
|
||||
USER nobody
|
||||
ENTRYPOINT ["/app"]
|
||||
```
|
||||
|
||||
The first step, uses a fat container to build, strip, and compress the compiled Go binary. Then, in the second step, the compiled and compressed binary is copied into a scratch (bare) container along with some SSL certificates and a `nobody` user in which to execute the container.
|
||||
|
||||
This will usually produce an image that is less than 10MB in size, making for
|
||||
much faster builds.
|
||||
|
||||
|
||||
[gh-actions]: https://github.com/features/actions
|
||||
[godoc]: https://godoc.org/github.com/sethvargo/go-githubactions
|
||||
576
vendor/github.com/sethvargo/go-githubactions/actions.go
generated
vendored
Normal file
576
vendor/github.com/sethvargo/go-githubactions/actions.go
generated
vendored
Normal file
@@ -0,0 +1,576 @@
|
||||
// Copyright 2020 The Authors
|
||||
//
|
||||
// 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 githubactions provides an SDK for authoring GitHub Actions in Go. It
|
||||
// has no external dependencies and provides a Go-like interface for interacting
|
||||
// with GitHub Actions' build system.
|
||||
package githubactions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sethvargo/go-envconfig"
|
||||
)
|
||||
|
||||
var (
|
||||
// osExit allows `os.Exit()` to be stubbed during testing.
|
||||
osExit = os.Exit
|
||||
)
|
||||
|
||||
const (
|
||||
addMaskCmd = "add-mask"
|
||||
|
||||
envCmd = "env"
|
||||
outputCmd = "output"
|
||||
pathCmd = "path"
|
||||
stateCmd = "state"
|
||||
|
||||
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
|
||||
multiLineFileDelim = "_GitHubActionsFileCommandDelimeter_"
|
||||
multilineFileCmd = "%s<<" + multiLineFileDelim + EOF + "%s" + EOF + multiLineFileDelim // ${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}
|
||||
|
||||
addMatcherCmd = "add-matcher"
|
||||
removeMatcherCmd = "remove-matcher"
|
||||
|
||||
groupCmd = "group"
|
||||
endGroupCmd = "endgroup"
|
||||
|
||||
stepSummaryCmd = "step-summary"
|
||||
|
||||
debugCmd = "debug"
|
||||
noticeCmd = "notice"
|
||||
warningCmd = "warning"
|
||||
errorCmd = "error"
|
||||
|
||||
errFileCmdFmt = "unable to write command to the environment file: %s"
|
||||
)
|
||||
|
||||
// New creates a new wrapper with helpers for outputting information in GitHub
|
||||
// actions format.
|
||||
func New(opts ...Option) *Action {
|
||||
a := &Action{
|
||||
w: os.Stdout,
|
||||
getenv: os.Getenv,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
a = opt(a)
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// Action is an internal wrapper around GitHub Actions' output and magic
|
||||
// strings.
|
||||
type Action struct {
|
||||
w io.Writer
|
||||
fields CommandProperties
|
||||
getenv GetenvFunc
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// IssueCommand issues a new GitHub actions Command. It panics if it cannot
|
||||
// write to the output stream.
|
||||
func (c *Action) IssueCommand(cmd *Command) {
|
||||
if _, err := fmt.Fprint(c.w, cmd.String()+EOF); err != nil {
|
||||
panic(fmt.Errorf("failed to issue command: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
// IssueFileCommand issues a new GitHub actions Command using environment files.
|
||||
// It panics if writing to the file fails.
|
||||
//
|
||||
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
|
||||
//
|
||||
// The TypeScript equivalent function:
|
||||
//
|
||||
// https://github.com/actions/toolkit/blob/4f7fb6513a355689f69f0849edeb369a4dc81729/packages/core/src/file-command.ts#L10-L23
|
||||
//
|
||||
// IssueFileCommand currently ignores the 'CommandProperties' field provided
|
||||
// with the 'Command' argument as it's scope is unclear in the current
|
||||
// TypeScript implementation.
|
||||
func (c *Action) IssueFileCommand(cmd *Command) {
|
||||
if err := c.issueFileCommand(cmd); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// issueFileCommand is an internal-only helper that issues the command and
|
||||
// returns an error to make testing easier.
|
||||
func (c *Action) issueFileCommand(cmd *Command) (retErr error) {
|
||||
e := strings.ReplaceAll(cmd.Name, "-", "_")
|
||||
e = strings.ToUpper(e)
|
||||
e = "GITHUB_" + e
|
||||
|
||||
filepath := c.getenv(e)
|
||||
msg := []byte(cmd.Message + EOF)
|
||||
f, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
retErr = fmt.Errorf(errFileCmdFmt, err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil && retErr == nil {
|
||||
retErr = err
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := f.Write(msg); err != nil {
|
||||
retErr = fmt.Errorf(errFileCmdFmt, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddMask adds a new field mask for the given string "p". After called, future
|
||||
// attempts to log "p" will be replaced with "***" in log output. It panics if
|
||||
// it cannot write to the output stream.
|
||||
func (c *Action) AddMask(p string) {
|
||||
// ::add-mask::<p>
|
||||
c.IssueCommand(&Command{
|
||||
Name: addMaskCmd,
|
||||
Message: p,
|
||||
})
|
||||
}
|
||||
|
||||
// AddMatcher adds a new matcher with the given file path. It panics if it
|
||||
// cannot write to the output stream.
|
||||
func (c *Action) AddMatcher(p string) {
|
||||
// ::add-matcher::<p>
|
||||
c.IssueCommand(&Command{
|
||||
Name: addMatcherCmd,
|
||||
Message: p,
|
||||
})
|
||||
}
|
||||
|
||||
// RemoveMatcher removes a matcher with the given owner name. It panics if it
|
||||
// cannot write to the output stream.
|
||||
func (c *Action) RemoveMatcher(o string) {
|
||||
// ::remove-matcher owner=<o>::
|
||||
c.IssueCommand(&Command{
|
||||
Name: removeMatcherCmd,
|
||||
Properties: CommandProperties{
|
||||
"owner": o,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// AddPath adds the string "p" to the path for the invocation. It panics if it
|
||||
// cannot write to the output file.
|
||||
//
|
||||
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#adding-a-system-path
|
||||
// https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/
|
||||
func (c *Action) AddPath(p string) {
|
||||
c.IssueFileCommand(&Command{
|
||||
Name: pathCmd,
|
||||
Message: p,
|
||||
})
|
||||
}
|
||||
|
||||
// SaveState saves state to be used in the "finally" post job entry point. It
|
||||
// panics if it cannot write to the output stream.
|
||||
//
|
||||
// On 2022-10-11, GitHub deprecated "::save-state name=<k>::<v>" in favor of
|
||||
// [environment files].
|
||||
//
|
||||
// [environment files]: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
|
||||
func (c *Action) SaveState(k, v string) {
|
||||
c.IssueFileCommand(&Command{
|
||||
Name: stateCmd,
|
||||
Message: fmt.Sprintf(multilineFileCmd, k, v),
|
||||
})
|
||||
}
|
||||
|
||||
// GetInput gets the input by the given name. It returns the empty string if the
|
||||
// input is not defined.
|
||||
func (c *Action) GetInput(i string) string {
|
||||
e := strings.ReplaceAll(i, " ", "_")
|
||||
e = strings.ToUpper(e)
|
||||
e = "INPUT_" + e
|
||||
return strings.TrimSpace(c.getenv(e))
|
||||
}
|
||||
|
||||
// Group starts a new collapsable region up to the next ungroup invocation. It
|
||||
// panics if it cannot write to the output stream.
|
||||
func (c *Action) Group(t string) {
|
||||
// ::group::<t>
|
||||
c.IssueCommand(&Command{
|
||||
Name: groupCmd,
|
||||
Message: t,
|
||||
})
|
||||
}
|
||||
|
||||
// EndGroup ends the current group. It panics if it cannot write to the output
|
||||
// stream.
|
||||
func (c *Action) EndGroup() {
|
||||
// ::endgroup::
|
||||
c.IssueCommand(&Command{
|
||||
Name: endGroupCmd,
|
||||
})
|
||||
}
|
||||
|
||||
// AddStepSummary writes the given markdown to the job summary. If a job summary
|
||||
// already exists, this value is appended.
|
||||
//
|
||||
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
|
||||
// https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
|
||||
func (c *Action) AddStepSummary(markdown string) {
|
||||
c.IssueFileCommand(&Command{
|
||||
Name: stepSummaryCmd,
|
||||
Message: markdown,
|
||||
})
|
||||
}
|
||||
|
||||
// AddStepSummaryTemplate adds a summary template by parsing the given Go
|
||||
// template using html/template with the given input data. See AddStepSummary
|
||||
// for caveats.
|
||||
//
|
||||
// This primarily exists as a convenience function that renders a template.
|
||||
func (c *Action) AddStepSummaryTemplate(tmpl string, data any) error {
|
||||
t, err := template.New("").Parse(tmpl)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse template: %w", err)
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := t.Execute(&b, data); err != nil {
|
||||
return fmt.Errorf("failed to execute template: %w", err)
|
||||
}
|
||||
|
||||
c.AddStepSummary(b.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetEnv sets an environment variable. It panics if it cannot write to the
|
||||
// output file.
|
||||
//
|
||||
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
|
||||
// https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/
|
||||
func (c *Action) SetEnv(k, v string) {
|
||||
c.IssueFileCommand(&Command{
|
||||
Name: envCmd,
|
||||
Message: fmt.Sprintf(multilineFileCmd, k, v),
|
||||
})
|
||||
}
|
||||
|
||||
// SetOutput sets an output parameter. It panics if it cannot write to the
|
||||
// output stream.
|
||||
//
|
||||
// On 2022-10-11, GitHub deprecated "::set-output name=<k>::<v>" in favor of
|
||||
// [environment files].
|
||||
//
|
||||
// [environment files]: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
|
||||
func (c *Action) SetOutput(k, v string) {
|
||||
c.IssueFileCommand(&Command{
|
||||
Name: outputCmd,
|
||||
Message: fmt.Sprintf(multilineFileCmd, k, v),
|
||||
})
|
||||
}
|
||||
|
||||
// Debugf prints a debug-level message. It follows the standard fmt.Printf
|
||||
// arguments, appending an OS-specific line break to the end of the message. It
|
||||
// panics if it cannot write to the output stream.
|
||||
func (c *Action) Debugf(msg string, args ...any) {
|
||||
// ::debug <c.fields>::<msg, args>
|
||||
c.IssueCommand(&Command{
|
||||
Name: debugCmd,
|
||||
Message: fmt.Sprintf(msg, args...),
|
||||
Properties: c.fields,
|
||||
})
|
||||
}
|
||||
|
||||
// Noticef prints a notice-level message. It follows the standard fmt.Printf
|
||||
// arguments, appending an OS-specific line break to the end of the message. It
|
||||
// panics if it cannot write to the output stream.
|
||||
func (c *Action) Noticef(msg string, args ...any) {
|
||||
// ::notice <c.fields>::<msg, args>
|
||||
c.IssueCommand(&Command{
|
||||
Name: noticeCmd,
|
||||
Message: fmt.Sprintf(msg, args...),
|
||||
Properties: c.fields,
|
||||
})
|
||||
}
|
||||
|
||||
// Warningf prints a warning-level message. It follows the standard fmt.Printf
|
||||
// arguments, appending an OS-specific line break to the end of the message. It
|
||||
// panics if it cannot write to the output stream.
|
||||
func (c *Action) Warningf(msg string, args ...any) {
|
||||
// ::warning <c.fields>::<msg, args>
|
||||
c.IssueCommand(&Command{
|
||||
Name: warningCmd,
|
||||
Message: fmt.Sprintf(msg, args...),
|
||||
Properties: c.fields,
|
||||
})
|
||||
}
|
||||
|
||||
// Errorf prints a error-level message. It follows the standard fmt.Printf
|
||||
// arguments, appending an OS-specific line break to the end of the message. It
|
||||
// panics if it cannot write to the output stream.
|
||||
func (c *Action) Errorf(msg string, args ...any) {
|
||||
// ::error <c.fields>::<msg, args>
|
||||
c.IssueCommand(&Command{
|
||||
Name: errorCmd,
|
||||
Message: fmt.Sprintf(msg, args...),
|
||||
Properties: c.fields,
|
||||
})
|
||||
}
|
||||
|
||||
// Fatalf prints a error-level message and exits. This is equivalent to Errorf
|
||||
// followed by os.Exit(1).
|
||||
func (c *Action) Fatalf(msg string, args ...any) {
|
||||
c.Errorf(msg, args...)
|
||||
osExit(1)
|
||||
}
|
||||
|
||||
// Infof prints message to stdout without any level annotations. It follows the
|
||||
// standard fmt.Printf arguments, appending an OS-specific line break to the end
|
||||
// of the message. It panics if it cannot write to the output stream.
|
||||
func (c *Action) Infof(msg string, args ...any) {
|
||||
if _, err := fmt.Fprintf(c.w, msg+EOF, args...); err != nil {
|
||||
panic(fmt.Errorf("failed to write info command: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
// WithFieldsSlice includes the provided fields in log output. "f" must be a
|
||||
// slice of k=v pairs. The given slice will be sorted. It panics if any of the
|
||||
// string in the given slice does not construct a valid 'key=value' pair.
|
||||
func (c *Action) WithFieldsSlice(f []string) *Action {
|
||||
m := make(CommandProperties)
|
||||
for _, s := range f {
|
||||
pair := strings.SplitN(s, "=", 2)
|
||||
if len(pair) < 2 {
|
||||
panic(fmt.Sprintf("%q is not a proper k=v pair!", s))
|
||||
}
|
||||
|
||||
m[pair[0]] = pair[1]
|
||||
}
|
||||
|
||||
return c.WithFieldsMap(m)
|
||||
}
|
||||
|
||||
// WithFieldsMap includes the provided fields in log output. The fields in "m"
|
||||
// are automatically converted to k=v pairs and sorted.
|
||||
func (c *Action) WithFieldsMap(m map[string]string) *Action {
|
||||
return &Action{
|
||||
w: c.w,
|
||||
fields: m,
|
||||
getenv: c.getenv,
|
||||
httpClient: c.httpClient,
|
||||
}
|
||||
}
|
||||
|
||||
// idTokenResponse is the response from minting an ID token.
|
||||
type idTokenResponse struct {
|
||||
Value string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
// GetIDToken returns the GitHub OIDC token from the GitHub Actions runtime.
|
||||
func (c *Action) GetIDToken(ctx context.Context, audience string) (string, error) {
|
||||
requestURL := c.getenv("ACTIONS_ID_TOKEN_REQUEST_URL")
|
||||
if requestURL == "" {
|
||||
return "", fmt.Errorf("missing ACTIONS_ID_TOKEN_REQUEST_URL in environment")
|
||||
}
|
||||
|
||||
requestToken := c.getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")
|
||||
if requestToken == "" {
|
||||
return "", fmt.Errorf("missing ACTIONS_ID_TOKEN_REQUEST_TOKEN in environment")
|
||||
}
|
||||
|
||||
u, err := url.Parse(requestURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse request URL: %w", err)
|
||||
}
|
||||
if audience != "" {
|
||||
q := u.Query()
|
||||
q.Set("audience", audience)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create HTTP request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+requestToken)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to make HTTP request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// This has moved to the io package in Go 1.16, but we still support up to Go
|
||||
// 1.13 for now.
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 64*1000))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
body = bytes.TrimSpace(body)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return "", fmt.Errorf("non-successful response from minting OIDC token: %s", body)
|
||||
}
|
||||
|
||||
var tokenResp idTokenResponse
|
||||
if err := json.Unmarshal(body, &tokenResp); err != nil {
|
||||
return "", fmt.Errorf("failed to process response as JSON: %w", err)
|
||||
}
|
||||
return tokenResp.Value, nil
|
||||
}
|
||||
|
||||
// Getenv retrieves the value of the environment variable named by the key.
|
||||
// It uses an internal function that can be set with `WithGetenv`.
|
||||
func (c *Action) Getenv(key string) string {
|
||||
return c.getenv(key)
|
||||
}
|
||||
|
||||
// GetenvFunc is an abstraction to make tests feasible for commands that
|
||||
// interact with environment variables.
|
||||
type GetenvFunc func(key string) string
|
||||
|
||||
// GitHubContext of current workflow.
|
||||
//
|
||||
// See: https://docs.github.com/en/actions/learn-github-actions/environment-variables
|
||||
type GitHubContext struct {
|
||||
Action string `env:"GITHUB_ACTION"`
|
||||
ActionPath string `env:"GITHUB_ACTION_PATH"`
|
||||
ActionRepository string `env:"GITHUB_ACTION_REPOSITORY"`
|
||||
Actions bool `env:"GITHUB_ACTIONS"`
|
||||
Actor string `env:"GITHUB_ACTOR"`
|
||||
APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"`
|
||||
BaseRef string `env:"GITHUB_BASE_REF"`
|
||||
Env string `env:"GITHUB_ENV"`
|
||||
EventName string `env:"GITHUB_EVENT_NAME"`
|
||||
EventPath string `env:"GITHUB_EVENT_PATH"`
|
||||
GraphqlURL string `env:"GITHUB_GRAPHQL_URL,default=https://api.github.com/graphql"`
|
||||
HeadRef string `env:"GITHUB_HEAD_REF"`
|
||||
Job string `env:"GITHUB_JOB"`
|
||||
Path string `env:"GITHUB_PATH"`
|
||||
Ref string `env:"GITHUB_REF"`
|
||||
RefName string `env:"GITHUB_REF_NAME"`
|
||||
RefProtected bool `env:"GITHUB_REF_PROTECTED"`
|
||||
RefType string `env:"GITHUB_REF_TYPE"`
|
||||
|
||||
// Repository is the owner and repository name. For example, octocat/Hello-World
|
||||
// It is not recommended to use this field to acquire the repository name
|
||||
// but to use the Repo method instead.
|
||||
Repository string `env:"GITHUB_REPOSITORY"`
|
||||
|
||||
// RepositoryOwner is the repository owner. For example, octocat
|
||||
// It is not recommended to use this field to acquire the repository owner
|
||||
// but to use the Repo method instead.
|
||||
RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"`
|
||||
|
||||
RetentionDays int64 `env:"GITHUB_RETENTION_DAYS"`
|
||||
RunAttempt int64 `env:"GITHUB_RUN_ATTEMPT"`
|
||||
RunID int64 `env:"GITHUB_RUN_ID"`
|
||||
RunNumber int64 `env:"GITHUB_RUN_NUMBER"`
|
||||
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
|
||||
SHA string `env:"GITHUB_SHA"`
|
||||
StepSummary string `env:"GITHUB_STEP_SUMMARY"`
|
||||
Workflow string `env:"GITHUB_WORKFLOW"`
|
||||
Workspace string `env:"GITHUB_WORKSPACE"`
|
||||
|
||||
// Event is populated by parsing the file at EventPath, if it exists.
|
||||
Event map[string]any
|
||||
}
|
||||
|
||||
// Repo returns the username of the repository owner and repository name.
|
||||
func (c *GitHubContext) Repo() (string, string) {
|
||||
if c == nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// Based on https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts
|
||||
if c.Repository != "" {
|
||||
parts := strings.SplitN(c.Repository, "/", 2)
|
||||
if len(parts) == 1 {
|
||||
return parts[0], ""
|
||||
}
|
||||
return parts[0], parts[1]
|
||||
}
|
||||
|
||||
// If c.Repository is empty attempt to get the repo from the Event data.
|
||||
var repoName string
|
||||
// NOTE: differs from context.ts. Fall back to GITHUB_REPOSITORY_OWNER
|
||||
ownerName := c.RepositoryOwner
|
||||
if c.Event != nil {
|
||||
if repo, ok := c.Event["repository"].(map[string]any); ok {
|
||||
if name, ok := repo["name"].(string); ok {
|
||||
repoName = name
|
||||
}
|
||||
if owner, ok := repo["owner"].(map[string]any); ok {
|
||||
if name, ok := owner["name"].(string); ok {
|
||||
ownerName = name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ownerName, repoName
|
||||
}
|
||||
|
||||
// Context returns the context of current action with the payload object
|
||||
// that triggered the workflow
|
||||
func (c *Action) Context() (*GitHubContext, error) {
|
||||
ctx := context.Background()
|
||||
lookuper := &wrappedLookuper{f: c.getenv}
|
||||
|
||||
var githubContext GitHubContext
|
||||
if err := envconfig.ProcessWith(ctx, &githubContext, lookuper); err != nil {
|
||||
return nil, fmt.Errorf("could not process github context variables: %w", err)
|
||||
}
|
||||
|
||||
if githubContext.EventPath != "" {
|
||||
eventData, err := os.ReadFile(githubContext.EventPath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("could not read event file: %w", err)
|
||||
}
|
||||
if eventData != nil {
|
||||
if err := json.Unmarshal(eventData, &githubContext.Event); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal event payload: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &githubContext, nil
|
||||
}
|
||||
|
||||
// wrappedLookuper creates a lookuper that wraps a given getenv func.
|
||||
type wrappedLookuper struct {
|
||||
f GetenvFunc
|
||||
}
|
||||
|
||||
// Lookup implements a custom lookuper.
|
||||
func (w *wrappedLookuper) Lookup(key string) (string, bool) {
|
||||
if v := w.f(key); v != "" {
|
||||
return v, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
156
vendor/github.com/sethvargo/go-githubactions/actions_root.go
generated
vendored
Normal file
156
vendor/github.com/sethvargo/go-githubactions/actions_root.go
generated
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
// Copyright 2020 The Authors
|
||||
//
|
||||
// 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 githubactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultAction = New()
|
||||
)
|
||||
|
||||
// IssueCommand issues an arbitrary GitHub actions Command.
|
||||
func IssueCommand(cmd *Command) {
|
||||
defaultAction.IssueCommand(cmd)
|
||||
}
|
||||
|
||||
// IssueFileCommand issues a new GitHub actions Command using environment files.
|
||||
func IssueFileCommand(cmd *Command) {
|
||||
defaultAction.IssueFileCommand(cmd)
|
||||
}
|
||||
|
||||
// AddMask adds a new field mask for the given string "p". After called, future
|
||||
// attempts to log "p" will be replaced with "***" in log output.
|
||||
func AddMask(p string) {
|
||||
defaultAction.AddMask(p)
|
||||
}
|
||||
|
||||
// AddMatcher adds a new matcher with the given file path.
|
||||
func AddMatcher(p string) {
|
||||
defaultAction.AddMatcher(p)
|
||||
}
|
||||
|
||||
// RemoveMatcher removes a matcher with the given owner name.
|
||||
func RemoveMatcher(o string) {
|
||||
defaultAction.RemoveMatcher(o)
|
||||
}
|
||||
|
||||
// AddPath adds the string "p" to the path for the invocation.
|
||||
func AddPath(p string) {
|
||||
defaultAction.AddPath(p)
|
||||
}
|
||||
|
||||
// SaveState saves state to be used in the "finally" post job entry point.
|
||||
func SaveState(k, v string) {
|
||||
defaultAction.SaveState(k, v)
|
||||
}
|
||||
|
||||
// GetInput gets the input by the given name.
|
||||
func GetInput(i string) string {
|
||||
return defaultAction.GetInput(i)
|
||||
}
|
||||
|
||||
// Group starts a new collapsable region up to the next ungroup invocation.
|
||||
func Group(t string) {
|
||||
defaultAction.Group(t)
|
||||
}
|
||||
|
||||
// EndGroup ends the current group.
|
||||
func EndGroup() {
|
||||
defaultAction.EndGroup()
|
||||
}
|
||||
|
||||
// AddStepSummary writes the given markdown to the job summary. If a job summary
|
||||
// already exists, this value is appended.
|
||||
func AddStepSummary(markdown string) {
|
||||
defaultAction.AddStepSummary(markdown)
|
||||
}
|
||||
|
||||
// AddStepSummaryTemplate adds a summary template by parsing the given Go
|
||||
// template using html/template with the given input data. See AddStepSummary
|
||||
// for caveats.
|
||||
//
|
||||
// This primarily exists as a convenience function that renders a template.
|
||||
func AddStepSummaryTemplate(tmpl string, data any) error {
|
||||
return defaultAction.AddStepSummaryTemplate(tmpl, data)
|
||||
}
|
||||
|
||||
// SetEnv sets an environment variable.
|
||||
func SetEnv(k, v string) {
|
||||
defaultAction.SetEnv(k, v)
|
||||
}
|
||||
|
||||
// SetOutput sets an output parameter.
|
||||
func SetOutput(k, v string) {
|
||||
defaultAction.SetOutput(k, v)
|
||||
}
|
||||
|
||||
// Debugf prints a debug-level message. The arguments follow the standard Printf
|
||||
// arguments.
|
||||
func Debugf(msg string, args ...any) {
|
||||
defaultAction.Debugf(msg, args...)
|
||||
}
|
||||
|
||||
// Noticef prints a notice-level message. The arguments follow the standard
|
||||
// Printf arguments.
|
||||
func Noticef(msg string, args ...any) {
|
||||
defaultAction.Noticef(msg, args...)
|
||||
}
|
||||
|
||||
// Errorf prints a error-level message. The arguments follow the standard Printf
|
||||
// arguments.
|
||||
func Errorf(msg string, args ...any) {
|
||||
defaultAction.Errorf(msg, args...)
|
||||
}
|
||||
|
||||
// Fatalf prints a error-level message and exits. This is equivalent to Errorf
|
||||
// followed by os.Exit(1).
|
||||
func Fatalf(msg string, args ...any) {
|
||||
defaultAction.Fatalf(msg, args...)
|
||||
}
|
||||
|
||||
// Infof prints a info-level message. The arguments follow the standard Printf
|
||||
// arguments.
|
||||
func Infof(msg string, args ...any) {
|
||||
defaultAction.Infof(msg, args...)
|
||||
}
|
||||
|
||||
// Warningf prints a warning-level message. The arguments follow the standard
|
||||
// Printf arguments.
|
||||
func Warningf(msg string, args ...any) {
|
||||
defaultAction.Warningf(msg, args...)
|
||||
}
|
||||
|
||||
// WithFieldsSlice includes the provided fields in log output. "f" must be a
|
||||
// slice of k=v pairs. The given slice will be sorted.
|
||||
func WithFieldsSlice(f []string) *Action {
|
||||
return defaultAction.WithFieldsSlice(f)
|
||||
}
|
||||
|
||||
// WithFieldsMap includes the provided fields in log output. The fields in "m"
|
||||
// are automatically converted to k=v pairs and sorted.
|
||||
func WithFieldsMap(m map[string]string) *Action {
|
||||
return defaultAction.WithFieldsMap(m)
|
||||
}
|
||||
|
||||
// GetIDToken returns the GitHub OIDC token from the GitHub Actions runtime.
|
||||
func GetIDToken(ctx context.Context, audience string) (string, error) {
|
||||
return defaultAction.GetIDToken(ctx, audience)
|
||||
}
|
||||
|
||||
func Context() (*GitHubContext, error) {
|
||||
return defaultAction.Context()
|
||||
}
|
||||
112
vendor/github.com/sethvargo/go-githubactions/command.go
generated
vendored
Normal file
112
vendor/github.com/sethvargo/go-githubactions/command.go
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright 2020 The Authors
|
||||
//
|
||||
// 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 githubactions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
cmdSeparator = "::"
|
||||
cmdPropertiesPrefix = " "
|
||||
)
|
||||
|
||||
// CommandProperties is a named "map[string]string" type to hold key-value pairs
|
||||
// passed to an actions command.
|
||||
type CommandProperties map[string]string
|
||||
|
||||
// String encodes the CommandProperties to a string as comma separated
|
||||
// 'key=value' pairs. The pairs are joined in a chronological order.
|
||||
func (props *CommandProperties) String() string {
|
||||
l := make([]string, 0, len(*props))
|
||||
for k, v := range *props {
|
||||
l = append(l, fmt.Sprintf("%s=%s", k, escapeProperty(v)))
|
||||
}
|
||||
|
||||
sort.Strings(l)
|
||||
return strings.Join(l, ",")
|
||||
}
|
||||
|
||||
// Command can be issued by a GitHub action by writing to `stdout` with
|
||||
// following format.
|
||||
//
|
||||
// ::name key=value,key=value::message
|
||||
//
|
||||
// Examples:
|
||||
// ::warning::This is the message
|
||||
// ::set-env name=MY_VAR::some value
|
||||
type Command struct {
|
||||
Name string
|
||||
Message string
|
||||
Properties CommandProperties
|
||||
}
|
||||
|
||||
// String encodes the Command to a string in the following format:
|
||||
//
|
||||
// ::name key=value,key=value::message
|
||||
func (cmd *Command) String() string {
|
||||
// https://github.com/actions/toolkit/blob/9ad01e4fd30025e8858650d38e95cfe9193a3222/packages/core/src/command.ts#L43-L45
|
||||
if cmd.Name == "" {
|
||||
cmd.Name = "missing.command"
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
builder.WriteString(cmdSeparator)
|
||||
builder.WriteString(cmd.Name)
|
||||
if len(cmd.Properties) > 0 {
|
||||
builder.WriteString(cmdPropertiesPrefix)
|
||||
builder.WriteString(cmd.Properties.String())
|
||||
}
|
||||
|
||||
builder.WriteString(cmdSeparator)
|
||||
builder.WriteString(escapeData(cmd.Message))
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// escapeData escapes string values for presentation in the output of a command.
|
||||
// This is a not-so-well-documented requirement of commands that define a
|
||||
// message:
|
||||
//
|
||||
// https://github.com/actions/toolkit/blob/9ad01e4fd30025e8858650d38e95cfe9193a3222/packages/core/src/command.ts#L74
|
||||
//
|
||||
// The equivalent toolkit function can be found here:
|
||||
//
|
||||
// https://github.com/actions/toolkit/blob/9ad01e4fd30025e8858650d38e95cfe9193a3222/packages/core/src/command.ts#L92
|
||||
//
|
||||
func escapeData(v string) string {
|
||||
v = strings.ReplaceAll(v, "%", "%25")
|
||||
v = strings.ReplaceAll(v, "\r", "%0D")
|
||||
v = strings.ReplaceAll(v, "\n", "%0A")
|
||||
return v
|
||||
}
|
||||
|
||||
// escapeData escapes command property values for presentation in the output of
|
||||
// a command.
|
||||
//
|
||||
// https://github.com/actions/toolkit/blob/9ad01e4fd30025e8858650d38e95cfe9193a3222/packages/core/src/command.ts#L68
|
||||
//
|
||||
// The equivalent toolkit function can be found here:
|
||||
//
|
||||
// https://github.com/actions/toolkit/blob/1cc56db0ff126f4d65aeb83798852e02a2c180c3/packages/core/src/command.ts#L99-L106
|
||||
func escapeProperty(v string) string {
|
||||
v = strings.ReplaceAll(v, "%", "%25")
|
||||
v = strings.ReplaceAll(v, "\r", "%0D")
|
||||
v = strings.ReplaceAll(v, "\n", "%0A")
|
||||
v = strings.ReplaceAll(v, ":", "%3A")
|
||||
v = strings.ReplaceAll(v, ",", "%2C")
|
||||
return v
|
||||
}
|
||||
20
vendor/github.com/sethvargo/go-githubactions/eof_crlf.go
generated
vendored
Normal file
20
vendor/github.com/sethvargo/go-githubactions/eof_crlf.go
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2021 The Authors
|
||||
//
|
||||
// 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.
|
||||
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package githubactions
|
||||
|
||||
const EOF = "\r\n"
|
||||
20
vendor/github.com/sethvargo/go-githubactions/eof_lf.go
generated
vendored
Normal file
20
vendor/github.com/sethvargo/go-githubactions/eof_lf.go
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2021 The Authors
|
||||
//
|
||||
// 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.
|
||||
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package githubactions
|
||||
|
||||
const EOF = "\n"
|
||||
59
vendor/github.com/sethvargo/go-githubactions/options.go
generated
vendored
Normal file
59
vendor/github.com/sethvargo/go-githubactions/options.go
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2021 The Authors
|
||||
//
|
||||
// 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 githubactions
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Option is a modifier for an Action.
|
||||
type Option func(*Action) *Action
|
||||
|
||||
// WithWriter sets the writer function on an Action. By default, this will
|
||||
// be `os.Stdout` from the standard library.
|
||||
func WithWriter(w io.Writer) Option {
|
||||
return func(a *Action) *Action {
|
||||
a.w = w
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
// WithFields sets the extra command field on an Action.
|
||||
func WithFields(fields CommandProperties) Option {
|
||||
return func(a *Action) *Action {
|
||||
a.fields = fields
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
// WithGetenv sets the `Getenv` function on an Action. By default, this will
|
||||
// be `os.Getenv` from the standard library.
|
||||
func WithGetenv(getenv GetenvFunc) Option {
|
||||
return func(a *Action) *Action {
|
||||
a.getenv = getenv
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets a custom HTTP client on the action. This is only used
|
||||
// when the action makes output HTTP requests (such as generating an OIDC
|
||||
// token).
|
||||
func WithHTTPClient(c *http.Client) Option {
|
||||
return func(a *Action) *Action {
|
||||
a.httpClient = c
|
||||
return a
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user