Skip to content

Commit

Permalink
feat(sg): handle system signals
Browse files Browse the repository at this point in the history
  • Loading branch information
ngalaiko committed Sep 17, 2024
1 parent 7d0365a commit f3e6a02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions sg/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func ContextWithEnv(ctx context.Context, env ...string) context.Context {

// Command should be used when returning exec.Cmd from tools to set opinionated standard fields.
func Command(ctx context.Context, path string, args ...string) *exec.Cmd {
// TODO: use exec.CommandContext when we have determined there are no side-effects.
cmd := exec.Command(path)
cmd := exec.CommandContext(ctx, path)
cmd.Args = append(cmd.Args, args...)
cmd.Dir = FromGitRoot(".")
cmd.Env = os.Environ()
Expand Down
12 changes: 12 additions & 0 deletions sg/initfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func generateInitFile(g *codegen.File, pkg *doc.Package, mks []Makefile) error {
}
g.P("logger := ", g.Import("go.einride.tech/sage/sg"), ".NewLogger(\"", loggerName, "\")")
g.P("ctx = ", g.Import("go.einride.tech/sage/sg"), ".WithLogger(ctx, logger)")
g.P("ctx, cancel := context.WithCancel(ctx)")
g.P("defer cancel()")

g.P("go func() {")
g.P("shutdownCh := make(chan ", g.Import("os"), ".Signal, 1)")
g.P(g.Import("os/signal"), ".Notify(shutdownCh, ",
g.Import("os"), ".Interrupt, ",
g.Import("syscall"), ".SIGTERM)",
)
g.P("<-shutdownCh")
g.P("cancel()")
g.P("}()")
if len(function.Decl.Type.Params.List) > 1 {
expected := countParams(function.Decl.Type.Params.List) - 1
g.P("if len(args) != ", expected, " {")
Expand Down

0 comments on commit f3e6a02

Please sign in to comment.