in lib/ec2macosinit/motd.go [21:68]
func (c *MOTDModule) Do(ctx *ModuleContext) (message string, err error) {
if !c.UpdateName {
return "Not requested to update MOTD", nil
}
// Create the macOS string
macosStr := "macOS"
// Create regex pattern to be replaced in the motd file
motdMacOSExpression, err := regexp.Compile("macOS.*")
if err != nil {
return "", fmt.Errorf("ec2macosinit: error compiling motd regex pattern: %s", err)
}
// Get the os product version number
osProductVersion, err := getOSProductVersion()
if err != nil {
return "", fmt.Errorf("ec2macosinit: error while getting product version: %s", err)
}
// Get the version name using the os product version number
versionName := getVersionName(osProductVersion)
// Create the version string to be written to the motd file
var motdString string
if versionName != "" {
motdString = fmt.Sprintf("%s %s %s", macosStr, versionName, osProductVersion)
} else {
motdString = fmt.Sprintf("%s %s", macosStr, osProductVersion)
}
// Read in the raw contents of the motd file
rawFileContents, err := os.ReadFile(motdFile)
if err != nil {
return "", fmt.Errorf("ec2macosinit: error reading motd file: %s", err)
}
// Use the regexp object to replace all instances of the pattern with the updated motd version string
replacedContents := motdMacOSExpression.ReplaceAll(rawFileContents, []byte(motdString))
// Write the updated contents back to the motd file
err = os.WriteFile(motdFile, replacedContents, 0644)
if err != nil {
return "", fmt.Errorf("ec2macosinit: error writing updated motd back to file: %s", err)
}
return fmt.Sprintf("successfully updated motd file [%s] with version string [%s]", motdFile, motdString), nil
}