• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

charmbracelet / glow / 9862755377

09 Jul 2024 07:02PM UTC coverage: 10.105%. Remained the same
9862755377

Pull #627

github

caarlos0
ci: run govulncheck, semgrep, etc

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Pull Request #627: ci: run govulncheck, semgrep, etc

1 of 2 new or added lines in 2 files covered. (50.0%)

54 existing lines in 2 files now uncovered.

192 of 1900 relevant lines covered (10.11%)

4.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.14
/main.go
1
package main
2

3
import (
4
        "errors"
5
        "fmt"
6
        "io"
7
        "net/http"
8
        "net/url"
9
        "os"
10
        "os/exec"
11
        "path/filepath"
12
        "strings"
13

14
        "github.com/caarlos0/env/v11"
15
        "github.com/charmbracelet/glamour"
16
        "github.com/charmbracelet/glow/ui"
17
        "github.com/charmbracelet/glow/utils"
18
        "github.com/charmbracelet/log"
19
        gap "github.com/muesli/go-app-paths"
20
        "github.com/spf13/cobra"
21
        "github.com/spf13/viper"
22
        "golang.org/x/term"
23
)
24

25
var (
26
        // Version as provided by goreleaser.
27
        Version = ""
28
        // CommitSHA as provided by goreleaser.
29
        CommitSHA = ""
30

31
        readmeNames      = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
32
        readmeBranches   = []string{"main", "master"}
33
        configFile       string
34
        pager            bool
35
        style            string
36
        width            uint
37
        showAllFiles     bool
38
        preserveNewLines bool
39
        mouse            bool
40

41
        rootCmd = &cobra.Command{
42
                Use:   "glow [SOURCE|DIR]",
43
                Short: "Render markdown on the CLI, with pizzazz!",
44
                Long: paragraph(
45
                        fmt.Sprintf("\nRender markdown on the CLI, %s!", keyword("with pizzazz")),
46
                ),
47
                SilenceErrors:    false,
48
                SilenceUsage:     true,
49
                TraverseChildren: true,
50
                Args:             cobra.MaximumNArgs(1),
UNCOV
51
                ValidArgsFunction: func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
×
52
                        return nil, cobra.ShellCompDirectiveDefault
×
53
                },
×
54
                PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
×
55
                        return validateOptions(cmd)
×
56
                },
×
57
                RunE: execute,
58
        }
59
)
60

61
// source provides a readable markdown source.
62
type source struct {
63
        reader io.ReadCloser
64
        URL    string
65
}
66

67
// sourceFromArg parses an argument and creates a readable source for it.
68
func sourceFromArg(arg string) (*source, error) {
4✔
69
        // from stdin
4✔
70
        if arg == "-" {
4✔
UNCOV
71
                return &source{reader: os.Stdin}, nil
×
72
        }
×
73

74
        // a GitHub or GitLab URL (even without the protocol):
75
        if u, ok := isGitHubURL(arg); ok {
6✔
76
                src, err := findGitHubREADME(u)
2✔
77
                if err != nil {
2✔
UNCOV
78
                        return nil, err
×
79
                }
×
80
                return src, nil
2✔
81
        }
82
        if u, ok := isGitLabURL(arg); ok {
2✔
UNCOV
83
                src, err := findGitLabREADME(u)
×
84
                if err != nil {
×
85
                        return nil, err
×
86
                }
×
87
                return src, nil
×
88
        }
89

90
        // HTTP(S) URLs:
91
        if u, err := url.ParseRequestURI(arg); err == nil && strings.Contains(arg, "://") {
2✔
UNCOV
92
                if u.Scheme != "" {
×
93
                        if u.Scheme != "http" && u.Scheme != "https" {
×
94
                                return nil, fmt.Errorf("%s is not a supported protocol", u.Scheme)
×
95
                        }
×
96
                        // consumer of the source is responsible for closing the ReadCloser.
UNCOV
97
                        resp, err := http.Get(u.String()) // nolint:bodyclose
×
98
                        if err != nil {
×
99
                                return nil, err
×
100
                        }
×
101
                        if resp.StatusCode != http.StatusOK {
×
102
                                return nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
×
103
                        }
×
104
                        return &source{resp.Body, u.String()}, nil
×
105
                }
106
        }
107

108
        // a directory:
109
        if len(arg) == 0 {
2✔
UNCOV
110
                // use the current working dir if no argument was supplied
×
111
                arg = "."
×
112
        }
×
113
        st, err := os.Stat(arg)
2✔
114
        if err == nil && st.IsDir() {
3✔
115
                var src *source
1✔
116
                _ = filepath.Walk(arg, func(path string, _ os.FileInfo, err error) error {
160✔
117
                        if err != nil {
159✔
UNCOV
118
                                return err
×
119
                        }
×
120
                        for _, v := range readmeNames {
1,108✔
121
                                if strings.EqualFold(filepath.Base(path), v) {
950✔
122
                                        r, err := os.Open(path)
1✔
123
                                        if err != nil {
1✔
UNCOV
124
                                                continue
×
125
                                        }
126

127
                                        u, _ := filepath.Abs(path)
1✔
128
                                        src = &source{r, u}
1✔
129

1✔
130
                                        // abort filepath.Walk
1✔
131
                                        return errors.New("source found")
1✔
132
                                }
133
                        }
134
                        return nil
158✔
135
                })
136

137
                if src != nil {
2✔
138
                        return src, nil
1✔
139
                }
1✔
140

UNCOV
141
                return nil, errors.New("missing markdown source")
×
142
        }
143

144
        // a file:
145
        r, err := os.Open(arg)
1✔
146
        u, _ := filepath.Abs(arg)
1✔
147
        return &source{r, u}, err
1✔
148
}
149

UNCOV
150
func validateOptions(cmd *cobra.Command) error {
×
151
        // grab config values from Viper
×
152
        width = viper.GetUint("width")
×
153
        mouse = viper.GetBool("mouse")
×
154
        pager = viper.GetBool("pager")
×
155
        preserveNewLines = viper.GetBool("preserveNewLines")
×
156

×
157
        // validate the glamour style
×
158
        style = viper.GetString("style")
×
159
        if style != glamour.AutoStyle && glamour.DefaultStyles[style] == nil {
×
160
                style = utils.ExpandPath(style)
×
161
                if _, err := os.Stat(style); os.IsNotExist(err) {
×
NEW
162
                        return fmt.Errorf("Specified style does not exist: %s", style)
×
163
                } else if err != nil {
×
164
                        return err
×
165
                }
×
166
        }
167

UNCOV
168
        isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
×
169
        // We want to use a special no-TTY style, when stdout is not a terminal
×
170
        // and there was no specific style passed by arg
×
171
        if !isTerminal && !cmd.Flags().Changed("style") {
×
172
                style = "notty"
×
173
        }
×
174

175
        // Detect terminal width
UNCOV
176
        if isTerminal && width == 0 && !cmd.Flags().Changed("width") {
×
177
                w, _, err := term.GetSize(int(os.Stdout.Fd()))
×
178
                if err == nil {
×
179
                        width = uint(w)
×
180
                }
×
181

UNCOV
182
                if width > 120 {
×
183
                        width = 120
×
184
                }
×
185
        }
UNCOV
186
        if width == 0 {
×
187
                width = 80
×
188
        }
×
189
        return nil
×
190
}
191

UNCOV
192
func stdinIsPipe() (bool, error) {
×
193
        stat, err := os.Stdin.Stat()
×
194
        if err != nil {
×
195
                return false, err
×
196
        }
×
197
        if stat.Mode()&os.ModeCharDevice == 0 || stat.Size() > 0 {
×
198
                return true, nil
×
199
        }
×
200
        return false, nil
×
201
}
202

UNCOV
203
func execute(cmd *cobra.Command, args []string) error {
×
204
        // if stdin is a pipe then use stdin for input. note that you can also
×
205
        // explicitly use a - to read from stdin.
×
206
        if yes, err := stdinIsPipe(); err != nil {
×
207
                return err
×
208
        } else if yes {
×
209
                src := &source{reader: os.Stdin}
×
210
                defer src.reader.Close() //nolint:errcheck
×
211
                return executeCLI(cmd, src, os.Stdout)
×
212
        }
×
213

UNCOV
214
        switch len(args) {
×
215
        // TUI running on cwd
UNCOV
216
        case 0:
×
217
                return runTUI("")
×
218

219
        // TUI with possible dir argument
UNCOV
220
        case 1:
×
221
                // Validate that the argument is a directory. If it's not treat it as
×
222
                // an argument to the non-TUI version of Glow (via fallthrough).
×
223
                info, err := os.Stat(args[0])
×
224
                if err == nil && info.IsDir() {
×
225
                        p, err := filepath.Abs(args[0])
×
226
                        if err == nil {
×
227
                                return runTUI(p)
×
228
                        }
×
229
                }
UNCOV
230
                fallthrough
×
231

232
        // CLI
UNCOV
233
        default:
×
234
                for _, arg := range args {
×
235
                        if err := executeArg(cmd, arg, os.Stdout); err != nil {
×
236
                                return err
×
237
                        }
×
238
                }
239
        }
240

UNCOV
241
        return nil
×
242
}
243

244
func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
4✔
245
        // create an io.Reader from the markdown source in cli-args
4✔
246
        src, err := sourceFromArg(arg)
4✔
247
        if err != nil {
4✔
UNCOV
248
                return err
×
249
        }
×
250
        defer src.reader.Close() //nolint:errcheck
4✔
251
        return executeCLI(cmd, src, w)
4✔
252
}
253

254
func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
4✔
255
        b, err := io.ReadAll(src.reader)
4✔
256
        if err != nil {
4✔
UNCOV
257
                return err
×
258
        }
×
259

260
        b = utils.RemoveFrontmatter(b)
4✔
261

4✔
262
        // render
4✔
263
        var baseURL string
4✔
264
        u, err := url.ParseRequestURI(src.URL)
4✔
265
        if err == nil {
8✔
266
                u.Path = filepath.Dir(u.Path)
4✔
267
                baseURL = u.String() + "/"
4✔
268
        }
4✔
269

270
        isCode := !utils.IsMarkdownFile(src.URL)
4✔
271

4✔
272
        // initialize glamour
4✔
273
        r, err := glamour.NewTermRenderer(
4✔
274
                utils.GlamourStyle(style, isCode),
4✔
275
                glamour.WithWordWrap(int(width)),
4✔
276
                glamour.WithBaseURL(baseURL),
4✔
277
                glamour.WithPreservedNewLines(),
4✔
278
        )
4✔
279
        if err != nil {
4✔
UNCOV
280
                return err
×
281
        }
×
282

283
        s := string(b)
4✔
284
        ext := filepath.Ext(src.URL)
4✔
285
        if isCode {
4✔
UNCOV
286
                s = utils.WrapCodeBlock(string(b), ext)
×
287
        }
×
288

289
        out, err := r.Render(s)
4✔
290
        if err != nil {
4✔
UNCOV
291
                return err
×
292
        }
×
293

294
        // trim lines
295
        lines := strings.Split(out, "\n")
4✔
296
        var content strings.Builder
4✔
297
        for i, s := range lines {
712✔
298
                content.WriteString(strings.TrimSpace(s))
708✔
299

708✔
300
                // don't add an artificial newline after the last split
708✔
301
                if i+1 < len(lines) {
1,412✔
302
                        content.WriteByte('\n')
704✔
303
                }
704✔
304
        }
305

306
        // display
307
        if pager || cmd.Flags().Changed("pager") {
4✔
UNCOV
308
                pagerCmd := os.Getenv("PAGER")
×
309
                if pagerCmd == "" {
×
310
                        pagerCmd = "less -r"
×
311
                }
×
312

UNCOV
313
                pa := strings.Split(pagerCmd, " ")
×
314
                c := exec.Command(pa[0], pa[1:]...) // nolint:gosec
×
315
                c.Stdin = strings.NewReader(content.String())
×
316
                c.Stdout = os.Stdout
×
317
                return c.Run()
×
318
        }
319

320
        fmt.Fprint(w, content.String()) //nolint: errcheck
4✔
321
        return nil
4✔
322
}
323

UNCOV
324
func runTUI(workingDirectory string) error {
×
325
        // Read environment to get debugging stuff
×
326
        cfg, err := env.ParseAs[ui.Config]()
×
327
        if err != nil {
×
328
                return fmt.Errorf("error parsing config: %v", err)
×
329
        }
×
330

UNCOV
331
        cfg.WorkingDirectory = workingDirectory
×
332

×
333
        cfg.ShowAllFiles = showAllFiles
×
334
        cfg.GlamourMaxWidth = width
×
335
        cfg.GlamourStyle = style
×
336
        cfg.EnableMouse = mouse
×
337
        cfg.PreserveNewLines = preserveNewLines
×
338

×
339
        // Run Bubble Tea program
×
340
        if _, err := ui.NewProgram(cfg).Run(); err != nil {
×
341
                return err
×
342
        }
×
343

UNCOV
344
        return nil
×
345
}
346

UNCOV
347
func main() {
×
348
        closer, err := setupLog()
×
349
        if err != nil {
×
350
                fmt.Println(err)
×
351
                os.Exit(1)
×
352
        }
×
353
        if err := rootCmd.Execute(); err != nil {
×
354
                _ = closer()
×
355
                os.Exit(1)
×
356
        }
×
357
        _ = closer()
×
358
}
359

360
func init() {
1✔
361
        tryLoadConfigFromDefaultPlaces()
1✔
362
        if len(CommitSHA) >= 7 {
1✔
UNCOV
363
                vt := rootCmd.VersionTemplate()
×
364
                rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
×
365
        }
×
366
        if Version == "" {
2✔
367
                Version = "unknown (built from source)"
1✔
368
        }
1✔
369
        rootCmd.Version = Version
1✔
370
        rootCmd.InitDefaultCompletionCmd()
1✔
371

1✔
372
        // "Glow Classic" cli arguments
1✔
373
        rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default %s)", viper.GetViper().ConfigFileUsed()))
1✔
374
        rootCmd.Flags().BoolVarP(&pager, "pager", "p", false, "display with pager")
1✔
375
        rootCmd.Flags().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path")
1✔
376
        rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width")
1✔
377
        rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)")
1✔
378
        rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output")
1✔
379
        rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)")
1✔
380
        _ = rootCmd.Flags().MarkHidden("mouse")
1✔
381

1✔
382
        // Config bindings
1✔
383
        _ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style"))
1✔
384
        _ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
1✔
385
        _ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
1✔
386
        _ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse"))
1✔
387
        _ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
1✔
388

1✔
389
        viper.SetDefault("style", glamour.AutoStyle)
1✔
390
        viper.SetDefault("width", 0)
1✔
391

1✔
392
        rootCmd.AddCommand(configCmd, manCmd)
1✔
393
}
394

395
func tryLoadConfigFromDefaultPlaces() {
1✔
396
        scope := gap.NewScope(gap.User, "glow")
1✔
397
        dirs, err := scope.ConfigDirs()
1✔
398
        if err != nil {
1✔
UNCOV
399
                fmt.Println("Could not load find configuration directory.")
×
400
                os.Exit(1)
×
401
        }
×
402

403
        if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
2✔
404
                dirs = append([]string{filepath.Join(c, "glow")}, dirs...)
1✔
405
        }
1✔
406

407
        if c := os.Getenv("GLOW_CONFIG_HOME"); c != "" {
1✔
UNCOV
408
                dirs = append([]string{c}, dirs...)
×
409
        }
×
410

411
        for _, v := range dirs {
5✔
412
                viper.AddConfigPath(v)
4✔
413
        }
4✔
414

415
        viper.SetConfigName("glow")
1✔
416
        viper.SetConfigType("yaml")
1✔
417
        viper.SetEnvPrefix("glow")
1✔
418
        viper.AutomaticEnv()
1✔
419

1✔
420
        if err := viper.ReadInConfig(); err != nil {
2✔
421
                if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
1✔
UNCOV
422
                        log.Warn("Could not parse configuration file", "err", err)
×
423
                }
×
424
        }
425

426
        if used := viper.ConfigFileUsed(); used != "" {
1✔
UNCOV
427
                log.Debug("Using configuration file", "path", viper.ConfigFileUsed())
×
428
                return
×
429
        }
×
430

431
        if viper.ConfigFileUsed() == "" {
2✔
432
                configFile = filepath.Join(dirs[0], "glow.yml")
1✔
433
        }
1✔
434
        if err := ensureConfigFile(); err != nil {
1✔
UNCOV
435
                log.Error("Could not create default configuration", "error", err)
×
436
        }
×
437
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc