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"
|
2019-11-09 22:12:54 +00:00
|
|
|
"io/ioutil"
|
2019-11-22 13:06:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-11-09 20:07:01 +00:00
|
|
|
"os"
|
2019-11-04 23:17:36 +00:00
|
|
|
|
2019-11-25 03:25:58 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
2019-11-25 09:03:33 +00:00
|
|
|
"github.com/muesli/go-wordwrap"
|
2019-11-22 03:44:54 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-11-15 18:35:04 +00:00
|
|
|
"github.com/charmbracelet/gold"
|
2019-11-04 23:17:36 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
var (
|
2019-11-24 04:06:01 +00:00
|
|
|
readmeNames = []string{"README.md", "README"}
|
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "gold SOURCE",
|
|
|
|
Short: "Render markdown on the CLI, with pizzazz!",
|
|
|
|
SilenceErrors: false,
|
|
|
|
SilenceUsage: false,
|
2019-11-24 01:40:29 +00:00
|
|
|
RunE: execute,
|
2019-11-22 03:44:54 +00:00
|
|
|
}
|
2019-11-25 05:42:34 +00:00
|
|
|
|
2019-11-22 03:44:54 +00:00
|
|
|
style string
|
2019-11-25 05:42:34 +00:00
|
|
|
width uint
|
2019-11-22 03:44:54 +00:00
|
|
|
)
|
|
|
|
|
2019-11-24 01:40:29 +00:00
|
|
|
func readerFromArg(s string) (io.ReadCloser, error) {
|
2019-11-22 02:28:26 +00:00
|
|
|
if s == "-" {
|
|
|
|
return os.Stdin, nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:55:09 +00:00
|
|
|
if u, ok := isGitHubURL(s); ok {
|
|
|
|
resp, err := findGitHubREADME(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2019-11-25 05:55:50 +00:00
|
|
|
if u, ok := isGitLabURL(s); ok {
|
|
|
|
resp, err := findGitLabREADME(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2019-11-25 03:55:09 +00:00
|
|
|
|
2019-11-22 13:06:12 +00:00
|
|
|
if u, err := url.ParseRequestURI(s); err == nil {
|
2019-11-25 04:14:25 +00:00
|
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
2019-11-22 13:06:12 +00:00
|
|
|
return nil, fmt.Errorf("%s is not a supported protocol", u.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(u.String())
|
2019-11-22 02:28:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-22 13:06:12 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
|
|
|
|
}
|
2019-11-22 02:28:26 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2019-11-24 04:06:01 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
for _, v := range readmeNames {
|
|
|
|
r, err := os.Open(v)
|
|
|
|
if err == nil {
|
2019-11-25 04:14:25 +00:00
|
|
|
return r, nil
|
2019-11-24 04:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("missing markdown source")
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:28:26 +00:00
|
|
|
return os.Open(s)
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:40:29 +00:00
|
|
|
func execute(cmd *cobra.Command, args []string) error {
|
2019-11-24 04:06:01 +00:00
|
|
|
var arg string
|
|
|
|
if len(args) > 0 {
|
|
|
|
arg = args[0]
|
2019-11-09 22:12:54 +00:00
|
|
|
}
|
2019-11-24 04:06:01 +00:00
|
|
|
in, err := readerFromArg(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
|
|
|
}
|
2019-11-22 02:28:26 +00:00
|
|
|
defer in.Close()
|
2019-11-22 02:52:21 +00:00
|
|
|
b, _ := ioutil.ReadAll(in)
|
2019-11-25 03:25:58 +00:00
|
|
|
|
|
|
|
r := gold.NewPlainTermRenderer()
|
|
|
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
r, err = gold.NewTermRenderer(style)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-09 20:07:01 +00:00
|
|
|
}
|
2019-11-25 03:25:58 +00:00
|
|
|
|
2019-11-24 19:35:01 +00:00
|
|
|
out := r.RenderBytes(b)
|
2019-11-25 05:42:34 +00:00
|
|
|
fmt.Printf("%s", wordwrap.WrapString(string(out), width))
|
2019-11-22 03:44:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.Flags().StringVarP(&style, "style", "s", "dark.json", "style JSON path")
|
2019-11-25 05:42:34 +00:00
|
|
|
rootCmd.Flags().UintVarP(&width, "width", "w", 100, "word-wrap at width")
|
2019-11-04 23:17:36 +00:00
|
|
|
}
|