From 350fa75ae7ec7466a35ed35257bc4279b03448fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kouakou=20kouam=C3=A9=20yannick?= Date: Sun, 12 May 2024 13:04:51 +0200 Subject: [PATCH] make suggestions asked by ci --- cmd/ls.go | 2 +- cmd/reset.go | 2 +- cmd/status.go | 2 +- pkg/db/db.go | 29 +++++++++++++-------------- pkg/db/db_test.go | 40 +++++++++++++++++--------------------- pkg/ui/interactiveTable.go | 2 +- pkg/ui/tui.go | 4 ++-- 7 files changed, 37 insertions(+), 44 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index 6b3c4f6..6c3feba 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -16,7 +16,7 @@ var lsCmd = &cobra.Command{ Long: `List all indexed projects along with their status Usage : pman ls `, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { filterFlag, _ := cmd.Flags().GetString("f") oldUi, _ := cmd.Flags().GetBool("o") data, err := db.GetAllRecords(db.DBName, StatusBucket) diff --git a/cmd/reset.go b/cmd/reset.go index 1c90373..f4cd8af 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -11,7 +11,7 @@ import ( var resetCmd = &cobra.Command{ Use: "reset", Short: "Deletes the current indexed projects, run pman init to reindex the projects", - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, _ []string) error { err := db.DeleteDb(db.DBName) if err != nil { fmt.Println(err) diff --git a/cmd/status.go b/cmd/status.go index 2ec84e6..ba4d671 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -11,7 +11,7 @@ import ( ) var ( - ErrBadUsageStatusCmd error = errors.New("bad usage of status command") + ErrBadUsageStatusCmd = errors.New("bad usage of status command") ) // statusCmd represents the status command diff --git a/pkg/db/db.go b/pkg/db/db.go index 52f1889..2698ae4 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -29,12 +29,12 @@ var ( // WriteToDB writes the data to the specified bucket in the database func WriteToDB(dbname string, data map[string]string, bucketName string) error { - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { log.Printf("%v : %v \n", ErrOpenDB, err) return errors.Join(ErrOpenDB, err) } - db, _ := bolt.Open(DBLoc, 0o600, nil) // create the database if it doesn't exist then open it + db, _ := bolt.Open(dbLoc, 0o600, nil) // create the database if it doesn't exist then open it defer db.Close() err = db.Update(func(tx *bolt.Tx) error { bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName)) @@ -53,21 +53,19 @@ func WriteToDB(dbname string, data map[string]string, bucketName string) error { } func DeleteFromDb(dbname, key, bucketName string) error { - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { log.Printf("%v : %v \n", ErrOpenDB, err) return errors.Join(ErrOpenDB, err) } - db, _ := bolt.Open(DBLoc, 0o600, nil) // create the database if it doesn't exist then open it + db, _ := bolt.Open(dbLoc, 0o600, nil) // create the database if it doesn't exist then open it defer db.Close() err = db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(bucketName)) if bucket == nil { return ErrBucketNotFound } - bucket.Delete([]byte(key)) - - return nil + return bucket.Delete([]byte(key)) }) return err } @@ -95,12 +93,12 @@ func GetDBLoc(dbname string) (string, error) { // GetRecord returns the value of the key from the specified bucket, and error if it does not exist func GetRecord(dbname, key, bucketName string) (string, error) { var rec string - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { log.Printf("%v : %v \n", ErrOpenDB, err) return "", err } - db, _ := bolt.Open(DBLoc, 0o600, nil) + db, _ := bolt.Open(dbLoc, 0o600, nil) defer db.Close() err = db.View(func(tx *bolt.Tx) error { @@ -123,13 +121,13 @@ func GetRecord(dbname, key, bucketName string) (string, error) { // GetAllRecords returns all the records from the specified bucket as a dictionary func GetAllRecords(dbname, bucketName string) (map[string]string, error) { - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { log.Printf("%v : %v \n", ErrOpenDB, err) return map[string]string{}, err } - db, _ := bolt.Open(DBLoc, 0o600, nil) + db, _ := bolt.Open(dbLoc, 0o600, nil) defer db.Close() records := make(map[string]string) err = db.View(func(tx *bolt.Tx) error { @@ -155,12 +153,12 @@ func GetAllRecords(dbname, bucketName string) (map[string]string, error) { // UpdateRec updates the value of the key in the specified bucket, usually used to update the status of a project func UpdateRec(dbname, key, val, bucketName string) error { - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { log.Print(err) return err } - db, _ := bolt.Open(DBLoc, 0o600, nil) + db, _ := bolt.Open(dbLoc, 0o600, nil) defer db.Close() err = db.Update(func(tx *bolt.Tx) error { @@ -179,10 +177,9 @@ func UpdateRec(dbname, key, val, bucketName string) error { } func DeleteDb(dbname string) error { - DBLoc, err := GetDBLoc(dbname) + dbLoc, err := GetDBLoc(dbname) if err != nil { return err } - os.Remove(DBLoc) - return nil + return os.Remove(dbLoc) } diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 7c3319b..49e6330 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/theredditbandit/pman/pkg/db" bolt "go.etcd.io/bbolt" ) @@ -23,7 +24,7 @@ func Test_GetDBLoc(t *testing.T) { os.Remove(actualPath) }) - assert.Equal(t, err, nil) + assert.NoError(t, err) assert.Contains(t, actualPath, expectedWords[0]) assert.Contains(t, actualPath, expectedWords[1]) assert.Contains(t, actualPath, expectedWords[2]) @@ -39,10 +40,6 @@ func Test_GetDBLoc(t *testing.T) { assert.ErrorIs(t, err, expectedErr) assert.Empty(t, actualPath) }) - - t.Run("Test getDBLoc with panic", func(t *testing.T) { - - }) } func Test_GetRecord(t *testing.T) { @@ -72,7 +69,7 @@ func Test_GetRecord(t *testing.T) { actualValue, err := db.GetRecord(dbname, key, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) assert.Empty(t, actualValue) }) @@ -90,7 +87,7 @@ func Test_GetRecord(t *testing.T) { actualValue, err := db.GetRecord(dbname, key, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) assert.Empty(t, actualValue) }) @@ -99,7 +96,7 @@ func Test_GetRecord(t *testing.T) { actualValue, err := db.GetRecord(dbname, key, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) assert.Empty(t, actualValue) }) } @@ -158,7 +155,7 @@ func Test_WriteToDB(t *testing.T) { err = db.WriteToDB(dbname, data, bucketName) - assert.ErrorIs(t, err, db.ErrCreateBucket) + require.ErrorIs(t, err, db.ErrCreateBucket) }) t.Run("Test WriteToDB with empty map key", func(t *testing.T) { @@ -175,7 +172,7 @@ func Test_WriteToDB(t *testing.T) { err = db.WriteToDB(dbname, data, bucketName) - assert.ErrorIs(t, err, db.ErrWriteToDB) + require.ErrorIs(t, err, db.ErrWriteToDB) }) t.Run("Test WriteToDB with empty dbname value", func(t *testing.T) { @@ -189,7 +186,7 @@ func Test_WriteToDB(t *testing.T) { err := db.WriteToDB(dbname, data, bucketName) - assert.ErrorIs(t, err, db.ErrOpenDB) + require.ErrorIs(t, err, db.ErrOpenDB) }) } @@ -240,7 +237,7 @@ func Test_DeleteFromDb(t *testing.T) { err := db.DeleteFromDb(dbname, key, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) }) t.Run("Test DeleteFromDb with key not found", func(t *testing.T) { @@ -274,7 +271,7 @@ func Test_DeleteFromDb(t *testing.T) { err := db.DeleteFromDb(dbname, key, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) }) } @@ -300,7 +297,7 @@ func Test_ListAllRecords(t *testing.T) { records, err := db.GetAllRecords(dbname, bucketName) assert.NoError(t, err) - assert.Equal(t, records, data) + assert.Equal(t,data, records) }) t.Run("Test ListAllRecords with empty dbname", func(t *testing.T) { @@ -310,17 +307,16 @@ func Test_ListAllRecords(t *testing.T) { records, err := db.GetAllRecords(dbname, bucketName) - assert.ErrorIs(t, err, expectedErr) - assert.Equal(t, records, expectedValue) + require.ErrorIs(t, err, expectedErr) + assert.Equal(t, expectedValue, records) }) t.Run("Test ListAllRecords with bucket not found", func(t *testing.T) { - expectedErr := db.ErrBucketNotFound records, err := db.GetAllRecords(dbname, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) assert.Nil(t, records) }) } @@ -371,7 +367,7 @@ func Test_UpdateRec(t *testing.T) { newValue := "updatedValue" err := db.UpdateRec(dbname, key, newValue, bucketName) - assert.ErrorIs(t, err, db.ErrDBNameEmpty) + require.ErrorIs(t, err, db.ErrDBNameEmpty) }) t.Run("Test UpdateRec with key not found", func(t *testing.T) { @@ -396,7 +392,7 @@ func Test_UpdateRec(t *testing.T) { err = db.UpdateRec(dbname, key, newValue, bucketName) - assert.ErrorIs(t, err, db.ErrProjectNotFound) + require.ErrorIs(t, err, db.ErrProjectNotFound) }) t.Run("Test UpdateRec with bucket not found", func(t *testing.T) { @@ -407,7 +403,7 @@ func Test_UpdateRec(t *testing.T) { err := db.UpdateRec(dbname, key, newValue, bucketName) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) }) } @@ -434,6 +430,6 @@ func Test_DeleteDb(t *testing.T) { err := db.DeleteDb(dbname) - assert.ErrorIs(t, err, expectedErr) + require.ErrorIs(t, err, expectedErr) }) } diff --git a/pkg/ui/interactiveTable.go b/pkg/ui/interactiveTable.go index 6fc590d..98b53eb 100644 --- a/pkg/ui/interactiveTable.go +++ b/pkg/ui/interactiveTable.go @@ -104,7 +104,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/tui.go b/pkg/ui/tui.go index 0bfea63..13ec32c 100644 --- a/pkg/ui/tui.go +++ b/pkg/ui/tui.go @@ -27,7 +27,7 @@ var ( type item struct { name string status string - lastEdited string + // lastEdited string } func (i item) Title() string { return i.name } @@ -183,7 +183,7 @@ func Tui() error { } if _, err := tea.NewProgram(model, tea.WithAltScreen()).Run(); err != nil { - return fmt.Errorf("Error running program: %w", err) + return fmt.Errorf("error running program: %w", err) } return nil