Skip to content

Commit 1d60864

Browse files
committed
Repo list
1 parent f397784 commit 1d60864

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

cmd/repo_add.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ var addCmd = &cobra.Command{
4747
if sshKey == "" {
4848
sshKey = home + "/.ssh/id_rsa"
4949
}
50-
tools.Clone(tools.RepoInit(args[0],"ssh",sshKey), args[1])
50+
tools.Clone(tools.RepoInit(args[0],"ssh",sshKey, args[1]))
5151
} else if githubToken != "" {
52-
tools.Clone(tools.RepoInit(args[0], "github", githubToken), args[1])
52+
tools.Clone(tools.RepoInit(args[0], "github", githubToken, args[1]))
5353
} else {
54-
tools.Clone(tools.RepoInit(args[0], "pubic", ""), args[1])
54+
tools.Clone(tools.RepoInit(args[0], "pubic", "", args[1]))
5555
}
5656
},
5757
}

cmd/repo_list.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright © 2019 Juan Ezquerro LLanes <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"standardized/internal"
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/viper"
23+
"path/filepath"
24+
"io/ioutil"
25+
"log"
26+
"fmt"
27+
)
28+
29+
// repoListCmd represents the repoList command
30+
var repoListCmd = &cobra.Command{
31+
Use: "list",
32+
Short: "List available repositories",
33+
Run: func(cmd *cobra.Command, args []string) {
34+
config_dir := tools.GetConfigDir()
35+
36+
files, err := ioutil.ReadDir(config_dir)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
for _, f := range files {
42+
viper.SetConfigFile(filepath.Join(config_dir, f.Name()) + "/auth.yaml")
43+
err := viper.ReadInConfig()
44+
if err != nil {
45+
panic(fmt.Errorf("Fatal error config file: %s \n", err))
46+
}
47+
fmt.Println(f.Name() + " : " + viper.GetString("url"))
48+
}
49+
},
50+
}
51+
52+
func init() {
53+
repoCmd.AddCommand(repoListCmd)
54+
}

internal/repoTools.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,28 @@ import (
2727
"fmt"
2828
)
2929

30-
func RepoInit (name string, auth_type string, auth_value string) string {
30+
func RepoInit (name string, auth_type string, auth_value string, url string) string {
3131
repo_path := filepath.Join(GetConfigDir(), name)
3232

3333
if _, err := os.Stat(repo_path); os.IsNotExist(err) {
3434
os.Mkdir(repo_path, os.ModePerm)
3535
}
3636

3737
f, _ := os.Create(repo_path + "/auth.yaml")
38-
f.Write([]byte("type: {{.auth_type}}\nvalue: {{.auth_value}}\n"))
38+
f.Write([]byte("type: {{.auth_type}}\nvalue: {{.auth_value}}\nurl: {{ .repo_url }}\n"))
3939
f.Close()
4040

4141
config := map[string]string{
4242
"auth_type": auth_type,
4343
"auth_value": auth_value,
44+
"repo_url" : url,
4445
}
4546
ParseTemplate(repo_path + "/auth.yaml", config)
4647

4748
return repo_path
4849
}
4950

50-
func ParseRepoAuth(path string) (string, string) {
51+
func ParseRepoAuth(path string) (string, string, string) {
5152
viper.SetConfigType("yaml")
5253
viper.SetConfigName("auth")
5354
viper.AddConfigPath(path)
@@ -56,33 +57,35 @@ func ParseRepoAuth(path string) (string, string) {
5657
panic(fmt.Errorf("Fatal error config file: %s \n", err))
5758
}
5859

59-
return viper.GetString("type"), viper.GetString("value")
60+
return viper.GetString("type"), viper.GetString("value"), viper.GetString("url")
6061
}
6162

62-
func Clone (path string, url string) {
63-
opts := GetCloneOptions(path, url)
63+
func Clone (path string) {
64+
opts := GetCloneOptions(path)
6465
_, err := git.PlainClone(filepath.Join(path, "src"), false, &opts)
6566

6667
if err != nil {
6768
log.Fatal(err)
6869
}
6970
}
7071

71-
func GetCloneOptions(path string, url string) git.CloneOptions {
72-
switch rtype, rauth := ParseRepoAuth(path); rtype {
72+
func GetCloneOptions(path string) git.CloneOptions {
73+
var rtype, rauth, rurl string
74+
75+
switch rtype, rauth, rurl = ParseRepoAuth(path); rtype {
7376
case "ssh":
7477
sshAuth, kr := ssh.NewPublicKeysFromFile("git", rauth, "")
7578
if kr != nil {
7679
log.Fatal(kr)
7780
}
7881
return git.CloneOptions{
79-
URL: url,
82+
URL: rurl,
8083
Auth: sshAuth,
8184
Progress: os.Stdout,
8285
}
8386
case "github":
8487
return git.CloneOptions{
85-
URL: url,
88+
URL: rurl,
8689
Auth: &http.BasicAuth{
8790
Username: "standardized", // yes, this can be anything except an empty string
8891
Password: rauth,
@@ -91,11 +94,11 @@ func GetCloneOptions(path string, url string) git.CloneOptions {
9194
}
9295
}
9396

94-
return git.CloneOptions{URL: url, Progress: os.Stdout}
97+
return git.CloneOptions{URL: rurl, Progress: os.Stdout}
9598
}
9699

97100
func GetPullOptions(path string) git.PullOptions {
98-
switch rtype, rauth := ParseRepoAuth(path); rtype {
101+
switch rtype, rauth, _ := ParseRepoAuth(path); rtype {
99102
case "ssh":
100103
sshAuth, kr := ssh.NewPublicKeysFromFile("git", rauth, "")
101104
if kr != nil {

0 commit comments

Comments
 (0)