Skip to content

Commit

Permalink
Allow passing environent variables to StreamProcessors
Browse files Browse the repository at this point in the history
Add support for an 'env' field to the StreamProcessor configuration
and append the environment variables found there to the os.Environ()
array.
The env field takes environment variables in the form of key=value.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
  • Loading branch information
stefanberger committed Jan 11, 2021
1 parent 092f9e6 commit 1917ca5
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions diff/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *compressedProcessor) Close() error {
return c.rc.Close()
}

func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string, args []string) Handler {
func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string, args, env []string) Handler {
set := make(map[string]struct{}, len(mediaTypes))
for _, m := range mediaTypes {
set[m] = struct{}{}
Expand All @@ -177,7 +177,7 @@ func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string
if _, ok := set[mediaType]; ok {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) {
payload := payloads[id]
return NewBinaryProcessor(ctx, mediaType, returnsMediaType, stream, path, args, payload)
return NewBinaryProcessor(ctx, mediaType, returnsMediaType, stream, path, args, env, payload)
}, true
}
return nil, false
Expand Down
3 changes: 2 additions & 1 deletion diff/stream_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
)

// NewBinaryProcessor returns a binary processor for use with processing content streams
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args []string, payload *types.Any) (StreamProcessor, error) {
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, env...)

var payloadC io.Closer
if payload != nil {
Expand Down
3 changes: 2 additions & 1 deletion diff/stream_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ import (
const processorPipe = "STREAM_PROCESSOR_PIPE"

// NewBinaryProcessor returns a binary processor for use with processing content streams
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args []string, payload *types.Any) (StreamProcessor, error) {
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, env...)

if payload != nil {
data, err := proto.Marshal(payload)
Expand Down
2 changes: 2 additions & 0 deletions services/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type StreamProcessor struct {
Path string `toml:"path"`
// Args to the binary
Args []string `toml:"args"`
// Environment variables for the binary
Env []string `toml:"env"`
}

// GetVersion returns the config file's version
Expand Down
2 changes: 1 addition & 1 deletion services/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
return nil, err
}
for id, p := range config.StreamProcessors {
diff.RegisterProcessor(diff.BinaryHandler(id, p.Returns, p.Accepts, p.Path, p.Args))
diff.RegisterProcessor(diff.BinaryHandler(id, p.Returns, p.Accepts, p.Path, p.Args, p.Env))
}

serverOpts := []grpc.ServerOption{
Expand Down

0 comments on commit 1917ca5

Please sign in to comment.