-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Maksim An <[email protected]>
- Loading branch information
Showing
4 changed files
with
386 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
*.exe | ||
.idea | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
|
||
"github.com/Microsoft/go-winio" | ||
) | ||
|
||
// Config is passed to the binary logging function | ||
type Config struct { | ||
ID string | ||
Namespace string | ||
Stdout io.Reader | ||
Stderr io.Reader | ||
} | ||
|
||
// LoggerFunc is a binary logging function signature | ||
type LoggerFunc func(context.Context, *Config, func() error) error | ||
|
||
// Run runs LoggerFunc | ||
func Run(fn LoggerFunc) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
var ( | ||
sout, serr net.Conn | ||
ready func() error | ||
) | ||
|
||
if soutEnv := os.Getenv("CONTAINER_STDOUT"); soutEnv != "" { | ||
locOut, err := winio.DialPipeContext(ctx, soutEnv) | ||
|
||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
sout = locOut | ||
} | ||
|
||
if serrEnv := os.Getenv("CONTAINER_STDERR"); serrEnv != "" { | ||
locSerr, err := winio.DialPipeContext(ctx, serrEnv) | ||
|
||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
serr = locSerr | ||
} | ||
|
||
waitEnv := os.Getenv("CONTAINER_WAIT") | ||
if waitEnv != "" { | ||
wait, err := winio.DialPipeContext(ctx, waitEnv) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
// Write to wait pipe | ||
ready = func() error { | ||
wait.Write([]byte("#")) | ||
return wait.Close() | ||
} | ||
} else { | ||
// Noop ready func | ||
ready = func() error { return nil } | ||
} | ||
|
||
config := &Config{ | ||
ID: os.Getenv("CONTAINER_ID"), | ||
Namespace: os.Getenv("CONTAINER_NAMESPACE"), | ||
Stdout: sout, | ||
Stderr: serr, | ||
} | ||
|
||
var errCh = make(chan error, 0) | ||
|
||
go func() { | ||
if err := fn(ctx, config, ready); err != nil { | ||
errCh <- err | ||
return | ||
} | ||
errCh <- nil | ||
}() | ||
|
||
err := <-errCh | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.