Add a FileContentFlag that loads the contents of a file into a flag
value.
This commit is contained in:
@@ -2,6 +2,7 @@ package kong
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -58,7 +59,7 @@ func (m *mapperValueAdapter) IsBool() bool {
|
|||||||
//
|
//
|
||||||
// Mappers can be associated with concrete fields via pointer, reflect.Type, reflect.Kind, or via a "type" tag.
|
// Mappers can be associated with concrete fields via pointer, reflect.Type, reflect.Kind, or via a "type" tag.
|
||||||
//
|
//
|
||||||
// Additionally, if a type implements this interface, it will be used.
|
// Additionally, if a type implements the MappverValue interface, it will be used.
|
||||||
type Mapper interface {
|
type Mapper interface {
|
||||||
// Decode ctx.Value with ctx.Scanner into target.
|
// Decode ctx.Value with ctx.Scanner into target.
|
||||||
Decode(ctx *DecodeContext, target reflect.Value) error
|
Decode(ctx *DecodeContext, target reflect.Value) error
|
||||||
@@ -462,3 +463,16 @@ func JoinEscaped(s []string, sep rune) string {
|
|||||||
}
|
}
|
||||||
return strings.Join(escaped, string(sep))
|
return strings.Join(escaped, string(sep))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FileContentFlag is a flag value that loads a file's contents into its value.
|
||||||
|
type FileContentFlag []byte
|
||||||
|
|
||||||
|
func (f *FileContentFlag) Decode(ctx *DecodeContext) error { // nolint: golint
|
||||||
|
filename := ctx.Scan.PopValue("filename")
|
||||||
|
data, err := ioutil.ReadFile(filename) // nolint: gosec
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open %q: %s", filename, err)
|
||||||
|
}
|
||||||
|
*f = FileContentFlag(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package kong_test
|
package kong_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -149,3 +152,17 @@ func TestMapperValue(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "foo", cli.Value.decoded)
|
require.Equal(t, "foo", cli.Value.decoded)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileContentFlag(t *testing.T) {
|
||||||
|
var cli struct {
|
||||||
|
File kong.FileContentFlag
|
||||||
|
}
|
||||||
|
f, err := ioutil.TempFile("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
fmt.Fprint(f, "hello world")
|
||||||
|
f.Close()
|
||||||
|
_, err = mustNew(t, &cli).Parse([]string{"--file", f.Name()})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, []byte("hello world"), []byte(cli.File))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user