forked from izqui/todos
/
configuration.go
173 lines (120 loc) · 3.33 KB
/
configuration.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
package main
import (
"encoding/json"
"errors"
"github.com/izqui/functional"
"os"
"os/user"
"path"
)
const (
TODOS_DIRECTORY = ".todos/"
HOME_DIRECTORY_CONFIG = "my home dir"
TODOS_CONF_FILENAME = "conf.json"
ISSUE_CACHE_FILENAME = "issues.json"
CLOSED_ISSUES_FILENAME = "closed.txt"
TOKEN_URL = "https://github.com/settings/tokens/new?scopes=repo,public_repo"
)
type Configuration struct {
GithubToken string `json:"github_token,omitempty"`
Owner string `json:"github_owner,omitempty"`
Repo string `json:"github_repo,omitempty"`
}
type ConfFile struct {
Config Configuration
File *os.File
}
type Issue struct {
File string `json:"file,omitempty"`
Hash string `json:"hash,omitempty"`
IssueNumber int `json:"issue_number,omitempty"`
Line int `json:"-"`
IssueURL string `json:"-"`
}
type IssueSlice []*Issue
func (slice IssueSlice) Len() int {
return len(slice)
}
func (slice IssueSlice) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func (slice IssueSlice) Less(i, j int) bool {
return slice[i].Hash < slice[j].Hash
}
func (slice IssueSlice) remove(i int) IssueSlice {
copy(slice[i:], slice[i+1:])
slice[len(slice)-1] = nil
return slice[:len(slice)-1]
}
type IssueCacheFile struct {
Issues IssueSlice
File *os.File
}
func OpenConfiguration(dir string) *ConfFile {
if dir == HOME_DIRECTORY_CONFIG {
usr, err := user.Current()
logOnError(err)
dir = usr.HomeDir
}
dir = path.Join(dir, TODOS_DIRECTORY)
//Create ~/.todos directory
err := os.MkdirAll(dir, 0777)
logOnError(err)
//Search for conf file inside directory
f, err := os.OpenFile(path.Join(dir, TODOS_CONF_FILENAME), os.O_RDWR|os.O_CREATE, 0660)
logOnError(err)
conf := Configuration{}
json.NewDecoder(f).Decode(&conf)
return &ConfFile{File: f, Config: conf}
}
func (conf *ConfFile) WriteConfiguration() error {
conf.File.Truncate(0)
conf.File.Seek(0, 0)
err := json.NewEncoder(conf.File).Encode(conf.Config)
logOnError(err)
return conf.File.Close()
}
func IssueCacheFilePath(dir string) string {
return path.Join(dir, TODOS_DIRECTORY, ISSUE_CACHE_FILENAME)
}
func LoadIssueCache(dir string) *IssueCacheFile {
filepath := IssueCacheFilePath(dir)
f, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0660)
logOnError(err)
issues := IssueSlice{}
json.NewDecoder(f).Decode(&issues)
return &IssueCacheFile{File: f, Issues: issues}
}
func (cache *IssueCacheFile) GetIssuesInFile(file string) IssueSlice {
array := IssueSlice{}
for _, is := range cache.Issues {
if is != nil && is.File == file {
array = append(array, is)
}
}
return array
}
func (cache *IssueCacheFile) RemoveIssue(issue Issue) error {
for i, is := range cache.Issues {
if is != nil && issue == *is {
cache.Issues.remove(i)
}
}
return errors.New("Not found")
}
/*
func (cache *IssueCacheFile) GetIssue(file, title string) (Issue, error) {
for _, i := range cache.Issues {
if i.File == file && i.Hash ==
return i, nil
}
return Issue{}, errors.New("Not found")
}*/
func (cache *IssueCacheFile) WriteIssueCache() error {
cache.File.Truncate(0)
cache.File.Seek(0, 0)
cache.Issues = functional.Filter(func(i *Issue) bool { return i != nil }, cache.Issues).([]*Issue)
err := json.NewEncoder(cache.File).Encode(cache.Issues)
logOnError(err)
return cache.File.Close()
}