|
| 1 | +package vanity |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/rotationalio/vanity/config" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + protocolGit = "git" |
| 14 | + protocolGitHub = "github" |
| 15 | + protocolGOGS = "gogs" |
| 16 | +) |
| 17 | + |
| 18 | +var godoc *url.URL = &url.URL{ |
| 19 | + Scheme: "https", |
| 20 | + Host: "godoc.org", |
| 21 | +} |
| 22 | + |
| 23 | +var validProtocols = map[string]struct{}{ |
| 24 | + protocolGit: {}, |
| 25 | + protocolGitHub: {}, |
| 26 | + protocolGOGS: {}, |
| 27 | +} |
| 28 | + |
| 29 | +type GoPackage struct { |
| 30 | + Domain string `json:"-"` // the vanity URL domain to use |
| 31 | + Module string `json:"-"` // the module name where go.mod is located; parsed from the repository |
| 32 | + Package string `json:"-"` // the full package path being requested for correct redirects |
| 33 | + Protocol string `json:"protocol"` // can be "git", "github", or "gogs" -- defaults to "git" |
| 34 | + Repository string `json:"repository"` // a path to the public repository starting with https:// |
| 35 | + Branch string `json:"branch"` // the name of the default branch -- defaults to "main" |
| 36 | + repo *url.URL `json:"-"` // the parsed repository URL |
| 37 | + user string `json:"-"` // the user or organization from the repository |
| 38 | +} |
| 39 | + |
| 40 | +func (p *GoPackage) Resolve(conf *config.Config) (err error) { |
| 41 | + // Verify there is a repository |
| 42 | + if p.Repository == "" { |
| 43 | + return ErrNoRepository |
| 44 | + } |
| 45 | + |
| 46 | + // Parse the repository |
| 47 | + if p.repo, err = url.Parse(p.Repository); err != nil { |
| 48 | + return ErrInvalidRepository |
| 49 | + } |
| 50 | + |
| 51 | + parts := strings.Split(p.repo.Path, "/") |
| 52 | + if len(parts) != 3 { |
| 53 | + return ErrInvalidRepository |
| 54 | + } |
| 55 | + |
| 56 | + p.user = parts[1] |
| 57 | + p.Module = parts[2] |
| 58 | + |
| 59 | + // Check protocol |
| 60 | + if p.Protocol == "" { |
| 61 | + p.Protocol = protocolGit |
| 62 | + } |
| 63 | + |
| 64 | + if _, ok := validProtocols[p.Protocol]; !ok { |
| 65 | + return ErrInvalidProtocol |
| 66 | + } |
| 67 | + |
| 68 | + // Manage the configuration |
| 69 | + if conf != nil { |
| 70 | + p.Domain = conf.Domain |
| 71 | + |
| 72 | + if p.Branch == "" { |
| 73 | + p.Branch = conf.DefaultBranch |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Check the ref |
| 78 | + if p.Branch == "" { |
| 79 | + p.Branch = "main" |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (p *GoPackage) WithRequest(r *http.Request) GoPackage { |
| 86 | + pkg := p.Module |
| 87 | + if r != nil { |
| 88 | + pkg = r.URL.Path |
| 89 | + } |
| 90 | + |
| 91 | + clone := GoPackage{ |
| 92 | + Domain: p.Domain, |
| 93 | + Module: p.Module, |
| 94 | + Package: pkg, |
| 95 | + Protocol: p.Protocol, |
| 96 | + Repository: p.Repository, |
| 97 | + Branch: p.Branch, |
| 98 | + repo: p.repo, |
| 99 | + user: p.user, |
| 100 | + } |
| 101 | + |
| 102 | + if clone.Domain == "" && r != nil { |
| 103 | + clone.Domain = r.Host |
| 104 | + } |
| 105 | + |
| 106 | + return clone |
| 107 | +} |
| 108 | + |
| 109 | +func (p GoPackage) Redirect() string { |
| 110 | + return godoc.ResolveReference( |
| 111 | + &url.URL{ |
| 112 | + Path: filepath.Join("/", p.Domain, p.Package), |
| 113 | + }, |
| 114 | + ).String() |
| 115 | +} |
| 116 | + |
| 117 | +func (p GoPackage) GoImportMeta() string { |
| 118 | + parts := []string{ |
| 119 | + p.Import(), |
| 120 | + p.Protocol, |
| 121 | + p.repo.String(), |
| 122 | + } |
| 123 | + |
| 124 | + return strings.Join(parts, " ") |
| 125 | +} |
| 126 | + |
| 127 | +func (p GoPackage) GoSourceMeta() string { |
| 128 | + parts := []string{ |
| 129 | + p.Import(), |
| 130 | + p.repo.String(), |
| 131 | + "", |
| 132 | + "", |
| 133 | + } |
| 134 | + parts[2], parts[3] = p.Source() |
| 135 | + return strings.Join(parts, " ") |
| 136 | +} |
| 137 | + |
| 138 | +func (p GoPackage) Import() string { |
| 139 | + return filepath.Join(p.Domain, p.Module) |
| 140 | +} |
| 141 | + |
| 142 | +func (p GoPackage) Source() (string, string) { |
| 143 | + switch p.Protocol { |
| 144 | + case protocolGit, protocolGitHub: |
| 145 | + return p.githubSource() |
| 146 | + case protocolGOGS: |
| 147 | + return p.gogsSource() |
| 148 | + default: |
| 149 | + return "", "" |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func (p GoPackage) githubSource() (string, string) { |
| 154 | + base := filepath.Join(p.user, p.Module) |
| 155 | + directoryPath := filepath.Join(base, "tree", p.Branch+"{/dir}") |
| 156 | + filePath := filepath.Join(base, "blob", p.Branch+"{/dir}", "{file}#L{line}") |
| 157 | + |
| 158 | + uri := p.repo.ResolveReference(&url.URL{Path: "/"}).String() |
| 159 | + return uri + directoryPath, uri + filePath |
| 160 | +} |
| 161 | + |
| 162 | +func (p GoPackage) gogsSource() (string, string) { |
| 163 | + base := filepath.Join(p.user, p.Module) |
| 164 | + directoryPath := filepath.Join(base, "src", p.Branch+"{/dir}") |
| 165 | + filePath := filepath.Join(base, "src", p.Branch+"{/dir}", "{file}#L{line}") |
| 166 | + |
| 167 | + uri := p.repo.ResolveReference(&url.URL{Path: "/"}).String() |
| 168 | + return uri + directoryPath, uri + filePath |
| 169 | +} |
0 commit comments