lang: core, funcs, types: Add ctx to simple func

Plumb through the standard context.Context so that a function can be
cancelled if someone requests this. It makes it less awkward to write
simple functions that might depend on io or network access.
This commit is contained in:
James Shubin
2024-05-09 19:25:46 -04:00
parent 3b754d5324
commit 415e22abe2
51 changed files with 166 additions and 108 deletions

View File

@@ -30,6 +30,7 @@
package coreos
import (
"context"
"os"
"github.com/purpleidea/mgmt/lang/funcs/simple"
@@ -54,8 +55,9 @@ func init() {
// IsDebian detects if the os family is debian.
// TODO: Detect OS changes.
func IsDebian(input []types.Value) (types.Value, error) {
func IsDebian(ctx context.Context, input []types.Value) (types.Value, error) {
exists := true
// TODO: use ctx around io operations
_, err := os.Stat("/etc/debian_version")
if os.IsNotExist(err) {
exists = false
@@ -67,8 +69,9 @@ func IsDebian(input []types.Value) (types.Value, error) {
// IsRedHat detects if the os family is redhat.
// TODO: Detect OS changes.
func IsRedHat(input []types.Value) (types.Value, error) {
func IsRedHat(ctx context.Context, input []types.Value) (types.Value, error) {
exists := true
// TODO: use ctx around io operations
_, err := os.Stat("/etc/redhat-release")
if os.IsNotExist(err) {
exists = false
@@ -80,8 +83,9 @@ func IsRedHat(input []types.Value) (types.Value, error) {
// IsArchLinux detects if the os family is archlinux.
// TODO: Detect OS changes.
func IsArchLinux(input []types.Value) (types.Value, error) {
func IsArchLinux(ctx context.Context, input []types.Value) (types.Value, error) {
exists := true
// TODO: use ctx around io operations
_, err := os.Stat("/etc/arch-release")
if os.IsNotExist(err) {
exists = false