Refactor wwctl <node|profile> edit and fix bugs

`wwctl node edit` appears to not be working properly since MergeNode. This
refactor, partly towards #918, resolves the issues with `node edit` and updates
`profile edit` to match.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-01-15 10:49:01 -07:00
parent adf5c500f0
commit 73a8ef8e0e
16 changed files with 290 additions and 417 deletions

View File

@@ -2,7 +2,11 @@ package util
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"gopkg.in/yaml.v3"
"io"
"io/fs"
"net"
@@ -575,3 +579,40 @@ func ByteToString(b int64) string {
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func HashFile(file *os.File) (string, error) {
if prevOffset, err := file.Seek(0, 0); err != nil {
return "", err
} else {
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
if _, err := file.Seek(prevOffset, 0); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
}
func EncodeYaml(data interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
encoder.SetIndent(2)
err := encoder.Encode(data)
return buf.Bytes(), err
}
func EqualYaml(a interface{}, b interface{}) (bool, error) {
aYaml, err := EncodeYaml(a)
if err != nil {
return false, err
}
bYaml, err := EncodeYaml(b)
if err != nil {
return false, err
}
return bytes.Equal(aYaml, bYaml), nil
}