| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 package hook | |
| 2 | |
| 3 import ( | |
| 4 "fmt" | |
| 5 "launchpad.net/juju/go/log" | |
| 6 "launchpad.net/juju/go/state" | |
| 7 "strings" | |
| 8 ) | |
| 9 | |
| 10 // Context holds all information necessary to run a hook command. | |
| 11 type Context struct { | |
| 12 state *state.State | |
| 13 local string | |
| 14 relation string | |
| 15 members []string | |
| 16 } | |
| 17 | |
| 18 // NewLocalContext returns a Context tied to a specific unit. | |
| 19 func NewLocalContext(state *state.State, unitName string) *Context { | |
|
rog
2012/03/15 15:29:11
are these constructors worth it?
can't we just exp
| |
| 20 return &Context{state, unitName, "", nil} | |
| 21 } | |
| 22 | |
| 23 // NewRelationContext returns a Context tied to a specific unit relation . | |
| 24 func NewRelationContext( | |
| 25 state *state.State, unitName, relationName string, members []string, | |
| 26 ) *Context { | |
| 27 return &Context{state, unitName, relationName, members} | |
| 28 } | |
| 29 | |
| 30 // NewBrokenContext returns a Context tied to a specific unit relation (which is | |
| 31 // known to have been broken). | |
| 32 func NewBrokenContext(state *state.State, unitName, relationName string) *Contex t { | |
| 33 return &Context{state, unitName, relationName, nil} | |
| 34 } | |
| 35 | |
| 36 // Vars returns an os.Environ-style list of strings that should be set when | |
| 37 // executing a hook in this Context. | |
| 38 func (ctx *Context) Vars() []string { | |
|
rog
2012/03/15 15:29:11
can't we just put UnitName and Relation as fields
| |
| 39 vars := []string{} | |
| 40 if ctx.local != "" { | |
| 41 vars = append(vars, "JUJU_UNIT_NAME="+ctx.local) | |
| 42 } | |
| 43 if ctx.relation != "" { | |
| 44 vars = append(vars, "JUJU_RELATION="+ctx.relation) | |
| 45 } | |
| 46 return vars | |
| 47 } | |
| 48 | |
| 49 // Log is the core of the `log` hook command, and is always meaningful in any | |
| 50 // Context. | |
| 51 func (ctx *Context) Log(debug bool, msg string) { | |
| 52 s := []string{} | |
| 53 if ctx.local != "" { | |
| 54 s = append(s, ctx.local) | |
| 55 } | |
| 56 if ctx.relation != "" { | |
| 57 s = append(s, ctx.relation) | |
| 58 } | |
| 59 full := fmt.Sprintf("Context<%s>: %s", strings.Join(s, ", "), msg) | |
| 60 if debug { | |
| 61 log.Debugf(full) | |
| 62 } else { | |
| 63 log.Printf(full) | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 // Members is the core of the `relation-list` hook command. It returns a list | |
| 68 // of unit names (excluding the local unit) participating in this context's | |
| 69 // relation, and is always meaningful in any Context. | |
| 70 func (ctx *Context) Members() []string { | |
|
rog
2012/03/15 15:29:11
i don't see the need for this method.
for one, a n
| |
| 71 if ctx.members != nil { | |
| 72 return ctx.members | |
| 73 } | |
| 74 return []string{} | |
| 75 } | |
| OLD | NEW |