-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcs.go
82 lines (64 loc) · 1.91 KB
/
vcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2019 Nirenjan Krishnan. All rights reserved.
package vanity
import (
"fmt"
"strings"
)
// This file manages the VCS structure
// SetRoot configures the root directory of the hosting provider where the
// package is hosted.
func (v *Vcs) SetRoot(r string) {
v.root = r
}
// SetProvider configures the Vcs structure to use the corresponding provider
func (v *Vcs) SetProvider(provider string) error {
switch strings.TrimSpace(strings.ToLower(provider)) {
case "github", "gitlab":
v.vcsType = "git"
v.dirFormat = "tree/master{/dir}"
v.fileFormat = "blob/master{/dir}/{file}#L{line}"
case "bitbucket", "gogs", "gitea":
// Default vcsType for Bitbucket is git, since Bitbucket is
// sunsetting the mercurial repositories.
v.vcsType = "git"
v.dirFormat = "src/master{/dir}"
v.fileFormat = "src/master{/dir}/{file}#L{line}"
default:
return fmt.Errorf("Unknown provider %v", provider)
}
v.provider = provider
return nil
}
// SetType sets the version control system type.
// It can be one of the following case-insensitive strings:
// Bazaar, Fossil, Git, Mercurial, Subversion
func (v *Vcs) SetType(t string) error {
switch strings.TrimSpace(strings.ToLower(t)) {
case "bazaar":
v.vcsType = "bzr"
case "fossil":
v.vcsType = "fossil"
case "git":
v.vcsType = "git"
case "mercurial":
v.vcsType = "hg"
case "subversion":
v.vcsType = "svn"
default:
return fmt.Errorf("Unknown VCS type %v", t)
}
return nil
}
// SetTemplates sets the URL templates for the directory and file
// listings. These are used by godoc to map the identifiers back to
// the source listings.
func (v *Vcs) SetTemplates(dir, file string) error {
// Check the file template, if it is not empty, it should
// contain at least one instance of {file}.
if file != "" && !strings.Contains(file, "{file}") {
return fmt.Errorf("Invalid file template %v", file)
}
v.dirFormat = dir
v.fileFormat = file
return nil
}