inferPackageName extracts the package name from an import path. Examples: - "fmt" -> "fmt" - "github.com/foo/bar" -> "bar" - "gopkg.in/yaml.v2" -> "yaml"
(path string)
| 102 | // - "github.com/foo/bar" -> "bar" |
| 103 | // - "gopkg.in/yaml.v2" -> "yaml" |
| 104 | func inferPackageName(path string) string { |
| 105 | // Get the last component of the path |
| 106 | if idx := strings.LastIndex(path, "/"); idx >= 0 { |
| 107 | path = path[idx+1:] |
| 108 | } |
| 109 | |
| 110 | // Remove version suffixes like .v2, .v3, etc. |
| 111 | if idx := strings.Index(path, ".v"); idx >= 0 { |
| 112 | path = path[:idx] |
| 113 | } |
| 114 | |
| 115 | // Remove other suffixes after dots (less common) |
| 116 | if idx := strings.Index(path, "."); idx >= 0 { |
| 117 | // Only if it looks like a version or special suffix |
| 118 | suffix := path[idx:] |
| 119 | if len(suffix) > 1 && (suffix[1] >= '0' && suffix[1] <= '9' || suffix[1] == 'v') { |
| 120 | path = path[:idx] |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return path |
| 125 | } |