func()

in internal/textconfig/textconfig.go [232:273]


func (h *Handle) Apply() error {
	if err := h.Cleanup(); err != nil {
		return fmt.Errorf("failed to cleanup file %q: %v", h.file, err)
	}

	var topBlocks []*Block
	var bottomBlocks []*Block

	for _, block := range h.blocks {
		switch block.position {
		case Top:
			topBlocks = append(topBlocks, block)
		case Bottom:
			bottomBlocks = append(bottomBlocks, block)
		}
	}

	data, err := os.ReadFile(h.file)
	if err != nil {
		return fmt.Errorf("failed to read file %q: %v", h.file, err)
	}

	lines := strings.Split(string(data), "\n")
	cleanedup := h.cleanup(lines)
	var output []string

	for _, block := range topBlocks {
		output = append(output, block.lines(h.opts.Delimiters, h.opts.Spacer)...)
	}

	output = append(output, cleanedup...)

	for _, block := range bottomBlocks {
		output = append(output, block.lines(h.opts.Delimiters, h.opts.Spacer)...)
	}

	if err := os.WriteFile(h.file, []byte(strings.Join(output, "\n")), h.mode); err != nil {
		return fmt.Errorf("failed to write file %q: %v", h.file, err)
	}

	return nil
}