Make FileContentFlag a struct to keep track of the path.
This commit is contained in:
@@ -465,7 +465,10 @@ func JoinEscaped(s []string, sep rune) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FileContentFlag is a flag value that loads a file's contents into its value.
|
// FileContentFlag is a flag value that loads a file's contents into its value.
|
||||||
type FileContentFlag []byte
|
type FileContentFlag struct {
|
||||||
|
Path string
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint
|
func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint
|
||||||
filename := ctx.Scan.PopValue("filename")
|
filename := ctx.Scan.PopValue("filename")
|
||||||
@@ -473,6 +476,7 @@ func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open %q: %s", filename, err)
|
return fmt.Errorf("failed to open %q: %s", filename, err)
|
||||||
}
|
}
|
||||||
*f = FileContentFlag(data)
|
f.Path = filename
|
||||||
|
f.Data = data
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -164,5 +164,6 @@ func TestFileContentFlag(t *testing.T) {
|
|||||||
f.Close()
|
f.Close()
|
||||||
_, err = mustNew(t, &cli).Parse([]string{"--file", f.Name()})
|
_, err = mustNew(t, &cli).Parse([]string{"--file", f.Name()})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, []byte("hello world"), []byte(cli.File))
|
require.Equal(t, []byte("hello world"), cli.File.Data)
|
||||||
|
require.Equal(t, f.Name(), cli.File.Path)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user