Skip to content

Commit

Permalink
Adding function for removal of duplicate strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Fearne committed Aug 12, 2022
1 parent 1765e5d commit 65160cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/storage/fs/cback/cback.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (fs *cback) Download(ctx context.Context, ref *provider.Reference) (io.Read

if md.Type == provider.ResourceType_RESOURCE_TYPE_FILE {

responseData, err := fs.getRequest(user.Username, url, requestType)
responseData, err := fs.getRequest(user.Username, url, requestType, nil)

if err != nil {
return nil, err
Expand Down
36 changes: 28 additions & 8 deletions pkg/storage/fs/cback/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ func mapReturn(fileType string) (int, error) {

}

func (fs *cback) getRequest(userName, url string, reqType string) (io.ReadCloser, error) {
func (fs *cback) getRequest(userName, url string, reqType string, body io.Reader) (io.ReadCloser, error) {

req, err := http.NewRequest(reqType, url, nil)
req, err := http.NewRequest(reqType, url, body)
req.SetBasicAuth(userName, fs.conf.ImpersonatorToken)

if body != nil {
req.Header.Add("Content-Type", "application/json")
}

if err != nil {
return nil, err
}
Expand All @@ -122,14 +126,18 @@ func (fs *cback) getRequest(userName, url string, reqType string) (io.ReadCloser
return nil, errtypes.PermissionDenied("cback: user has no permissions to get the backup")
}

if resp.StatusCode == 400 {
return nil, errtypes.BadRequest("cback")
}

return resp.Body, nil
}

func (fs *cback) listSnapshots(userName string, backupID int) ([]snapshotResponse, error) {

url := fs.conf.APIURL + "/backups/" + strconv.Itoa(backupID) + "/snapshots"
requestType := "GET"
responseData, err := fs.getRequest(userName, url, requestType)
responseData, err := fs.getRequest(userName, url, requestType, nil)

if err != nil {
return nil, err
Expand All @@ -148,7 +156,7 @@ func (fs *cback) matchBackups(userName, pathInput string) (*backUpResponse, erro

url := fs.conf.APIURL + "/backups/"
requestType := "GET"
responseData, err := fs.getRequest(userName, url, requestType)
responseData, err := fs.getRequest(userName, url, requestType, nil)

if err != nil {
return nil, err
Expand Down Expand Up @@ -198,7 +206,7 @@ func (fs *cback) statResource(backupID int, snapID, userName, path, source strin
url := fs.conf.APIURL + "/backups/" + strconv.Itoa(backupID) + "/snapshots/" + snapID + "/" + path + "?content=false"
requestType := "OPTIONS"

responseData, err := fs.getRequest(userName, url, requestType)
responseData, err := fs.getRequest(userName, url, requestType, nil)

if err != nil {
return nil, err
Expand Down Expand Up @@ -231,7 +239,7 @@ func (fs *cback) fileSystem(backupID int, snapID, userName, path, source string)
url := fs.conf.APIURL + "/backups/" + strconv.Itoa(backupID) + "/snapshots/" + snapID + "/" + path + "?content=true"
requestType := "OPTIONS"

responseData, err := fs.getRequest(userName, url, requestType)
responseData, err := fs.getRequest(userName, url, requestType, nil)

if err != nil {
return nil, err
Expand Down Expand Up @@ -281,7 +289,7 @@ func (fs *cback) timeConv(timeInput string) (int64, error) {
func (fs *cback) pathFinder(userName, path string) ([]string, error) {
url := fs.conf.APIURL + "/backups/"
requestType := "GET"
responseData, err := fs.getRequest(userName, url, requestType)
responseData, err := fs.getRequest(userName, url, requestType, nil)
matchFound := false

if err != nil {
Expand Down Expand Up @@ -312,7 +320,7 @@ func (fs *cback) pathFinder(userName, path string) ([]string, error) {
}

if matchFound {
return returnString, nil
return duplicateRemoval(returnString), nil
}

return nil, errtypes.NotFound("cback: resource not found")
Expand All @@ -339,3 +347,15 @@ func (fs *cback) pathTrimmer(snapshotList []snapshotResponse, resp *backUpRespon

return ssID, searchPath
}

func duplicateRemoval(strSlice []string) []string {
inList := make(map[string]bool)
var list []string
for _, str := range strSlice {
if _, value := inList[str]; !value {
inList[str] = true
list = append(list, str)
}
}
return list
}

0 comments on commit 65160cd

Please sign in to comment.