-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject.go
133 lines (111 loc) · 2.43 KB
/
project.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
type Process struct{
name string
command string
color string
dir string
}
type Project struct{
name string
global map[string]Process
local map[string]Process
dir string
}
type ExportableProject struct{
Name string `json:"name"`
Global map[string]string `json:"global"`
Local map[string]string `json:"local"`
}
// Write the project to the json file
func (p *Project) Write() (err error) {
var f *os.File
f, err = os.OpenFile("hack.json", os.O_RDWR | os.O_CREATE | os.O_TRUNC, 0666)
if err != nil {
return
}
defer f.Close()
enc := json.NewEncoder(f)
err = enc.Encode(p.AsJson())
if err != nil {
return
}
return
}
func (p *Project) AsJson() (e *ExportableProject){
e = &ExportableProject{}
e.Name = p.name
e.Local = make(map[string]string)
for _, process := range p.local {
e.Local[process.name] = process.command
}
e.Global = make(map[string]string)
for _, process := range p.global {
e.Global[process.name] = process.command
}
return
}
func (p *Project) Size() (i int) {
i = len(p.global) + len(p.local)
return
}
func (p *Project) Processes(global bool) (target map[string]Process) {
if global {
target = p.global
}else{
target = p.local
}
return
}
func loadProject(path string, p *Project) (err error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return
}
jsonToProject(content, p)
return
}
func jsonToProject(content []byte, project *Project) {
var projMap map[string]*json.RawMessage
err := json.Unmarshal(content, &projMap)
var local map[string]string
if projMap["local"] == nil {
local = make(map[string]string)
} else {
err = json.Unmarshal(*projMap["local"], &local)
if err != nil {
return
}
}
var global map[string]string
if projMap["global"] == nil {
global = make(map[string]string)
} else {
err = json.Unmarshal(*projMap["global"], &global)
if err != nil {
return
}
}
colors := []string{"g", "y", "b", "m", "c"}
numColors := len(colors)
count := 0
project.local = make(map[string]Process, len(local))
for name, command := range local {
project.local[name] = Process{name, command, colors[count], project.dir}
count++
if(count >= numColors){
count = 0
}
}
project.global = make(map[string]Process, len(global))
for name, command := range global {
project.global[name] = Process{name, command, colors[count], project.dir}
if(count > numColors){
count = 0
}
}
}