Skip to content

Commit

Permalink
fix(lints): fix code smell from CI
Browse files Browse the repository at this point in the history
  • Loading branch information
corrieriluca committed Aug 4, 2023
1 parent a45d91b commit 976b0ab
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
25 changes: 20 additions & 5 deletions internal/burrito/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,20 @@ func (c *Config) Load(flags *pflag.FlagSet) error {
name = strings.ReplaceAll(name, "-", "_")
return pflag.NormalizedName(name)
})
v.BindPFlags(flags)
err := v.BindPFlags(flags)
if err != nil {
fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err)
return err
}

// Nested configuration options set with environment variables use an
// underscore as a separator.
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
bindEnvironmentVariables(v, *c)
err = bindEnvironmentVariables(v, *c)
if err != nil {
fmt.Fprintf(os.Stderr, "Error binding environment variables: %s\n", err)
return err
}

return v.Unmarshal(c)
}
Expand All @@ -137,7 +145,7 @@ func (c *Config) Load(flags *pflag.FlagSet) error {
// fields to environment variables. This is a workaround to a limitation of
// Viper, found here:
// https://github.com/spf13/viper/issues/188#issuecomment-399884438
func bindEnvironmentVariables(v *viper.Viper, iface interface{}, parts ...string) {
func bindEnvironmentVariables(v *viper.Viper, iface interface{}, parts ...string) error {
ifv := reflect.ValueOf(iface)
ift := reflect.TypeOf(iface)
for i := 0; i < ift.NumField(); i++ {
Expand All @@ -149,11 +157,18 @@ func bindEnvironmentVariables(v *viper.Viper, iface interface{}, parts ...string
}
switch val.Kind() {
case reflect.Struct:
bindEnvironmentVariables(v, val.Interface(), append(parts, tv)...)
err := bindEnvironmentVariables(v, val.Interface(), append(parts, tv)...)
if err != nil {
return err
}
default:
v.BindEnv(strings.Join(append(parts, tv), "."))
err := v.BindEnv(strings.Join(append(parts, tv), "."))
if err != nil {
return err
}
}
}
return nil
}

func TestConfig() *Config {
Expand Down
2 changes: 0 additions & 2 deletions internal/controllers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ var (
scheme = runtime.NewScheme()
)

const ()

type Controllers struct {
config *config.Config
}
Expand Down
5 changes: 4 additions & 1 deletion internal/controllers/terraformpullrequest/comment/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (c *DefaultComment) Generate(commit string) (string, error) {
Layers: reportedLayers,
}
comment := bytes.NewBufferString("")
defaultTemplate.Execute(comment, data)
err := defaultTemplate.Execute(comment, data)
if err != nil {
return "", err
}
return comment.String(), nil
}
8 changes: 1 addition & 7 deletions internal/controllers/terraformpullrequest/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ type Github struct {
}

func (g *Github) IsConfigPresent(c *config.Config) bool {
if &c.Controller.GithubConfig == nil {
return false
}
if c.Controller.GithubConfig.APIToken == "" {
return false
}
return true
return c.Controller.GithubConfig.APIToken != ""
}

func (g *Github) Init(c *config.Config) error {
Expand Down
8 changes: 1 addition & 7 deletions internal/controllers/terraformpullrequest/gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ type Gitlab struct {
}

func (g *Gitlab) IsConfigPresent(c *config.Config) bool {
if &c.Controller.GitlabConfig == nil {
return false
}
if c.Controller.GitlabConfig.APIToken == "" {
return false
}
return true
return c.Controller.GitlabConfig.APIToken != ""
}

func (g *Gitlab) Init(c *config.Config) error {
Expand Down
14 changes: 10 additions & 4 deletions internal/testing/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
func LoadResources(client client.Client, path string) {
log := logf.FromContext(context.TODO())

resources := parseResources(path)
resources, err := parseResources(path)
if err != nil {
panic(err)
}
for _, r := range resources {
log.Info(fmt.Sprintf("Creating %s, %s/%s", r.GetObjectKind().GroupVersionKind().Kind, r.GetNamespace(), r.GetName()))
err := client.Create(context.TODO(), r)
Expand All @@ -30,14 +33,14 @@ func LoadResources(client client.Client, path string) {
}
}

func parseResources(path string) []client.Object {
func parseResources(path string) ([]client.Object, error) {
log := logf.FromContext(context.TODO())
_ = configv1alpha1.AddToScheme(scheme.Scheme)
decoder := scheme.Codecs.UniversalDeserializer()

list := []client.Object{}
r := []byte{}
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, walkErr error) error {
if d.IsDir() {
return nil
}
Expand All @@ -55,6 +58,9 @@ func parseResources(path string) []client.Object {
r = append(r, data...)
return nil
})
if err != nil {
return nil, err
}

for _, doc := range strings.Split(string(r), "---") {
if doc == "" || doc == "\n" {
Expand All @@ -68,5 +74,5 @@ func parseResources(path string) []client.Object {
list = append(list, obj.(client.Object))

}
return list
return list, nil
}
6 changes: 3 additions & 3 deletions internal/webhook/github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"

"net/http"
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
}
defer payloadFile.Close()

payloadBytes, err := ioutil.ReadAll(payloadFile)
payloadBytes, err := io.ReadAll(payloadFile)
if err != nil {
t.Fatalf("failed to read payload file: %v", err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
}
defer payloadFile.Close()

payloadBytes, err := ioutil.ReadAll(payloadFile)
payloadBytes, err := io.ReadAll(payloadFile)
if err != nil {
t.Fatalf("failed to read payload file: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/webhook/gitlab/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gitlab_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"os"

"net/http"
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestGitlab_GetEvent_PushEvent(t *testing.T) {
}
defer payloadFile.Close()

payloadBytes, err := ioutil.ReadAll(payloadFile)
payloadBytes, err := io.ReadAll(payloadFile)
if err != nil {
t.Fatalf("failed to read payload file: %v", err)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestGitlab_GetEvent_MergeRequestEvent(t *testing.T) {
}
defer payloadFile.Close()

payloadBytes, err := ioutil.ReadAll(payloadFile)
payloadBytes, err := io.ReadAll(payloadFile)
if err != nil {
t.Fatalf("failed to read payload file: %v", err)
}
Expand Down

0 comments on commit 976b0ab

Please sign in to comment.