Skip to content

Commit

Permalink
rebase on master
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick2009 committed May 17, 2024
1 parent d36532d commit fefa698
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 42 deletions.
3 changes: 2 additions & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/theredditbandit/pman/pkg/db"
"github.com/theredditbandit/pman/pkg/utils"
)

Expand All @@ -23,7 +24,7 @@ var infoCmd = &cobra.Command{
return ErrBadUsageInfoCmd
}
projectName := args[0]
infoData, err := utils.ReadREADME(projectName)
infoData, err := utils.ReadREADME(db.DBName, projectName)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/theredditbandit/pman/pkg/db"
)

const dbname = db.DBTestName
const bucketName = "testBucket"
const key = "testKey"
const (
dbname = db.DBTestName
bucketName = "testBucket"
key = "testKey"
)

func Test_GetDBLoc(t *testing.T) {
t.Run("Test getDBLoc", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func RenderInteractiveTable(data map[string]string) error {
var rows []table.Row
for proj, status := range data {
alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(proj)
lastEdited := utils.GetLastModifiedTime(db.DBName, proj)
if err == nil {
pname := fmt.Sprintf("%s (%s)", proj, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
Expand Down
3 changes: 2 additions & 1 deletion pkg/ui/pager/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/theredditbandit/pman/pkg/db"
"github.com/theredditbandit/pman/pkg/utils"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ func (m model) footerView() string {
}

func LaunchRenderer(file string) error {
content, err := utils.ReadREADME(file)
content, err := utils.ReadREADME(db.DBName, file)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RenderTable(data map[string]string) error {
var tableData [][]string
for p, status := range data {
alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(p)
lastEdited := utils.GetLastModifiedTime(db.DBName, p)
if err == nil {
pname := fmt.Sprintf("%s (%s)", p, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
Expand Down
49 changes: 14 additions & 35 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var (
ErrBeautifyMD = errors.New("error beautifying markdown")
ErrGetProject = errors.New("error getting project")
ErrReadREADME = errors.New("error reading README")
)

Expand All @@ -36,40 +37,23 @@ func FilterByStatus(data map[string]string, status string) map[string]string {
return filteredData
}

// Deprecated: Use ui.RenderTable instead
func PrintData(data map[string]string) {
for k, v := range data {
alias, err := db.GetRecord(db.DBName, k, pkg.ProjectAliasBucket)
if err == nil {
log.Printf("%s : %s (%s) \n", TitleCase(v), k, alias)
} else {
log.Printf("%s : %s \n", TitleCase(v), k)
}
}
}

func GetLastModifiedTime(pname string) string {
func GetLastModifiedTime(dbname, pname string) string {
var lastModTime time.Time
var lastModFile string
today := time.Now()
_ = lastModFile
pPath, err := db.GetRecord(db.DBName, pname, pkg.ProjectPaths)
pPath, err := db.GetRecord(dbname, pname, pkg.ProjectPaths)
if err != nil {
return "Something went wrong"
}
err = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
_ = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {

Check failure on line 49 in pkg/utils/utils.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

unused-parameter: parameter 'err' seems to be unused, consider removing or renaming it as _ (revive)
if !info.IsDir() && info.ModTime().After(lastModTime) {
lastModTime = info.ModTime()
lastModFile = info.Name()
}
return nil
})
if err != nil {
return "Something went wrong"
}

switch fmt.Sprint(lastModTime.Date()) {
case fmt.Sprint(today.Date()):
return fmt.Sprintf("Today %s", lastModTime.Format("15:04"))
Expand All @@ -95,25 +79,20 @@ func BeautifyMD(data []byte) (string, error) {
}

// ReadREADME: returns the byte array of README.md of a project
func ReadREADME(projectName string) ([]byte, error) {
path, err := db.GetRecord(db.DBName, projectName, pkg.ProjectPaths)
if err != nil {
actualName, err := db.GetRecord(db.DBName, projectName, pkg.ProjectAliasBucket)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrReadREADME, err)
}
func ReadREADME(dbname, projectName string) ([]byte, error) {
actualName, err := db.GetRecord(dbname, projectName, pkg.ProjectAliasBucket)
if err == nil {
projectName = actualName
path, err = db.GetRecord(db.DBName, projectName, pkg.ProjectPaths)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrReadREADME, err)
}
}
path, err := db.GetRecord(dbname, projectName, pkg.ProjectPaths)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrGetProject, err)
}
pPath := filepath.Join(path, "README.md")
data, err := os.ReadFile(pPath)
if err != nil {
return nil, fmt.Errorf("something went wrong while reading README for %s: %w", projectName, err)
return nil, errors.Join(ErrReadREADME, err)
}
return data, nil
}
Loading

0 comments on commit fefa698

Please sign in to comment.