From b4ad26e294697963d35a00d70eb6231928041a68 Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Thu, 16 May 2024 00:42:31 +0530 Subject: [PATCH 1/7] add a friendlier message when a README file does not exist --- pkg/utils/utils.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 231d7c6..affdef2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -101,6 +101,11 @@ func ReadREADME(dbname, projectName string) ([]byte, error) { } } pPath := filepath.Join(path, "README.md") + _, err = os.Stat(pPath) + if os.IsNotExist(err) { + msg := fmt.Sprintf("# README does not exist for %s", projectName) + return []byte(msg), nil + } data, err := os.ReadFile(pPath) if err != nil { return nil, errors.Join(ErrReadREADME, fmt.Errorf("something went wrong while reading README for %s: %w", projectName, err)) From 08415560062b7bcdf9708fcc38eeac9bdaab4b45 Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Thu, 16 May 2024 00:49:48 +0530 Subject: [PATCH 2/7] update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ee1434..731c343 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Use "pman [command] --help" for more information about a command. ## How does it work ### init -when you run `pman init .` in any directory, it will look for subdirectories that contain a README.md or a .git folder and consider it as a project directory. +When you run `pman init .` in any directory, it will look for subdirectories that contain a README.md or a .git folder and consider it as a project directory. ![image](https://github.com/theredditbandit/pman/assets/85390033/b9d6fcd3-41ca-4bd2-aa32-9b3e9bff1be8) ### set, status, info and filter From 38649d760ef773c81526d26d1c2f70ca1bfc20eb Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Sat, 18 May 2024 18:49:53 +0530 Subject: [PATCH 3/7] wip: make pman ls faster --- pkg/indexer.go | 1 + pkg/utils/utils.go | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/indexer.go b/pkg/indexer.go index dff5575..685cba2 100644 --- a/pkg/indexer.go +++ b/pkg/indexer.go @@ -13,6 +13,7 @@ const ( StatusBucket = "projects" ProjectPaths = "projectPaths" ProjectAliasBucket = "projectAliases" + LastUpdatedBucket = "lastUpdated" ) var ( diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index affdef2..4056523 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -42,9 +42,7 @@ func FilterByStatuses(data map[string]string, status []string) map[string]string func GetLastModifiedTime(dbname, pname string) string { var lastModTime time.Time - var lastModFile string today := time.Now() - _ = lastModFile pPath, err := db.GetRecord(dbname, pname, pkg.ProjectPaths) if err != nil { return "Something went wrong" @@ -55,7 +53,6 @@ func GetLastModifiedTime(dbname, pname string) string { } if !info.IsDir() && info.ModTime().After(lastModTime) { lastModTime = info.ModTime() - lastModFile = info.Name() } return nil }) From 914be5952770917908fe0674984fa5ff85af471a Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Mon, 20 May 2024 23:04:22 +0530 Subject: [PATCH 4/7] fix tests for update implementation of ReadREADME --- pkg/utils/utils_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 27852e7..d15a81a 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -1,6 +1,7 @@ package utils_test import ( + "fmt" "os" "testing" "time" @@ -181,7 +182,7 @@ func Test_ReadREADME(t *testing.T) { }) t.Run("Test ReadREADME with invalid README file name", func(t *testing.T) { - expected := []byte(nil) + expected := []byte(fmt.Sprintf("# README does not exist for %s", projectName)) t.Cleanup(func() { err := db.DeleteDb(dbname) @@ -201,7 +202,7 @@ func Test_ReadREADME(t *testing.T) { actual, err := utils.ReadREADME(dbname, projectName) assert.Equal(t, expected, actual) - require.ErrorIs(t, err, utils.ErrReadREADME) + require.NoError(t, err) }) } From 62794deb35838c1042b01089cff8a3dfe20d777b Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Tue, 21 May 2024 06:58:31 +0530 Subject: [PATCH 5/7] record last edit time whenever db changes are made --- cmd/alias.go | 10 +++++++++- cmd/delete.go | 9 +++++++++ cmd/root.go | 1 + cmd/set.go | 11 +++++++++++ pkg/indexer.go | 11 +++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/cmd/alias.go b/cmd/alias.go index e85ff66..b925a84 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -3,6 +3,8 @@ package cmd import ( "errors" "fmt" + "log" + "time" "github.com/spf13/cobra" @@ -42,7 +44,13 @@ avlpn or something smaller and use that to query pman`, if err != nil { return err } - + lastEdit := make(map[string]string) + lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) + if err != nil { + log.Print(err) + return err + } return nil }, } diff --git a/cmd/delete.go b/cmd/delete.go index b759380..2f7ccac 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -3,6 +3,8 @@ package cmd import ( "errors" "fmt" + "log" + "time" "github.com/spf13/cobra" @@ -52,6 +54,13 @@ var delCmd = &cobra.Command{ if err != nil { return err } + lastEdit := make(map[string]string) + lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) + if err != nil { + log.Print(err) + return err + } fmt.Printf("Successfully deleted %s from the db \n", projName) return nil }, diff --git a/cmd/root.go b/cmd/root.go index 4889b96..ffbf314 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ const ( StatusBucket = "projects" ProjectPathBucket = "projectPaths" ProjectAliasBucket = "projectAliases" + ConfigBucket = "config" version = "1.0" ) diff --git a/cmd/set.go b/cmd/set.go index e72ad48..4ac54bd 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -3,6 +3,8 @@ package cmd import ( "errors" "fmt" + "log" + "time" "github.com/spf13/cobra" @@ -47,6 +49,15 @@ var setCmd = &cobra.Command{ fmt.Println("Error updating record : ", err) return err } + + lastEdit := make(map[string]string) + lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) + if err != nil { + log.Print(err) + return err + } + fmt.Printf("Project %s set to status %s\n", pname, status) return nil }, diff --git a/pkg/indexer.go b/pkg/indexer.go index 685cba2..4e0d6b1 100644 --- a/pkg/indexer.go +++ b/pkg/indexer.go @@ -2,9 +2,11 @@ package pkg import ( "errors" + "fmt" "log" "os" "path/filepath" + "time" "github.com/theredditbandit/pman/pkg/db" ) @@ -14,6 +16,7 @@ const ( ProjectPaths = "projectPaths" ProjectAliasBucket = "projectAliases" LastUpdatedBucket = "lastUpdated" + ConfigBucket = "config" ) var ( @@ -61,6 +64,14 @@ func InitDirs(args []string) error { log.Print(err) return err } + lastEdit := make(map[string]string) + lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) + if err != nil { + log.Print(err) + return err + } + return nil } From 33f6f03bbc857055781ea564001e711db3711e26 Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Tue, 21 May 2024 11:51:50 +0530 Subject: [PATCH 6/7] perf: make pman ls ~4x faster this is done by only checking the lastEdited value once a day and then caching those values in the db --- cmd/ls.go | 6 +++-- pkg/ui/interactiveTable.go | 45 +++++++++++++++++++++++++++++++++++--- pkg/ui/statusTable.go | 44 ++++++++++++++++++++++++++++++++++--- pkg/utils/utils.go | 21 ++++++++++++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index b2e5007..1ad9b3b 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -20,6 +20,7 @@ var lsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) error { filterFlag, _ := cmd.Flags().GetString("f") oldUI, _ := cmd.Flags().GetBool("c") + refreshLastEditTime, _ := cmd.Flags().GetBool("r") data, err := db.GetAllRecords(db.DBName, StatusBucket) if err != nil { return err @@ -29,9 +30,9 @@ var lsCmd = &cobra.Command{ data = utils.FilterByStatuses(data, strings.Split(filterFlag, ",")) } if oldUI { - return ui.RenderTable(data) + return ui.RenderTable(data, refreshLastEditTime) } - return ui.RenderInteractiveTable(data) + return ui.RenderInteractiveTable(data, refreshLastEditTime) }, } @@ -39,4 +40,5 @@ func init() { rootCmd.AddCommand(lsCmd) lsCmd.Flags().String("f", "", "Filter projects by status. Usage : pman ls --f ") lsCmd.Flags().Bool("c", false, "list projects using the colorful table. Usage : pman ls --c") + lsCmd.Flags().Bool("r", false, "Refresh Last Edited time: pman ls --r") } diff --git a/pkg/ui/interactiveTable.go b/pkg/ui/interactiveTable.go index feb2eaa..ae79e3e 100644 --- a/pkg/ui/interactiveTable.go +++ b/pkg/ui/interactiveTable.go @@ -57,16 +57,55 @@ func (m tableModel) View() string { return baseStyle.Render(m.table.View()) + "\n" } -func RenderInteractiveTable(data map[string]string) error { +func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool) error { + var rows []table.Row + var lastEdited string + col := []table.Column{ {Title: "Status", Width: 20}, {Title: "Project", Width: 40}, {Title: "Last Edited", Width: 20}, } - var rows []table.Row + + if refreshLastEditedTime { + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } else { + rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket) + if err != nil { // lastRefreshTime key does not exist in db + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + } + for proj, status := range data { alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket) - lastEdited := utils.GetLastModifiedTime(db.DBName, proj) + if refreshLastEditedTime { + lastEdited = utils.GetLastModifiedTime(db.DBName, proj) + rec := map[string]string{proj: lastEdited} + err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket) + if err != nil { + return err + } + } else { + lE, err := db.GetRecord(db.DBName, proj, pkg.LastUpdatedBucket) + if err != nil { + return err + } + lastEdited = lE + } if err == nil { pname := fmt.Sprintf("%s (%s)", proj, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited diff --git a/pkg/ui/statusTable.go b/pkg/ui/statusTable.go index 73bcf9c..2ae266a 100644 --- a/pkg/ui/statusTable.go +++ b/pkg/ui/statusTable.go @@ -14,13 +14,51 @@ import ( ) // RenderTable: renders the given data as a table -func RenderTable(data map[string]string) error { +func RenderTable(data map[string]string, refreshLastEditedTime bool) error { var tableData [][]string + var lastEdited string + + if refreshLastEditedTime { + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } else { + rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket) + if err != nil { // lastRefreshTime key does not exist in db + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + } + for p, status := range data { alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket) - lastEdited := utils.GetLastModifiedTime(db.DBName, p) + if refreshLastEditedTime { + lastEdited = utils.GetLastModifiedTime(db.DBName, p) + rec := map[string]string{p: lastEdited} + err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket) + if err != nil { + return err + } + } else { + lE, err := db.GetRecord(db.DBName, p, pkg.LastUpdatedBucket) + if err != nil { + return err + } + lastEdited = lE + } if err == nil { - pname := fmt.Sprintf("%s (%s)", p, alias) + pname := fmt.Sprintf("%s (%s) nil error", p, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited tableData = append(tableData, row) } else { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 4056523..94c86d2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "strconv" "time" "github.com/charmbracelet/glamour" @@ -109,3 +110,23 @@ func ReadREADME(dbname, projectName string) ([]byte, error) { } return data, nil } + +func UpdateLastEditedTime() error { + r := fmt.Sprint(time.Now().Unix()) + rec := map[string]string{"lastRefreshTime": r} + err := db.WriteToDB(db.DBName, rec, pkg.ConfigBucket) + if err != nil { + return err + } + return nil +} + +func DayPassed(t string) bool { + oneDay := 86400 + now := time.Now().Unix() + recTime, _ := strconv.ParseInt(t, 10, 64) + if now-recTime > int64(oneDay) { + return true + } + return false +} From e1d8577f4c0bac9c3a32f4bd7ae7b73a8e54b1c2 Mon Sep 17 00:00:00 2001 From: aryan <85390033+theredditbandit@users.noreply.github.com> Date: Tue, 21 May 2024 12:04:27 +0530 Subject: [PATCH 7/7] implement ci suggestions --- cmd/alias.go | 2 +- cmd/delete.go | 2 +- cmd/set.go | 2 +- pkg/indexer.go | 2 +- pkg/utils/utils.go | 5 +---- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cmd/alias.go b/cmd/alias.go index b925a84..d56a6e8 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -45,7 +45,7 @@ avlpn or something smaller and use that to query pman`, return err } lastEdit := make(map[string]string) - lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + lastEdit["lastWrite"] = fmt.Sprint(time.Now().Format("02 Jan 06 15:04")) err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) if err != nil { log.Print(err) diff --git a/cmd/delete.go b/cmd/delete.go index 2f7ccac..ab66fdd 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -55,7 +55,7 @@ var delCmd = &cobra.Command{ return err } lastEdit := make(map[string]string) - lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + lastEdit["lastWrite"] = fmt.Sprint(time.Now().Format("02 Jan 06 15:04")) err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) if err != nil { log.Print(err) diff --git a/cmd/set.go b/cmd/set.go index 4ac54bd..81d5673 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -51,7 +51,7 @@ var setCmd = &cobra.Command{ } lastEdit := make(map[string]string) - lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + lastEdit["lastWrite"] = fmt.Sprint(time.Now().Format("02 Jan 06 15:04")) err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) if err != nil { log.Print(err) diff --git a/pkg/indexer.go b/pkg/indexer.go index 4e0d6b1..36d0a5c 100644 --- a/pkg/indexer.go +++ b/pkg/indexer.go @@ -65,7 +65,7 @@ func InitDirs(args []string) error { return err } lastEdit := make(map[string]string) - lastEdit["lastWrite"] = fmt.Sprintf(time.Now().Format("02 Jan 06 15:04")) + lastEdit["lastWrite"] = fmt.Sprint(time.Now().Format("02 Jan 06 15:04")) err = db.WriteToDB(db.DBName, lastEdit, ConfigBucket) if err != nil { log.Print(err) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 94c86d2..1da3031 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -125,8 +125,5 @@ func DayPassed(t string) bool { oneDay := 86400 now := time.Now().Unix() recTime, _ := strconv.ParseInt(t, 10, 64) - if now-recTime > int64(oneDay) { - return true - } - return false + return now-recTime > int64(oneDay) }