<@UB89GSWCF> can you advise <@U07RLUFFH7W> on how ...
# atmos-dev
e
@Andriy Knysh (Cloud Posse) can you advise @Haitham on how to do
!repo-root
? https://github.com/cloudposse/atmos/pull/808/files#r1943095714
a
i'm not familiar what
!repo-root
YAML tag should do
do we have a doc about it?
base_path: !repo-root
a
if we start from any subfolder (at any level), how would we know in principle where the "the root of the git repository" is?
h
we get it from GetGitRoot
Copy code
func GetGitRoot(startPath string) (string, error) {
	// Open the repository starting from the given path
	repo, err := git.PlainOpenWithOptions(startPath, &git.PlainOpenOptions{
		DetectDotGit: true,
	})
	if err != nil {
		return "", fmt.Errorf("failed to open Git repository: %w", err)
	}
	// Get the worktree to extract the repository's root directory
	worktree, err := repo.Worktree()
	if err != nil {
		return "", fmt.Errorf("failed to get worktree: %w", err)
	}
	// Return the absolute path to the root directory
	rootPath, err := filepath.Abs(worktree.Filesystem.Root())
	if err != nil {
		return "", fmt.Errorf("failed to resolve absolute path: %w", err)
	}
	return rootPath, nil
}
I need help to apply base_path: !repo-root Custom Tags in YAML
image.png
when I just compare base_path with !repo-root its work but @Erik Osterman (Cloud Posse) say that is not correct way I should handle it as tag . that is what I need help on it
same as !store, !env, !template, !terraform.output, etc.
@Erik Osterman (Cloud Posse) @Andriy Knysh (Cloud Posse) I found solution When using Viper to read configuration files, it does not natively support custom YAML tags like
!repo-root
. To handle this, we need to preprocess the YAML file to resolve the
!repo-root
tag before passing it to Viper
I tested it Viper can not read tags
• I will Use the
gopkg.in/yaml.v3
package to parse the YAML file and resolve the
!repo-root
tag. then merge to Viber
a
in
pkg/utils/yaml_utils.go
we have
Copy code
func UnmarshalYAMLFromFile[T any](atmosConfig *schema.AtmosConfiguration, input string, file string) (T, error) {
	var zeroValue T
	var node yaml.Node
	b := []byte(input)

	// Unmarshal into yaml.Node
	if err := yaml.Unmarshal(b, &node); err != nil {
		return zeroValue, err
	}

	if err := processCustomTags(atmosConfig, &node, file); err != nil {
		return zeroValue, err
	}

	// Decode the yaml.Node into the desired type T
	var data T
	if err := node.Decode(&data); err != nil {
		return zeroValue, err
	}

	return data, nil
}
the function unmarshals YAML and processes the custom YAML tags
you can add the new tag
!root-repo
to the list and process it
e
Viper and yaml tags are 2 totally different things
👍 1
a
the func
processCustomTags
does the following: it detects the custom YAML tags and "preserves" them in the Go structure (w/o "preserving", the YAML parser just removes all the tags)
e
You have one function to get the root. Use that one with the viper arg, when the value is the string !repo-root, and use the same function for yaml tag.
a
then, in
internal/exec/yaml_func_utils.go
we have the code to actially process all the tags by calling the corresponding functions
✅ 1
depending on where in the pipeline you want to get the value for a tag (when reading YAML files, or as the last step when actually processing the Go structure), you can do the
!repo-root
processing in these places:
• In
pkg/utils/yaml_utils.go
in the
getValueWithTag
func - see the example for
!include
. The
!include
tag is actually processed WHEN we read the YAML files (Atmos manifests)
• All the other custom tags are processed way after all YAML files are read, processed, and deep-merged - so we do it in
internal/exec/yaml_func_utils.go
in the
ProcessCustomYamlTags
func
in your case (
!repo-root
tag), you probably want to process it when you read the YAML file, similar to the
!include
tag (see the first case above)
Copy code
if tag == AtmosYamlFuncRepoRoot {

// call the func to find repo root 

return repoRoot
}
h
yes the case I have need to process it when you read the YAML
a
should be not complicated, all the patterns are in place, you just add the above code to get the repo root and return it as YAML node value
✅ 1
h
I will try it thanks
@Erik Osterman (Cloud Posse) I added !repo-root same as !store, !env, !template, !terraform.output, etc
👍 1