...

Source file src/gopkg.in/evanphx/json-patch.v4/cmd/json-patch/file_flag.go

Documentation: gopkg.in/evanphx/json-patch.v4/cmd/json-patch

     1  package main
     2  
     3  // Borrowed from Concourse: https://github.com/concourse/atc/blob/master/atccmd/file_flag.go
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  // FileFlag is a flag for passing a path to a file on disk. The file is
    12  // expected to be a file, not a directory, that actually exists.
    13  type FileFlag string
    14  
    15  // UnmarshalFlag implements go-flag's Unmarshaler interface
    16  func (f *FileFlag) UnmarshalFlag(value string) error {
    17  	stat, err := os.Stat(value)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	if stat.IsDir() {
    23  		return fmt.Errorf("path '%s' is a directory, not a file", value)
    24  	}
    25  
    26  	abs, err := filepath.Abs(value)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	*f = FileFlag(abs)
    32  
    33  	return nil
    34  }
    35  
    36  // Path is the path to the file
    37  func (f FileFlag) Path() string {
    38  	return string(f)
    39  }
    40  

View as plain text