Skip to content

Commit 034c5f1

Browse files
authored
feat: set path for stdin cli (#1076)
1 parent 6539915 commit 034c5f1

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

cmd/vale/flag.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func init() {
2626
pflag.StringVar(&Flags.Output, "output", "CLI", `An output style ("line", "JSON", or a template file).`)
2727
pflag.StringVar(&Flags.InExt, "ext", ".txt",
2828
fmt.Sprintf(`An extension to associate with stdin (%s).`, toCodeStyle(`--ext=.md`)))
29+
pflag.StringVar(&Flags.InPath, "path", "",
30+
fmt.Sprintf(`A file path to associate with stdin (%s).`, toCodeStyle(`--path=docs/example.md`)))
2931

3032
pflag.StringVar(&Flags.AlertLevel, "minAlertLevel", "",
3133
fmt.Sprintf(`The minimum level to display (%s).`, toCodeStyle(`--minAlertLevel=error`)))

internal/core/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ type CLIFlags struct {
167167
Built string
168168
Glob string
169169
InExt string
170+
InPath string
170171
Output string
171172
Path string
172173
Sources string

internal/core/file.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func NewFile(src string, config *Config) (*File, error) {
5353
var format, ext string
5454
var fbytes []byte
5555
var lookup bool
56+
path := src
5657

5758
if system.FileExists(src) {
5859
fbytes, _ = os.ReadFile(src)
@@ -62,14 +63,24 @@ func NewFile(src string, config *Config) (*File, error) {
6263
ext, format = FormatFromExt(src, config.Formats)
6364
}
6465
} else {
65-
ext, format = FormatFromExt(config.Flags.InExt, config.Formats)
6666
fbytes = []byte(src)
67-
src = "stdin" + config.Flags.InExt
6867
lookup = true
68+
// For stdin, allow an explicit path override to drive path-based config.
69+
if config.Flags.InPath != "" {
70+
path = config.Flags.InPath
71+
} else {
72+
path = "stdin" + config.Flags.InExt
73+
}
74+
// If --ext was explicitly set, respect it; otherwise infer from the path.
75+
if config.Flags.InExt != ".txt" {
76+
ext, format = FormatFromExt(config.Flags.InExt, config.Formats)
77+
} else {
78+
ext, format = FormatFromExt(path, config.Formats)
79+
}
6980
}
7081

71-
filepaths := []string{src}
72-
normed := system.ReplaceFileExt(src, config.Formats)
82+
filepaths := []string{path}
83+
normed := system.ReplaceFileExt(path, config.Formats)
7384

7485
baseStyles := config.GBaseStyles
7586
checks := make(map[string]bool)
@@ -95,7 +106,7 @@ func NewFile(src string, config *Config) (*File, error) {
95106
sec, err := glob.Compile(syntax)
96107
if err != nil {
97108
return &File{}, err
98-
} else if sec.Match(src) {
109+
} else if sec.Match(path) {
99110
lang = code
100111
break
101112
}
@@ -105,8 +116,8 @@ func NewFile(src string, config *Config) (*File, error) {
105116
for sec, p := range config.Stylesheets {
106117
pat, err := glob.Compile(sec)
107118
if err != nil {
108-
return &File{}, NewE100(src, err)
109-
} else if pat.Match(src) {
119+
return &File{}, NewE100(path, err)
120+
} else if pat.Match(path) {
110121
transform = p
111122
break
112123
}
@@ -120,11 +131,11 @@ func NewFile(src string, config *Config) (*File, error) {
120131
lines := strings.SplitAfter(strings.Clone(content), "\n")
121132

122133
file := File{
123-
NormedExt: ext, Format: format, RealExt: filepath.Ext(src),
134+
NormedExt: ext, Format: format, RealExt: filepath.Ext(path),
124135
BaseStyles: baseStyles, Checks: checks, Lines: lines, Content: content,
125136
Comments: make(map[string]bool), history: make(map[string]int),
126137
simple: config.Flags.Simple, Transform: transform,
127-
limits: make(map[string]int), Path: src, Metrics: make(map[string]int),
138+
limits: make(map[string]int), Path: path, Metrics: make(map[string]int),
128139
NLP: nlp.Info{Endpoint: config.NLPEndpoint, Lang: lang},
129140
Lookup: lookup, NormedPath: normed,
130141
}

0 commit comments

Comments
 (0)