Skip to content

Commit

Permalink
refactor ReadReadme func (#62)
Browse files Browse the repository at this point in the history
* add scaffolding for pman cd

* add director func

* refactor ReadREADME func to only do one thing

implement the logic to get the project path in a separate function

* remove cd.go
  • Loading branch information
theredditbandit authored Jul 2, 2024
1 parent d014145 commit 00bd6f6
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,36 @@ func BeautifyMD(data []byte) (string, error) {
return out, nil
}

// ReadREADME: returns the byte array of README.md of a project
func ReadREADME(dbname, projectName string) ([]byte, error) {
// GetProjectPath: returns the path to the README inside the project
func GetProjectPath(dbname, projectName string) (string, error) {
path, err := db.GetRecord(dbname, projectName, c.ProjectPaths)
if err != nil {
actualName, err := db.GetRecord(dbname, projectName, c.ProjectAliasBucket)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrGetAlias, err)
return "", errors.Join(ErrGetAlias, err)
}
projectName = actualName
path, err = db.GetRecord(dbname, projectName, c.ProjectPaths)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrGetProject, err)
return "", errors.Join(ErrGetProject, err)
}
}
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
return pPath, err
}

// ReadREADME: returns the byte array of README.md of a project
func ReadREADME(dbname, projectName string) ([]byte, error) {
pPath, err := GetProjectPath(dbname, projectName)
if err != nil {
if os.IsNotExist(err) {
msg := fmt.Sprintf("# README does not exist for %s", projectName)
return []byte(msg), nil
}
return nil, err
}
data, err := os.ReadFile(pPath)
if err != nil {
Expand All @@ -112,6 +121,7 @@ func UpdateLastEditedTime() error {
return err
}
return nil

}

func DayPassed(t string) bool {
Expand Down

0 comments on commit 00bd6f6

Please sign in to comment.