-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
89 lines (76 loc) · 1.78 KB
/
publish.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
package plasmactlpublish
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"github.com/launchrctl/launchr"
)
func getRepoInfo() (repoName, lastCommitShortSHA string, err error) {
// Open repository
r, err := git.PlainOpen(".")
if err != nil {
return "", "", err
}
// Get repository name
remote, err := r.Remote("origin")
if err != nil {
return "", "", err
}
repoName = remote.Config().URLs[0]
repoName = filepath.Base(repoName)
repoName = repoName[:len(repoName)-4]
// Get last commit information
ref, err := r.Head()
if err != nil {
return "", "", err
}
lastCommitShortSHA = ref.Hash().String()[:7]
return repoName, lastCommitShortSHA, nil
}
func listFiles(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
launchr.Term().Printfln("Listing files in %s:", dir)
for _, file := range files {
if file.IsDir() {
continue
}
info, err := file.Info()
if err != nil {
return err
}
size := humanReadableSize(info.Size())
launchr.Term().Printfln("%s %10s %s %s", info.Mode(), size, info.ModTime().Format(time.Stamp), file.Name())
}
return nil
}
func humanReadableSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp])
}
func isURLAccessible(url string, code *int) bool {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return false
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
*code = resp.StatusCode
return resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices
}