MCPcopy Index your code
hub / github.com/kopia/kopia / constructTargetPairs

Method constructTargetPairs

cli/command_restore.go:178–250  ·  view source on GitHub ↗

constructTargetPairs builds the sourceIdPathPairs array for this command for the two forms of command: expansion of one or more placeholders or restoring of a single source to a single destination.

(rep repo.Repository)

Source from the content-addressed store, hash-verified

176// command for the two forms of command: expansion of one or more
177// placeholders or restoring of a single source to a single destination.
178func (c *commandRestore) constructTargetPairs(rep repo.Repository) error {
179 targetPairs := make([]restoreSourceTarget, 0, len(c.restoreTargetPaths))
180
181 for _, p := range c.restoreTargetPaths {
182 tp := restore.PathIfPlaceholder(p)
183 if tp != "" {
184 absp, err := filepath.Abs(p)
185 if err != nil {
186 return errors.Wrapf(err, "restore can't resolve path for %q", p)
187 }
188
189 targetPairs = append(targetPairs, restoreSourceTarget{
190 source: absp,
191 target: restore.PathIfPlaceholder(absp),
192 isplaceholder: true,
193 })
194 }
195 }
196
197 switch tplen, restpslen := len(targetPairs), len(c.restoreTargetPaths); {
198 case tplen == 0 && restpslen == 1:
199 // This means that none of the restoreTargetPaths are placeholders and we
200 // have 1 arg: a source path that should also be used as a destination.
201 source := c.restoreTargetPaths[0]
202
203 si, err := snapshot.ParseSourceInfo(source, rep.ClientOptions().Hostname, rep.ClientOptions().Username)
204 if err != nil {
205 return errors.Errorf("invalid path to be used as source: '%s': %s", source, err)
206 }
207
208 if si.Path == "" {
209 return errors.New("the source must contain a path element")
210 }
211
212 if si.Host != rep.ClientOptions().Hostname || si.UserName != rep.ClientOptions().Username {
213 return errors.New("the source must be a path in with the same username/hostname to be used as a target too")
214 }
215
216 c.restores = []restoreSourceTarget{
217 {
218 source: source,
219 target: si.Path,
220 isplaceholder: false,
221 },
222 }
223
224 return nil
225 case tplen == 0 && restpslen == 2:
226 // This means that none of the restoreTargetPaths are placeholders and we
227 // have two args: a sourceID and a destination directory.
228 absp, err := filepath.Abs(c.restoreTargetPaths[1])
229 if err != nil {
230 return errors.Wrapf(err, "restore can't resolve path for %q", c.restoreTargetPaths[1])
231 }
232
233 c.restores = []restoreSourceTarget{
234 {
235 source: c.restoreTargetPaths[0],

Callers 1

restoreOutputMethod · 0.95

Calls 4

PathIfPlaceholderFunction · 0.92
ParseSourceInfoFunction · 0.92
ErrorfMethod · 0.80
ClientOptionsMethod · 0.65

Tested by

no test coverage detected