func (c *DebugHooksCommand) validateHooks() error { if len(c.hooks) == 0 { return nil } service := names.UnitService(c.Target) relations, err := c.apiClient.ServiceCharmRelations(service) if err != nil { return err } validHooks := make(map[string]bool) for _, hook := range hooks.UnitHooks() { validHooks[string(hook)] = true } for _, relation := range relations { for _, hook := range hooks.RelationHooks() { hook := fmt.Sprintf("%s-%s", relation, hook) validHooks[hook] = true } } for _, hook := range c.hooks { if !validHooks[hook] { names := make([]string, 0, len(validHooks)) for hookName, _ := range validHooks { names = append(names, hookName) } sort.Strings(names) logger.Infof("unknown hook %s, valid hook names: %v", hook, names) return fmt.Errorf("unit %q does not contain hook %q", c.Target, 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 }