This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
remote_cmds.go
73 lines (60 loc) · 2.02 KB
/
remote_cmds.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
package src
import (
"errors"
"fmt"
"log"
"net/http"
"strings"
"sourcegraph.com/sourcegraph/go-sourcegraph/sourcegraph"
)
func init() {
remoteGroup, err := CLI.AddCommand("remote",
"remote operations",
"The remote command displays information about the remote repository corresponding to the local repository. Its subcommands perform operations on the remote repository.",
&remoteCmd,
)
if err != nil {
log.Fatal(err)
}
remoteGroup.SubcommandsOptional = true
setDefaultRepoURIOpt(remoteGroup)
initRemoteRepoCmds(remoteGroup)
initRemoteImportBuildCmd(remoteGroup)
}
type RemoteCmd struct {
RepoURI string `short:"r" long:"repo" description:"repository URI (defaults to VCS 'srclib' or 'origin' remote URL)"`
}
var remoteCmd RemoteCmd
func (c *RemoteCmd) Execute(args []string) error {
if len(args) > 0 {
return fmt.Errorf("no such subcommand: src remote %v; see src remote --help", strings.Join(args, " "))
}
cl := NewAPIClientWithAuthIfPresent()
rrepo, err := c.getRemoteRepo(cl)
if err != nil {
return err
}
printRemoteRepo(rrepo)
log.Println()
log.Println("# Run 'src remote --help' to see other remote operations you can perform.")
return nil
}
func (c *RemoteCmd) getRemoteRepo(cl *sourcegraph.Client) (*sourcegraph.Repo, error) {
if c.RepoURI == "" {
lrepo, err := openLocalRepo()
var errMsg string
if lrepo == nil {
errMsg = "no git/hg repository found in or above the current dir"
} else if err == errNoVCSCloneURL {
errMsg = err.Error() + "\n\n"
} else {
errMsg = err.Error()
}
return nil, errors.New(errMsg + "; to specify which remote repository to act upon instead of attempting automatic detection, use --repo (e.g., '--repo github.com/owner/repo')")
}
rrepo, _, err := cl.Repos.GetOrCreate(sourcegraph.RepoSpec{URI: c.RepoURI}, nil)
if sourcegraph.IsHTTPErrorCode(err, http.StatusNotFound) {
return nil, fmt.Errorf("No repository exists on the remote with the URI %q. To add it, use 'src remote add'. The underlying error was: %s", c.RepoURI, err)
}
return rrepo, err
}