Skip to content

Commit 42a9bbf

Browse files
committed
Expose context to parent shell. Missed out somehow before
1 parent 08bd972 commit 42a9bbf

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

context.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ishell
22

33
// Context is an ishell context. It embeds ishell.Actions.
44
type Context struct {
5-
values map[string]interface{}
5+
contextValues
66
progressBar ProgressBar
77
err error
88

@@ -24,35 +24,38 @@ func (c *Context) Err(err error) {
2424
c.err = err
2525
}
2626

27+
// ProgressBar returns the progress bar for the current shell context.
28+
func (c *Context) ProgressBar() ProgressBar {
29+
return c.progressBar
30+
}
31+
32+
// contextValues is the map for values in the context.
33+
type contextValues map[string]interface{}
34+
2735
// Get returns the value associated with this context for key, or nil
2836
// if no value is associated with key. Successive calls to Get with
2937
// the same key returns the same result.
30-
func (c *Context) Get(key string) interface{} {
31-
return c.values[key]
38+
func (c contextValues) Get(key string) interface{} {
39+
return c[key]
3240
}
3341

3442
// Set sets the key in this context to value.
35-
func (c *Context) Set(key string, value interface{}) {
36-
if c.values == nil {
37-
c.values = make(map[string]interface{})
43+
func (c *contextValues) Set(key string, value interface{}) {
44+
if *c == nil {
45+
*c = make(map[string]interface{})
3846
}
39-
c.values[key] = value
47+
(*c)[key] = value
4048
}
4149

4250
// Del deletes key and its value in this context.
43-
func (c *Context) Del(key string) {
44-
delete(c.values, key)
51+
func (c contextValues) Del(key string) {
52+
delete(c, key)
4553
}
4654

4755
// Keys returns all keys in the context.
48-
func (c *Context) Keys() (keys []string) {
49-
for key := range c.values {
56+
func (c contextValues) Keys() (keys []string) {
57+
for key := range c {
5058
keys = append(keys, key)
5159
}
5260
return
5361
}
54-
55-
// ProgressBar returns the progress bar for the current shell context.
56-
func (c *Context) ProgressBar() ProgressBar {
57-
return c.progressBar
58-
}

ishell.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ type Shell struct {
4747
multiChoiceActive bool
4848
haltChan chan struct{}
4949
historyFile string
50-
contextValues map[string]interface{}
5150
autoHelp bool
5251
rawArgs []string
5352
progressBar ProgressBar
53+
contextValues
5454
Actions
5555
}
5656

@@ -624,11 +624,17 @@ func newContext(s *Shell, cmd *Cmd, args []string) *Context {
624624
}
625625
return &Context{
626626
Actions: s.Actions,
627-
values: s.contextValues,
628627
progressBar: copyShellProgressBar(s),
629628
Args: args,
630629
RawArgs: s.rawArgs,
631630
Cmd: *cmd,
631+
contextValues: func() contextValues {
632+
values := contextValues{}
633+
for k := range s.contextValues {
634+
values[k] = s.contextValues[k]
635+
}
636+
return values
637+
}(),
632638
}
633639
}
634640

0 commit comments

Comments
 (0)