This repository has been archived by the owner on Oct 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcut.go
155 lines (132 loc) · 2.99 KB
/
shortcut.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
package main
import (
"fmt"
"os"
"path"
)
// pathIsValid checks is path is a valid directory.
func pathIsValid(req string) error {
if !path.IsAbs(req) {
return os.ErrInvalid
}
info, err := os.Stat(req)
if err != nil {
return err
}
if !info.IsDir() {
return os.ErrNotExist
}
return nil
}
// A shortcuts contains several paths with same base. The most used path
// is considerated as 'Main'.
type Shortcut struct {
Main string // Main path
Paths map[string]int // All paths managed by shortcut
}
func NewShortcut(path string) *Shortcut {
return &Shortcut{path, map[string]int{path: 1}}
}
// Update updates count accesses to path if exists or adds it.
// The Main is updated if necessary.
func (s *Shortcut) Update(path string) {
if _, ok := s.Paths[path]; ok {
s.Paths[path]++
if s.Paths[path] > s.Paths[s.Main] {
s.Main = path
}
} else {
s.Paths[path] = 1
}
}
// Removes a path from Shortcut. If this path is the Main, main is reaffected
// to the new most used path, or "" if not exists.
func (s *Shortcut) Remove(path string) {
delete(s.Paths, path)
if path == s.Main {
c := 0
s.Main = ""
for k, v := range s.Paths {
if v > c {
s.Main = k
c = v
}
}
}
}
// Returns if shortcut contains paths
func (s *Shortcut) IsEmpty() bool {
return len(s.Paths) == 0
}
type Shortcuts map[string]*Shortcut
func NewShortcuts() Shortcuts {
return make(map[string]*Shortcut)
}
func (s Shortcuts) Remove(base string, path string) bool {
// Here, path is absolute
if shortcut, ok := s[base]; ok {
fmt.Println("Remove:", base, "->", path)
shortcut.Remove(path)
if shortcut.IsEmpty() {
fmt.Println("Delete:", base)
delete(s, base)
return true
}
}
return false
}
func (s Shortcuts) RemoveAllInvalidPaths(req string) {
// req is absolute
for req != "/" {
d, b := path.Dir(req), path.Base(req)
if err := pathIsValid(req); err != nil {
s.Remove(b, req)
}
req = d
}
}
func (s Shortcuts) Get(req string) string {
fmt.Println("Get")
if shortcut, ok := s[req]; ok {
// Execute for all paths in shortcut
for !shortcut.IsEmpty() {
if err := pathIsValid(shortcut.Main); err == nil {
// Update shortcut
s.Update(shortcut.Main)
fmt.Println("Shortcut updated:", shortcut)
return shortcut.Main
} else {
// Path not valid, remove it and check the next
// If there is only one paths, shortcut will be destroy
if del := s.Remove(req, shortcut.Main); del {
return ""
}
}
}
return ""
}
return ""
}
func (s Shortcuts) Add(req string) (string, error) {
err := pathIsValid(req)
if err != nil {
s.RemoveAllInvalidPaths(req)
return "", err
}
// Add shortcut for each subfile
s.Update(req)
return req, nil
}
func (s Shortcuts) Update(req string) {
for req != "/" {
d, b := path.Dir(req), path.Base(req)
if _, ok := s[b]; !ok {
s[b] = NewShortcut(req)
fmt.Println("New shortcut created:", b, "->", s[b])
} else {
s[b].Update(req)
fmt.Println("Shortcut updated:", b, "->", s[b])
}
req = d
}
}