github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/datafs/context.go (about) 1 package datafs 2 3 import ( 4 "context" 5 "io" 6 "io/fs" 7 "os" 8 9 "github.com/hairyhenderson/gomplate/v4/internal/config" 10 ) 11 12 // withContexter is an fs.FS that can be configured with a custom context 13 // copied from go-fsimpl - see internal/types.go 14 type withContexter interface { 15 WithContext(ctx context.Context) fs.FS 16 } 17 18 type withDataSourceser interface { 19 WithDataSources(sources map[string]config.DataSource) fs.FS 20 } 21 22 // WithDataSourcesFS injects a datasource map into the filesystem fs, if the 23 // filesystem supports it (i.e. has a WithDataSources method). This is used for 24 // the mergefs filesystem. 25 func WithDataSourcesFS(sources map[string]config.DataSource, fsys fs.FS) fs.FS { 26 if fsys, ok := fsys.(withDataSourceser); ok { 27 return fsys.WithDataSources(sources) 28 } 29 30 return fsys 31 } 32 33 type stdinCtxKey struct{} 34 35 func ContextWithStdin(ctx context.Context, r io.Reader) context.Context { 36 return context.WithValue(ctx, stdinCtxKey{}, r) 37 } 38 39 func StdinFromContext(ctx context.Context) io.Reader { 40 if r, ok := ctx.Value(stdinCtxKey{}).(io.Reader); ok { 41 return r 42 } 43 44 return os.Stdin 45 }