From b8e023e0102d79a2fa3b6b7dbc383b464251a06b Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Mon, 13 May 2024 10:23:24 +0530 Subject: [PATCH 1/2] run golangci-lint on the entire codebase everytime --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6bea658..5774ba1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,5 +36,5 @@ jobs: - name: Run golangci-lint uses: golangci/golangci-lint-action@v6 with: - only-new-issues: true # this should be removed at some point, but there are too many issues to address - version: latest \ No newline at end of file + only-new-issues: false + version: latest From 15348b4ca89eb751cbe09bdaf6841bc70c8a2ac4 Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Mon, 13 May 2024 10:57:03 +0530 Subject: [PATCH 2/2] implement ci suggestions --- cmd/ls.go | 4 ++-- pkg/db/db.go | 4 ++-- pkg/ui/delegate.go | 21 ++++++++------------- pkg/ui/interactiveTable.go | 22 ++++++++++++---------- pkg/ui/statusTable.go | 20 ++++++++++---------- pkg/ui/tui.go | 10 ++++++---- 6 files changed, 40 insertions(+), 41 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index 03075ee..d7ac692 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -18,7 +18,7 @@ var lsCmd = &cobra.Command{ `, RunE: func(cmd *cobra.Command, _ []string) error { filterFlag, _ := cmd.Flags().GetString("f") - oldUi, _ := cmd.Flags().GetBool("o") + oldUI, _ := cmd.Flags().GetBool("o") data, err := db.GetAllRecords(StatusBucket) if err != nil { return err @@ -27,7 +27,7 @@ var lsCmd = &cobra.Command{ fmt.Println("Filtering by status : ", filterFlag) data = utils.FilterByStatus(data, filterFlag) } - if oldUi { + if oldUI { return ui.RenderTable(data) } return ui.RenderInteractiveTable(data) diff --git a/pkg/db/db.go b/pkg/db/db.go index 6a5b197..9b4d199 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -97,7 +97,7 @@ func GetRecord(key string, bucketName string) (string, error) { } v := bucket.Get([]byte(key)) if v == nil { - return fmt.Errorf("Key not found in db\n") + return fmt.Errorf("key not found in db") } rec = string(v) return nil @@ -123,7 +123,7 @@ func GetAllRecords(bucketName string) (map[string]string, error) { err = db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(bucketName)) if bucket == nil { - return fmt.Errorf("Database not found. \nThis could be because no project dir has been initialized yet.") + return fmt.Errorf("database not found. \nThis could be because no project dir has been initialized yet") } err := bucket.ForEach(func(k, v []byte) error { records[string(k)] = string(v) diff --git a/pkg/ui/delegate.go b/pkg/ui/delegate.go index 6ca04b3..a4f3928 100644 --- a/pkg/ui/delegate.go +++ b/pkg/ui/delegate.go @@ -10,23 +10,19 @@ import ( func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate { d := list.NewDefaultDelegate() - d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd { - var title string - - if i, ok := m.SelectedItem().(item); ok { - title = i.Title() - } else { + i, ok := m.SelectedItem().(item) + if !ok { return nil } - - switch msg := msg.(type) { - case tea.KeyMsg: + title := i.Title() + if msg, ok := msg.(tea.KeyMsg); ok { switch { case key.Matches(msg, keys.choose): - p.LaunchRenderer(title) - return tea.Quit // TODO : change this to return to the list - + err := p.LaunchRenderer(title) + if err == nil { + return tea.Quit // TODO : change this to return to the list + } case key.Matches(msg, keys.remove): index := m.Index() m.RemoveItem(index) @@ -36,7 +32,6 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate { return m.NewStatusMessage(statusMessageStyle("Deleted " + title)) } } - return nil } diff --git a/pkg/ui/interactiveTable.go b/pkg/ui/interactiveTable.go index 0ec3773..437dfa3 100644 --- a/pkg/ui/interactiveTable.go +++ b/pkg/ui/interactiveTable.go @@ -22,12 +22,11 @@ type tableModel struct { table table.Model } -func (m tableModel) Init() tea.Cmd { return nil } +func (tableModel) Init() tea.Cmd { return nil } func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd - switch msg := msg.(type) { - case tea.KeyMsg: + if msg, ok := msg.(tea.KeyMsg); ok { switch msg.String() { case "esc": if m.table.Focused() { @@ -39,7 +38,10 @@ func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit case "enter": project := m.table.SelectedRow()[1] - p.LaunchRenderer(project) + err := p.LaunchRenderer(project) + if err != nil { + return m, tea.Quit + } } } m.table, cmd = m.table.Update(msg) @@ -57,15 +59,15 @@ func RenderInteractiveTable(data map[string]string) error { {Title: "Last Edited", Width: 20}, } var rows []table.Row - for p, status := range data { - alias, err := db.GetRecord(p, pkg.ProjectAliasBucket) - lastEdited := utils.GetLastModifiedTime(p) + for proj, status := range data { + alias, err := db.GetRecord(proj, pkg.ProjectAliasBucket) + lastEdited := utils.GetLastModifiedTime(proj) if err == nil { - pname := fmt.Sprintf("%s (%s)", p, alias) + pname := fmt.Sprintf("%s (%s)", proj, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited rows = append(rows, row) } else { - row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited + row := []string{utils.TitleCase(status), proj, lastEdited} // Status | projectName | lastEdited rows = append(rows, row) } } @@ -104,7 +106,7 @@ func RenderInteractiveTable(data map[string]string) error { m := tableModel{t} if _, err := tea.NewProgram(m).Run(); err != nil { - return fmt.Errorf("Error running program: %s", err) + return fmt.Errorf("error running program: %w", err) } return nil } diff --git a/pkg/ui/statusTable.go b/pkg/ui/statusTable.go index 9850491..4838447 100644 --- a/pkg/ui/statusTable.go +++ b/pkg/ui/statusTable.go @@ -15,30 +15,30 @@ import ( // RenderTable: renders the given data as a table func RenderTable(data map[string]string) error { - var TableData [][]string + var tableData [][]string for p, status := range data { alias, err := db.GetRecord(p, pkg.ProjectAliasBucket) lastEdited := utils.GetLastModifiedTime(p) if err == nil { pname := fmt.Sprintf("%s (%s)", p, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited - TableData = append(TableData, row) + tableData = append(tableData, row) } else { row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited - TableData = append(TableData, row) + tableData = append(tableData, row) } } - if len(TableData) == 0 { + if len(tableData) == 0 { fmt.Printf("No projects found in the database\n\n") fmt.Printf("Add projects to the database using \n\n") fmt.Println("pman init .") fmt.Println("or") fmt.Println("pman add ") - return fmt.Errorf("no database initialised") + return fmt.Errorf("no database initialized") } - sort.Slice(TableData, func(i, j int) bool { - valI := TableData[i][1] - valJ := TableData[j][1] + sort.Slice(tableData, func(i, j int) bool { + valI := tableData[i][1] + valJ := tableData[j][1] return valI < valJ }) re := lipgloss.NewRenderer(os.Stdout) @@ -62,12 +62,12 @@ func RenderTable(data map[string]string) error { BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))). Headers(headers...). Width(90). - Rows(TableData...). + Rows(tableData...). StyleFunc(func(row, _ int) lipgloss.Style { if row == 0 { return headerStyle } - color, ok := statusColors[fmt.Sprint(TableData[row-1][0])] + color, ok := statusColors[fmt.Sprint(tableData[row-1][0])] if ok { return baseStyle.Copy().Foreground(color) } diff --git a/pkg/ui/tui.go b/pkg/ui/tui.go index 31df716..a7f8035 100644 --- a/pkg/ui/tui.go +++ b/pkg/ui/tui.go @@ -25,9 +25,8 @@ var ( ) type item struct { - name string - status string - lastEdited string + name string + status string } func (i item) Title() string { return i.name } @@ -92,7 +91,10 @@ func newModel() (model, error) { } }) - formattedData, _ := d.([]item) + formattedData, ok := d.([]item) + if !ok { + return model{}, fmt.Errorf("something went wrong while creating a list of items") + } items := make([]list.Item, len(data)) for i := 0; i < len(data); i++ { items[i] = formattedData[i]