Skip to content

Commit

Permalink
fix bad error formating, usage of io/utils...
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Apr 25, 2023
1 parent 1779151 commit c7e3db8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func mainEnd() {

// If we terminate with Ctrl/Cmd-C we call end()
func catchCtrlC() {
c := make(chan os.Signal)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
Expand Down
5 changes: 2 additions & 3 deletions gm_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -27,7 +26,7 @@ func buildMd(infile string) {
dir = "."
}
// read the input
markdown, err := ioutil.ReadAll(input)
markdown, err := io.ReadAll(input)
check(err, "Problem reading the markdown.")

//compile the input
Expand All @@ -45,7 +44,7 @@ func buildMd(infile string) {
if os.MkdirAll(filepath.Dir(outfile), os.ModePerm) != nil {
check(err, "Problem to reach/create folder:", filepath.Dir(outfile))
}
err = ioutil.WriteFile(outfile, html, 0644)
err = os.WriteFile(outfile, html, 0644)
check(err, "Problem modifying", outfile)
}
}
Expand Down
4 changes: 2 additions & 2 deletions gm_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func compile(markdown []byte) (html []byte, err error) {
// convert md to html code
err = mdParser.Convert(markdown, &htmlBuf)
if err != nil {
return nil, fmt.Errorf("Problem parsing markdown code to html with goldmark.\n %w", err)
return nil, fmt.Errorf("problem parsing markdown code to html with goldmark: %w", err)
}

// combine the template and the resulting
Expand All @@ -49,7 +49,7 @@ func compile(markdown []byte) (html []byte, err error) {
htmlBuf.Reset()
err = mdTemplate.Execute(&htmlBuf, data)
if err != nil {
return nil, fmt.Errorf("Problem building HTML from template.\n %w", err)
return nil, fmt.Errorf("problem building HTML from template: %w", err)
}

return htmlBuf.Bytes(), nil
Expand Down
14 changes: 6 additions & 8 deletions gm_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -157,7 +156,7 @@ func SetParameters() {
css = "https://kpym.github.io/markdown-css/" + css + ".min.css"
}
// set the template
t, err := ioutil.ReadFile(htmlshell)
t, err := os.ReadFile(htmlshell)
if err == nil {
htmlshell = string(t)
}
Expand All @@ -180,7 +179,7 @@ func SetParameters() {
// if the positional parameter is like `path/folder/` then `path/folder` is served and `/` is requested
func setServeParameters() {
if pflag.NArg() > 1 {
check(errors.New("Only one file or folder can be specified for serving."))
check(errors.New("only one file or folder can be specified for serving"))
}

filename := "."
Expand All @@ -197,7 +196,7 @@ func setServeParameters() {
serveDir = filepath.Dir(filename)
serveFile = filepath.Base(filename)
default:
check(fmt.Errorf("The specified path '%s'is not a file or folder.", filename))
check(fmt.Errorf("the specified path '%s'is not a file or folder", filename))
}

// set the default title
Expand All @@ -216,20 +215,19 @@ func setBuildParameters() {
// check for positional parameters
if len(inpatterns) == 0 {
pflag.Usage()
check(errors.New("At least one input 'file.md', 'p*ttern' or 'stdin' should be provided."))
check(errors.New("at least one input 'file.md', 'p*ttern' or 'stdin' should be provided"))
}

// check the "out dir"
if outdir != "" {
if os.MkdirAll(outdir, os.ModePerm) != nil {
check(fmt.Errorf("The specified output folder '%s' is not reachable.", outdir))
check(fmt.Errorf("the specified output folder '%s' is not reachable", outdir))
}
}
}

// setGoldMark creates a new markdown parser with configuration based on the parameter flags.
// The code is borrowed from:
// https://github.com/gohugoio/hugo/blob/d90e37e0c6e812f9913bf256c9c81aa05b7a08aa/markup/goldmark/convert.go
// The code is borrowed from: https://github.com/gohugoio/hugo/blob/d90e37e0c6e812f9913bf256c9c81aa05b7a08aa/markup/goldmark/convert.go
func setGoldMark() {
var (
rendererOptions []renderer.Option
Expand Down
5 changes: 2 additions & 3 deletions gm_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -86,7 +85,7 @@ func serveFiles() {
}
return
}
if content, err := ioutil.ReadFile(filename); err == nil {
if content, err := os.ReadFile(filename); err == nil {
if html, err := compile(content); err == nil {
info(" serve converted .md file.")
w.Write(html)
Expand Down Expand Up @@ -122,7 +121,7 @@ func serveFiles() {
<-ticker.C
if exit.isYes() {
info("\nNo request for 2 seconds. Exit.\n\n")
os.Exit(0)
mainEnd()
}
exit.yes() // should be rest to no by the next request in less than 2 seconds
}
Expand Down

0 comments on commit c7e3db8

Please sign in to comment.