summaryrefslogtreecommitdiff
path: root/vendor/github.com/google/yamlfmt/internal/hotfix/strip_directives.go
blob: 63e1c6e67afa2be20c2bf40fbcb3edc4d1f0bd3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hotfix

import (
	"bufio"
	"bytes"
	"context"
	"strings"

	"github.com/google/yamlfmt"
)

type directiveKey string

var contextDirectivesKey directiveKey = "directives"

type Directive struct {
	line    int
	content string
}

func ContextWithDirectives(ctx context.Context, directives []Directive) context.Context {
	return context.WithValue(ctx, contextDirectivesKey, directives)
}

func DirectivesFromContext(ctx context.Context) []Directive {
	return ctx.Value(contextDirectivesKey).([]Directive)
}

func MakeFeatureStripDirectives(lineSepChar string) yamlfmt.Feature {
	return yamlfmt.Feature{
		Name:         "Strip Directives",
		BeforeAction: stripDirectivesFeature(lineSepChar),
		AfterAction:  restoreDirectivesFeature(lineSepChar),
	}
}

func stripDirectivesFeature(lineSepChar string) yamlfmt.FeatureFunc {
	return func(ctx context.Context, content []byte) (context.Context, []byte, error) {
		directives := []Directive{}
		reader := bytes.NewReader(content)
		scanner := bufio.NewScanner(reader)
		result := ""
		currLine := 1
		for scanner.Scan() {
			line := scanner.Text()
			if strings.HasPrefix(line, "%") {
				directives = append(directives, Directive{
					line:    currLine,
					content: line,
				})
			} else {
				result += line + lineSepChar
			}
			currLine++
		}
		return ContextWithDirectives(ctx, directives), []byte(result), nil
	}
}

func restoreDirectivesFeature(lineSepChar string) yamlfmt.FeatureFunc {
	return func(ctx context.Context, content []byte) (context.Context, []byte, error) {
		directives := DirectivesFromContext(ctx)
		directiveIdx := 0
		doneDirectives := directiveIdx == len(directives)
		reader := bytes.NewReader(content)
		scanner := bufio.NewScanner(reader)
		result := ""
		currLine := 1
		for scanner.Scan() {
			if !doneDirectives && currLine == directives[directiveIdx].line {
				result += directives[directiveIdx].content + lineSepChar
				currLine++
				directiveIdx++
				doneDirectives = directiveIdx == len(directives)
			}
			result += scanner.Text() + lineSepChar
			currLine++
		}
		// Edge case: There technically can be a directive as the final line. This would be
		// useless as far as I can tell so maybe yamlfmt should just remove it anyway LOL but
		// no we'll keep it.
		if !doneDirectives && currLine == directives[directiveIdx].line {
			result += directives[directiveIdx].content + lineSepChar
		}
		return ctx, []byte(result), nil
	}
}