Skip to content

Commit

Permalink
Remove duplicate ReadFileBytes utility code and check for string zero…
Browse files Browse the repository at this point in the history
… value (#48)

* Remove duplicate code and check string zero value for ReadFileBytes
  • Loading branch information
pgporada committed Jan 4, 2024
1 parent 31cf77b commit c2f045e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 51 deletions.
2 changes: 2 additions & 0 deletions assets/cla/consent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@
email: Michael94Ellis@gmail.com
- name: Peter Oettig
email: oss-cla@oettig.de
- name: Phil Porada
email: philporada@gmail.com

43 changes: 0 additions & 43 deletions internal/utils/file.go

This file was deleted.

13 changes: 7 additions & 6 deletions pkg/identity/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package identity
import (
"encoding/json"
"fmt"
"github.com/greenpau/go-authcrunch/internal/utils"
"github.com/greenpau/go-authcrunch/pkg/errors"
"github.com/greenpau/go-authcrunch/pkg/requests"
"github.com/greenpau/go-authcrunch/pkg/util"
"github.com/greenpau/versioned"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/greenpau/go-authcrunch/pkg/errors"
"github.com/greenpau/go-authcrunch/pkg/requests"
"github.com/greenpau/go-authcrunch/pkg/util"
fileutil "github.com/greenpau/go-authcrunch/pkg/util/file"
"github.com/greenpau/versioned"
)

var (
Expand Down Expand Up @@ -142,7 +143,7 @@ func NewDatabase(fp string) (*Database, error) {
if fileInfo.IsDir() {
return nil, errors.ErrNewDatabase.WithArgs(fp, "path points to a directory")
}
b, err := utils.ReadFileBytes(fp)
b, err := fileutil.ReadFileBytes(fp)
if err != nil {
return nil, errors.ErrNewDatabase.WithArgs(fp, err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/util/file/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package file
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -80,6 +81,9 @@ func ReadFile(filePath string) (string, error) {
}

func expandHomePath(fp string) (string, error) {
if fp == "" {
return fp, fmt.Errorf("cannot expand an empty string")
}
if fp[0] != '~' {
return fp, nil
}
Expand All @@ -93,8 +97,10 @@ func expandHomePath(fp string) (string, error) {

// ReadFileBytes expands home directory and reads a file.
func ReadFileBytes(fp string) ([]byte, error) {
var err error
fp, err = expandHomePath(fp)
if fp == "" {
return nil, fmt.Errorf("cannot expand an empty string")
}
fp, err := expandHomePath(fp)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c2f045e

Please sign in to comment.