small pixel drawing of a pufferfish zoa

Support OS_RELEASE vars
Jes Olson j3s@c3f.net
Sat, 01 Oct 2022 16:13:37 -0500
commit

199f7a08fcbd03f317122152f709a3b5417f0ef9

parent

bc741c28734756de9c855bc7e61d04452761f49b

3 files changed, 29 insertions(+), 29 deletions(-)

jump to
M READMEREADME

@@ -149,8 +149,6 @@ zoa is starting with a minimal set of built-in functions

because i only want to maintain functions in order to address severe pain-points. most things should just be handled via shell. * as standards-compliant as reasonable - but not ball-breakingly so tbh, see $HOSTNAME for an example - of zoa not being "fully" standards compliant.

@@ -218,8 +216,8 @@

$ARCH - the name of the hardware type on which the system is running ARCH=x86_64 - $HOSTNAME - the name of this node. - HOSTNAME=nostromo.j3s.sh + $NODENAME - the name of this node. + NODENAME=nostromo.j3s.sh * there is no shortname vs fqdn standard, so this env var may vary by distro

@@ -319,7 +317,7 @@

however, if you want main to be a little more capable, here's one starting point: - case $HOSTNAME in + case $NODENAME in git.j3s.sh) # note that the CERTS env var will # pass into any scripts called after
M env/env.goenv/env.go

@@ -2,6 +2,7 @@ package env

import ( "bufio" + "errors" "fmt" "log" "os"

@@ -12,15 +13,7 @@ "mvdan.cc/sh/v3/expand"

) func GenerateEnv() (expand.Environ, error) { - // syscall.Uname _should_ be supported on all *nix systems and is backed - // by posix var env expand.Environ - uname, err := getUname() - if err != nil { - log.Fatal(err) - } - - // shell := "/bin/sh"? // $PATH is annoyingly non-standard, so we hardcode the var // to the binary paths described in the fhs standard.

@@ -31,10 +24,15 @@ //

// users can always call their special binaries with their full paths // if they are resistant to moving them for some reason. path := envString("PATH", "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin") - uname_os := envString("OS", uname.Sysname) - uname_release := envString("RELEASE", uname.Release) - uname_arch := envString("ARCH", uname.Arch) - uname_hostname := envString("HOSTNAME", uname.Nodename) + + uname, err := getUname() + if err != nil { + log.Fatal(err) + } + unameOS := envString("OS", uname.Sysname) + unameRelease := envString("RELEASE", uname.Release) + unameArch := envString("ARCH", uname.Arch) + unameNodename := envString("NODENAME", uname.Nodename) // jes rant: standards are extremely annoying about hostnames. //

@@ -46,8 +44,8 @@ // systems allow a 256-byte hostname and an 8-byte

// nodename), but this is true on Linux. The same // holds for setdomainname(2) and the domainname field." // - // in practice, there's usually not a difference between HOSTNAME - // and NODENAME, so i've chosen to expose the HOSTNAME variable for the + // in practice, there's usually not a difference between hostname + // and NODENAME, so i've chosen to expose the NODENAME variable for the // sake of simplicity (most users expect this). // // if this becomes an issue, i'll revisit it. i doubt it though.

@@ -66,16 +64,17 @@ // and are useful for identifying specific Linux distros, or their versions.

// // if you rely on these variables, I highly suggest checking for their // existence with test -z before utilizing them. there be no standards here. - env = expand.ListEnviron(path, // normie shit - uname_os, uname_hostname, uname_release, uname_arch) // uname-derivated env vars - // os_release, err := getOSRelease() - // if err != nil { - // return env, err - // } else { - // env = expand.ListEnviron(env.Get, envString("OS_RELEASE_ID", os_release.ID)) - // env += envString("OS_RELEASE_VERSION_ID", os_release.VersionID) - // } + os_release, err := getOSRelease() + if err != nil { + return env, err + } + osReleaseID := envString("OS_RELEASE_ID", os_release.ID) + osReleaseVersionID := envString("OS_RELEASE_VERSION_ID", os_release.VersionID) + + env = expand.ListEnviron(path, + unameOS, unameNodename, unameRelease, unameArch, + osReleaseID, osReleaseVersionID) return env, nil }

@@ -133,6 +132,9 @@ // into a struct

func getOSRelease() (OSRelease, error) { var osr = OSRelease{} f, err := os.Open("/etc/os-release") + if errors.Is(err, os.ErrNotExist) { + return osr, nil + } if err != nil { return osr, err }
M test/maintest/main

@@ -31,7 +31,7 @@

if [ "$(uname -n)" = "nostromo" ]; then echo NOSTROMO fi -echo $HOSTNAME +echo $NODENAME zoa-script 3-more