Skip to content

Commit 4a4eec4

Browse files
committed
chore: refactor error messages, change the logic of internal code
1 parent 279f3d9 commit 4a4eec4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

pkg/toc/toc.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,30 @@ func Run() {
4545
}
4646

4747
func (t *toc) logic() {
48+
red := color.New(color.FgRed, color.Bold).PrintlnFunc()
49+
4850
resp, err := t.readFile()
4951
if err != nil {
50-
color.Red(err.Error())
52+
red("ERROR: " + err.Error())
5153
os.Exit(1)
5254
}
5355

5456
err = t.parseHTML(resp)
5557
if err != nil {
56-
color.Red(err.Error())
58+
red("ERROR: " + err.Error())
5759
os.Exit(1)
5860
}
5961

60-
if t.Options.Append == true {
61-
if err = t.writeToFile(string(resp)); err != nil {
62-
color.Red(err.Error())
63-
os.Exit(1)
64-
}
65-
color.Green("✔ Table of contents generated successfully")
66-
} else {
62+
if t.Options.Append != true {
6763
fmt.Print(t.String())
64+
return
65+
}
66+
67+
if err = t.writeToFile(string(resp)); err != nil {
68+
red("ERROR: " + err.Error())
69+
os.Exit(1)
6870
}
71+
color.Green("✔ Table of contents generated successfully")
6972

7073
}
7174

@@ -140,7 +143,7 @@ func (t *toc) add(content string) {
140143

141144
func (t *toc) readFile() ([]byte, error) {
142145
if _, err := os.Stat(t.Options.Path); os.IsNotExist(err) {
143-
return nil, fmt.Errorf(fmt.Sprintf("path (%s) doesn't exists", t.Options.Path))
146+
return nil, fmt.Errorf(fmt.Sprintf("path '%s' doesn't exists", t.Options.Path))
144147
}
145148

146149
file, err := ioutil.ReadFile(t.Options.Path)

0 commit comments

Comments
 (0)