MCPcopy
hub / github.com/purpleidea/mgmt / Call

Method Call

lang/core/deploy/directory.go:141–274  ·  view source on GitHub ↗

Call this function with the input args and return the value if it is possible to do so at this time.

(ctx context.Context, args []types.Value)

Source from the content-addressed store, hash-verified

139// Call this function with the input args and return the value if it is possible
140// to do so at this time.
141func (obj *DirectoryFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) {
142 if len(args) < 1 {
143 return nil, fmt.Errorf("not enough args")
144 }
145 directory := args[0].Str()
146
147 if obj.data == nil {
148 return nil, funcs.ErrCantSpeculate
149 }
150 p := strings.TrimSuffix(obj.data.Base, "/")
151 if p == obj.data.Base { // didn't trim, so we fail
152 // programming error
153 return nil, fmt.Errorf("no trailing slash on Base, got: `%s`", p)
154 }
155
156 if !strings.HasPrefix(directory, "/") {
157 return nil, fmt.Errorf("directory was not absolute, got: `%s`", directory)
158 //p += "/" // be forgiving ?
159 }
160 if !strings.HasSuffix(directory, "/") {
161 return nil, fmt.Errorf("directory path must be a dir")
162 }
163 p += directory
164
165 if obj.init == nil || obj.data == nil {
166 return nil, funcs.ErrCantSpeculate
167 }
168 fs, err := obj.init.World.Fs(ctx, obj.data.FsURI) // open the remote file system
169 if err != nil {
170 return nil, errwrap.Wrapf(err, "can't load data from file system `%s`", obj.data.FsURI)
171 }
172 // reldir could be something unique to this input arg, but might as well
173 // let them overlap since this make this all a copy of the deploy dirs.
174 // XXX: If we ever get involved with event stuff in this folder, it may
175 // get messy, so look into that if we ever get there.
176 reldir := p
177
178 if !strings.HasPrefix(reldir, "/") {
179 return nil, fmt.Errorf("path must be absolute")
180 }
181 if !strings.HasSuffix(reldir, "/") {
182 return nil, fmt.Errorf("path must be a dir")
183 }
184 // TODO: clean this so we don't get `///` or similar garbage?
185 if reldir == "//" {
186 return nil, fmt.Errorf("path can't be two slashes")
187 }
188 // NOTE: The above checks ensure we don't get "//" as input!
189
190 // XXX: do we want to make the dest dir a hash of the input path?
191 prefix := fmt.Sprintf("%s/", path.Join(DirectoryDeployPrefix, reldir))
192
193 result, err := obj.init.Local.VarDir(ctx, prefix)
194 if err != nil {
195 return nil, err
196 }
197
198 // Sync the deploy dir contents into our local var dir, `rsync --delete`

Callers

nothing calls this directly

Calls 14

WrapfFunction · 0.92
VarDirMethod · 0.80
ModeMethod · 0.80
StrMethod · 0.65
FsMethod · 0.65
ReadDirMethod · 0.65
NameMethod · 0.65
IsDirMethod · 0.65
ReadFileMethod · 0.65
WriteFileMethod · 0.65
RemoveAllMethod · 0.45
StatMethod · 0.45

Tested by

no test coverage detected