-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidea.go
More file actions
148 lines (126 loc) · 2.74 KB
/
idea.go
File metadata and controls
148 lines (126 loc) · 2.74 KB
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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/rodaine/table"
)
// Idea Struct
type Idea struct {
Id int
Date, State, Idea string
}
// Ideas List
type Ideas []Idea
var filename = ".ideas.js"
// Create Initial File to store data
func Init() error {
if !fileExist(filename) {
file, err := os.Create(filename)
file.Close()
return err
}
return nil
}
// Load Ideas From File
func (ideas *Ideas) Load() error {
var data []byte
if fileExist(filename) {
data, _ = ioutil.ReadFile(filename)
} else {
dir, _ := os.UserHomeDir()
data, _ = ioutil.ReadFile(dir + "/" + filename)
}
err := json.Unmarshal([]byte(data), &ideas)
return err
}
// Save ideas to file
func (ideas Ideas) Save() error {
//data to json
data, err := json.Marshal(ideas)
if err != nil {
return err
}
if fileExist(filename) {
err = ioutil.WriteFile(filename, data, 0644)
return err
}
dir, _ := os.UserHomeDir()
err = ioutil.WriteFile(dir+"/"+filename, data, 0644)
return err
}
// Create new ideas
func (ideas Ideas) Create(note string) error {
id := 1
if len(ideas) > 0 {
id = ideas[len(ideas)-1].Id + 1
}
//get current date
date := time.Now()
idea := Idea{
Id: id,
Date: date.Format("Monday, 02 Jan 2006"),
State: "OPEN",
Idea: note,
}
ideas = append(ideas, idea)
return ideas.Save()
}
// Remove Ideas
func (ideas Ideas) Remove(id int) error {
for i, v := range ideas {
if v.Id == id {
ideas = append(ideas[:i], ideas[i+1:]...)
}
}
return ideas.Save()
}
// List all ideas
func (ideas Ideas) List() {
// Format table colors
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
//create table
tbl := table.New("ID", "Date", "State", "Idea")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, v := range ideas {
// add table row
tbl.AddRow(v.Id, v.Date, v.State, v.Idea)
}
tbl.Print()
}
// Solve idea
func (ideas Ideas) Solve(Id int) error {
i := 0
for k, v := range ideas {
if Id == v.Id {
i = k
}
}
ideas[i].State = "SOLVED"
return ideas.Save()
}
func (ideas Ideas) ListByState(state string) {
// Format table colors
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
//create table
tbl := table.New("ID", "Date", "State", "Idea")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, v := range ideas {
if v.State == strings.ToUpper(state) {
tbl.AddRow(v.Id, v.Date, v.State, v.Idea)
}
}
tbl.Print()
}
func fileExist(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}