Expand the path for a specified style file

This commit is contained in:
Nicolas Martin 2020-12-16 16:23:58 +01:00 committed by Nicolas M
parent 753a7c6993
commit d9242c9a72
3 changed files with 32 additions and 4 deletions

1
go.mod
View file

@ -14,6 +14,7 @@ require (
github.com/mattn/go-runewidth v0.0.9
github.com/meowgorithm/babyenv v1.3.0
github.com/microcosm-cc/bluemonday v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/gitcha v0.1.2-0.20200908172931-5aa4fdccf2f6
github.com/muesli/go-app-paths v0.2.1
github.com/muesli/reflow v0.2.0

19
main.go
View file

@ -135,13 +135,23 @@ func sourceFromArg(arg string) (*source, error) {
return &source{r, u}, err
}
func validateOptions(cmd *cobra.Command) {
func validateOptions(cmd *cobra.Command) error {
// grab config values from Viper
style = viper.GetString("style")
width = viper.GetUint("width")
localOnly = viper.GetBool("local")
mouse = viper.GetBool("mouse")
// validate the glamour style
style = viper.GetString("style")
if style != "auto" && glamour.DefaultStyles[style] == nil {
style = utils.ExpandPath(style)
if _, err := os.Stat(style); os.IsNotExist(err) {
return fmt.Errorf("Specified style does not exist: %s", style)
} else if err != nil {
return err
}
}
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
// We want to use a special no-TTY style, when stdout is not a terminal
// and there was no specific style passed by arg
@ -163,11 +173,14 @@ func validateOptions(cmd *cobra.Command) {
if width == 0 {
width = 80
}
return nil
}
func execute(cmd *cobra.Command, args []string) error {
initConfig()
validateOptions(cmd)
if err := validateOptions(cmd); err != nil {
return err
}
if len(args) == 0 {
return executeArg(cmd, "", os.Stdout)

View file

@ -1,6 +1,11 @@
package utils
import "regexp"
import (
"os"
"regexp"
"github.com/mitchellh/go-homedir"
)
func RemoveFrontmatter(content []byte) []byte {
if frontmatterBoundaries := detectFrontmatter(content); frontmatterBoundaries[0] == 0 {
@ -17,3 +22,12 @@ func detectFrontmatter(c []byte) []int {
}
return []int{-1, -1}
}
// Expands tilde and all environment variables from the given path.
func ExpandPath(path string) string {
s, err := homedir.Expand(path)
if err == nil {
return os.ExpandEnv(s)
}
return os.ExpandEnv(path)
}