You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: yaml.go
    path of file: ./repos/arduino-cli/internal/arduino/sketch
    
    the code of the file until where you have to start completion: // This file is part of arduino-cli.
//
// Copyright 2020-2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// 
    
func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
	var srcYaml []string
	if path.Exist() {
		src, err := path.ReadFileAsLines()
		if err != nil {
			return err
		}
		lastLine := len(src) - 1
		if lastLine > 0 && src[lastLine] == "" {
			srcYaml = src[:lastLine]
		} else {
			srcYaml = src
		}
	}

	// Generate the new yaml key/value pair
	v, err := yaml.Marshal(newValue)
	if err != nil {
		return err
	}
	updatedLine := key + ": " + strings.TrimSpace(string(v))

	// Update or add the key/value pair into the original yaml
	addMissing := (newValue != "")
	for i, line := range srcYaml {
		if strings.HasPrefix(line, key+": ") {
			if newValue == "" {
				// Remove the key/value pair
				srcYaml = append(srcYaml[:i], srcYaml[i+1:]...)
			} else {
				// Update the key/value pair
				srcYaml[i] = updatedLine
			}
			addMissing = false
			break
		}
	}
	if addMissing {
		lastLine := len(srcYaml) - 1
		if lastLine >= 0 && srcYaml[lastLine] == "" {
			srcYaml[lastLine] = updatedLine
		} else {
			srcYaml = append(srcYaml, updatedLine)
		}
	}

	// Validate the new yaml
	dstYaml := []byte(strings.Join(srcYaml, fmt.Sprintln()) + fmt.Sprintln())
	var dst interface{}
	if err := yaml.Unmarshal(dstYaml, &dst); err != nil {
		return fmt.Errorf("%s: %w", tr("could not update sketch project file"), err)
	}
	dstMap, ok := dst.(map[string]interface{})
	if !ok {
		return fmt.Errorf(tr("could not update sketch project file"))
	}
	writtenValue, notRemoved := dstMap[key]
	if (newValue == "" && notRemoved) || (newValue != "" && newValue != writtenValue) {
		return fmt.Errorf(tr("could not update sketch project file"))
	}

	// Write back the updated YAML
	return path.WriteFile(dstYaml)
}