Files
holos/internal/util/util.go
Jeff McCune 7ded38bc3f v1alpha5: strip down the core and author schemas (#306)
This patch strips down the v1alpha4 core and author schemas to only with
is absolutely necessary for all holos users.  Aspects of platform
configuration applicable to some, even most, but not all users will be
moved into documentation topics organized as a recipe book.

The functionality removed from the v1alpha4 author schemas in v1alpha5
will move into self contained examples documented as topics on the docs
site.

The overall purpose is to have a focused, composeable, maintainable
author schema to help people get started and ideally we can support for
years with making breaking changes.

With this patch the v1alpha5 helm guide test passes.  We're not going to
have this guide anymore but it demonstrates we're back to where we were
with v1alpha4.
2024-11-06 15:22:17 -08:00

58 lines
1.2 KiB
Go

package util
import (
"os"
"path/filepath"
"github.com/holos-run/holos/internal/errors"
)
// EnsureNewline adds a trailing newline if not already there.
func EnsureNewline(b []byte) []byte {
if len(b) > 0 && b[len(b)-1] != '\n' {
b = append(b, '\n')
}
return b
}
// FindCueMod returns the root module location containing the cue.mod.
func FindCueMod(target string) (dir string, err error) {
dir, err = filepath.Abs(target)
if err != nil {
err = errors.Wrap(err)
return
}
for {
if _, err = os.Stat(filepath.Join(dir, "cue.mod")); err == nil {
break
} else if !os.IsNotExist(err) {
return "", errors.Wrap(err)
}
parent := filepath.Dir(dir)
if parent == dir {
return "", errors.Format("no cue.mod from root to leaf: %v", target)
}
dir = parent
}
return
}
func FindRootLeaf(target string) (root string, leaf string, err error) {
if root, err = FindCueMod(target); err != nil {
return "", "", errors.Wrap(err)
}
absPath, err := filepath.Abs(target)
if err != nil {
return "", "", errors.Wrap(err)
}
if leaf, err = filepath.Rel(root, absPath); err != nil {
return "", "", errors.Wrap(err)
}
// Needed for CUE to load the path properly.
leaf = "." + string(os.PathSeparator) + leaf
return
}