diff --git a/cli/edit_test.go b/cli/edit_test.go index fcf1026..fb3bbf8 100644 --- a/cli/edit_test.go +++ b/cli/edit_test.go @@ -123,7 +123,7 @@ func TestEditArgs(t *testing.T) { t.Run(testItem.name, func(t *testing.T) { if testItem.provided != nil { var whenString string - if (testItem.provided.When == time.Time{}) { + if (testItem.provided.When.Equal(time.Time{})) { whenString = helpers.TimeFormat(time.Now()) } else { whenString = helpers.TimeFormat(testItem.provided.When) diff --git a/cli/print.go b/cli/print.go index 41e19c3..04f5727 100644 --- a/cli/print.go +++ b/cli/print.go @@ -198,12 +198,12 @@ func init() { // verifySingleFormat ensures that there is only 1 output format used. func verifySingleFormat() { if !printOutputPretty && !printOutputYAML && !printOutputJSON { - defaultFormat := viper.GetString("default.format") - if defaultFormat == "yaml" || defaultFormat == "yml" { + switch viper.GetString("default.format") { + case "yaml", "yml": printOutputYAML = true - } else if defaultFormat == "json" { + case "json": printOutputJSON = true - } else { + default: printOutputPretty = true } } else { diff --git a/model/work.go b/model/work.go index 9243c13..c6433ed 100644 --- a/model/work.go +++ b/model/work.go @@ -43,7 +43,7 @@ type prettyWork struct { // NewWork is the generator for work. func NewWork(title, description, author string, duration int, tags []string, when time.Time) *Work { now, _ := helpers.GetStringAsDateTime(helpers.TimeFormat(time.Now())) - if (when == time.Time{}) { + if (when.Equal(time.Time{})) { when = now } sort.Strings(tags) diff --git a/model/work_test.go b/model/work_test.go index 19dbb2d..757cf3d 100644 --- a/model/work_test.go +++ b/model/work_test.go @@ -128,7 +128,7 @@ func TestNewWork(t *testing.T) { // Instead of mocking ID and time.Now(), just set the // result of it to the expected value - if (testItem.wWhen == time.Time{}) { + if (testItem.wWhen.Equal(time.Time{})) { assert.Equal(t, actual.When, actual.CreatedAt) assert.Equal(t, actual.WhenQueryEpoch, actual.CreatedAt.Unix()) diff --git a/repository/bboltRepo.go b/repository/bboltRepo.go index 24bdb8e..7a11e4f 100644 --- a/repository/bboltRepo.go +++ b/repository/bboltRepo.go @@ -34,7 +34,9 @@ func (*bboltRepo) Init() error { helpers.LogError(fmt.Sprintf("Error opening file: %s", openErr.Error()), "read db error - bolt") return openErr } - defer db.Close() + defer func() { + _ = db.Close() + }() viewErr := db.Select( q.Eq("WhenQueryEpoch", 0)).Find(&foundWls) @@ -68,7 +70,9 @@ func (*bboltRepo) Save(wl *model.Work) error { helpers.LogError(fmt.Sprintf("Error opening file: %s", openErr.Error()), "read db error - bolt") return openErr } - defer db.Close() + defer func() { + _ = db.Close() + }() helpers.LogDebug("Saving file...", "save model - bolt") if err := db.Save(wl); err != nil { @@ -87,7 +91,9 @@ func (*bboltRepo) GetAllBetweenDates(startDate, endDate time.Time, filter *model helpers.LogError(fmt.Sprintf("Error opening file: %s", openErr.Error()), "read db error - bolt") return nil, openErr } - defer db.Close() + defer func() { + _ = db.Close() + }() sel := q.And( q.Gte("WhenQueryEpoch", startDate.Unix()), @@ -119,7 +125,9 @@ func (*bboltRepo) GetByID(ID string, filter *model.Work) (*model.Work, error) { helpers.LogError(fmt.Sprintf("Error opening file: %s", openErr.Error()), "read db error - bolt") return nil, openErr } - defer db.Close() + defer func() { + _ = db.Close() + }() sel := q.And( q.Re("ID", helpers.RegexCaseInsensitive+ID), @@ -146,7 +154,9 @@ func (*bboltRepo) GetAll() ([]*model.Work, error) { helpers.LogError(fmt.Sprintf("Error opening file: %s", openErr.Error()), "read db error - bolt") return nil, openErr } - defer db.Close() + defer func() { + _ = db.Close() + }() err := db.All(&all) return all, err diff --git a/repository/yamlFileRepo.go b/repository/yamlFileRepo.go index 322d169..a8d9190 100644 --- a/repository/yamlFileRepo.go +++ b/repository/yamlFileRepo.go @@ -3,7 +3,6 @@ package repository import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -281,7 +280,7 @@ func getFileByID(ID string) (string, error) { } func parseFileToWork(filePath string) (*model.Work, error) { - yamlFile, err := ioutil.ReadFile(filepath.Clean(filePath)) + yamlFile, err := os.ReadFile(filepath.Clean(filePath)) if err != nil { return nil, fmt.Errorf("%s %s. %e", e.RepoGetFilesRead, filePath, err) } diff --git a/server/create.go b/server/create.go index 4e0c71e..f27502e 100644 --- a/server/create.go +++ b/server/create.go @@ -20,7 +20,9 @@ func Create(resp http.ResponseWriter, req *http.Request) { } // #nosec CWE-703 -- From my understanding, IO errors can occur, which are potentially an issue during file IO. // I haven't seen a similar example of harm to a network IO so will ignore for now. - defer req.Body.Close() + defer func() { + _ = req.Body.Close() + }() wl := model.NewWork( body.Title, diff --git a/server/edit.go b/server/edit.go index e1ef640..e64e893 100644 --- a/server/edit.go +++ b/server/edit.go @@ -22,7 +22,9 @@ func Edit(resp http.ResponseWriter, req *http.Request) { } // #nosec CWE-703 -- From my understanding, IO errors can occur, which are potentially an issue during file IO. // I haven't seen a similar example of harm to a network IO so will ignore for now. - defer req.Body.Close() + defer func() { + _ = req.Body.Close() + }() newWl := model.NewWork( body.Title, diff --git a/server/root.go b/server/root.go index c6d4ef5..cee7d59 100644 --- a/server/root.go +++ b/server/root.go @@ -31,7 +31,6 @@ var ( httpRouter *mux.Router = mux.NewRouter().StrictSlash(true) wlService service.WorklogService wlRepo repository.WorklogRepository - wlConfig repository.ConfigRepository ) var ( @@ -110,8 +109,8 @@ func interruptAndExit(server *http.Server) { helpers.LogInfo("server stopping...", "shutdown") ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() - // #nosec G104 -- There could be an error while shutting down, but its during shutdown - server.Shutdown(ctx) + + _ = server.Shutdown(ctx) } func InitCobra() { @@ -175,7 +174,5 @@ func InitConfig() { os.Exit(e.StartupErrors) } - wlConfig = repository.NewYamlConfig( - filepath.Dir(cfgFile)) wlService = service.NewWorklogService(wlRepo) } diff --git a/service/worklogService.go b/service/worklogService.go index c4924b4..64956f9 100644 --- a/service/worklogService.go +++ b/service/worklogService.go @@ -3,8 +3,8 @@ package service import ( "encoding/json" "fmt" - "io/ioutil" "net/http" + "os" "sort" "strings" "time" @@ -83,7 +83,7 @@ func (*service) GetWorklogsBetween(start, end time.Time, filter *model.Work) ([] var worklogs model.WorkList var err error - if (end == time.Time{}) { + if (end.Equal(time.Time{})) { end = time.Date(3000, time.January, 1, 0, 0, 0, 0, time.Now().Location()) } @@ -133,7 +133,7 @@ func (*service) ExportTo(path string) (int, error) { } // #nosec G306 -- Not concerned that others on machine can access the exported values // if the user wants to update permissions later, they can - err = ioutil.WriteFile(path, data, 0644) + err = os.WriteFile(path, data, 0644) if err != nil { return http.StatusInternalServerError, fmt.Errorf("error saving file. %s", err.Error()) }