...

Source file src/pkg/cmd/go/internal/modfile/gopkgin.go

     1	// Copyright 2018 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// TODO: Figure out what gopkg.in should do.
     6	
     7	package modfile
     8	
     9	import "strings"
    10	
    11	// ParseGopkgIn splits gopkg.in import paths into their constituent parts
    12	func ParseGopkgIn(path string) (root, repo, major, subdir string, ok bool) {
    13		if !strings.HasPrefix(path, "gopkg.in/") {
    14			return
    15		}
    16		f := strings.Split(path, "/")
    17		if len(f) >= 2 {
    18			if elem, v, ok := dotV(f[1]); ok {
    19				root = strings.Join(f[:2], "/")
    20				repo = "github.com/go-" + elem + "/" + elem
    21				major = v
    22				subdir = strings.Join(f[2:], "/")
    23				return root, repo, major, subdir, true
    24			}
    25		}
    26		if len(f) >= 3 {
    27			if elem, v, ok := dotV(f[2]); ok {
    28				root = strings.Join(f[:3], "/")
    29				repo = "github.com/" + f[1] + "/" + elem
    30				major = v
    31				subdir = strings.Join(f[3:], "/")
    32				return root, repo, major, subdir, true
    33			}
    34		}
    35		return
    36	}
    37	
    38	func dotV(name string) (elem, v string, ok bool) {
    39		i := len(name) - 1
    40		for i >= 0 && '0' <= name[i] && name[i] <= '9' {
    41			i--
    42		}
    43		if i <= 2 || i+1 >= len(name) || name[i-1] != '.' || name[i] != 'v' || name[i+1] == '0' && len(name) != i+2 {
    44			return "", "", false
    45		}
    46		return name[:i-1], name[i:], true
    47	}
    48	

View as plain text