-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
118 lines (99 loc) · 2.87 KB
/
push.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"os"
"path"
"sync"
"github.com/maxchehab/geddit"
"github.com/ttacon/chalk"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// Push displays all files and directories that are available
func Push(args []string, _ []Command) (err error) {
username, password, subreddit := GetCredentials()
session, err := GetSession(username, password)
if err != nil {
return err
}
manifest, err := RetrieveManifestFromReddit(subreddit, session)
if err != nil {
return err
}
var options []string
for _, repo := range manifest.Repositories {
options = append(options, repo.Name)
}
selectedPath, err := os.Getwd()
if err != nil {
return err
}
message := fmt.Sprintf("Confirm push of repository [%v%v%v]", chalk.Cyan, path.Base(selectedPath), chalk.Reset)
confirm := false
prompt := &survey.Confirm{
Message: message,
}
survey.AskOne(prompt, &confirm, nil)
if !confirm {
return nil
}
pathsToUpload, err := GetAllFilesInDirectory(selectedPath)
if err != nil {
return err
}
var wg sync.WaitGroup
files := make(chan File, len(pathsToUpload))
fileErrors := make(chan error, len(pathsToUpload))
for _, path := range pathsToUpload {
wg.Add(1)
func(path string) {
defer wg.Done()
file, err := UploadFileByPath(path, selectedPath, session, subreddit)
files <- file
fileErrors <- err
}(path)
}
wg.Wait()
close(files)
close(fileErrors)
for err := range fileErrors {
if err != nil {
return err
}
}
var respository Repository
respository.Name = path.Base(selectedPath)
for file := range files {
respository.Files = append(respository.Files, file)
}
if manifest.Contains(respository) {
message := fmt.Sprintf("The repository [%v%v%v] has already been created. Would you like to update or rename the repository.", chalk.Cyan, respository.Name, chalk.Reset)
action := ""
prompt := &survey.Select{
Message: message,
Options: []string{"update", "rename"},
}
survey.AskOne(prompt, &action, nil)
if action == "rename" {
message = fmt.Sprintf("Rename [%v%v%v] to:", chalk.Cyan, respository.Name, chalk.Reset)
rename := ""
prompt := &survey.Input{
Message: message,
}
survey.AskOne(prompt, &rename, nil)
Prompt(fmt.Sprintf("Renamed [%v%v%v] to [%v%v%v]", chalk.Cyan, respository.Name, chalk.ResetColor, chalk.Cyan, rename, chalk.ResetColor))
respository.Name = rename
manifest.Repositories = append(manifest.Repositories, respository)
} else {
manifest.Update(respository)
}
} else {
manifest.Repositories = append(manifest.Repositories, respository)
}
Prompt(fmt.Sprintf("Updating manifest for [%v%v%v]", chalk.Cyan, respository.Name, chalk.ResetColor))
if len(manifest.Location) > 0 {
session.EditUserText(geddit.NewEdit(manifest.ToString(), "t3_"+manifest.Location))
} else {
session.Submit(geddit.NewTextSubmission(subreddit, "manifest.json", manifest.ToString(), false, nil))
}
return
}