@@ -2,24 +2,25 @@ package storage
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 pathutil "path"
78 "strconv"
89 "strings"
910 "sync"
1011 "time"
1112
12- clientv3 "go.etcd.io/etcd/client/v3"
1313 "go.etcd.io/etcd/client/pkg/v3/transport"
14+ clientv3 "go.etcd.io/etcd/client/v3"
1415)
1516
1617const DefaultPrefix = "/chart_backend_bucket"
1718
1819var (
1920 DefileDialTimeOut = "5s"
2021 TimeStampKey = "timestamp"
21- ErrNotExistEndpoints = fmt . Errorf ("endpoints cannot connect !" )
22- ErrNotExist = fmt . Errorf ("not exist!" )
22+ ErrNotExistEndpoints = errors . New ("endpoints cannot connect !" )
23+ ErrNotExist = errors . New ("not exist!" )
2324)
2425
2526type etcdOpts struct {
@@ -38,7 +39,7 @@ type etcdStorage struct {
3839 mu sync.RWMutex
3940}
4041
41- //connection cut off
42+ // connection cut off
4243func isServerErr (err error ) bool {
4344 if err != nil {
4445 if err == context .Canceled {
@@ -52,11 +53,9 @@ func isServerErr(err error) bool {
5253 return false
5354}
5455
55- //prapare {basepath} dir
56+ // prapare {basepath} dir
5657func (e * etcdStorage ) probe () error {
57- var (
58- err error
59- )
58+ var err error
6059 ctx , cancel := context .WithCancel (e .ctx )
6160 _ , err = e .c .Put (ctx , e .base , "" )
6261 cancel ()
@@ -100,13 +99,11 @@ func (e *etcdStorage) delTimeStamp(path string) error {
10099 return err
101100}
102101
103- //
104102func (e * etcdStorage ) ListObjects (prefix string ) ([]Object , error ) {
105- var (
106- objs []Object
107- )
103+ var objs []Object
108104 ctx , cancel := context .WithTimeout (e .ctx , e .opts .dialtimeout )
109105 newpath := pathutil .Join (e .base , prefix )
106+ newpath = normalizePath (newpath )
110107 resps , err := e .c .Get (ctx , newpath , clientv3 .WithPrefix ())
111108 cancel ()
112109 if err != nil {
@@ -118,7 +115,7 @@ func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
118115 if objectPathIsInvalid (path ) {
119116 continue
120117 }
121- //TODO need optimizate
118+ // TODO need optimizate
122119 if strings .HasSuffix (path , TimeStampKey ) {
123120 continue
124121 }
@@ -134,13 +131,10 @@ func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
134131 }
135132 }
136133 return objs , nil
137-
138134}
139135
140136func (e * etcdStorage ) GetObject (path string ) (Object , error ) {
141- var (
142- modifytime time.Time
143- )
137+ var modifytime time.Time
144138 ctx , cancel := context .WithTimeout (e .ctx , e .opts .dialtimeout )
145139 newpath := pathutil .Join (e .base , path )
146140 resps , err := e .c .Get (ctx , newpath )
@@ -164,9 +158,7 @@ func (e *etcdStorage) GetObject(path string) (Object, error) {
164158}
165159
166160func (e * etcdStorage ) PutObject (path string , content []byte ) error {
167- var (
168- updatetime = time .Now ()
169- )
161+ updatetime := time .Now ()
170162 ctx , cancel := context .WithTimeout (e .ctx , e .opts .dialtimeout )
171163 newpath := pathutil .Join (e .base , path )
172164 _ , err := e .c .Put (ctx , newpath , string (content ))
@@ -191,9 +183,7 @@ func (e *etcdStorage) DeleteObject(path string) error {
191183}
192184
193185func parseConf (endpoints string , cafile , certfile , keyfile string , dialtime time.Duration ) clientv3.Config {
194- var (
195- es []string
196- )
186+ var es []string
197187 if endpoints == "" {
198188 panic (ErrNotExistEndpoints )
199189 }
@@ -215,9 +205,7 @@ func parseConf(endpoints string, cafile, certfile, keyfile string, dialtime time
215205}
216206
217207func NewEtcdCSBackend (endpoints string , cafile , certfile , keyfile string , prefix string ) Backend {
218- var (
219- basepath string
220- )
208+ var basepath string
221209 DialTimeOut , _ := time .ParseDuration (DefileDialTimeOut )
222210 cli , err := clientv3 .New (parseConf (endpoints , cafile , certfile , keyfile , DialTimeOut ))
223211 if err != nil {
@@ -249,5 +237,4 @@ func NewEtcdCSBackend(endpoints string, cafile, certfile, keyfile string, prefix
249237 panic (err )
250238 }
251239 return e
252-
253240}
0 commit comments