This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ops.go
205 lines (185 loc) · 4.92 KB
/
ops.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2020 TriggerMesh Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package file
import (
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"strings"
"time"
git "gopkg.in/src-d/go-git.v4"
)
const (
tmpPath = "/tmp/tm/"
)
const letterBytesDNS = "abcdefghijklmnopqrstuvwxyz"
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// RandString accepts integer value N and returns random string with length of N
func RandString(n int) string {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[random.Intn(len(letterBytes))]
}
return string(b)
}
// RandStringDNS accepts integer value N and returns "DNS-friendly" random string with length of N
func RandStringDNS(n int) string {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[random.Intn(len(letterBytesDNS))]
}
return string(b)
}
// IsLocal return true if path is local filesystem
func IsLocal(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
// IsDir return true if path is directory
func IsDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
// IsRemote return true if path is URL
func IsRemote(path string) bool {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "git@") {
return true
}
if _, err := http.Head("https://" + path); err != nil {
return false
}
return true
}
// IsGit most likely return true if path is URL to git repository
func IsGit(path string) bool {
if strings.HasSuffix(path, ".git") || strings.HasPrefix(path, "git@") {
return true
}
url, err := url.Parse(path)
if err != nil {
return false
}
if url.Scheme == "" {
url.Scheme = "https"
}
resp, err := http.Head(url.String() + ".git")
if err != nil {
return false
}
for _, status := range []int{200, 301, 302, 401} {
if resp.StatusCode == status {
return true
}
}
return false
}
// IsGitFile accepts string and, if this string is git repsotiry,
// returns URL to clone and relative file path inside cloned repository directory
// Otherwise it returns empty strings
func IsGitFile(path string) (string, string) {
uri := strings.Split(path, "/")
if len(uri) < 8 {
return "", ""
}
repository := fmt.Sprintf("https://%s/%s/%s", uri[0], uri[1], uri[2])
file := strings.Join(uri[5:], "/")
if strings.HasPrefix(uri[0], "http") {
repository = fmt.Sprintf("%s//%s/%s/%s", uri[0], uri[2], uri[3], uri[4])
file = strings.Join(uri[7:], "/")
}
if !IsGit(repository) {
return "", ""
}
return repository, file
}
// IsRegistry return true if path "behaves" like URL to docker registry
func IsRegistry(path string) bool {
url, err := url.Parse(path)
if err != nil {
return false
}
// Need to find the way how to detect docker registry
if strings.Contains(url.String(), "docker.") {
return true
}
if url.Scheme == "" {
url.Scheme = "https"
}
resp, err := http.Head(url.String())
if err != nil {
return false
}
if resp.StatusCode == 405 {
return true
}
return false
}
// Download receives URL and return path to saved file
func Download(url string) (string, error) {
path := fmt.Sprintf("%s/download", tmpPath)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return "", err
}
path = fmt.Sprintf("%s/%s", path, RandString(10))
out, err := os.Create(path)
if err != nil {
return "", err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return path, nil
}
// Clone runs `git clone` operation for specified URL and returns local path to repository root directory
func Clone(url string) (string, error) {
path := fmt.Sprintf("%s/git/%s", tmpPath, RandString(10))
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return "", err
}
_, err := git.PlainClone(path, false, &git.CloneOptions{
URL: url,
})
return path, err
}
// Write creates file named as passed filename and writes data into this file
func Write(filename, data string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(data)
return err
}
// MakeDir created directory and all missing parents
func MakeDir(path string) error {
return os.MkdirAll(path, os.FileMode(0700))
}