diff --git a/.drone.yml b/.drone.yml index f9b4154..cee9aee 100644 --- a/.drone.yml +++ b/.drone.yml @@ -61,9 +61,7 @@ steps: commands: - cp .infomark.yml.ci .infomark.yml - export INFOMARK_CONFIG_DIR=`pwd` - - - - go test ./api/app -cover -v --goblin.timeout 15s - - go test ./api/helper -cover -v + - go test ./... -cover -v --goblin.timeout 15s environment: GOPROXY: https://gomods.patwie.com/ diff --git a/api/app/account.go b/api/app/account.go index 6cecf56..8778a5a 100644 --- a/api/app/account.go +++ b/api/app/account.go @@ -348,6 +348,10 @@ func (rs *AccountResource) GetEnrollmentsHandler(w http.ResponseWriter, r *http. // get enrollments enrollments, err := rs.Stores.User.GetEnrollments(accessClaims.LoginID) + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } // render JSON reponse if err = render.RenderList(w, r, rs.newUserEnrollmentsResponse(enrollments)); err != nil { diff --git a/api/app/account_test.go b/api/app/account_test.go index e18dba4..12de247 100644 --- a/api/app/account_test.go +++ b/api/app/account_test.go @@ -302,8 +302,8 @@ func TestAccount(t *testing.T) { g.Assert(err).Equal(nil) g.Assert(w.Code).Equal(http.StatusOK) - if !strings.HasSuffix(w.HeaderMap["Content-Type"][0], "jpeg") { - g.Assert(strings.HasSuffix(w.HeaderMap["Content-Type"][0], "jpg")).Equal(true) + if !strings.HasSuffix(w.Header().Get("Content-Type"), "jpeg") { + g.Assert(strings.HasSuffix(w.Header().Get("Content-Type"), "jpg")).Equal(true) } }) @@ -344,7 +344,7 @@ func TestAccount(t *testing.T) { w = tape.GetWithClaims("/api/v1/account/avatar", 1, true) g.Assert(err).Equal(nil) g.Assert(w.Code).Equal(http.StatusOK) - g.Assert(strings.HasSuffix(w.HeaderMap["Content-Type"][0], "png")).Equal(true) + g.Assert(strings.HasSuffix(w.Header().Get("Content-Type"), "png")).Equal(true) }) diff --git a/api/app/api.go b/api/app/api.go index 51f04af..abd527d 100644 --- a/api/app/api.go +++ b/api/app/api.go @@ -25,13 +25,6 @@ import ( "github.com/jmoiron/sqlx" ) -type ctxKey int - -const ( - ctxAccount ctxKey = iota - ctxProfile -) - // UserStore defines user related database queries type UserStore interface { Get(userID int64) (*model.User, error) diff --git a/api/app/course.go b/api/app/course.go index 0e98a77..ee25d9b 100644 --- a/api/app/course.go +++ b/api/app/course.go @@ -59,6 +59,10 @@ func NewCourseResource(stores *Stores) *CourseResource { func (rs *CourseResource) IndexHandler(w http.ResponseWriter, r *http.Request) { // fetch collection of courses from database courses, err := rs.Stores.Course.GetAll() + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } // render JSON reponse if err = render.RenderList(w, r, rs.newCourseListResponse(courses)); err != nil { diff --git a/api/app/course_test.go b/api/app/course_test.go index 056e5e4..469d5f5 100644 --- a/api/app/course_test.go +++ b/api/app/course_test.go @@ -144,6 +144,7 @@ func TestCourse(t *testing.T) { enrollmentsExpected, err := stores.Course.FindEnrolledUsers(courseActive.ID, []string{"0", "1", "2"}, "%chi%", ) + g.Assert(err).Equal(nil) w := tape.GetWithClaims("/api/v1/courses/1/enrollments?q=chi", 1, false) enrollmentsActual := []enrollmentResponse{} @@ -264,6 +265,7 @@ func TestCourse(t *testing.T) { // verify body courseReturn := &courseResponse{} err = json.NewDecoder(w.Body).Decode(&courseReturn) + g.Assert(err).Equal(nil) g.Assert(courseReturn.Name).Equal(entrySent.Name) g.Assert(courseReturn.Description).Equal(entrySent.Description) g.Assert(courseReturn.BeginsAt.Equal(entrySent.BeginsAt)).Equal(true) diff --git a/api/app/grade_test.go b/api/app/grade_test.go index e4912a2..d1f4ede 100644 --- a/api/app/grade_test.go +++ b/api/app/grade_test.go @@ -496,6 +496,7 @@ func TestGrade(t *testing.T) { PublicDockerImage: null.StringFrom("ff"), PrivateDockerImage: null.StringFrom("ff"), }, sheet1.ID) + g.Assert(err).Equal(nil) task2, err := stores.Task.Create(&model.Task{ Name: "2", @@ -503,9 +504,7 @@ func TestGrade(t *testing.T) { PublicDockerImage: null.StringFrom("ff"), PrivateDockerImage: null.StringFrom("ff"), }, sheet2.ID) - - _ = task1 - _ = task2 + g.Assert(err).Equal(nil) uid1 := int64(42) uid2 := int64(43) diff --git a/api/app/group.go b/api/app/group.go index 3fe05ac..75d8e87 100644 --- a/api/app/group.go +++ b/api/app/group.go @@ -68,6 +68,10 @@ func (rs *GroupResource) IndexHandler(w http.ResponseWriter, r *http.Request) { course := r.Context().Value(common.CtxKeyCourse).(*model.Course) groups, err = rs.Stores.Group.GroupsOfCourse(course.ID) + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } // render JSON reponse if err = render.RenderList(w, r, rs.newGroupListResponse(groups)); err != nil { diff --git a/api/app/group_test.go b/api/app/group_test.go index c60cf6f..445edca 100644 --- a/api/app/group_test.go +++ b/api/app/group_test.go @@ -119,6 +119,7 @@ func TestGroup(t *testing.T) { entryReturn := &GroupResponse{} err = json.NewDecoder(w.Body).Decode(&entryReturn) + g.Assert(err).Equal(nil) g.Assert(entryReturn.Tutor.ID).Equal(tutorID) g.Assert(entryReturn.CourseID).Equal(int64(1)) g.Assert(entryReturn.Description).Equal("blah blahe") diff --git a/api/app/material_test.go b/api/app/material_test.go index 2841dd3..f43fbf6 100644 --- a/api/app/material_test.go +++ b/api/app/material_test.go @@ -309,6 +309,7 @@ func TestMaterial(t *testing.T) { // tutors w, err = tape.UploadWithClaims("/api/v1/courses/1/materials/1/file", filename, "application/zip", 2, false) + g.Assert(err).Equal(nil) g.Assert(w.Code).Equal(http.StatusForbidden) // admin @@ -339,13 +340,13 @@ func TestMaterial(t *testing.T) { // a file should be now served w = tape.GetWithClaims("/api/v1/courses/1/materials/1/file", 1, true) - g.Assert(w.HeaderMap["Content-Type"][0]).Equal("application/zip") + g.Assert(w.Header().Get("Content-Type")).Equal("application/zip") g.Assert(w.Code).Equal(http.StatusOK) course, err := stores.Material.IdentifyCourseOfMaterial(1) g.Assert(err).Equal(nil) - _, params, err := mime.ParseMediaType(w.HeaderMap["Content-Disposition"][0]) + _, params, err := mime.ParseMediaType(w.Header().Get("Content-Disposition")) g.Assert(err).Equal(nil) g.Assert(params["filename"]).Equal(fmt.Sprintf("%s-empty.zip", course.Name)) }) @@ -364,13 +365,13 @@ func TestMaterial(t *testing.T) { // a file should be now served w = tape.GetWithClaims("/api/v1/courses/1/materials/1/file", 1, true) - g.Assert(w.HeaderMap["Content-Type"][0]).Equal("application/pdf") + g.Assert(w.Header().Get("Content-Type")).Equal("application/pdf") g.Assert(w.Code).Equal(http.StatusOK) course, err := stores.Material.IdentifyCourseOfMaterial(1) g.Assert(err).Equal(nil) - _, params, err := mime.ParseMediaType(w.HeaderMap["Content-Disposition"][0]) + _, params, err := mime.ParseMediaType(w.Header().Get("Content-Disposition")) g.Assert(err).Equal(nil) g.Assert(params["filename"]).Equal(fmt.Sprintf("%s-empty.pdf", course.Name)) }) diff --git a/api/app/sheet.go b/api/app/sheet.go index 485da24..1a5c2d5 100644 --- a/api/app/sheet.go +++ b/api/app/sheet.go @@ -65,6 +65,10 @@ func (rs *SheetResource) IndexHandler(w http.ResponseWriter, r *http.Request) { course := r.Context().Value(common.CtxKeyCourse).(*model.Course) sheets, err = rs.Stores.Sheet.SheetsOfCourse(course.ID) + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } givenRole := r.Context().Value(common.CtxKeyCourseRole).(authorize.CourseRole) diff --git a/api/app/sheet_test.go b/api/app/sheet_test.go index df2ee51..369ccf5 100644 --- a/api/app/sheet_test.go +++ b/api/app/sheet_test.go @@ -170,6 +170,7 @@ func TestSheet(t *testing.T) { // tutors w, err = tape.UploadWithClaims("/api/v1/courses/1/sheets/1/file", filename, "application/zip", 2, false) + g.Assert(err).Equal(nil) g.Assert(w.Code).Equal(http.StatusForbidden) // admin diff --git a/api/app/task.go b/api/app/task.go index 510bd2e..bb4715c 100644 --- a/api/app/task.go +++ b/api/app/task.go @@ -63,6 +63,10 @@ func (rs *TaskResource) IndexHandler(w http.ResponseWriter, r *http.Request) { // we use middle to detect whether there is a sheet given sheet := r.Context().Value(common.CtxKeySheet).(*model.Sheet) tasks, err = rs.Stores.Task.TasksOfSheet(sheet.ID) + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } // render JSON reponse if err = render.RenderList(w, r, newTaskListResponse(tasks)); err != nil { diff --git a/api/app/task_test.go b/api/app/task_test.go index e4726e1..eabbe8f 100644 --- a/api/app/task_test.go +++ b/api/app/task_test.go @@ -129,6 +129,7 @@ func TestTask(t *testing.T) { taskReturn := &TaskResponse{} err = json.NewDecoder(w.Body).Decode(&taskReturn) + g.Assert(err).Equal(nil) g.Assert(taskReturn.Name).Equal("new Task") g.Assert(taskReturn.MaxPoints).Equal(88) g.Assert(taskReturn.PrivateDockerImage.Valid).Equal(true) diff --git a/api/app/user.go b/api/app/user.go index 196e745..624bb45 100644 --- a/api/app/user.go +++ b/api/app/user.go @@ -66,6 +66,10 @@ func (rs *UserResource) IndexHandler(w http.ResponseWriter, r *http.Request) { // fetch collection of users from database users, err := rs.Stores.User.GetAll() + if err != nil { + render.Render(w, r, ErrInternalServerErrorWithDetails(err)) + return + } // render JSON reponse if err = render.RenderList(w, r, newUserListResponse(users)); err != nil { diff --git a/api/cronjob/submission_zipper.go b/api/cronjob/submission_zipper.go index 8efdca2..8cdc90b 100644 --- a/api/cronjob/submission_zipper.go +++ b/api/cronjob/submission_zipper.go @@ -178,8 +178,6 @@ Please log in to grade these solutions. fmt.Println(" --> already done", sheet.ID) } - } else { - // fmt.Println("ok", sheet.ID) } } diff --git a/api/helper/file_carrier.go b/api/helper/file_carrier.go index 8ffcd51..a4e1b42 100644 --- a/api/helper/file_carrier.go +++ b/api/helper/file_carrier.go @@ -212,9 +212,14 @@ func FileExists(path string) bool { } // FileTouch creates an empty file -func FileTouch(path string) error { +func FileTouch(path string) (err error) { emptyFile, err := os.Create(path) - defer emptyFile.Close() + + defer func() { + if lerr := emptyFile.Close(); lerr != nil { + err = lerr + } + }() return err } diff --git a/api/server.go b/api/server.go index 27375fb..bbf7db4 100644 --- a/api/server.go +++ b/api/server.go @@ -122,7 +122,7 @@ func (srv *Server) Start() { log.Info("starting cronjob for zipping submissions...") srv.Cron.Start() - quit := make(chan os.Signal) + quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt) sig := <-quit log.Info("Shutting down server... Reason:", sig) diff --git a/api/worker.go b/api/worker.go index fbffba9..1d7a6cd 100644 --- a/api/worker.go +++ b/api/worker.go @@ -57,7 +57,7 @@ func (srv *Worker) Start() { go consumer.HandleLoop(deliveries) - quit := make(chan os.Signal) + quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt) sig := <-quit log.Println("Shutting down Worker... Reason:", sig) diff --git a/api/worker/submission_handler.go b/api/worker/submission_handler.go index 3b08659..07a7bd9 100644 --- a/api/worker/submission_handler.go +++ b/api/worker/submission_handler.go @@ -200,6 +200,11 @@ func (h *RealSubmissionHandler) Handle(body []byte) error { // 2. fetch submission file from server r, err := http.NewRequest("GET", msg.SubmissionFileURL, nil) + if err != nil { + DefaultLogger.Printf("error: %v\n", err) + return err + } + r.Header.Add("Authorization", "Bearer "+msg.AccessToken) if err := downloadFile(r, submissionPath); err != nil { DefaultLogger.Printf("error: %v\n", err) @@ -210,7 +215,12 @@ func (h *RealSubmissionHandler) Handle(body []byte) error { // 3. fetch framework file from server r, err = http.NewRequest("GET", msg.FrameworkFileURL, nil) + if err != nil { + DefaultLogger.Printf("error: %v\n", err) + return err + } r.Header.Add("Authorization", "Bearer "+msg.AccessToken) + if err := downloadFile(r, frameworkPath); err != nil { DefaultLogger.Printf("error: %v\n", err) return err diff --git a/auth/authenticate/middleware.go b/auth/authenticate/middleware.go index f95805c..b2c789d 100644 --- a/auth/authenticate/middleware.go +++ b/auth/authenticate/middleware.go @@ -92,8 +92,6 @@ func RequiredValidAccessClaims(next http.Handler) http.Handler { // serve next ctx := context.WithValue(r.Context(), common.CtxKeyAccessClaims, accessClaims) next.ServeHTTP(w, r.WithContext(ctx)) - return - }) } @@ -146,6 +144,10 @@ func NewLoginLimiter(prefix string, limit string, redisURL string) (*LoginLimite MaxRetry: 3, }) + if err != nil { + return nil, err + } + // store := memory.NewStore() return &LoginLimiter{Store: &store, Rate: &rate, Prefix: prefix}, nil @@ -182,7 +184,7 @@ func RateLimitMiddleware(prefix string, limit string, redisURL string) func(h ht context, err := ll.Get(r, keyFunc) if err != nil { - panic(err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } diff --git a/cmd/console.go b/cmd/console.go index 58ac574..73eedda 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -80,7 +80,7 @@ var UtilsCompletionCmd = &cobra.Command{ case "zsh": RootCmd.GenZshCompletion(os.Stdout) default: - log.Fatalln("Unknown shell %s, only bash and zsh are available\n", args[0]) + log.Fatalf("Unknown shell %s, only bash and zsh are available\n", args[0]) } }, } diff --git a/cmd/console/course_cmd.go b/cmd/console/course_cmd.go index f6e433e..8dab312 100644 --- a/cmd/console/course_cmd.go +++ b/cmd/console/course_cmd.go @@ -60,12 +60,12 @@ var UserEnrollInCourse = &cobra.Command{ user, err := stores.User.Get(userID) if err != nil { - log.Fatal("user with id %v not found\n", userID) + log.Fatalf("user with id %v not found\n", userID) } course, err := stores.Course.Get(courseID) if err != nil { - log.Fatal("user with id %v not found\n", userID) + log.Fatalf("user with id %v not found\n", userID) } if err := stores.Course.Enroll(course.ID, user.ID, role); err != nil { diff --git a/cmd/console/database_cmd.go b/cmd/console/database_cmd.go index 4795e79..d7a2820 100644 --- a/cmd/console/database_cmd.go +++ b/cmd/console/database_cmd.go @@ -166,7 +166,7 @@ var DatabaseRestoreCmd = &cobra.Command{ err = shell2.Wait() // io.Copy(os.Stdout, &b2) if err != nil { - log.Fatal("load db from was not successfull\n %s", err) + log.Fatalf("load db from was not successfull\n %s", err) } }, } @@ -211,6 +211,9 @@ var DatabaseBackupCmd = &cobra.Command{ shell1.Wait() w.Close() err := shell2.Wait() + if err != nil { + panic(err) + } destination, err := os.Create(file) if err != nil { @@ -219,7 +222,7 @@ var DatabaseBackupCmd = &cobra.Command{ defer destination.Close() _, err = io.Copy(destination, &b2) if err != nil { - log.Fatal("storing snapshot was not successfull\n %s", err) + log.Fatalf("storing snapshot was not successfull\n %s", err) } }, diff --git a/cmd/console/group_cmd.go b/cmd/console/group_cmd.go index a7ba48f..8d1f724 100644 --- a/cmd/console/group_cmd.go +++ b/cmd/console/group_cmd.go @@ -460,6 +460,7 @@ var GroupParseBidsSolution = &cobra.Command{ // we perform the update as a transaction tx, err := db.Begin() + failWhenSmallestWhiff(err) // delete assignments so far _, err = tx.Exec(` DELETE FROM diff --git a/database/oracle.go b/database/oracle.go index 0dabadb..9b71518 100644 --- a/database/oracle.go +++ b/database/oracle.go @@ -213,60 +213,26 @@ type StatementData struct { Value interface{} } -// // isZero tests whether the incoming value is the default value. -// // https://stackoverflow.com/a/27494761/7443104 -// func isZero2(v reflect.Value) bool { -// switch v.Kind() { -// case reflect.Func, reflect.Map, reflect.Slice: -// return v.IsNil() -// case reflect.Array: -// z := true -// for i := 0; i < v.Len(); i++ { -// z = z && isZero(v.Index(i)) -// } -// return z -// case reflect.Struct: -// z := true -// for i := 0; i < v.NumField(); i++ { -// if v.Field(i).CanSet() { -// fmt.Println("Field ", i, v.Field(i), isZero(v.Field(i))) -// z = z && isZero(v.Field(i)) -// } else { -// fmt.Println("Cannot set ", i, v.Field(i)) -// } -// } -// return z -// case reflect.Ptr: -// if !v.IsNil() { -// return isZero(reflect.Indirect(v)) -// } -// } -// // Compare other types directly: -// z := reflect.Zero(v.Type()) -// result := v.Interface() == z.Interface() - -// return result +// We switched from PATCH to PUT +// func isZero(value reflect.Value) bool { +// switch value.Kind() { +// case reflect.String: +// return value.Len() == 0 +// case reflect.Bool: +// return !value.Bool() +// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: +// return value.Int() == 0 +// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: +// return value.Uint() == 0 +// case reflect.Float32, reflect.Float64: +// return value.Float() == 0 +// case reflect.Interface, reflect.Ptr: +// return value.IsNil() +// } + +// return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface()) // } -func isZero(value reflect.Value) bool { - switch value.Kind() { - case reflect.String: - return value.Len() == 0 - case reflect.Bool: - return !value.Bool() - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return value.Int() == 0 - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return value.Uint() == 0 - case reflect.Float32, reflect.Float64: - return value.Float() == 0 - case reflect.Interface, reflect.Ptr: - return value.IsNil() - } - - return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface()) -} - // PackStatementData reads a struct and extract necessary data for a query. // This skips the primary key "id" automatically. No data modification is made. func (d *DatabaseSyntax) PackStatementData(src interface{}) ([]StatementData, error) { @@ -286,6 +252,9 @@ func (d *DatabaseSyntax) PackStatementData(src interface{}) ([]StatementData, er structVal := reflect.ValueOf(src).Elem() // structType := objectType.Elem() data, err := parseStruct(reflect.TypeOf(src)) + if err != nil { + return nil, err + } // structVal := reflect.ValueOf(src).Elem() for _, name := range columns { @@ -307,7 +276,7 @@ func (d *DatabaseSyntax) PackStatementData(src interface{}) ([]StatementData, er if reflect.TypeOf(null_string) == current_value.Type() { // sql.NUll // Valid is true if String is not NULL - if current_value.Field(0).Field(1).Bool() == true { + if current_value.Field(0).Field(1).Bool() { statementDatas = append(statementDatas, StatementData{ Column: name, Value: current_value.Field(0).Field(0).String(), @@ -329,8 +298,6 @@ func (d *DatabaseSyntax) PackStatementData(src interface{}) ([]StatementData, er Column: name, Value: current_value.Interface(), //current_value.Interface(), }) - } else { - // fmt.Println("field ", name, " is zero", current_value.Interface(), current_value.Kind()) // DEBUG } } @@ -365,8 +332,6 @@ func (d *DatabaseSyntax) PackStatementData(src interface{}) ([]StatementData, er // } - } else { - // fmt.Println("field ", name, " is NOT present") // DEBUG } } @@ -411,6 +376,9 @@ func (d *DatabaseSyntax) InsertStatement(table string, src interface{}) (string, // set "created_at" and "updated_at" current_time := time.Now() structInfo, err := parseStruct(reflect.TypeOf(src)) + if err != nil { + return "", nil, err + } _, present := structInfo.fields["created_at"] if present { // we will need to set created_at @@ -504,6 +472,9 @@ func (d *DatabaseSyntax) UpdateStatement(table string, id int64, src interface{} } structInfo, err := parseStruct(reflect.TypeOf(src)) + if err != nil { + return "", nil, err + } _, present := structInfo.fields["updated_at"] if present { // we will need to set updated_at diff --git a/docs/generate.go b/docs/generate.go index e02d3ec..cd26673 100644 --- a/docs/generate.go +++ b/docs/generate.go @@ -68,7 +68,7 @@ func main() { routes := GetAllRoutes() for _, route := range routes { found := false - for url, _ := range endpoints { + for url := range endpoints { if route.Path == url { for _, action := range endpoints[url] { @@ -231,7 +231,7 @@ func main() { // create all list responses pre := strings.Repeat(" ", 4) - for url, _ := range endpoints { + for url := range endpoints { for _, action := range endpoints[url] { for _, r := range action.Details.Responses { text := strings.TrimSpace(r.Text) @@ -258,7 +258,7 @@ func main() { } f.WriteString(fmt.Sprintf("paths:\n")) - for url, _ := range endpoints { + for url := range endpoints { f.WriteString(fmt.Sprintf(" %s:\n", url)) for _, action := range endpoints[url] { diff --git a/docs/swagger/endpoints.go b/docs/swagger/endpoints.go index 9161b25..1db1051 100644 --- a/docs/swagger/endpoints.go +++ b/docs/swagger/endpoints.go @@ -19,7 +19,6 @@ package swagger import ( - "errors" "fmt" "go/ast" "go/token" @@ -48,7 +47,7 @@ func ParseResponse(source string) (*Response, error) { func ParseParameter(source string) (*Parameter, error) { tmp := strings.Split(source, ",") if len(tmp) != 2 { - return nil, errors.New(fmt.Sprintf("error in \"%s\"", source)) + return nil, fmt.Errorf("error in \"%s\"", source) } stringName := strings.TrimSpace(tmp[0]) stringType := strings.TrimSpace(tmp[1]) diff --git a/docs/swagger/errors.go b/docs/swagger/errors.go index 4e73770..b8ed724 100644 --- a/docs/swagger/errors.go +++ b/docs/swagger/errors.go @@ -24,8 +24,4 @@ var ( errTagSyntax = errors.New("bad syntax for struct tag pair") errTagKeySyntax = errors.New("bad syntax for struct tag key") errTagValueSyntax = errors.New("bad syntax for struct tag value") - - errKeyNotSet = errors.New("tag key does not exist") - errTagNotExist = errors.New("tag does not exist") - errTagKeyMismatch = errors.New("mismatch between key and tag.key") ) diff --git a/docs/swagger/struct.go b/docs/swagger/struct.go index 537cc48..2931c89 100644 --- a/docs/swagger/struct.go +++ b/docs/swagger/struct.go @@ -47,7 +47,7 @@ type Field struct { func IdentString(str string, ch int) string { preSpace := strings.Repeat(" ", ch) lines := strings.Split(str, "\n") - for k, _ := range lines { + for k := range lines { lines[k] = preSpace + lines[k] } diff --git a/email/email.go b/email/email.go index 9dd40a9..f530285 100644 --- a/email/email.go +++ b/email/email.go @@ -164,7 +164,7 @@ func (sm *SendMailer) Send(e *Email) error { pw.Write([]byte(fmt.Sprintf("To: %s\n", e.To))) pw.Write([]byte(fmt.Sprintf("Subject: %s\n", e.Subject))) pw.Write([]byte(fmt.Sprintf("\n"))) // blank line separating headers from body - pw.Write([]byte(fmt.Sprintf("%s", e.Body))) + pw.Write([]byte(e.Body)) err = pw.Close() if err != nil { @@ -188,5 +188,5 @@ func LoadAndFillTemplate(file string, data map[string]string) (string, error) { } var tpl bytes.Buffer err = t.Execute(&tpl, data) - return tpl.String(), nil + return tpl.String(), err } diff --git a/go.sum b/go.sum index 24f441a..8f88076 100644 --- a/go.sum +++ b/go.sum @@ -16,19 +16,17 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v1.13.1 h1:5VBhsO6ckUxB0A8CE5LlUJdXzik9cbEbBTQ/ggeml7M= +github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo= github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/franela/goblin v0.0.0-20181003173013-ead4ad1d2727 h1:eouy4stZdUKn7n98c1+rdUTxWMg+jvhP+oHt0K8fiug= github.com/franela/goblin v0.0.0-20181003173013-ead4ad1d2727/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -70,7 +68,6 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= diff --git a/makefile b/makefile index 419897e..f9e79c1 100644 --- a/makefile +++ b/makefile @@ -1,28 +1,16 @@ -.PHONY: setup build +.PHONY: cover lint fmt build +test: + go test ./... -covermode=atomic -coverprofile=coverage.txt cover: test ## Run all the tests and opens the coverage report go tool cover -html=coverage.txt -.PHONY: fmt fmt: ## Run goimports on all go files find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do goimports -w "$$file"; done -.PHONY: lint lint: ## Run all the linters - gometalinter --vendor --disable-all \ - --enable=deadcode \ - --enable=ineffassign \ - --enable=gosimple \ - --enable=staticcheck \ - --enable=gofmt \ - --enable=goimports \ - --enable=misspell \ - --enable=errcheck \ - --enable=vet \ - --enable=vetshadow \ - --deadline=10m \ - ./... + golangci-lint run -D errcheck build: go build infomark.go diff --git a/service/consumer.go b/service/consumer.go index 301ed11..8083aa9 100644 --- a/service/consumer.go +++ b/service/consumer.go @@ -111,8 +111,10 @@ func (c *Consumer) Setup() (<-chan amqp.Delivery, error) { return nil, fmt.Errorf("Queue Declare: %s", err) } - logger.Info("declared Queue (%d messages, %d consumers), binding to Exchange", - state.Messages, state.Consumers) + logger.WithFields(logrus.Fields{ + "messages": state.Messages, + "consumers": state.Consumers, + }).Info("declared Queue, binding to Exchange") if err = c.channel.QueueBind( c.Config.Queue, // name of the queue @@ -124,7 +126,7 @@ func (c *Consumer) Setup() (<-chan amqp.Delivery, error) { return nil, fmt.Errorf("Queue Bind: %s", err) } - logger.Info("Queue bound to Exchange, starting Consume (Consumer tag '%s')", c.Config.Tag) + logger.Info("Queue bound to Exchange, starting Consume") deliveries, err := c.channel.Consume( c.Config.Queue, // name c.Config.Tag, // consumerTag,