diff --git a/assets/cla/consent.yaml b/assets/cla/consent.yaml index cf3edf6..2fef2d0 100644 --- a/assets/cla/consent.yaml +++ b/assets/cla/consent.yaml @@ -43,4 +43,6 @@ email: Michael94Ellis@gmail.com - name: Peter Oettig email: oss-cla@oettig.de +- name: Phil Porada + email: philporada@gmail.com diff --git a/internal/utils/file.go b/internal/utils/file.go deleted file mode 100644 index e33d7a7..0000000 --- a/internal/utils/file.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2022 Paul Greenberg greenpau@outlook.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package utils - -import ( - "io/ioutil" - "os" - "path/filepath" -) - -func expandHomePath(fp string) (string, error) { - if fp[0] != '~' { - return fp, nil - } - hd, err := os.UserHomeDir() - if err != nil { - return fp, err - } - fp = filepath.Join(hd, fp[1:]) - return fp, nil -} - -// ReadFileBytes expands home directory and reads a file. -func ReadFileBytes(fp string) ([]byte, error) { - var err error - fp, err = expandHomePath(fp) - if err != nil { - return nil, err - } - return ioutil.ReadFile(fp) -} diff --git a/pkg/identity/database.go b/pkg/identity/database.go index ced82da..c3efffe 100644 --- a/pkg/identity/database.go +++ b/pkg/identity/database.go @@ -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 ( @@ -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) } diff --git a/pkg/util/file/utils.go b/pkg/util/file/utils.go index 3c7229e..98fab21 100644 --- a/pkg/util/file/utils.go +++ b/pkg/util/file/utils.go @@ -17,6 +17,7 @@ package file import ( "bufio" "bytes" + "fmt" "io/ioutil" "os" "path/filepath" @@ -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 } @@ -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 }