2019-11-04 23:17:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-22 03:44:54 +00:00
|
|
|
"errors"
|
2019-11-04 23:17:36 +00:00
|
|
|
"fmt"
|
2019-11-22 02:52:21 +00:00
|
|
|
"io"
|
2024-07-09 19:13:05 +00:00
|
|
|
"io/fs"
|
2019-11-22 13:06:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-11-09 20:07:01 +00:00
|
|
|
"os"
|
2019-12-31 10:13:06 +00:00
|
|
|
"os/exec"
|
2019-11-25 23:08:06 +00:00
|
|
|
"path/filepath"
|
2019-12-25 06:01:56 +00:00
|
|
|
"strings"
|
2019-11-04 23:17:36 +00:00
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
"github.com/caarlos0/env/v11"
|
2021-06-14 20:52:00 +00:00
|
|
|
"github.com/charmbracelet/glamour"
|
|
|
|
"github.com/charmbracelet/glow/ui"
|
|
|
|
"github.com/charmbracelet/glow/utils"
|
2024-07-09 19:13:32 +00:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-07-03 15:11:29 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2020-10-23 04:24:03 +00:00
|
|
|
gap "github.com/muesli/go-app-paths"
|
2019-11-22 03:44:54 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-10-23 04:24:03 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-04-29 13:11:55 +00:00
|
|
|
"golang.org/x/term"
|
2019-11-04 23:17:36 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
var (
|
2022-10-25 14:40:51 +00:00
|
|
|
// Version as provided by goreleaser.
|
|
|
|
Version = ""
|
|
|
|
// CommitSHA as provided by goreleaser.
|
2019-12-09 12:50:17 +00:00
|
|
|
CommitSHA = ""
|
|
|
|
|
2024-07-09 12:50:10 +00:00
|
|
|
readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
|
|
|
|
configFile string
|
|
|
|
pager bool
|
|
|
|
style string
|
|
|
|
width uint
|
|
|
|
showAllFiles bool
|
2024-07-11 15:08:57 +00:00
|
|
|
showLineNumbers bool
|
2024-07-09 12:50:10 +00:00
|
|
|
preserveNewLines bool
|
|
|
|
mouse bool
|
2019-11-24 04:06:01 +00:00
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
rootCmd = &cobra.Command{
|
2024-07-03 15:11:29 +00:00
|
|
|
Use: "glow [SOURCE|DIR]",
|
|
|
|
Short: "Render markdown on the CLI, with pizzazz!",
|
|
|
|
Long: paragraph(
|
|
|
|
fmt.Sprintf("\nRender markdown on the CLI, %s!", keyword("with pizzazz")),
|
|
|
|
),
|
2020-05-25 17:05:32 +00:00
|
|
|
SilenceErrors: false,
|
2024-07-03 15:11:29 +00:00
|
|
|
SilenceUsage: true,
|
2020-05-25 17:05:32 +00:00
|
|
|
TraverseChildren: true,
|
2024-07-03 16:41:11 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
ValidArgsFunction: func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
|
|
},
|
2024-07-03 15:11:29 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
|
|
return validateOptions(cmd)
|
|
|
|
},
|
|
|
|
RunE: execute,
|
2019-11-22 03:44:54 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-03-31 06:57:49 +00:00
|
|
|
// source provides a readable markdown source.
|
2020-03-31 06:53:35 +00:00
|
|
|
type source struct {
|
2019-11-25 23:08:06 +00:00
|
|
|
reader io.ReadCloser
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
2020-03-31 06:57:49 +00:00
|
|
|
// sourceFromArg parses an argument and creates a readable source for it.
|
|
|
|
func sourceFromArg(arg string) (*source, error) {
|
2020-03-31 06:53:35 +00:00
|
|
|
// from stdin
|
2020-03-31 06:57:49 +00:00
|
|
|
if arg == "-" {
|
2020-03-31 06:53:35 +00:00
|
|
|
return &source{reader: os.Stdin}, nil
|
2019-11-22 02:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 16:18:59 +00:00
|
|
|
// a GitHub or GitLab URL (even without the protocol):
|
2024-07-09 19:07:39 +00:00
|
|
|
src, err := readmeURL(arg)
|
2024-07-11 15:02:03 +00:00
|
|
|
if src != nil && err == nil {
|
|
|
|
// if there's an error, try next methods...
|
|
|
|
return src, nil
|
2019-11-25 05:55:50 +00:00
|
|
|
}
|
2019-11-25 03:55:09 +00:00
|
|
|
|
2019-12-10 16:18:59 +00:00
|
|
|
// HTTP(S) URLs:
|
2020-10-14 23:58:04 +00:00
|
|
|
if u, err := url.ParseRequestURI(arg); err == nil && strings.Contains(arg, "://") {
|
2019-11-25 23:34:15 +00:00
|
|
|
if u.Scheme != "" {
|
|
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
|
|
|
return nil, fmt.Errorf("%s is not a supported protocol", u.Scheme)
|
|
|
|
}
|
2022-12-19 22:38:26 +00:00
|
|
|
// consumer of the source is responsible for closing the ReadCloser.
|
|
|
|
resp, err := http.Get(u.String()) // nolint:bodyclose
|
2019-11-25 23:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
|
|
|
|
}
|
2020-03-31 06:53:35 +00:00
|
|
|
return &source{resp.Body, u.String()}, nil
|
2019-11-22 13:06:12 +00:00
|
|
|
}
|
2019-11-22 02:28:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 00:39:33 +00:00
|
|
|
// a directory:
|
2020-03-31 06:57:49 +00:00
|
|
|
if len(arg) == 0 {
|
2020-01-13 00:39:33 +00:00
|
|
|
// use the current working dir if no argument was supplied
|
2020-03-31 06:57:49 +00:00
|
|
|
arg = "."
|
2020-01-13 00:39:33 +00:00
|
|
|
}
|
2020-03-31 06:57:49 +00:00
|
|
|
st, err := os.Stat(arg)
|
2020-01-13 00:39:33 +00:00
|
|
|
if err == nil && st.IsDir() {
|
2020-03-31 06:53:35 +00:00
|
|
|
var src *source
|
2024-07-03 15:11:29 +00:00
|
|
|
_ = filepath.Walk(arg, func(path string, _ os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-13 00:39:33 +00:00
|
|
|
for _, v := range readmeNames {
|
|
|
|
if strings.EqualFold(filepath.Base(path), v) {
|
|
|
|
r, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := filepath.Abs(path)
|
2020-03-31 06:53:35 +00:00
|
|
|
src = &source{r, u}
|
|
|
|
|
|
|
|
// abort filepath.Walk
|
2020-01-13 00:39:33 +00:00
|
|
|
return errors.New("source found")
|
|
|
|
}
|
2019-11-24 04:06:01 +00:00
|
|
|
}
|
2020-01-13 00:39:33 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-03-31 06:53:35 +00:00
|
|
|
|
2020-01-13 00:39:33 +00:00
|
|
|
if src != nil {
|
|
|
|
return src, nil
|
2019-11-24 04:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("missing markdown source")
|
|
|
|
}
|
|
|
|
|
2020-01-13 00:39:33 +00:00
|
|
|
// a file:
|
2020-03-31 06:57:49 +00:00
|
|
|
r, err := os.Open(arg)
|
|
|
|
u, _ := filepath.Abs(arg)
|
2020-03-31 06:53:35 +00:00
|
|
|
return &source{r, u}, err
|
2019-11-22 02:28:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:23:58 +00:00
|
|
|
func validateOptions(cmd *cobra.Command) error {
|
2020-10-23 06:46:59 +00:00
|
|
|
// grab config values from Viper
|
|
|
|
width = viper.GetUint("width")
|
2020-11-20 20:13:43 +00:00
|
|
|
mouse = viper.GetBool("mouse")
|
2021-02-04 00:34:44 +00:00
|
|
|
pager = viper.GetBool("pager")
|
2024-07-09 12:50:10 +00:00
|
|
|
preserveNewLines = viper.GetBool("preserveNewLines")
|
2020-10-23 06:46:59 +00:00
|
|
|
|
2020-12-16 15:23:58 +00:00
|
|
|
// validate the glamour style
|
|
|
|
style = viper.GetString("style")
|
2024-07-03 15:11:29 +00:00
|
|
|
if style != glamour.AutoStyle && glamour.DefaultStyles[style] == nil {
|
2020-12-16 15:23:58 +00:00
|
|
|
style = utils.ExpandPath(style)
|
2024-07-09 19:13:05 +00:00
|
|
|
if _, err := os.Stat(style); errors.Is(err, fs.ErrNotExist) {
|
2020-12-16 15:23:58 +00:00
|
|
|
return fmt.Errorf("Specified style does not exist: %s", style)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:11:55 +00:00
|
|
|
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
|
2020-05-05 05:48:05 +00:00
|
|
|
// We want to use a special no-TTY style, when stdout is not a terminal
|
|
|
|
// and there was no specific style passed by arg
|
|
|
|
if !isTerminal && !cmd.Flags().Changed("style") {
|
|
|
|
style = "notty"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect terminal width
|
2020-10-23 06:46:59 +00:00
|
|
|
if isTerminal && width == 0 && !cmd.Flags().Changed("width") {
|
2021-04-29 13:11:55 +00:00
|
|
|
w, _, err := term.GetSize(int(os.Stdout.Fd()))
|
2020-05-05 05:48:05 +00:00
|
|
|
if err == nil {
|
|
|
|
width = uint(w)
|
|
|
|
}
|
2020-10-23 06:46:59 +00:00
|
|
|
|
|
|
|
if width > 120 {
|
|
|
|
width = 120
|
|
|
|
}
|
2020-05-05 05:48:05 +00:00
|
|
|
}
|
|
|
|
if width == 0 {
|
|
|
|
width = 80
|
|
|
|
}
|
2020-12-16 15:23:58 +00:00
|
|
|
return nil
|
2020-05-05 05:48:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 14:40:37 +00:00
|
|
|
func stdinIsPipe() (bool, error) {
|
|
|
|
stat, err := os.Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if stat.Mode()&os.ModeCharDevice == 0 || stat.Size() > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:40:29 +00:00
|
|
|
func execute(cmd *cobra.Command, args []string) error {
|
2021-10-21 14:40:37 +00:00
|
|
|
// if stdin is a pipe then use stdin for input. note that you can also
|
|
|
|
// explicitly use a - to read from stdin.
|
|
|
|
if yes, err := stdinIsPipe(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if yes {
|
|
|
|
src := &source{reader: os.Stdin}
|
2022-10-25 14:40:51 +00:00
|
|
|
defer src.reader.Close() //nolint:errcheck
|
2021-10-21 14:40:37 +00:00
|
|
|
return executeCLI(cmd, src, os.Stdout)
|
|
|
|
}
|
|
|
|
|
2021-01-15 20:58:58 +00:00
|
|
|
switch len(args) {
|
|
|
|
// TUI running on cwd
|
|
|
|
case 0:
|
2024-07-03 15:11:29 +00:00
|
|
|
return runTUI("")
|
2021-01-15 20:58:58 +00:00
|
|
|
|
|
|
|
// TUI with possible dir argument
|
|
|
|
case 1:
|
|
|
|
// Validate that the argument is a directory. If it's not treat it as
|
|
|
|
// an argument to the non-TUI version of Glow (via fallthrough).
|
|
|
|
info, err := os.Stat(args[0])
|
|
|
|
if err == nil && info.IsDir() {
|
|
|
|
p, err := filepath.Abs(args[0])
|
|
|
|
if err == nil {
|
2024-07-03 15:11:29 +00:00
|
|
|
return runTUI(p)
|
2021-01-15 20:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fallthrough
|
2020-05-15 17:09:46 +00:00
|
|
|
|
2021-01-15 20:58:58 +00:00
|
|
|
// CLI
|
|
|
|
default:
|
|
|
|
for _, arg := range args {
|
|
|
|
if err := executeArg(cmd, arg, os.Stdout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-15 17:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-15 20:58:58 +00:00
|
|
|
|
2020-05-15 17:09:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
|
2019-12-10 16:18:59 +00:00
|
|
|
// create an io.Reader from the markdown source in cli-args
|
2020-03-31 06:57:49 +00:00
|
|
|
src, err := sourceFromArg(arg)
|
2019-11-22 02:28:26 +00:00
|
|
|
if err != nil {
|
2019-11-22 03:44:54 +00:00
|
|
|
return err
|
2019-11-09 22:12:54 +00:00
|
|
|
}
|
2022-10-25 14:40:51 +00:00
|
|
|
defer src.reader.Close() //nolint:errcheck
|
2021-10-21 14:40:37 +00:00
|
|
|
return executeCLI(cmd, src, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
|
2022-10-25 14:40:51 +00:00
|
|
|
b, err := io.ReadAll(src.reader)
|
2019-12-06 17:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-25 03:25:58 +00:00
|
|
|
|
2020-10-30 15:14:36 +00:00
|
|
|
b = utils.RemoveFrontmatter(b)
|
|
|
|
|
2019-12-31 10:13:06 +00:00
|
|
|
// render
|
2019-12-23 03:27:27 +00:00
|
|
|
var baseURL string
|
2019-11-25 23:08:06 +00:00
|
|
|
u, err := url.ParseRequestURI(src.URL)
|
|
|
|
if err == nil {
|
|
|
|
u.Path = filepath.Dir(u.Path)
|
2019-12-23 03:27:27 +00:00
|
|
|
baseURL = u.String() + "/"
|
2019-11-25 23:08:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
isCode := !utils.IsMarkdownFile(src.URL)
|
2020-03-31 04:58:21 +00:00
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
// initialize glamour
|
2019-12-28 02:03:20 +00:00
|
|
|
r, err := glamour.NewTermRenderer(
|
2024-07-09 19:13:32 +00:00
|
|
|
glamour.WithColorProfile(lipgloss.ColorProfile()),
|
2024-07-03 15:11:29 +00:00
|
|
|
utils.GlamourStyle(style, isCode),
|
2019-12-28 02:03:20 +00:00
|
|
|
glamour.WithWordWrap(int(width)),
|
|
|
|
glamour.WithBaseURL(baseURL),
|
2022-07-16 09:41:56 +00:00
|
|
|
glamour.WithPreservedNewLines(),
|
2019-12-28 02:03:20 +00:00
|
|
|
)
|
2019-12-07 18:49:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 21:36:55 +00:00
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
s := string(b)
|
|
|
|
ext := filepath.Ext(src.URL)
|
|
|
|
if isCode {
|
|
|
|
s = utils.WrapCodeBlock(string(b), ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := r.Render(s)
|
2019-12-25 06:01:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:13:06 +00:00
|
|
|
// trim lines
|
2024-07-03 15:11:29 +00:00
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
var content strings.Builder
|
2019-12-25 06:01:56 +00:00
|
|
|
for i, s := range lines {
|
2024-07-03 15:11:29 +00:00
|
|
|
content.WriteString(strings.TrimSpace(s))
|
2019-12-25 06:01:56 +00:00
|
|
|
|
2019-12-31 05:36:15 +00:00
|
|
|
// don't add an artificial newline after the last split
|
2019-12-25 06:01:56 +00:00
|
|
|
if i+1 < len(lines) {
|
2024-07-03 15:11:29 +00:00
|
|
|
content.WriteByte('\n')
|
2019-12-25 06:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:13:06 +00:00
|
|
|
// display
|
2021-02-04 00:34:44 +00:00
|
|
|
if pager || cmd.Flags().Changed("pager") {
|
|
|
|
pagerCmd := os.Getenv("PAGER")
|
|
|
|
if pagerCmd == "" {
|
|
|
|
pagerCmd = "less -r"
|
2019-12-31 10:13:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:34:44 +00:00
|
|
|
pa := strings.Split(pagerCmd, " ")
|
2022-10-25 14:40:51 +00:00
|
|
|
c := exec.Command(pa[0], pa[1:]...) // nolint:gosec
|
2024-07-03 15:11:29 +00:00
|
|
|
c.Stdin = strings.NewReader(content.String())
|
2019-12-31 10:13:06 +00:00
|
|
|
c.Stdout = os.Stdout
|
|
|
|
return c.Run()
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
fmt.Fprint(w, content.String()) //nolint: errcheck
|
2019-12-25 06:01:56 +00:00
|
|
|
return nil
|
2019-11-22 03:44:54 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
func runTUI(workingDirectory string) error {
|
2020-09-07 23:14:59 +00:00
|
|
|
// Read environment to get debugging stuff
|
2024-07-03 15:11:29 +00:00
|
|
|
cfg, err := env.ParseAs[ui.Config]()
|
|
|
|
if err != nil {
|
2020-09-07 23:14:59 +00:00
|
|
|
return fmt.Errorf("error parsing config: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-15 20:58:58 +00:00
|
|
|
cfg.WorkingDirectory = workingDirectory
|
2024-07-03 15:11:29 +00:00
|
|
|
|
2020-09-07 23:14:59 +00:00
|
|
|
cfg.ShowAllFiles = showAllFiles
|
2024-07-11 15:08:57 +00:00
|
|
|
cfg.ShowLineNumbers = showLineNumbers
|
2020-10-24 03:30:09 +00:00
|
|
|
cfg.GlamourMaxWidth = width
|
|
|
|
cfg.GlamourStyle = style
|
2021-06-14 20:52:00 +00:00
|
|
|
cfg.EnableMouse = mouse
|
2024-07-09 12:50:10 +00:00
|
|
|
cfg.PreserveNewLines = preserveNewLines
|
2020-10-18 01:46:02 +00:00
|
|
|
|
2020-09-07 23:14:59 +00:00
|
|
|
// Run Bubble Tea program
|
2021-06-14 20:52:00 +00:00
|
|
|
if _, err := ui.NewProgram(cfg).Run(); err != nil {
|
2020-09-07 23:14:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
func main() {
|
2024-07-03 15:11:29 +00:00
|
|
|
closer, err := setupLog()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-11-22 03:44:54 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2024-07-03 15:11:29 +00:00
|
|
|
_ = closer()
|
2020-10-27 14:12:50 +00:00
|
|
|
os.Exit(1)
|
2019-11-22 03:44:54 +00:00
|
|
|
}
|
2024-07-03 15:11:29 +00:00
|
|
|
_ = closer()
|
2019-11-22 03:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-07-03 15:11:29 +00:00
|
|
|
tryLoadConfigFromDefaultPlaces()
|
2019-12-09 13:01:47 +00:00
|
|
|
if len(CommitSHA) >= 7 {
|
2019-12-09 12:50:17 +00:00
|
|
|
vt := rootCmd.VersionTemplate()
|
2019-12-09 13:01:47 +00:00
|
|
|
rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
|
2019-12-09 12:50:17 +00:00
|
|
|
}
|
|
|
|
if Version == "" {
|
|
|
|
Version = "unknown (built from source)"
|
|
|
|
}
|
|
|
|
rootCmd.Version = Version
|
2024-07-03 16:41:11 +00:00
|
|
|
rootCmd.InitDefaultCompletionCmd()
|
2019-12-09 12:50:17 +00:00
|
|
|
|
2020-05-25 15:45:56 +00:00
|
|
|
// "Glow Classic" cli arguments
|
2024-07-09 01:33:49 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default %s)", viper.GetViper().ConfigFileUsed()))
|
2019-12-31 10:13:06 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&pager, "pager", "p", false, "display with pager")
|
2024-07-03 15:11:29 +00:00
|
|
|
rootCmd.Flags().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path")
|
2020-05-05 05:43:11 +00:00
|
|
|
rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width")
|
2020-09-07 22:31:54 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)")
|
2024-07-11 15:08:57 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&showLineNumbers, "line-numbers", "l", false, "show line numbers (TUI-mode only)")
|
2024-07-09 12:50:10 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output")
|
2020-11-20 20:13:43 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)")
|
2022-10-25 14:40:51 +00:00
|
|
|
_ = rootCmd.Flags().MarkHidden("mouse")
|
2020-04-23 22:05:38 +00:00
|
|
|
|
2020-10-23 04:24:03 +00:00
|
|
|
// Config bindings
|
|
|
|
_ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style"))
|
2020-10-23 06:46:59 +00:00
|
|
|
_ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
|
2024-07-03 15:11:29 +00:00
|
|
|
_ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
|
2020-11-20 20:13:43 +00:00
|
|
|
_ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse"))
|
2024-07-09 12:50:10 +00:00
|
|
|
_ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
|
2024-07-11 15:08:57 +00:00
|
|
|
_ = viper.BindPFlag("showLineNumbers", rootCmd.Flags().Lookup("line-numbers"))
|
|
|
|
_ = viper.BindPFlag("showAllFiles", rootCmd.Flags().Lookup("all"))
|
2024-07-09 12:50:10 +00:00
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
viper.SetDefault("style", glamour.AutoStyle)
|
2020-10-23 06:46:59 +00:00
|
|
|
viper.SetDefault("width", 0)
|
2020-10-23 04:24:03 +00:00
|
|
|
|
2024-07-03 16:59:11 +00:00
|
|
|
rootCmd.AddCommand(configCmd, manCmd)
|
2020-10-23 04:24:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
func tryLoadConfigFromDefaultPlaces() {
|
|
|
|
scope := gap.NewScope(gap.User, "glow")
|
|
|
|
dirs, err := scope.ConfigDirs()
|
|
|
|
if err != nil {
|
2024-07-09 13:17:18 +00:00
|
|
|
fmt.Println("Could not load find configuration directory.")
|
2024-07-03 15:11:29 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-23 04:24:03 +00:00
|
|
|
|
2024-07-09 13:47:44 +00:00
|
|
|
if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
|
|
|
|
dirs = append([]string{filepath.Join(c, "glow")}, dirs...)
|
2024-07-09 01:33:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 13:47:44 +00:00
|
|
|
if c := os.Getenv("GLOW_CONFIG_HOME"); c != "" {
|
|
|
|
dirs = append([]string{c}, dirs...)
|
2024-07-09 01:33:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
for _, v := range dirs {
|
|
|
|
viper.AddConfigPath(v)
|
2020-10-23 04:24:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
viper.SetConfigName("glow")
|
|
|
|
viper.SetConfigType("yaml")
|
2020-10-23 04:24:03 +00:00
|
|
|
viper.SetEnvPrefix("glow")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
2024-07-03 15:11:29 +00:00
|
|
|
log.Warn("Could not parse configuration file", "err", err)
|
2020-10-23 04:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:11:29 +00:00
|
|
|
if used := viper.ConfigFileUsed(); used != "" {
|
|
|
|
log.Debug("Using configuration file", "path", viper.ConfigFileUsed())
|
2024-07-09 13:17:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-09 13:47:44 +00:00
|
|
|
if viper.ConfigFileUsed() == "" {
|
|
|
|
configFile = filepath.Join(dirs[0], "glow.yml")
|
|
|
|
}
|
2024-07-09 13:17:18 +00:00
|
|
|
if err := ensureConfigFile(); err != nil {
|
2024-07-09 13:47:44 +00:00
|
|
|
log.Error("Could not create default configuration", "error", err)
|
2024-07-03 15:11:29 +00:00
|
|
|
}
|
2019-11-04 23:17:36 +00:00
|
|
|
}
|