-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from mouuff/feature/gzip
Feature/gzip
- Loading branch information
Showing
22 changed files
with
354 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package provider | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"time" | ||
) | ||
|
||
// GetLatestVersionFromPath finds the latest version from a path | ||
// This is used by provider zip and gzip | ||
// Example: /example/binaries-v1.4.53.zip is going to match "v1.4.53" | ||
func GetLatestVersionFromPath(path string) (string, error) { | ||
// TODO unittest | ||
versionRegex := regexp.MustCompile(`v[0-9]+\.[0-9]+\.[0-9]+`) | ||
version := string(versionRegex.Find([]byte(filepath.Base(path)))) | ||
if version == "" { | ||
return "", ErrProviderUnavaiable | ||
} | ||
return version, nil | ||
} | ||
|
||
// GlobNewestFile same as filepath.Glob but returns only one file with the latest modification time | ||
func GlobNewestFile(pattern string) (string, error) { | ||
matches, err := filepath.Glob(pattern) | ||
if err != nil { | ||
return "", err | ||
} | ||
var newestTime time.Time | ||
var newestFile string | ||
for _, match := range matches { | ||
file, err := os.Stat(match) | ||
if err == nil { | ||
if newestFile == "" || newestTime.Before(file.ModTime()) { | ||
newestFile = match | ||
newestTime = file.ModTime() | ||
} | ||
} | ||
} | ||
if newestFile == "" { | ||
return "", ErrFileNotFound | ||
} | ||
return newestFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package provider_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
provider "github.com/mouuff/go-rocket-update/pkg/provider" | ||
) | ||
|
||
func TestGetLatestVersionFromPath(t *testing.T) { | ||
version, err := provider.GetLatestVersionFromPath("binaries-v1.2.3.zip") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if version != "v1.2.3" { | ||
t.Error("version != 'v1.2.3'") | ||
} | ||
|
||
version, err = provider.GetLatestVersionFromPath("binaries-v.zip") | ||
if err == nil { | ||
t.Error("Should return an error") | ||
} | ||
} | ||
|
||
func TestGlobNewestFile(t *testing.T) { | ||
filename := "Allum1-v1.1.0.tar.gz" | ||
currentTime := time.Now().Local().Add(time.Second) | ||
err := os.Chtimes(filepath.Join("testdata", filename), currentTime, currentTime) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
match, err := provider.GlobNewestFile(filepath.Join("testdata", "Allum1-v*.tar.gz")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if filepath.Base(match) != filename { | ||
t.Error("Expected " + filename) | ||
} | ||
|
||
match, err = provider.GlobNewestFile(filepath.Join("testdata", "doesntexists")) | ||
if err == nil { | ||
t.Error("Should return an error if file doesn't exists") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package provider | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
// Decompress gets a provider to decompress zip or tar.gz files | ||
func Decompress(path string) (Provider, error) { | ||
if strings.HasSuffix(path, ".zip") { | ||
return &Zip{ | ||
Path: path, | ||
}, nil | ||
} else if strings.HasSuffix(path, ".tar.gz") { | ||
return &Gzip{ | ||
Path: path, | ||
}, nil | ||
} | ||
return nil, errors.New("provider.Decompress unknown file type for file: " + path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package provider_test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
provider "github.com/mouuff/go-rocket-update/pkg/provider" | ||
) | ||
|
||
func TestProviderDecompressZip(t *testing.T) { | ||
p, err := provider.Decompress(filepath.Join("testdata", "Allum1-v1.0.0.zip")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := p.Open(); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer p.Close() | ||
|
||
err = ProviderTestWalkAndRetrieve(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestProviderDecompressGzip(t *testing.T) { | ||
p, err := provider.Decompress(filepath.Join("testdata", "Allum1-v1.0.0.tar.gz")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := p.Open(); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer p.Close() | ||
|
||
err = ProviderTestWalkAndRetrieve(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.