Skip to content

Commit 025ec13

Browse files
fix: Improved error logging and updated Cobra to v1.0.0
1 parent b6532b2 commit 025ec13

File tree

8 files changed

+256
-31
lines changed

8 files changed

+256
-31
lines changed

cmd/categories.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/jedib0t/go-pretty/table"
99
"github.com/karl-cardenas-coding/disaster-cli/library"
10+
log "github.com/sirupsen/logrus"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -59,7 +60,14 @@ The returned category may be used with the --filter flag for the event cmd.`,
5960
if outputFlag == "json" {
6061
json, err := json.Marshal(records)
6162
if err != nil {
62-
fmt.Println(err)
63+
log.WithFields(log.Fields{
64+
"package": "cmd",
65+
"file": "categories.go",
66+
"parent_function": "Run",
67+
"function": "json.Marshal",
68+
"error": err,
69+
"data": fmt.Sprint(cmd, "./documentation/"),
70+
}).Error("Error marshaling JSON", ISSUE_MSG)
6371
}
6472
os.Stdout.Write(json)
6573
}

cmd/disaster.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"log"
64
"os"
75

6+
log "github.com/sirupsen/logrus"
7+
88
"github.com/spf13/cobra"
99
"github.com/spf13/cobra/doc"
1010
)
@@ -19,6 +19,10 @@ var (
1919
DownloadTempPath string
2020
)
2121

22+
const (
23+
ISSUE_MSG = " Please open up a Github issue to report this error! https://github.com/karl-cardenas-coding/disaster-cli"
24+
)
25+
2226
var rootCmd = &cobra.Command{
2327
Use: "disaster",
2428
Short: "A CLI tool for determining natural catastrophe near you, or a location specified",
@@ -28,13 +32,27 @@ var rootCmd = &cobra.Command{
2832
if generateDocFlag {
2933
err := doc.GenMarkdownTree(cmd, "./documentation/")
3034
if err != nil {
31-
log.Fatal(err)
35+
log.WithFields(log.Fields{
36+
"package": "cmd",
37+
"file": "disaster.go",
38+
"parent_function": "generateDocFlag",
39+
"function": "doc.GenMarkdownTree"
40+
"error": err,
41+
"data": fmt.Sprint(cmd, "./documentation/"),
42+
}).Fatal("Error generating markdown content", ISSUE_MSG)
3243
}
3344
}
3445

3546
err := cmd.Help()
3647
if err != nil {
37-
log.Fatal(err)
48+
log.WithFields(log.Fields{
49+
"package": "cmd",
50+
"file": "disaster.go",
51+
"parent_function": "generateDocFlag",
52+
"function": "cmd.Help",
53+
"error": err,
54+
"data": nil,
55+
}).Fatal("Error outputting help!", ISSUE_MSG)
3856
}
3957
},
4058
}
@@ -46,11 +64,26 @@ func init() {
4664
eventsCmd.Flags().BoolVarP(&DisplayMapFlag, "display-map", "d", false, "Displays the Google Maps URL")
4765
eventsCmd.Flags().StringSliceVarP(&FiltersFlag, "filter", "f", []string{}, "filter events by passing in categories (comma seperated")
4866
updateCmd.Flags().StringVarP(&DownloadTempPath, "temp-location", "l", "", "Specify the temporary directory to use for the update process")
67+
68+
// Establish logging default
69+
log.SetFormatter(&log.TextFormatter{
70+
DisableColors: false,
71+
FullTimestamp: true,
72+
})
73+
log.SetOutput(os.Stdout)
74+
75+
log.SetLevel(log.WarnLevel)
4976
}
5077

5178
func Execute() {
5279
if err := rootCmd.Execute(); err != nil {
53-
fmt.Println(err)
80+
log.WithFields(log.Fields{
81+
"package": "cmd",
82+
"file": "disaster.go",
83+
"function": "Execute",
84+
"error": err,
85+
"data": nil,
86+
}).Fatal("Error executing the CLI!", ISSUE_MSG)
5487
os.Exit(1)
5588
}
5689
}

cmd/events.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/jedib0t/go-pretty/table"
99
"github.com/karl-cardenas-coding/disaster-cli/library"
10+
log "github.com/sirupsen/logrus"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -75,7 +76,14 @@ func outputJSON(records library.EventResponse, filters []string) {
7576

7677
json, err := json.Marshal(&list)
7778
if err != nil {
78-
fmt.Println(err)
79+
log.WithFields(log.Fields{
80+
"package": "cmd",
81+
"file": "events.go",
82+
"parent_function": "outputJSON",
83+
"function": "json.Marshal",
84+
"error": err,
85+
"data": list,
86+
}).Error("Error marshalling JSON", ISSUE_MSG)
7987
}
8088
os.Stdout.Write(json)
8189

@@ -85,7 +93,14 @@ func outputJSON(records library.EventResponse, filters []string) {
8593

8694
json, err := json.Marshal(&records)
8795
if err != nil {
88-
fmt.Println(err)
96+
log.WithFields(log.Fields{
97+
"package": "cmd",
98+
"file": "events.go",
99+
"parent_function": "outputJSON",
100+
"function": "json.Marshal",
101+
"error": err,
102+
"data": records,
103+
}).Error("Error marshalling JSON", ISSUE_MSG)
89104
}
90105
os.Stdout.Write(json)
91106

0 commit comments

Comments
 (0)