Skip to content

Commit

Permalink
add message diff for doc linter (#25814)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 authored May 3, 2024
1 parent a79abea commit 763868f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/tools/document-lint/check/check_iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,29 @@ func newCheckBase(line int, key string, mdField *model.Field) checkBase {
mdField: mdField,
}
}

// for some special diff, we need to show message instead of diff
type diffWithMessage struct {
checkBase
msg string
skip bool
}

func (i diffWithMessage) Fix(line string) (string, error) {
return line, nil
}

func (i diffWithMessage) String() string {
return i.msg
}
func (i diffWithMessage) ShouldSkip() bool {
return i.skip
}

func newDiffWithMessage(msg string, skip bool) Checker {
return diffWithMessage{
checkBase: newCheckBase(0, msg, nil),
msg: msg,
skip: skip,
}
}
4 changes: 4 additions & 0 deletions internal/tools/document-lint/check/check_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func NewResourceDiff(tf *schema.Resource) *ResourceDiff {

func (r *ResourceDiff) DiffAll() {
if r.md == nil {
if r.MDFile == "" {
r.Diff = append(r.Diff, newDiffWithMessage(fmt.Sprintf("%s has no document", r.tf.ResourceType), r.tf.IsDeprecated()))
return
}
mark := md.MustNewMarkFromFile(r.MDFile)
r.md = mark.BuildResourceDoc()
}
Expand Down
4 changes: 4 additions & 0 deletions internal/tools/document-lint/check/document_fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (f *Fixer) TryFix() (err error) {
if len(f.Diff) == 0 {
return
}
if d, ok := f.Diff[0].(diffWithMessage); ok {
log.Printf("%s: %s", f.ResourceType, d.msg)
return
}
content, err := os.ReadFile(f.MDFile)
if err != nil {
log.Printf("open %s: %v", f.MDFile, err)
Expand Down
10 changes: 10 additions & 0 deletions internal/tools/document-lint/schema/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ func (r *Resource) HasPathFor(path []string) bool {
}
return true
}

func (r *Resource) IsDeprecated() bool {
if r.Schema != nil {
return r.Schema.DeprecationMessage != ""
} else if _, ok := r.SDKResource.(sdk.ResourceWithDeprecationReplacedBy); ok {
return true
}

return false
}

0 comments on commit 763868f

Please sign in to comment.