Skip to content

Commit

Permalink
make suggestions asked by ci
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick2009 committed May 12, 2024
1 parent 3981616 commit 350fa75
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 13 additions & 16 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
40 changes: 18 additions & 22 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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])
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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)
})
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
})
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}

Expand All @@ -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) {
Expand All @@ -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)
})
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
})
}

Expand All @@ -434,6 +430,6 @@ func Test_DeleteDb(t *testing.T) {

err := db.DeleteDb(dbname)

assert.ErrorIs(t, err, expectedErr)
require.ErrorIs(t, err, expectedErr)
})
}
2 changes: 1 addition & 1 deletion pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
type item struct {
name string
status string
lastEdited string
// lastEdited string
}

func (i item) Title() string { return i.name }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 350fa75

Please sign in to comment.