| 1 | // Mgmt |
| 2 | // Copyright (C) 2013-2024+ James Shubin and the project contributors |
| 3 | // Written by James Shubin <[email protected]> and the project contributors |
| 4 | // |
| 5 | // This program is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | // |
| 18 | // Additional permission under GNU GPL version 3 section 7 |
| 19 | // |
| 20 | // If you modify this program, or any covered work, by linking or combining it |
| 21 | // with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | // modules which link with this program, contain a copy of their source code in |
| 23 | // the authoritative form) containing parts covered by the terms of any other |
| 24 | // license, the licensors of this program grant you additional permission to |
| 25 | // convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | // the original author, James Shubin, additional permission to update this |
| 27 | // additional permission if he deems it necessary to achieve the goals of this |
| 28 | // additional permission. |
| 29 | |
| 30 | // Package vars provides a framework for language vars. |
| 31 | package varsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ |
| 32 | |
| 33 | import ( |
| 34 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 35 | "stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 36 | |
| 37 | "github.com/purpleidea/mgmt/lang/interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/" |
| 38 | ) |
| 39 | |
| 40 | const ( |
| 41 | // ConstNamespace is the string prefix for all top-level built-in vars. |
| 42 | ConstNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ConstNamespace. = "const" |
| 43 | |
| 44 | // ResourceNamespace is the string prefix for all top-level resource |
| 45 | // specific built-in vars, that exist under the ConstNamespace header. |
| 46 | ResourceNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ResourceNamespace. = "res" |
| 47 | ) |
| 48 | |
| 49 | // Value is a shortcut to using this type. |
| 50 | // XXX: Eventually we might get rid of this entirely and use types.Value instead |
| 51 | // of interfaces.Var which seems to be unnecessary at the moment. |
| 52 | type Valuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Value# = interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var# |
| 53 | |
| 54 | // registeredVars is a global map of all possible vars which can be used. You |
| 55 | // should never touch this map directly. Use methods like Register instead. |
| 56 | var registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars. = make(map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) // must initialize |
| 57 | |
| 58 | // Register takes a var and its name and makes it available for use. It is |
| 59 | // commonly called in the init() method of the var at program startup. There is |
| 60 | // no matching Unregister function. |
| 61 | func Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Register().(name string, fn func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) { |
| 62 | if _, ok := registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars.[name]; ok { |
| 63 | panic(fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("a var named %s is already registered", name)) |
| 64 | } |
| 65 | //gob.Register(fn()) |
| 66 | registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars.[name] = fn |
| 67 | } |
| 68 | |
| 69 | // ModuleRegister is exactly like Register, except that it registers within a |
| 70 | // named module. This is a helper function. |
| 71 | func ModuleRegisterscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ModuleRegister().(module, name string, v func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) { |
| 72 | Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Register().(module+interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.ModuleSepscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/ModuleSep.+name, v) |
| 73 | } |
| 74 | |
| 75 | // resourceConstHelper is a helper function to manage the const topology. |
| 76 | func resourceConstHelperscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/resourceConstHelper().(kind, param, field string) string { |
| 77 | // const.res.file.state.exists = "exists" |
| 78 | // TODO: should it be: const.res.file.params.state.exists = "exists" ? |
| 79 | chunks := []string{ |
| 80 | ConstNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ConstNamespace., |
| 81 | ResourceNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/ResourceNamespace., |
| 82 | kind, |
| 83 | param, |
| 84 | field, |
| 85 | } |
| 86 | //return ConstNamespace + interfaces.ModuleSep + ResourceNamespace + interfaces.ModuleSep + kind + interfaces.ModuleSep + param + interfaces.ModuleSep + field |
| 87 | return stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Joinscip-go gomod github.com/golang/go/src go1.21 strings/Join().(chunks, interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.ModuleSepscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/ModuleSep.) // cleaner code |
| 88 | } |
| 89 | |
| 90 | // RegisterResourceParam registers a single const param for a resource. You |
| 91 | // might prefer to use RegisterResourceParams instead. |
| 92 | func RegisterResourceParamscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/RegisterResourceParam().(kind, param, field string, value func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) { |
| 93 | Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Register().(resourceConstHelperscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/resourceConstHelper().(kind, param, field), value) |
| 94 | } |
| 95 | |
| 96 | // RegisterResourceParams registers a map of const params for a resource. The |
| 97 | // params mapping keys are the param name and the param field name. Finally, the |
| 98 | // value is the specific type value for that constant. |
| 99 | func RegisterResourceParamsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/RegisterResourceParams().(kind string, params map[string]map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) { |
| 100 | for param, mapping := range params { |
| 101 | for field, value := range mapping { |
| 102 | Registerscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Register().(resourceConstHelperscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/resourceConstHelper().(kind, param, field), value) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Lookup returns a pointer to the var implementation. |
| 108 | func Lookupscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Lookup().(name string) (interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#, error) { |
| 109 | f, exists := registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars.[name] |
| 110 | if !exists { |
| 111 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("not found") |
| 112 | } |
| 113 | return f(), nil |
| 114 | } |
| 115 | |
| 116 | // LookupPrefix returns a map of names to vars that start with a module prefix. |
| 117 | // This search automatically adds the period separator. So if you want vars in |
| 118 | // the `const` prefix, search for `const`, not `const.` and it will find all the |
| 119 | // correctly registered vars. This removes that prefix from the result in the |
| 120 | // map keys that it returns. If you search for an empty prefix, then this will |
| 121 | // return all the top-level functions that aren't in a module. |
| 122 | func LookupPrefixscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/LookupPrefix().(prefix string) map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var# { |
| 123 | result := make(map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) |
| 124 | for name, f := range registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars. { |
| 125 | // requested top-level vars, and no module separators... |
| 126 | if prefix == "" { |
| 127 | if !stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Containsscip-go gomod github.com/golang/go/src go1.21 strings/Contains().(name, interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.ModuleSepscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/ModuleSep.) { |
| 128 | result[name] = f // copy |
| 129 | } |
| 130 | continue |
| 131 | } |
| 132 | sep := prefix + interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.ModuleSepscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/ModuleSep. |
| 133 | if !stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(name, sep) { |
| 134 | continue |
| 135 | } |
| 136 | s := stringsscip-go gomod github.com/golang/go/src go1.21 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.21 strings/TrimPrefix().(name, sep) // remove the prefix |
| 137 | result[s] = f // copy |
| 138 | } |
| 139 | return result |
| 140 | } |
| 141 | |
| 142 | // Map returns a map from all registered var names to a function to return that |
| 143 | // one. We return a copy of our internal registered var store so that this |
| 144 | // result can be manipulated safely. We return the vars that produce the Var |
| 145 | // interface because we might use this result to create multiple vars, and each |
| 146 | // one might need to have its own unique memory address to work properly. |
| 147 | func Mapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/Map().() map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var# { |
| 148 | m := make(map[string]func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Varscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Var#) |
| 149 | for name, fn := range registeredVarsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs/vars`/registeredVars. { // copy |
| 150 | m[name] = fn |
| 151 | } |
| 152 | return m |
| 153 | } |