Skip to content

Commit

Permalink
Run goimports and update all caps constants to idiomatic go
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 committed Oct 1, 2020
1 parent a61cd66 commit effb92e
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 101 deletions.
4 changes: 2 additions & 2 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
func verifyChecksumOfReleaseAsset(assetPath string, checksumMap map[string]bool, algorithm string) *FetchError {
computedChecksum, err := computeChecksum(assetPath, algorithm)
if err != nil {
return newError(ERROR_WHILE_COMPUTING_CHECKSUM, err.Error())
return newError(errorWhileComputingChecksum, err.Error())
}
if found, _ := checksumMap[computedChecksum]; !found {
keys := reflect.ValueOf(checksumMap).MapKeys()
return newError(CHECKSUM_DOES_NOT_MATCH, fmt.Sprintf("Expected to checksum value to be one of %s, but instead got %s for Release Asset at %s. This means that either you are using the wrong checksum value in your call to fetch, (e.g. did you update the version of the module you're installing but not the checksum?) or that someone has replaced the asset with a potentially dangerous one and you should be very careful about proceeding.", keys, computedChecksum, assetPath))
return newError(checksumDoesNotMatch, fmt.Sprintf("Expected to checksum value to be one of %s, but instead got %s for Release Asset at %s. This means that either you are using the wrong checksum value in your call to fetch, (e.g. did you update the version of the module you're installing but not the checksum?) or that someone has replaced the asset with a potentially dangerous one and you should be very careful about proceeding.", keys, computedChecksum, assetPath))
}
fmt.Printf("Release asset checksum verified for %s\n", assetPath)

Expand Down
8 changes: 4 additions & 4 deletions fetch_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (e *FetchError) Error() string {
func newError(errorCode int, details string) *FetchError {
return &FetchError{
errorCode: errorCode,
details: details,
err: nil,
details: details,
err: nil,
}
}

Expand All @@ -28,7 +28,7 @@ func wrapError(err error) *FetchError {
}
return &FetchError{
errorCode: -1,
details: err.Error(),
err: err,
details: err.Error(),
err: err,
}
}
14 changes: 7 additions & 7 deletions fetch_error_constants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

const INVALID_TAG_CONSTRAINT_EXPRESSION = 100
const invalidTagConstraintExpression = 100

const GITHUB_REPO_URL_MALFORMED_OR_NOT_PARSEABLE = 300
const githubRepoUrlMalformedOrNotParseable = 300

const INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED = 401
const REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED = 404
const invalidGithubTokenOrAccessDenied = 401
const repoDoesNotExistOrAccessDenied = 404

const FAILED_TO_DOWNLOAD_FILE = 500
const CHECKSUM_DOES_NOT_MATCH = 510
const ERROR_WHILE_COMPUTING_CHECKSUM = 520
const failedToDownloadFile = 500
const checksumDoesNotMatch = 510
const errorWhileComputingChecksum = 520
2 changes: 1 addition & 1 deletion fetch_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ func TestNewError(t *testing.T) {
t.Parallel()

_ = newError(1, "My error details")
}
}
14 changes: 7 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"io/ioutil"
"os"
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"bytes"
"archive/zip"
"strings"
)

Expand Down Expand Up @@ -38,10 +38,10 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
return zipFilePath, wrapError(err)
}
if resp.StatusCode != http.StatusOK {
return zipFilePath, newError(FAILED_TO_DOWNLOAD_FILE, fmt.Sprintf("Failed to download file at the url %s. Received HTTP Response %d.", req.URL.String(), resp.StatusCode))
return zipFilePath, newError(failedToDownloadFile, fmt.Sprintf("Failed to download file at the url %s. Received HTTP Response %d.", req.URL.String(), resp.StatusCode))
}
if resp.Header.Get("Content-Type") != "application/zip" {
return zipFilePath, newError(FAILED_TO_DOWNLOAD_FILE, fmt.Sprintf("Failed to download file at the url %s. Expected HTTP Response's \"Content-Type\" header to be \"application/zip\", but was \"%s\"", req.URL.String(), resp.Header.Get("Content-Type")))
return zipFilePath, newError(failedToDownloadFile, fmt.Sprintf("Failed to download file at the url %s. Expected HTTP Response's \"Content-Type\" header to be \"application/zip\", but was \"%s\"", req.URL.String(), resp.Header.Get("Content-Type")))
}

// Copy the contents of the downloaded file to our empty file
Expand Down Expand Up @@ -78,7 +78,7 @@ func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
// Check if (pathPrefix + "/") is a prefix in f.Name, if yes, we extract this file.

zipPathIsFile := !zipPath.FileInfo().IsDir()
return (zipPathIsFile && zipPath.Name == pathPrefix) || strings.Index(zipPath.Name, pathPrefix + "/") == 0
return (zipPathIsFile && zipPath.Name == pathPrefix) || strings.Index(zipPath.Name, pathPrefix+"/") == 0
}

// Decompress the file at zipFileAbsPath and move only those files under filesToExtractFromZipPath to localPath
Expand Down
46 changes: 23 additions & 23 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"testing"
"path/filepath"
"io/ioutil"
"fmt"
"strings"
"testing"
)

// Although other tests besides those in this file require this env var, this init() func will cover all tests.
Expand All @@ -28,11 +28,11 @@ func TestDownloadGitTagZipFile(t *testing.T) {

enterpriseGitHubExample := GitHubInstance{
BaseUrl: "github.acme.com",
ApiUrl: "github.acme.com/api/v3",
ApiUrl: "github.acme.com/api/v3",
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
gitTag string
Expand All @@ -47,7 +47,7 @@ func TestDownloadGitTagZipFile(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
GitTag: tc.gitTag,
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestDownloadGitBranchZipFile(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
branchName string
Expand All @@ -109,7 +109,7 @@ func TestDownloadGitBranchZipFile(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
BranchName: tc.branchName,
}
Expand All @@ -135,7 +135,7 @@ func TestDownloadBadGitBranchZipFile(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
branchName string
Expand All @@ -148,7 +148,7 @@ func TestDownloadBadGitBranchZipFile(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
BranchName: tc.branchName,
}
Expand All @@ -170,7 +170,7 @@ func TestDownloadGitCommitFile(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
commitSha string
Expand All @@ -186,7 +186,7 @@ func TestDownloadGitCommitFile(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
CommitSha: tc.commitSha,
}
Expand All @@ -212,7 +212,7 @@ func TestDownloadBadGitCommitFile(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
commitSha string
Expand All @@ -230,7 +230,7 @@ func TestDownloadBadGitCommitFile(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
CommitSha: tc.commitSha,
}
Expand All @@ -252,7 +252,7 @@ func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
repoOwner string
repoName string
gitTag string
Expand All @@ -265,7 +265,7 @@ func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
gitHubCommit := GitHubCommit{
Repo: GitHubRepo{
Owner: tc.repoOwner,
Name: tc.repoName,
Name: tc.repoName,
},
GitTag: tc.gitTag,
}
Expand All @@ -286,17 +286,17 @@ func TestExtractFiles(t *testing.T) {
}

cases := []struct {
instance GitHubInstance
instance GitHubInstance
localFilePath string
filePathToExtract string
expectedNumFiles int
nonemptyFiles []string
}{
{publicGitHub, "test-fixtures/fetch-test-public-0.0.1.zip", "/", 1, nil},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.2.zip", "/", 2, nil},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"} },
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"}},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.3.zip", "/folder", 2, nil},
{publicGitHub, "test-fixtures/fetch-test-public-0.0.4.zip", "/aaa", 2, []string{"/hello.txt", "/subaaa/subhello.txt"} },
{publicGitHub, "test-fixtures/fetch-test-public-0.0.4.zip", "/aaa", 2, []string{"/hello.txt", "/subaaa/subhello.txt"}},
}

for _, tc := range cases {
Expand All @@ -315,21 +315,21 @@ func TestExtractFiles(t *testing.T) {
// Count the number of files in the directory
var numFiles int
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if ! info.IsDir() {
if !info.IsDir() {
numFiles++
}
return nil
})

if (numFiles != tc.expectedNumFiles) {
if numFiles != tc.expectedNumFiles {
t.Fatalf("While extracting %s, expected to find %d file(s), but found %d. Local path = %s", tc.localFilePath, tc.expectedNumFiles, numFiles, tempDir)
}

// Ensure that files declared to be non-empty are in fact non-empty
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)

if ! info.IsDir() && stringInSlice(relativeFilename, tc.nonemptyFiles) {
if !info.IsDir() && stringInSlice(relativeFilename, tc.nonemptyFiles) {
if info.Size() == 0 {
t.Fatalf("Expected %s in %s to have non-zero file size, but found file size = %d.\n", relativeFilename, tc.localFilePath, info.Size())
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestExtractFilesExtractFile(t *testing.T) {
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)

if ! info.IsDir() {
if !info.IsDir() {
if relativeFilename != localFileName {
t.Fatalf("Expected local file %s to be created, but not found.\n", localFileName)
}
Expand Down
10 changes: 5 additions & 5 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ParseUrlIntoGithubInstance(repoUrl string, apiv string) (GitHubInstance, *F

u, err := url.Parse(repoUrl)
if err != nil {
return instance, newError(GITHUB_REPO_URL_MALFORMED_OR_NOT_PARSEABLE, fmt.Sprintf("GitHub Repo URL %s is malformed.", repoUrl))
return instance, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s is malformed.", repoUrl))
}

baseUrl := u.Host
Expand Down Expand Up @@ -138,12 +138,12 @@ func ParseUrlIntoGitHubRepo(url string, token string, instance GitHubInstance) (

regex, regexErr := regexp.Compile("https?://(?:www\\.)?" + instance.BaseUrl + "/(.+?)/(.+?)(?:$|\\?|#|/)")
if regexErr != nil {
return gitHubRepo, newError(GITHUB_REPO_URL_MALFORMED_OR_NOT_PARSEABLE, fmt.Sprintf("GitHub Repo URL %s is malformed.", url))
return gitHubRepo, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s is malformed.", url))
}

matches := regex.FindStringSubmatch(url)
if len(matches) != 3 {
return gitHubRepo, newError(GITHUB_REPO_URL_MALFORMED_OR_NOT_PARSEABLE, fmt.Sprintf("GitHub Repo URL %s could not be parsed correctly", url))
return gitHubRepo, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s could not be parsed correctly", url))
}

gitHubRepo = GitHubRepo{
Expand Down Expand Up @@ -239,7 +239,7 @@ func callGitHubApi(repo GitHubRepo, path string, customHeaders map[string]string

type writeCounter struct {
written uint64
suffix string // contains " / SIZE MB" if size is known, otherwise empty
suffix string // contains " / SIZE MB" if size is known, otherwise empty
}

func newWriteCounter(total int64) *writeCounter {
Expand Down Expand Up @@ -279,7 +279,7 @@ func writeResonseToDisk(resp *http.Response, destPath string, withProgress bool)
defer resp.Body.Close()

var readCloser io.Reader
if withProgress{
if withProgress {
readCloser = io.TeeReader(resp.Body, newWriteCounter(resp.ContentLength))
} else {
readCloser = resp.Body
Expand Down
Loading

0 comments on commit effb92e

Please sign in to comment.