Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package db tests #23

Merged
merged 20 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ go.work

# End of https://www.toptal.com/developers/gitignore/api/go
pman
coverage.out
.idea/*
16 changes: 11 additions & 5 deletions cmd/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/theredditbandit/pman/pkg/db"
)

var (
ErrBadUsageAliasCmd = errors.New("bad usage of alias command")
)

// aliasCmd represents the alias command
var aliasCmd = &cobra.Command{
Use: "alias",
Expand All @@ -17,22 +21,24 @@ var aliasCmd = &cobra.Command{
avlpn or something smaller and use that to query pman`,
RunE: func(_ *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("Please provide a project name and an alias")
fmt.Println("Usage: pman alias <a-long-project-name> <alias>")
return ErrBadUsageAliasCmd
}
pname := args[0]
alias := args[1]
_, err := db.GetRecord(pname, StatusBucket)
_, err := db.GetRecord(db.DBName, pname, StatusBucket)
if err != nil {
return fmt.Errorf("%s project does not exist in db", pname)
fmt.Printf("%s project does not exist in db", pname)
return err
}
fmt.Printf("Aliasing %s to %s \n", pname, alias)
data := map[string]string{alias: pname}
revData := map[string]string{pname: alias}
err = db.WriteToDB(data, ProjectAliasBucket)
err = db.WriteToDB(db.DBName, data, ProjectAliasBucket)
if err != nil {
return err
}
err = db.WriteToDB(revData, ProjectAliasBucket)
err = db.WriteToDB(db.DBName, revData, ProjectAliasBucket)
if err != nil {
return err
}
Expand Down
24 changes: 13 additions & 11 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,41 @@ import (
"github.com/theredditbandit/pman/pkg/db"
)

var (
ErrBadUsageDelCmd = errors.New("bad usage of delete command")
)

var delCmd = &cobra.Command{
Use: "delete",
Short: "Deletes a project from the index database. This does not delete the project from the filesystem",
Aliases: []string{"del", "d"},
RunE: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("Please provide a project name")
fmt.Println("Usage : pman delete <projectName>")
return ErrBadUsageDelCmd
}
projName := args[0]
_, err := db.GetRecord(projName, StatusBucket)
_, err := db.GetRecord(db.DBName, projName, StatusBucket)
if err != nil {
fmt.Printf("%s is not a valid project to be deleted\n", projName)
fmt.Println("Note : projects cannot be deleted using their alias")

// not a real error here
return nil
return err
}
err = db.DeleteFromDb(projName, ProjectPathBucket)
err = db.DeleteFromDb(db.DBName, projName, ProjectPathBucket)
if err != nil {
return err
}
err = db.DeleteFromDb(projName, StatusBucket)
err = db.DeleteFromDb(db.DBName, projName, StatusBucket)
if err != nil {
return err
}
alias, err := db.GetRecord(projName, ProjectAliasBucket)
alias, err := db.GetRecord(db.DBName, projName, ProjectAliasBucket)
if err == nil {
err = db.DeleteFromDb(alias, ProjectAliasBucket)
err = db.DeleteFromDb(db.DBName, alias, ProjectAliasBucket)
if err != nil {
return err
}
err = db.DeleteFromDb(projName, ProjectAliasBucket)
err = db.DeleteFromDb(db.DBName, projName, ProjectAliasBucket)
if err != nil {
return err
}
Expand All @@ -50,7 +53,6 @@ var delCmd = &cobra.Command{
return err
}
fmt.Printf("Successfully deleted %s from the db \n", projName)

return nil
},
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
"github.com/theredditbandit/pman/pkg/utils"
)

var (
ErrBadUsageInfoCmd = errors.New("bad usage of info command")
)

var infoCmd = &cobra.Command{
Use: "info",
Short: "The info command pretty prints the README.md file present at the root of the specified project.",
Aliases: []string{"ifo", "ifno", "ino"},
RunE: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("Please provide a project name")
fmt.Println("Please provide a project name")
return ErrBadUsageInfoCmd
}
projectName := args[0]
infoData, err := utils.ReadREADME(projectName)
Expand All @@ -27,7 +32,6 @@ var infoCmd = &cobra.Command{
return err
}
fmt.Print(md)

return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var lsCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, _ []string) error {
filterFlag, _ := cmd.Flags().GetString("f")
oldUI, _ := cmd.Flags().GetBool("o")
data, err := db.GetAllRecords(StatusBucket)
data, err := db.GetAllRecords(db.DBName, StatusBucket)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ var resetCmd = &cobra.Command{
Use: "reset",
Short: "Deletes the current indexed projects, run pman init to reindex the projects",
RunE: func(_ *cobra.Command, _ []string) error {
err := db.DeleteDb()
err := db.DeleteDb(db.DBName)
if err != nil {
fmt.Println(err)
return err
}

fmt.Println("Successfully reset the database, run pman init to reindex the projects")

return nil
},
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ const (
version = "1.0"
)

var (
ErrNoArgs = errors.New("this command has no argument")
)

var rootCmd = &cobra.Command{
Use: "pman",
Short: "A cli project manager",
Version: version,
Use: "pman",
Short: "A cli project manager",
Version: version,
SilenceUsage: true,
RunE: func(_ *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("this command has no argument")
return ErrNoArgs
}
return nil
},
Expand Down
18 changes: 12 additions & 6 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/theredditbandit/pman/pkg/db"
)

var (
ErrFlagNotImplemented = errors.New("flag not implemented yet")
ErrBadUsageSetCmd = errors.New("bad usage of set command")
)

var setCmd = &cobra.Command{
Use: "set",
Short: "Set the status of a project",
Expand All @@ -22,26 +27,27 @@ var setCmd = &cobra.Command{
interactiveFlag, _ := cmd.Flags().GetBool("i") // TODO: Implement this
if interactiveFlag {
cmd.SilenceUsage = true
return errors.New("Not implemented yet")
return ErrFlagNotImplemented
}
if len(args) != 2 {
return errors.New("Please provide a directory name")
fmt.Println("Please provide a directory name")
return ErrBadUsageSetCmd
}
var pname string
alias := args[0]
status := args[1]
project, err := db.GetRecord(alias, ProjectAliasBucket)
project, err := db.GetRecord(db.DBName, alias, ProjectAliasBucket)
if err == nil {
pname = project
} else {
pname = alias
}
err = db.UpdateRec(pname, status, StatusBucket)
err = db.UpdateRec(db.DBName, pname, status, StatusBucket)
if err != nil {
return fmt.Errorf("Error updating record : %w", err)
fmt.Println("Error updating record : ", err)
return err
}
fmt.Printf("Project %s set to status %s\n", pname, status)

return nil
},
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/theredditbandit/pman/pkg/utils"
)

var (
ErrBadUsageStatusCmd = errors.New("bad usage of status command")
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Expand All @@ -18,24 +22,25 @@ var statusCmd = &cobra.Command{
RunE: func(_ *cobra.Command, args []string) error {
var alias string
if len(args) != 1 {
return errors.New("Please provide a project name")
fmt.Println("Please provide a project name")
return ErrBadUsageStatusCmd
theredditbandit marked this conversation as resolved.
Show resolved Hide resolved
}
projName := args[0]
actualName, err := db.GetRecord(projName, ProjectAliasBucket)
actualName, err := db.GetRecord(db.DBName, projName, ProjectAliasBucket)
if err == nil { // check if user has supplied an alias instead of actual project name
alias = projName
projName = actualName
}
status, err := db.GetRecord(projName, StatusBucket)
status, err := db.GetRecord(db.DBName, projName, StatusBucket)
if err != nil {
return fmt.Errorf("%s is not a valid project name : Err -> %w", projName, err)
fmt.Printf("%s is not a valid project name : Err -> %s", projName, err)
return err
}
if alias != "" {
fmt.Printf("Status of %s (%s) : %s\n", projName, alias, utils.TitleCase(status))
} else {
fmt.Printf("Status of %s : %s\n", projName, utils.TitleCase(status))
}

return nil
},
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/glamour v0.7.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.1
github.com/thoas/go-funk v0.9.3
go.etcd.io/bbolt v1.3.10
golang.org/x/text v0.15.0
Expand All @@ -18,6 +19,7 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/gorilla/css v1.0.1 // indirect
Expand All @@ -32,6 +34,7 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -41,4 +44,5 @@ require (
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
Expand All @@ -96,7 +100,9 @@ golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading