Skip to content

Commit

Permalink
refactor: Fix lint errors and add lint (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonjek authored Apr 14, 2024
1 parent acedc0d commit fc12909
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 75 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ jobs:
files-changed:
uses: ./.github/workflows/files-changed.yml

lint:
if: needs.files-changed.outputs.lint == 'true' || needs.files-changed.outputs.actions == 'true'
needs: files-changed
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- name: Install tools
run: make tools
- name: Run generate templ files
run: make generate
- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: latest
skip-cache: true
args: --timeout=10m --verbose
test:
if: needs.files-changed.outputs.app == 'true' || needs.files-changed.outputs.actions == 'true'
needs: files-changed
Expand All @@ -30,8 +54,8 @@ jobs:
check-latest: true
- name: Install tools
run: make tools
- name: Run build
run: make build
- name: Run generate templ files
run: make generate
- name: Run tests
run: make test
build:
Expand Down
56 changes: 56 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
linters:
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- asciicheck
- depguard
- dupword
- durationcheck
- errorlint
- gosec
- gocritic
- gofmt
- gofumpt
- goimports
- misspell
- nolintlint
- predeclared
- revive
- testifylint
- unconvert

linters-settings:
gofmt:
simplify: true
depguard:
rules:
main:
deny:
- pkg: "io/ioutil"
desc: "Use corresponding 'os' or 'io' functions instead."
- pkg: "github.com/pkg/errors"
desc: "Use 'errors' or 'fmt' instead of github.com/pkg/errors"
- pkg: "golang.org/x/exp/slices"
desc: "Use 'slices' instead."

revive:
# https://github.com/mgechev/revive/blob/master/defaults.toml
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
enable-all-rules: true
ignore-generated-header: true
severity: warning
rules:
- name: line-length-limit
severity: error
arguments: [120]
- name: add-constant
disabled: true
- name: cognitive-complexity
disabled: true
- name: cyclomatic
disabled: true
- name: unused-receiver
disabled: true
- name: unhandled-error
arguments:
- "fmt.Println"
- "fmt.Printf"
87 changes: 44 additions & 43 deletions internal/notes/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Note struct {
Id int `form:"id" json:"id"`
ID int `form:"id" json:"id"`
Title string `form:"title" json:"title" binding:"required"`
Body string `form:"body" json:"body" binding:"required"`
Created time.Time `json:"created"`
Expand All @@ -21,41 +21,43 @@ type CreateNote struct {
Body string `form:"body" json:"body" binding:"required"`
}

var notes []Note = []Note{
{
Id: 5,
Title: "htmx.js",
Body: "</> htmx - high power tools for HTML",
Created: time.Date(2022, 8, 10, 21, 21, 0, 0, time.UTC),
},
{
Id: 4,
Title: "templ",
Body: "A language for writing HTML user interfaces in Go.",
Created: time.Date(2021, 5, 16, 22, 33, 0, 0, time.UTC),
},
{
Id: 3,
Title: "Pico CSS",
Body: "Minimal CSS Framework for semantic HTML",
Created: time.Date(2019, 12, 11, 10, 8, 0, 0, time.UTC),
},
{
Id: 2,
Title: "GoLang",
Body: "The Go programming language.",
Created: time.Date(2018, 9, 25, 22, 20, 0, 0, time.UTC),
},
{
Id: 1,
Title: "Ionic",
Body: "Premium hand-crafted icons built by Ionic, for Ionic apps and web apps everywhere.",
Created: time.Date(2013, 10, 30, 12, 34, 0, 0, time.UTC),
},
}
var (
notes = []Note{
{
ID: 5,
Title: "htmx.js",
Body: "</> htmx - high power tools for HTML",
Created: time.Date(2022, 8, 10, 21, 21, 0, 0, time.UTC),
},
{
ID: 4,
Title: "templ",
Body: "A language for writing HTML user interfaces in Go.",
Created: time.Date(2021, 5, 16, 22, 33, 0, 0, time.UTC),
},
{
ID: 3,
Title: "Pico CSS",
Body: "Minimal CSS Framework for semantic HTML",
Created: time.Date(2019, 12, 11, 10, 8, 0, 0, time.UTC),
},
{
ID: 2,
Title: "GoLang",
Body: "The Go programming language.",
Created: time.Date(2018, 9, 25, 22, 20, 0, 0, time.UTC),
},
{
ID: 1,
Title: "Ionic",
Body: "Premium hand-crafted icons built by Ionic, for Ionic apps and web apps everywhere.",
Created: time.Date(2013, 10, 30, 12, 34, 0, 0, time.UTC),
},
}

// Last note ID
var currentID uint32 = uint32(len(notes))
// Last note ID
currentID = uint32(len(notes))
)

func getNextID() uint32 {
return atomic.AddUint32(&currentID, 1)
Expand All @@ -68,7 +70,7 @@ func GetNoteByID(idStr string) (Note, error) {
}

for _, note := range notes {
if note.Id == id {
if note.ID == id {
return note, nil
}
}
Expand All @@ -93,7 +95,7 @@ func Count() int {

func Add(n CreateNote) Note {
note := Note{
Id: int(getNextID()),
ID: int(getNextID()),
Title: n.Title,
Body: n.Body,
Created: time.Now(),
Expand All @@ -104,7 +106,7 @@ func Add(n CreateNote) Note {

func Update(n Note) {
for i, note := range notes {
if note.Id == n.Id {
if note.ID == n.ID {
notes[i] = n
break
}
Expand All @@ -118,25 +120,24 @@ func Delete(idStr string) error {
}

for idx, note := range notes {
if note.Id == id {
if note.ID == id {
notes = append(notes[:idx], notes[idx+1:]...)
return nil
}

}
return fmt.Errorf("note with ID %d not found", id)
}

func findIndex(arr []Note, n int) (int, bool) {
var index int = -1
index := -1

// Initialize to -1 to represent no ID found yet
maxID := -1

for i, elem := range arr {
if elem.Id < n && elem.Id > maxID {
if elem.ID < n && elem.ID > maxID {
index = i
maxID = elem.Id
maxID = elem.ID
}
}

Expand Down
1 change: 0 additions & 1 deletion internal/web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func CreateMiddlewareStack(ms ...Middleware) Middleware {
}

func (w *wrappedWriter) WriteHeader(sc int) {
w.ResponseWriter.WriteHeader(sc)
w.statusCode = sc
}

Expand Down
4 changes: 2 additions & 2 deletions internal/web/templ/components/modal_edit_note.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
templ ModalEditNote(note notes.Note) {
<dialog open id="dialog">
<form
hx-put={ "/note/" + strconv.Itoa(note.Id) }
hx-target={ "#note-" + strconv.Itoa(note.Id) }
hx-put={ "/note/" + strconv.Itoa(note.ID) }
hx-target={ "#note-" + strconv.Itoa(note.ID) }
hx-target-error="#error"
hx-swap="outerHTML"
hx-indicator=".htmx-indicator"
Expand Down
12 changes: 6 additions & 6 deletions internal/web/templ/components/note.templ
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ templ noteData(note notes.Note) {
<ul dir="rtl">
<li><small><a class="contrast"
hx-trigger="click"
hx-get={ "/edit/" + strconv.Itoa(note.Id) }
hx-get={ "/edit/" + strconv.Itoa(note.ID) }
hx-target="#dialog"
hx-target-error="#error"
hx-swap="outerHTML">Edit <ion-icon name="create"/></a></small></li>
<li><small><a class="contrast"
hx-trigger="click"
hx-delete={ "/note/" + strconv.Itoa(note.Id) }
hx-target={ "#note-" + strconv.Itoa(note.Id) }
hx-delete={ "/note/" + strconv.Itoa(note.ID) }
hx-target={ "#note-" + strconv.Itoa(note.ID) }
hx-target-error="#error"
hx-swap="outerHTML">Delete <ion-icon name="trash"/></a></small></li>
</ul>
Expand All @@ -43,15 +43,15 @@ templ noteData(note notes.Note) {
}

templ NoteItem(note notes.Note) {
<article id={ "note-" + strconv.Itoa(note.Id) }>
<article id={ "note-" + strconv.Itoa(note.ID) }>
@noteData(note)
</article>
}

templ LastNote(note notes.Note) {
<article
id={ "note-" + strconv.Itoa(note.Id) }
hx-get={ "/notes/load-more?note=" + strconv.Itoa(note.Id) }
id={ "note-" + strconv.Itoa(note.ID) }
hx-get={ "/notes/load-more?note=" + strconv.Itoa(note.ID) }
hx-push-url="false"
hx-trigger="intersect once"
hx-target="#notes"
Expand Down
Loading

0 comments on commit fc12909

Please sign in to comment.