Skip to content

Commit e97b4a4

Browse files
author
xinbg
committed
Merge branch 'master' of github.com:FrontMage/dynamo.cli
2 parents ad33197 + eee73fc commit e97b4a4

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

db/dbsession.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func GetDynamoSession(accessKeyID, secretAccessKey, region string) *dynamodb.Dyn
2323
return DynamoDB
2424
}
2525

26-
// ListTables returns all table names from dynamoDB
26+
// ListTable returns all table names from dynamoDB
2727
func ListTable(receiver []*string, lastEvaluatedTableName *string) ([]*string, error) {
2828
if result, err := DynamoDB.ListTables(&dynamodb.ListTablesInput{
2929
ExclusiveStartTableName: lastEvaluatedTableName,

main.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
cli "gopkg.in/urfave/cli.v2"
1818
)
1919

20-
// TODO better suggest
20+
// TODO better suggest, suggest based on hash key and range key
2121
var tableNameSuggestions []prompt.Suggest
2222

2323
// Key bindings, reserved, might use them oneday
@@ -71,16 +71,13 @@ func executor(in string) {
7171

7272
ctx, cancel := context.WithCancel(context.Background())
7373
sigCh := make(chan os.Signal, 1)
74-
signal.Notify(sigCh, os.Interrupt)
74+
signal.Notify(sigCh, os.Interrupt) // sigCh only listens to os.Interrupt
7575
// Listen to the os interrupt signal which is ctrl+c
7676
// when ctrl+c is pressed, cancel current query
77-
// killSigCh for notify this goroutine to terminate, otherwise it will keep listening
78-
killSigCh := make(chan struct{}, 1)
7977
go func() {
8078
select {
8179
case <-sigCh:
8280
cancel()
83-
case <-killSigCh:
8481
return
8582
}
8683
}()
@@ -93,13 +90,10 @@ func executor(in string) {
9390
// so that new prompts won't popup
9491
select {
9592
case <-ctx.Done():
96-
killSigCh <- struct{}{}
9793
return
9894
case r := <-resultCh:
99-
killSigCh <- struct{}{}
10095
fmt.Println(r)
10196
case e := <-errCh:
102-
killSigCh <- struct{}{}
10397
fmt.Println(e)
10498
}
10599
}
@@ -179,6 +173,7 @@ func completer(d prompt.Document) []prompt.Suggest {
179173

180174
func main() {
181175
// grmon.Start()
176+
defer recover()
182177
var accessKeyID string
183178
var secretAccessKey string
184179
var region string
@@ -235,5 +230,4 @@ func main() {
235230
if err := app.Run(os.Args); err != nil {
236231
fmt.Println(err.Error())
237232
}
238-
defer recover()
239233
}

signal/define.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package signal
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// KeyValueStore stores key value pairs, proved simple set get functions
8+
type KeyValueStore interface {
9+
// Set sets a key value pair
10+
Set(key string, value interface{})
11+
// Get gets a value by key
12+
Get(key string) interface{}
13+
}
14+
15+
// Context is the context for router handler functions
16+
type Context interface {
17+
// IsNext returns a flag indicates whether the next function should be called
18+
IsNext() bool
19+
// Next can set a flag, tell the trigger to call next function
20+
Next()
21+
// ResetIsNext resets the next flag, makes IsNext return false
22+
ResetIsNext()
23+
// Context needs KeyValueStore to pass payloads
24+
KeyValueStore
25+
}
26+
27+
// Signal defines a signal dispatcher
28+
type Signal struct {
29+
PathMap map[string][]func(Context)
30+
}
31+
32+
// Register register a handler function after some path
33+
func (r *Signal) Register(path string, handlers ...func(Context)) {
34+
if path == "" {
35+
panic("Invalid path")
36+
} else if r.PathMap[path] == nil {
37+
r.PathMap[path] = handlers
38+
} else {
39+
for _, h := range handlers {
40+
r.PathMap[path] = append(r.PathMap[path], h)
41+
}
42+
}
43+
}
44+
45+
// Trigger triggers a bunch of handlers registered unser some path
46+
func (r *Signal) Trigger(path string, ctx Context) {
47+
if path == "" {
48+
panic("Invalid path")
49+
} else if r.PathMap[path] == nil {
50+
panic(fmt.Sprintf("Path %s not exits", path))
51+
} else {
52+
for _, h := range r.PathMap[path] {
53+
h(ctx)
54+
if ctx.IsNext() {
55+
ctx.ResetIsNext()
56+
} else {
57+
break
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)