func (c *DebugHooksCommand) validateHooks(unit *state.Unit) error { if len(c.hooks) == 0 { return nil } service, err := unit.Service() if err != nil { return err } eps, err := service.Endpoints() if err != nil { return err } validHooks := make(map[string]bool) for _, hook := range hooks.UnitHooks() { validHooks[string(hook)] = true } for _, ep := range eps { for _, hook := range hooks.RelationHooks() { hook := fmt.Sprintf("%s-%s", ep.Relation.Name, hook) validHooks[hook] = true } } for _, hook := range c.hooks { if !validHooks[hook] { return fmt.Errorf("unit %q does not contain hook %q", unit.Name(), hook) } } return nil }
// Hooks returns a map of all possible valid hooks, taking relations // into account. It's a map to enable fast lookups, and the value is // always true. func (m Meta) Hooks() map[string]bool { allHooks := make(map[string]bool) // Unit hooks for _, hookName := range hooks.UnitHooks() { allHooks[string(hookName)] = true } // Relation hooks for hookName := range m.Provides { generateRelationHooks(hookName, allHooks) } for hookName := range m.Requires { generateRelationHooks(hookName, allHooks) } for hookName := range m.Peers { generateRelationHooks(hookName, allHooks) } return allHooks }