/
add.go
190 lines (164 loc) · 4.21 KB
/
add.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package apps
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/get3w/get3w"
"github.com/get3w/get3w/home"
"github.com/get3w/get3w/pkg/timeutils"
"github.com/get3w/get3w/server/api"
"github.com/get3w/get3w/storage"
"github.com/labstack/echo"
)
func addFromLocal(dirPath string, config *home.Config) (string, bool, error) {
appName := filepath.Base(dirPath)
for _, app := range config.Apps {
if strings.ToLower(app.Name) == strings.ToLower(appName) {
return "", false, fmt.Errorf(`App "%s" already exists`, appName)
}
}
configPath := filepath.Join(dirPath, storage.KeyConfig)
if _, err := os.Stat(configPath); err == nil {
return dirPath, true, nil
}
return dirPath, false, nil
}
func addFromCloud(dirPath, origin string, authConfig *home.AuthConfig) (string, error) {
var err error
nameParts := strings.SplitN(strings.Trim(origin, "/"), "/", 2)
owner, name := nameParts[0], nameParts[1]
appPath := filepath.Join(dirPath, name)
parser, err := storage.NewLocalParser(authConfig.Username, appPath)
if err != nil {
return "", err
}
client := get3w.NewClient(authConfig.AccessToken)
fmt.Printf("Getting repository '%s/%s'...\n", owner, name)
fmt.Print("Counting objects: ")
output, _, err := client.Apps.FilesChecksum(owner, name)
if err != nil {
return "", err
}
fmt.Printf("%d, done.\n", len(output.Files))
for path, remoteChecksum := range output.Files {
download := false
if !parser.Storage.IsExist(parser.Storage.GetSourceKey(path)) {
download = true
} else {
checksum, _ := parser.Storage.Checksum(parser.Storage.GetSourceKey(path))
if checksum != remoteChecksum {
download = true
}
}
if download {
fmt.Printf("Receiving object: %s", path)
fileOutput, _, err := client.Apps.GetFile(owner, name, path)
if err != nil {
return "", err
}
data, err := base64.StdEncoding.DecodeString(fileOutput.Content)
if err != nil {
return "", err
}
parser.Storage.Write(parser.Storage.GetSourceKey(path), data)
fmt.Println(", done.")
}
}
// parser.Config.Repository = repo
// err = parser.WriteConfig()
// if err != nil {
// return err
// }
builder, err := storage.NewLocalParser(authConfig.Username, appPath)
if err != nil {
return "", err
}
return appPath, builder.Build(true)
}
// Add app, for open and clone operation
func Add() echo.HandlerFunc {
return func(c echo.Context) error {
if api.IsAnonymous(c) {
return api.ErrorUnauthorized(c, nil)
}
owner := api.Owner(c)
input := &get3w.AppAddInput{}
err := api.LoadRequestInput(c, input)
if err != nil {
return api.ErrorBadRequest(c, err)
}
dirPath := input.DirPath
dirExists := true
if dirPath == "" {
dirExists = false
} else {
stat, err := os.Lstat(dirPath)
if err != nil {
dirExists = false
} else if !stat.IsDir() {
dirExists = false
}
}
if !dirExists {
return api.ErrorNotFound(c, nil)
}
config, err := home.LoadConfig()
if err != nil {
return api.ErrorBadRequest(c, err)
}
success := true
configExists := true
var app *get3w.App
var appPath string
if input.Origin != "" {
appPath, err = addFromCloud(dirPath, input.Origin, &config.AuthConfig)
if err != nil {
return api.ErrorBadRequest(c, err)
}
} else {
appPath, configExists, err = addFromLocal(dirPath, config)
if err != nil {
return api.ErrorBadRequest(c, err)
}
}
if input.Check && !configExists {
success = false
}
if success {
parser, err := storage.NewLocalParser(config.AuthConfig.Username, appPath)
if err != nil {
return api.ErrorInternal(c, err)
}
app = &get3w.App{
Owner: owner,
Name: parser.Name,
Description: parser.Config.Description,
Tags: "",
Path: appPath,
Private: false,
CreatedAt: timeutils.ToString(time.Now()),
UpdatedAt: timeutils.ToString(time.Now()),
}
exists := false
for _, app := range config.Apps {
if app.Path == appPath {
exists = true
break
}
}
if !exists {
config.Apps = append(config.Apps, app)
config.Save()
}
}
output := &get3w.AppAddOutput{
Config: configExists,
App: app,
}
return c.JSON(http.StatusOK, output)
}
}