Skip to content

Commit 8e0b752

Browse files
authored
Apply linter suggestions (#2786)
* Refactor: if-elif-else => switch Signed-off-by: Matej Vašek <[email protected]> * Cleanup: calls to embedded fields Signed-off-by: Matej Vašek <[email protected]> * Fix: add missing err checks Signed-off-by: Matej Vašek <[email protected]> * Cleanup: fix format of error messages Signed-off-by: Matej Vašek <[email protected]> * Cleanup: omit type where possible Signed-off-by: Matej Vašek <[email protected]> * Cleanup: apply De Morgan's law Signed-off-by: Matej Vašek <[email protected]> * Cleanup: call ReplaceAll where possible Signed-off-by: Matej Vašek <[email protected]> * Cleanup: fix format of error messages Signed-off-by: Matej Vašek <[email protected]> * Cleanup: fix format of error messages Signed-off-by: Matej Vašek <[email protected]> --------- Signed-off-by: Matej Vašek <[email protected]>
1 parent 36e2720 commit 8e0b752

File tree

26 files changed

+52
-46
lines changed

26 files changed

+52
-46
lines changed

Diff for: cmd/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (c buildConfig) Validate() (err error) {
359359

360360
// Platform is only supported with the S2I builder at this time
361361
if c.Platform != "" && c.Builder != builders.S2I {
362-
err = errors.New("Only S2I builds currently support specifying platform")
362+
err = errors.New("only S2I builds currently support specifying platform")
363363
return
364364
}
365365

Diff for: cmd/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func newHelpTemplate(cmd *cobra.Command) *template.Template {
534534
fm := template.FuncMap{
535535
"indent": func(i int, c string, v string) string {
536536
indentation := strings.Repeat(c, i)
537-
return indentation + strings.Replace(v, "\n", "\n"+indentation, -1)
537+
return indentation + strings.ReplaceAll(v, "\n", "\n"+indentation)
538538
},
539539
}
540540
t.Funcs(fm)

Diff for: cmd/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func newDeleteConfig(cmd *cobra.Command, args []string) (cfg deleteConfig, err e
105105
// logicially inconsistent to supply only a namespace.
106106
// Either use the function's local state in its entirety, or specify
107107
// both a name and a namespace to ignore any local function source.
108-
err = fmt.Errorf("must also specify a name when specifying namespace.")
108+
err = fmt.Errorf("must also specify a name when specifying namespace")
109109
}
110110
if cfg.Name != "" && cmd.Flags().Changed("path") {
111111
// logically inconsistent to provide both a name and a path to source.

Diff for: cmd/deploy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func build(cmd *cobra.Command, flag string, f fn.Function, client *fn.Client, bu
378378
return f, false, err
379379
}
380380
} else if _, err = strconv.ParseBool(flag); err != nil {
381-
return f, false, fmt.Errorf("--build ($FUNC_BUILD) %q not recognized. Should be 'auto' or a truthy value such as 'true', 'false', '0', or '1'.", flag)
381+
return f, false, fmt.Errorf("invalid value for the build flag (%q), valid value is either 'auto' or a boolean", flag)
382382
} else if !build {
383383
return f, false, nil
384384
}

Diff for: cmd/describe.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func newDescribeConfig(cmd *cobra.Command, args []string) (cfg describeConfig, e
118118
// logicially inconsistent to supply only a namespace.
119119
// Either use the function's local state in its entirety, or specify
120120
// both a name and a namespace to ignore any local function source.
121-
err = fmt.Errorf("must also specify a name when specifying namespace.")
121+
err = fmt.Errorf("must also specify a name when specifying namespace")
122122
}
123123
if cfg.Name != "" && cmd.Flags().Changed("path") {
124124
// logically inconsistent to provide both a name and a path to source.

Diff for: cmd/format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func write(out io.Writer, s Formatter, formatName string) {
4444
case URL:
4545
err = s.URL(out)
4646
default:
47-
err = fmt.Errorf("format not recognized: %v\n", formatName)
47+
err = fmt.Errorf("format not recognized: %v", formatName)
4848
}
4949
if err != nil {
5050
panic(err)

Diff for: cmd/func-util/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939
os.Exit(137)
4040
}()
4141

42-
var cmd func(context.Context) error = unknown
42+
var cmd = unknown
4343

4444
switch filepath.Base(os.Args[0]) {
4545
case "deploy":

Diff for: cmd/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func newListConfig(cmd *cobra.Command) (cfg listConfig, err error) {
130130

131131
// specifying both -A and --namespace is logically inconsistent
132132
if cmd.Flags().Changed("namespace") && viper.GetBool("all-namespaces") {
133-
err = errors.New("Both --namespace and --all-namespaces specified.")
133+
err = errors.New("both --namespace and --all-namespaces specified")
134134
}
135135

136136
return

Diff for: cmd/repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func runRepositoryRemove(_ *cobra.Command, args []string, newClient ClientFactor
529529
}
530530

531531
if len(repositories) == 0 {
532-
return errors.New("No repositories installed. use 'add' to install")
532+
return errors.New("no repositories installed. use 'add' to install")
533533
}
534534

535535
// Confirm (interactive prompt mode)

Diff for: cmd/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (c runConfig) Validate(cmd *cobra.Command, f fn.Function) (err error) {
350350
}
351351

352352
if !c.Container && !oci.IsSupported(f.Runtime) {
353-
return fmt.Errorf("The %q runtime currently requires being run in a container", f.Runtime)
353+
return fmt.Errorf("the %q runtime currently requires being run in a container", f.Runtime)
354354
}
355355

356356
// When the docker runner respects the StartTimeout, this validation check

Diff for: docs/generator/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
fm = template.FuncMap{
2222
"indent": func(i int, c string, v string) string {
2323
indentation := strings.Repeat(c, i)
24-
return indentation + strings.Replace(v, "\n", "\n"+indentation, -1)
24+
return indentation + strings.ReplaceAll(v, "\n", "\n"+indentation)
2525
},
2626
"rootCmdUse": func() string {
2727
return rootName

Diff for: hack/cmd/update-builder/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ func fixupGoBuildpackARM64(ctx context.Context, config *builder.Config) error {
818818
goBuildpackVersion string
819819
)
820820
for i, moduleConfig := range config.Buildpacks {
821-
uri := moduleConfig.ImageOrURI.URI
821+
uri := moduleConfig.URI
822822
if strings.Contains(uri, "buildpacks/go:") {
823823
goBuildpackIndex = i
824824
goBuildpackVersion = uri[strings.LastIndex(uri, ":")+1:]
@@ -879,7 +879,7 @@ func fixupGoBuildpackARM64(ctx context.Context, config *builder.Config) error {
879879

880880
re := regexp.MustCompile(`^urn:cnb:registry:paketo-buildpacks/([\w-]+)@([\d.]+)$`)
881881
for i, dep := range packageConfig.Dependencies {
882-
m := re.FindStringSubmatch(dep.BuildpackURI.URI)
882+
m := re.FindStringSubmatch(dep.URI)
883883
if len(m) != 3 {
884884
return fmt.Errorf("cannot match buildpack name")
885885
}
@@ -931,7 +931,7 @@ func fixupGoBuildpackARM64(ctx context.Context, config *builder.Config) error {
931931
return err
932932
}
933933

934-
config.Buildpacks[goBuildpackIndex].BuildpackURI.URI = "file://" + filepath.Join(goBuildpackSrcDir, "build", "buildpackage.cnb")
934+
config.Buildpacks[goBuildpackIndex].URI = "file://" + filepath.Join(goBuildpackSrcDir, "build", "buildpackage.cnb")
935935
fmt.Println(goBuildpackSrcDir)
936936
return nil
937937
}
@@ -1176,6 +1176,9 @@ func patchStack(stackTomlPath string) error {
11761176
m["run"].(map[string]any)["platforms"] = map[string]any{"linux/arm64": args}
11771177

11781178
output, err := toml.Marshal(data)
1179+
if err != nil {
1180+
return fmt.Errorf("cannot marshal config: %w", err)
1181+
}
11791182
err = os.WriteFile(stackTomlPath, output, 0644)
11801183
if err != nil {
11811184
return fmt.Errorf("cannot write patched stack toml: %w", err)

Diff for: pkg/builders/buildpacks/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:553c041"
120120
// Build the Function at path.
121121
func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platform) (err error) {
122122
if len(platforms) != 0 {
123-
return errors.New("the pack builder does not support specifying target platforms directly.")
123+
return errors.New("the pack builder does not support specifying target platforms directly")
124124
}
125125

126126
// Builder image from the function if defined, default otherwise.

Diff for: pkg/builders/s2i/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
197197
for _, e := range errs {
198198
fmt.Fprintf(os.Stderr, "ERROR: %s\n", e)
199199
}
200-
return errors.New("Unable to build via the s2i builder.")
200+
return errors.New("unable to build via the s2i builder")
201201
}
202202

203203
// Create the S2I builder instance if not overridden

Diff for: pkg/docker/creds/credentials_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func TestCheckAuthEmptyCreds(t *testing.T) {
210210

211211
// generate Certificates
212212
func generateCert(t *testing.T) (tls.Certificate, *x509.Certificate) {
213-
var randReader io.Reader = rand.Reader
213+
var randReader = rand.Reader
214214

215215
caPublicKey, caPrivateKey, err := ed25519.GenerateKey(randReader)
216216
if err != nil {

Diff for: pkg/docker/pusher.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (n *Pusher) pushImage(ctx context.Context, f fn.Function, credentials Crede
187187
}
188188

189189
if f.Build.Image == "" {
190-
return "", errors.New("Function has no associated image. Has it been built?")
190+
return "", errors.New("function has no associated image. Has it been built?")
191191
}
192192

193193
registry, err := GetRegistry(f.Build.Image)

Diff for: pkg/docker/runner.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, startTimeout time.Durat
121121
Timeout: &timeoutSecs,
122122
}
123123
if err = c.ContainerStop(ctx, id, ctrStopOpts); err != nil {
124-
return fmt.Errorf("error stopping container %v: %v\n", id, err)
124+
return fmt.Errorf("error stopping container %v: %v", id, err)
125125
}
126126
if err = c.ContainerRemove(ctx, id, container.RemoveOptions{}); err != nil {
127-
return fmt.Errorf("error removing container %v: %v\n", id, err)
127+
return fmt.Errorf("error removing container %v: %v", id, err)
128128
}
129129
if err = conn.Close(); err != nil {
130-
return fmt.Errorf("error closing connection to container: %v\n", err)
130+
return fmt.Errorf("error closing connection to container: %v", err)
131131
}
132132
if err = c.Close(); err != nil {
133-
return fmt.Errorf("error closing daemon client: %v\n", err)
133+
return fmt.Errorf("error closing daemon client: %v", err)
134134
}
135135
return nil
136136
}

Diff for: pkg/functions/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ func (c *Client) Route(ctx context.Context, f Function) (string, Function, error
893893
}
894894

895895
if f.Deploy.Namespace == "" {
896-
return "", Function{}, errors.New("Unable to route function without a namespace. Is it deployed?")
896+
return "", Function{}, errors.New("unable to route function without a namespace. Is it deployed?")
897897
}
898898

899899
// Return the correct route.

Diff for: pkg/functions/template.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func (t template) Write(ctx context.Context, f *Function) error {
7474
f.Run.Envs = t.config.RunEnvs
7575
}
7676
if f.Deploy.HealthEndpoints.Liveness == "" {
77-
f.Deploy.HealthEndpoints.Liveness = t.config.HealthEndpoints.Liveness
77+
f.Deploy.HealthEndpoints.Liveness = t.config.Liveness
7878
}
7979
if f.Deploy.HealthEndpoints.Readiness == "" {
80-
f.Deploy.HealthEndpoints.Readiness = t.config.HealthEndpoints.Readiness
80+
f.Deploy.HealthEndpoints.Readiness = t.config.Readiness
8181
}
8282
if f.Invoke == "" && t.config.Invoke != "http" {
8383
f.Invoke = t.config.Invoke

Diff for: pkg/git/github/github.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func (c Client) CreateWebHook(ctx context.Context, repoOwner, repoName, payloadU
4141
return err
4242
}
4343

44-
if res.Response.StatusCode != http.StatusCreated {
44+
if res.StatusCode != http.StatusCreated {
4545
payload, err := io.ReadAll(res.Body)
4646
if err != nil {
4747
return fmt.Errorf("failed to read response body: %w", err)
4848
}
4949

5050
return fmt.Errorf("failed to create webhook on repository %v/%v, status code: %v, error : %v",
51-
repoOwner, repoName, res.Response.StatusCode, payload)
51+
repoOwner, repoName, res.StatusCode, payload)
5252
}
5353

5454
return nil

Diff for: pkg/k8s/dialer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type contextDialer struct {
7070
}
7171

7272
func (c *contextDialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
73-
if !(network == "tcp" || network == "tcp4" || network == "tcp6") {
73+
if network != "tcp" && network != "tcp4" && network != "tcp6" {
7474
return nil, fmt.Errorf("unsupported network: %q", network)
7575
}
7676

Diff for: pkg/knative/deployer.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (d *Deployer) Deploy(ctx context.Context, f fn.Function) (fn.DeploymentResu
141141
namespace = f.Deploy.Namespace
142142
}
143143
if namespace == "" {
144-
return fn.DeploymentResult{}, fmt.Errorf("deployer requires either a target namespace or that the function be already deployed.")
144+
return fn.DeploymentResult{}, fmt.Errorf("deployer requires either a target namespace or that the function be already deployed")
145145
}
146146

147147
// Clients
@@ -583,8 +583,8 @@ func updateService(f fn.Function, previousService *v1.Service, newEnv []corev1.E
583583
revisionAnnotations[k] = v
584584
}
585585

586-
service.ObjectMeta.Annotations = annotations
587-
service.Spec.Template.ObjectMeta.Annotations = revisionAnnotations
586+
service.Annotations = annotations
587+
service.Spec.Template.Annotations = revisionAnnotations
588588

589589
// I hate that we have to do this. Users should not see these values.
590590
// It is an implementation detail. These health endpoints should not be
@@ -611,8 +611,8 @@ func updateService(f fn.Function, previousService *v1.Service, newEnv []corev1.E
611611
return nil, err
612612
}
613613

614-
service.ObjectMeta.Labels = labels
615-
service.Spec.Template.ObjectMeta.Labels = labels
614+
service.Labels = labels
615+
service.Spec.Template.Labels = labels
616616

617617
err = flags.UpdateImage(&service.Spec.Template.Spec.PodSpec, f.Deploy.Image)
618618
if err != nil {
@@ -622,8 +622,8 @@ func updateService(f fn.Function, previousService *v1.Service, newEnv []corev1.E
622622
cp.Env = newEnv
623623
cp.EnvFrom = newEnvFrom
624624
cp.VolumeMounts = newVolumeMounts
625-
service.Spec.ConfigurationSpec.Template.Spec.Volumes = newVolumes
626-
service.Spec.ConfigurationSpec.Template.Spec.PodSpec.ServiceAccountName = f.Deploy.ServiceAccountName
625+
service.Spec.Template.Spec.Volumes = newVolumes
626+
service.Spec.Template.Spec.ServiceAccountName = f.Deploy.ServiceAccountName
627627
return service, nil
628628
}
629629
}
@@ -1056,48 +1056,48 @@ func setServiceOptions(template *v1.RevisionTemplateSpec, options fn.Options) er
10561056
}
10571057

10581058
// in the container always set Requests/Limits & Concurrency values based on the contents of config
1059-
template.Spec.PodSpec.Containers[0].Resources.Requests = nil
1060-
template.Spec.PodSpec.Containers[0].Resources.Limits = nil
1059+
template.Spec.Containers[0].Resources.Requests = nil
1060+
template.Spec.Containers[0].Resources.Limits = nil
10611061
template.Spec.ContainerConcurrency = nil
10621062

10631063
if options.Resources != nil {
10641064
if options.Resources.Requests != nil {
1065-
template.Spec.PodSpec.Containers[0].Resources.Requests = corev1.ResourceList{}
1065+
template.Spec.Containers[0].Resources.Requests = corev1.ResourceList{}
10661066

10671067
if options.Resources.Requests.CPU != nil {
10681068
value, err := resource.ParseQuantity(*options.Resources.Requests.CPU)
10691069
if err != nil {
10701070
return err
10711071
}
1072-
template.Spec.PodSpec.Containers[0].Resources.Requests[corev1.ResourceCPU] = value
1072+
template.Spec.Containers[0].Resources.Requests[corev1.ResourceCPU] = value
10731073
}
10741074

10751075
if options.Resources.Requests.Memory != nil {
10761076
value, err := resource.ParseQuantity(*options.Resources.Requests.Memory)
10771077
if err != nil {
10781078
return err
10791079
}
1080-
template.Spec.PodSpec.Containers[0].Resources.Requests[corev1.ResourceMemory] = value
1080+
template.Spec.Containers[0].Resources.Requests[corev1.ResourceMemory] = value
10811081
}
10821082
}
10831083

10841084
if options.Resources.Limits != nil {
1085-
template.Spec.PodSpec.Containers[0].Resources.Limits = corev1.ResourceList{}
1085+
template.Spec.Containers[0].Resources.Limits = corev1.ResourceList{}
10861086

10871087
if options.Resources.Limits.CPU != nil {
10881088
value, err := resource.ParseQuantity(*options.Resources.Limits.CPU)
10891089
if err != nil {
10901090
return err
10911091
}
1092-
template.Spec.PodSpec.Containers[0].Resources.Limits[corev1.ResourceCPU] = value
1092+
template.Spec.Containers[0].Resources.Limits[corev1.ResourceCPU] = value
10931093
}
10941094

10951095
if options.Resources.Limits.Memory != nil {
10961096
value, err := resource.ParseQuantity(*options.Resources.Limits.Memory)
10971097
if err != nil {
10981098
return err
10991099
}
1100-
template.Spec.PodSpec.Containers[0].Resources.Limits[corev1.ResourceMemory] = value
1100+
template.Spec.Containers[0].Resources.Limits[corev1.ResourceMemory] = value
11011101
}
11021102

11031103
if options.Resources.Limits.Concurrency != nil {

Diff for: pkg/oci/go_builder.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (b goBuilder) WritePlatform(cfg buildJob, p v1.Platform) (layers []imageLay
6868
fmt.Printf("mv %v %v\n", rel(cfg.buildDir(), target), rel(cfg.buildDir(), blob))
6969
}
7070
err = os.Rename(target, blob)
71+
if err != nil {
72+
return nil, fmt.Errorf("cannot rename blob: %w", err)
73+
}
7174

7275
// NOTE: base is intentionally blank indiciating it is to be built without
7376
// a base layer.

Diff for: pkg/oci/mock/registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewRegistry() *Registry {
2828
}
2929

3030
func (r *Registry) Addr() net.Addr {
31-
return r.Server.Listener.Addr()
31+
return r.Listener.Addr()
3232
}
3333

3434
func (r *Registry) Close() {

Diff for: pkg/pipelines/tekton/tasks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ spec:
436436
// GetClusterTasks returns multi-document yaml containing tekton tasks used by func.
437437
func GetClusterTasks() string {
438438
tasks := getBuildpackTask() + "\n---\n" + getS2ITask() + "\n---\n" + getDeployTask() + "\n---\n" + getScaffoldTask()
439-
tasks = strings.Replace(tasks, "kind: Task", "kind: ClusterTask", -1)
439+
tasks = strings.ReplaceAll(tasks, "kind: Task", "kind: ClusterTask")
440440
tasks = strings.ReplaceAll(tasks, "apiVersion: tekton.dev/v1", "apiVersion: tekton.dev/v1beta1")
441441
return tasks
442442
}

Diff for: pkg/tar/tar.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Extract(input io.Reader, destDir string) error {
2828

2929
r := tar.NewReader(input)
3030

31-
var first bool = true
31+
var first = true
3232
for {
3333
var hdr *tar.Header
3434
hdr, err = r.Next()

0 commit comments

Comments
 (0)