| 1 |
package parser |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strconv" |
| 6 |
) |
| 7 |
|
| 8 |
// Content of this file was copied from the package golang.org/x/mod/modfile |
| 9 |
// https://github.com/golang/mod/blob/v0.2.0/modfile/read.go#L877 |
| 10 |
// Under the BSD-3-Clause licence: |
| 11 |
// golang.org/x/[email protected]/LICENSE |
| 12 |
/* |
| 13 |
Copyright (c) 2009 The Go Authors. All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are |
| 17 |
met: |
| 18 |
|
| 19 |
* Redistributions of source code must retain the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer. |
| 21 |
* Redistributions in binary form must reproduce the above |
| 22 |
copyright notice, this list of conditions and the following disclaimer |
| 23 |
in the documentation and/or other materials provided with the |
| 24 |
distribution. |
| 25 |
* Neither the name of Google Inc. nor the names of its |
| 26 |
contributors may be used to endorse or promote products derived from |
| 27 |
this software without specific prior written permission. |
| 28 |
|
| 29 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 30 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 31 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 32 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 33 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 34 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 39 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 40 |
*/ |
| 41 |
|
| 42 |
var ( |
| 43 |
slashSlash = []byte("//") |
| 44 |
moduleStr = []byte("module") |
| 45 |
) |
| 46 |
|
| 47 |
// modulePath returns the module path from the gomod file text. |
| 48 |
// If it cannot find a module path, it returns an empty string. |
| 49 |
// It is tolerant of unrelated problems in the go.mod file. |
| 50 |
func modulePath(mod []byte) string { |
| 51 |
for len(mod) > 0 { |
| 52 |
line := mod |
| 53 |
mod = nil |
| 54 |
if i := bytes.IndexByte(line, '\n'); i >= 0 { |
| 55 |
line, mod = line[:i], line[i+1:] |
| 56 |
} |
| 57 |
if i := bytes.Index(line, slashSlash); i >= 0 { |
| 58 |
line = line[:i] |
| 59 |
} |
| 60 |
line = bytes.TrimSpace(line) |
| 61 |
if !bytes.HasPrefix(line, moduleStr) { |
| 62 |
continue |
| 63 |
} |
| 64 |
line = line[len(moduleStr):] |
| 65 |
n := len(line) |
| 66 |
line = bytes.TrimSpace(line) |
| 67 |
if len(line) == n || len(line) == 0 { |
| 68 |
continue |
| 69 |
} |
| 70 |
|
| 71 |
if line[0] == '"' || line[0] == '`' { |
| 72 |
p, err := strconv.Unquote(string(line)) |
| 73 |
if err != nil { |
| 74 |
return "" // malformed quoted string or multiline module path |
| 75 |
} |
| 76 |
return p |
| 77 |
} |
| 78 |
|
| 79 |
return string(line) |
| 80 |
} |
| 81 |
return "" // missing module path |
| 82 |
} |