MCPcopy Create free account
hub / github.com/cli/cli / writeToOutput

Function writeToOutput

pkg/cmd/repo/read-file/read_file.go:261–304  ·  view source on GitHub ↗

writeToOutput writes file content to a local path and returns the final destination. A symlink target is refused, a directory target receives the file under its remote basename, and missing parent directories are created. Existing files are only overwritten when clobber is true.

(file *repoFile, output string, clobber bool)

Source from the content-addressed store, hash-verified

259// A symlink target is refused, a directory target receives the file under its remote basename,
260// and missing parent directories are created. Existing files are only overwritten when clobber is true.
261func writeToOutput(file *repoFile, output string, clobber bool) (string, error) {
262 dest := output
263
264 // A trailing separator signals the user intends dest to be a directory even if it does not exist yet.
265 asDir := strings.HasSuffix(dest, "/") || strings.HasSuffix(dest, string(os.PathSeparator))
266
267 if lr, err := lstatF(dest); err == nil {
268 if lr.isSymlink {
269 return "", fmt.Errorf("output path is a symlink")
270 }
271 if lr.isDir {
272 asDir = true
273 }
274 } else if !os.IsNotExist(err) {
275 return "", err
276 }
277
278 if asDir {
279 dest = filepath.Join(dest, file.Name)
280 }
281
282 if lr, err := lstatF(dest); err == nil {
283 if lr.isSymlink {
284 return "", fmt.Errorf("output path is a symlink")
285 }
286 if !clobber {
287 return "", fmt.Errorf("output path already exists: %q (use --clobber to overwrite)", dest)
288 }
289 } else if !os.IsNotExist(err) {
290 return "", err
291 }
292
293 if dir := filepath.Dir(dest); dir != "" && dir != "." {
294 if err := mkdirAllF(dir, 0755); err != nil {
295 return "", err
296 }
297 }
298
299 if err := writeFileF(dest, file.Content, 0644); err != nil {
300 return "", err
301 }
302
303 return dest, nil
304}

Callers 2

readFileRunFunction · 0.85
Test_writeToOutputFunction · 0.85

Calls 2

JoinMethod · 0.80
ErrorfMethod · 0.65

Tested by 1

Test_writeToOutputFunction · 0.68