Skip to content

Commit

Permalink
#115 Feature/enable linting (#175)
Browse files Browse the repository at this point in the history
* re-enable linting

* check

* error handling

* remove unused function, simplify != false

* fix formatting issues

* use err

* ignore error for now

* update

* regex perf issue

* fatalf fix

* copy similar errors

* next time save before committing 🤦

Co-authored-by: Minimind <882485+jeff-mccoy@users.noreply.github.com>
  • Loading branch information
matt-strong and jeff-mccoy authored Dec 14, 2021
1 parent 67a6f28 commit 92ff8c0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 44 deletions.
19 changes: 9 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ repos:
rev: v0.4.0
hooks:
- id: go-fmt
# Commenting this one out for now since it fails
## Normally we wouldn't need to do a local hook but we need to modify the shell script that gets run to first change directories into the `cli` folder
# - repo: local
# hooks:
# - id: golangci-lint
# name: golangci-lint
# entry: hooks/run-golangci-lint.sh
# types: [ go ]
# language: script
# pass_filenames: false
# description: "Runs `golangci-lint`, requires https://github.com/golangci/golangci-lint"
- repo: local
hooks:
- id: golangci-lint
name: golangci-lint
entry: hooks/run-golangci-lint.sh
types: [ go ]
language: script
pass_filenames: false
description: "Runs `golangci-lint`, requires https://github.com/golangci/golangci-lint"
4 changes: 2 additions & 2 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var rootCmd = &cobra.Command{
return
}
}
cmd.Help()
_ = cmd.Help()
},
}

Expand Down Expand Up @@ -64,6 +64,6 @@ func setLogLevel(logLevel string) {
case "panic":
logrus.SetLevel(logrus.PanicLevel)
default:
logrus.Fatal("Unrecognized log level entry: %s", logLevel)
logrus.Fatalf("Unrecognized log level entry: %s", logLevel)
}
}
36 changes: 19 additions & 17 deletions cli/internal/git/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,27 @@ func checkoutHashAsBranch(path string, hash plumbing.Hash, branch plumbing.Refer

objRef, err := repo.Object(plumbing.AnyObject, hash)

var commitHash plumbing.Hash
switch objRef := objRef.(type) {
case *object.Tag:
commitHash = objRef.Target
case *object.Commit:
commitHash = objRef.Hash
default:
// This shouldn't ever hit, but we should at least log it if someday it
// does get hit
logContext.Debug("Unsupported tag hash type: " + objRef.Type().String())
logContext.Fatal("Checkout failed. Hash type not supported.")
}
if err != nil {
var commitHash plumbing.Hash
switch objRef := objRef.(type) {
case *object.Tag:
commitHash = objRef.Target
case *object.Commit:
commitHash = objRef.Hash
default:
// This shouldn't ever hit, but we should at least log it if someday it
// does get hit
logContext.Debug("Unsupported tag hash type: " + objRef.Type().String())
logContext.Fatal("Checkout failed. Hash type not supported.")
}

options := &git.CheckoutOptions{
Hash: commitHash,
Branch: branch,
Create: true,
options := &git.CheckoutOptions{
Hash: commitHash,
Branch: branch,
Create: true,
}
checkout(path, options)
}
checkout(path, options)
}

// checkout performs a `git checkout` on the path provided using the options provided
Expand Down
6 changes: 0 additions & 6 deletions cli/internal/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"net/url"
"os"
"path"
"regexp"
"strings"

Expand Down Expand Up @@ -48,11 +47,6 @@ func transformURL(baseUrl string, url string) string {
return output
}

func transformRepoDirToURL(baseUrl string, repoDir string) string {
baseDir := path.Base(repoDir)
return baseUrl + "/zarf-git-user/" + baseDir
}

func credentialFilePath() string {
homePath, _ := os.UserHomeDir()
return homePath + "/.git-credentials"
Expand Down
14 changes: 12 additions & 2 deletions cli/internal/helm/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ func DownloadChartFromGit(chart config.ZarfChart, destination string) {

// Tell helm where to save the archive and create the package
client.Destination = destination
client.Run(tempPath+"/chart", nil)
name, err := client.Run(tempPath+"/chart", nil)

if err != nil {
logContext.Debug(err)
logContext.Fatal("Helm is unable to save the archive and create the package:", name)
}

_ = os.RemoveAll(tempPath)
}
Expand Down Expand Up @@ -79,7 +84,12 @@ func DownloadPublishedChart(chart config.ZarfChart, destination string) {

// Ensure the name is consistent for deployments
destinationTarball := StandardName(destination, chart)
os.Rename(saved, destinationTarball)
err = os.Rename(saved, destinationTarball)

if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to rename tarball")
}
}

// StandardName generates a predictable full path for a helm chart for Zarf
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func HandleIfURL(packagePath string, shasum string, insecureDeploy bool) string

// Check the extension on the package is what we expect
if !isValidFileExtension(providedURL.Path) {
logrus.Fatalf("Only %s file extensions are permitted.\n", config.GetValidPackageExtensions)
logrus.Fatalf("Only %s file extensions are permitted.\n", config.GetValidPackageExtensions())
}

// Download the package
Expand Down
7 changes: 4 additions & 3 deletions cli/internal/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ func Create(confirm bool) {
func addLocalAssets(tempPath componentPaths, assets config.ZarfComponent) {
if len(assets.Charts) > 0 {
logrus.Info("Loading static helm charts")
utils.CreateDirectory(tempPath.charts, 0700)
_ = utils.CreateDirectory(tempPath.charts, 0700)
re := regexp.MustCompile(`\\.git$`)
for _, chart := range assets.Charts {
isGitURL, _ := regexp.MatchString("\\.git$", chart.Url)
if isGitURL {
matched := re.MatchString(chart.Url)
if matched {
helm.DownloadChartFromGit(chart, tempPath.charts)
} else {
helm.DownloadPublishedChart(chart, tempPath.charts)
Expand Down
6 changes: 5 additions & 1 deletion cli/internal/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func Inspect(packageName string) {
utils.ColorPrintYAML(text)

// Load the config to get the build version
config.LoadConfig(tempPath.base + "/zarf.yaml")
if err := config.LoadConfig(tempPath.base + "/zarf.yaml"); err != nil {
logrus.Fatal(err)
logrus.Fatalf("Unable to read the zarf.yaml file from %s", tempPath.base)
}

fmt.Printf("The package was built with Zarf CLI version %s\n", config.GetBuildData().Version)
cleanup(tempPath)

Expand Down
10 changes: 8 additions & 2 deletions cli/internal/pki/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ func generateFromTemplate(certFile, keyFile string, template, parent *x509.Certi
if err != nil {
return err
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return err
}
certOut.Close()

return savePrivateKey(key, keyFile)
Expand All @@ -216,7 +219,10 @@ func savePrivateKey(key *rsa.PrivateKey, keyFile string) error {
defer keyOut.Close()

keyBytes := x509.MarshalPKCS1PrivateKey(key)
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes})
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes})
if err != nil {
return err
}

return nil
}

0 comments on commit 92ff8c0

Please sign in to comment.