/
getcmd.go
72 lines (65 loc) · 2.59 KB
/
getcmd.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
package gincmd
import (
"fmt"
"strings"
ginclient "github.com/G-Node/gin-cli/ginclient"
"github.com/G-Node/gin-cli/ginclient/config"
"github.com/G-Node/gin-cli/git"
"github.com/spf13/cobra"
)
func isValidRepoPath(path string) bool {
return strings.Contains(path, "/")
}
func getRepo(cmd *cobra.Command, args []string) {
prStyle := determinePrintStyle(cmd)
srvalias, _ := cmd.Flags().GetString("server")
conf := config.Read()
if srvalias == "" {
srvalias = conf.DefaultServer
}
repostr := args[0]
gincl := ginclient.New(srvalias)
requirelogin(cmd, gincl, prStyle != psJSON)
if !isValidRepoPath(repostr) {
Die(fmt.Sprintf("Invalid repository path '%s'. Full repository name should be the owner's username followed by the repository name, separated by a '/'.\nType 'gin help get' for information and examples.", repostr))
}
clonechan := make(chan git.RepoFileStatus)
go gincl.CloneRepo(repostr, clonechan)
formatOutput(clonechan, prStyle, 0)
defaultRemoteIfUnset("origin")
new, err := ginclient.CommitIfNew()
if new {
// Push the new commit to initialise origin
uploadchan := make(chan git.RepoFileStatus)
go gincl.Upload(nil, []string{"origin"}, uploadchan)
for range uploadchan {
// Wait for channel to close
}
}
CheckError(err)
}
// GetCmd sets up the 'get' repository subcommand
func GetCmd() *cobra.Command {
description := "Download a remote repository to a new directory and initialise the directory with the default options. The local directory is referred to as the 'clone' of the repository."
args := map[string]string{
"<repopath>": "The repository path must be specified on the command line. A repository path is the owner's username, followed by a \"/\" and the repository name.",
}
examples := map[string]string{
"Get and initialise the repository named 'example' owned by user 'alice'": "$ gin get alice/example",
"Get and initialise the repository named 'eegdata' owned by user 'peter'": "$ gin get peter/eegdata",
}
var cmd = &cobra.Command{
// Use: "get [--json | --verbose] <repopath>",
Use: "get [--json] <repopath>",
Short: "Retrieve (clone) a repository from the remote server",
Long: formatdesc(description, args),
Example: formatexamples(examples),
Args: cobra.ExactArgs(1),
Run: getRepo,
DisableFlagsInUseLine: true,
}
cmd.Flags().Bool("json", false, jsonHelpMsg)
// cmd.Flags().Bool("verbose", false, verboseHelpMsg)
cmd.Flags().String("server", "", "Specify server `alias` for the repository. See also 'gin servers'.")
return cmd
}