11package main
22
33import (
4+ "encoding/json"
45 "errors"
56 "fmt"
67 "io"
78 "math"
89 "os"
910 "path/filepath"
1011 "runtime/debug"
12+ "strings"
1113
1214 flag "github.com/spf13/pflag"
1315
@@ -1037,6 +1039,7 @@ Topics:
10371039 kinds Show concept page for file kinds
10381040 kinds-cli Summarize the 'kinds' subcommand surface
10391041 placeholder-grammar Show placeholder vocabulary reference
1042+ patterns Show maintainability patterns across rules
10401043`
10411044
10421045// runHelp implements the "help" subcommand.
@@ -1057,12 +1060,53 @@ func runHelp(args []string) int {
10571060 return runHelpKindsCLI ()
10581061 case "placeholder-grammar" :
10591062 return runHelpConcept ("placeholder-grammar" )
1063+ case "patterns" :
1064+ return runHelpPatterns (args [1 :])
10601065 default :
10611066 fmt .Fprintf (os .Stderr , "mdsmith: help: unknown topic %q\n " , args [0 ])
10621067 return 2
10631068 }
10641069}
10651070
1071+ func runHelpPatterns (args []string ) int {
1072+ format := "text"
1073+ if len (args ) >= 2 && (args [0 ] == "-f" || args [0 ] == "--format" ) {
1074+ format = args [1 ]
1075+ }
1076+ rules , err := ruledocs .ListRules ()
1077+ if err != nil {
1078+ fmt .Fprintf (os .Stderr , "mdsmith: %v\n " , err )
1079+ return 2
1080+ }
1081+ type rec struct {
1082+ ID string `json:"id"`
1083+ Name string `json:"name"`
1084+ Signal string `json:"signal"`
1085+ Fix string `json:"fix"`
1086+ ForDiagnostic bool `json:"for-diagnostic"`
1087+ }
1088+ items := make ([]rec , 0 )
1089+ for _ , r := range rules {
1090+ if r .Maintainability == nil {
1091+ continue
1092+ }
1093+ items = append (items , rec {r .ID , r .Name , r .Maintainability .Signal , r .Maintainability .Fix , r .Maintainability .ForDiagnostic })
1094+ }
1095+ if format == "json" {
1096+ enc := json .NewEncoder (os .Stdout )
1097+ enc .SetIndent ("" , " " )
1098+ if err := enc .Encode (items ); err != nil {
1099+ fmt .Fprintf (os .Stderr , "mdsmith: %v\n " , err )
1100+ return 2
1101+ }
1102+ return 0
1103+ }
1104+ for _ , it := range items {
1105+ fmt .Printf ("%s %s\n signal: %s\n fix: %s\n for-diagnostic: %t\n \n " , it .ID , it .Name , it .Signal , it .Fix , it .ForDiagnostic )
1106+ }
1107+ return 0
1108+ }
1109+
10661110const helpKindsText = `File Kinds
10671111
10681112A kind is a named bundle of rule settings that can be applied to a set of
@@ -1192,11 +1236,28 @@ func listAllRules() int {
11921236}
11931237
11941238func showRule (query string ) int {
1195- content , err := ruledocs .LookupRule ( query )
1239+ rules , err := ruledocs .ListRules ( )
11961240 if err != nil {
11971241 fmt .Fprintf (os .Stderr , "mdsmith: %v\n " , err )
11981242 return 2
11991243 }
1244+ var chosen * ruledocs.RuleInfo
1245+ q := strings .ToUpper (query )
1246+ for i := range rules {
1247+ if strings .ToUpper (rules [i ].ID ) == q || rules [i ].Name == query {
1248+ chosen = & rules [i ]
1249+ break
1250+ }
1251+ }
1252+ if chosen == nil {
1253+ fmt .Fprintf (os .Stderr , "mdsmith: unknown rule %q\n " , query )
1254+ return 2
1255+ }
1256+ content := ruledocs .StripFrontMatter (chosen .Content )
1257+ if chosen .Maintainability != nil {
1258+ content += "\n \n ## Maintainability pattern\n \n "
1259+ content += fmt .Sprintf ("- Signal: %s\n - Fix: %s\n - For diagnostic: %t\n " , chosen .Maintainability .Signal , chosen .Maintainability .Fix , chosen .Maintainability .ForDiagnostic )
1260+ }
12001261 fmt .Print (content )
12011262 return 0
12021263}
0 commit comments