File: file_flag.go

package info (click to toggle)
golang-github-evanphx-json-patch 5.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 3
file content (39 lines) | stat: -rw-r--r-- 766 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

// Borrowed from Concourse: https://github.com/concourse/atc/blob/master/atccmd/file_flag.go

import (
	"fmt"
	"os"
	"path/filepath"
)

// FileFlag is a flag for passing a path to a file on disk. The file is
// expected to be a file, not a directory, that actually exists.
type FileFlag string

// UnmarshalFlag implements go-flag's Unmarshaler interface
func (f *FileFlag) UnmarshalFlag(value string) error {
	stat, err := os.Stat(value)
	if err != nil {
		return err
	}

	if stat.IsDir() {
		return fmt.Errorf("path '%s' is a directory, not a file", value)
	}

	abs, err := filepath.Abs(value)
	if err != nil {
		return err
	}

	*f = FileFlag(abs)

	return nil
}

// Path is the path to the file
func (f FileFlag) Path() string {
	return string(f)
}