-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.go
138 lines (126 loc) · 3.16 KB
/
utils.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
package main
import (
"encoding/json"
"errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"time"
)
var EmptyError = errors.New("-1")
var LogFilePath = "dateLog"
type Item struct {
Id int `json:"id"`
NodeId string `json:"node_id"`
Name string `json:"name"`
FullName string `json:"full_name"`
Private bool `json:"private"`
Owner struct {
Login string `json:"login"`
Id int64 `json:"id"`
NodeId string `json:"node_id"`
AvatarUrl string `json:"avatar_url"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"owner"`
HtmlUrl string `json:"html_url"`
Description string `json:"description"`
Fork bool `json:"fork"`
Url string `json:"url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PushedAt time.Time `json:"pushed_at"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
ForksCount int `json:"forks_count"`
Archived bool `json:"archived"`
Disabled bool `json:"disabled"`
AllowForking bool `json:"allow_forking"`
IsTemplate bool `json:"is_template"`
Topics []string `json:"topics"`
Visibility string `json:"visibility"`
Forks int `json:"forks"`
Watchers int `json:"watchers"`
DefaultBranch string `json:"default_branch"`
Score float64 `json:"score"`
}
type DateLog struct {
New []*Item `json:"new"`
Update []*Item `json:"update"`
}
func CheckFileExists(filename string) bool {
_, err := os.Stat(filename)
if err != nil {
return false
}
return true
}
func CreateFile(filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
return nil
}
func CreateDir(dirPath string) error {
if err := os.Mkdir(dirPath, os.ModePerm); err != nil {
return err
}
return nil
}
func ReadYamlFile(path string, obj any) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(content, obj)
if err != nil {
return err
}
return nil
}
func ReadJsonFile(filepath string, obj any) error {
file, err := os.OpenFile(filepath, os.O_RDONLY, os.ModeAppend)
if err != nil {
return err
}
byteValue, _ := ioutil.ReadAll(file)
if len(byteValue) == 0 {
return EmptyError
}
if err = json.Unmarshal(byteValue, obj); err != nil {
return err
}
return nil
}
func ReadFile(filepath string) []byte {
file, err := os.OpenFile(filepath, os.O_RDONLY, os.ModeAppend)
if err != nil {
return nil
}
byteValue, _ := ioutil.ReadAll(file)
return byteValue
}
func WriteFile(filepath string, content []byte) error {
if !CheckFileExists(filepath) {
_ = CreateFile(filepath)
}
file, err := os.OpenFile(filepath, os.O_RDWR|os.O_TRUNC, os.ModeAppend)
if err != nil {
return err
}
_, err = file.Write(content)
if err != nil {
return err
}
return nil
}
func GetCurrentDirectory() string {
currentDir, _ := os.Executable()
exPath := filepath.Dir(currentDir)
return exPath
}