• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 15377299429

01 Jun 2025 04:49PM UTC coverage: 71.173%. Remained the same
15377299429

Pull #4826

github

camilamacedo86
feat: improve UX and maintainability by auto-uncommenting webhook blocks

This change enhances the user experience by automatically uncommenting
webhook-related code blocks during scaffolding. It also improves
maintainability by eliminating the need to manually uncomment code when
running end-to-end tests for examples and mocks.
Pull Request #4826: ✨ (kustomize/v2; go/v4) feat: improve UX and maintainability by auto-uncommenting webhook blocks when possible

2311 of 3247 relevant lines covered (71.17%)

16.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/plugins/golang/repository.go
1
/*
2
Copyright 2017 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package golang
18

19
import (
20
        "encoding/json"
21
        "errors"
22
        "fmt"
23
        "os"
24
        "os/exec"
25

26
        "golang.org/x/tools/go/packages"
27
)
28

29
// module and goMod arg just enough of the output of `go mod edit -json` for our purposes
30
type goMod struct {
31
        Module module
32
}
33
type module struct {
34
        Path string
35
}
36

37
// findGoModulePath finds the path of the current module, if present.
38
func findGoModulePath() (string, error) {
×
39
        cmd := exec.Command("go", "mod", "edit", "-json")
×
40
        cmd.Env = append(cmd.Env, os.Environ()...)
×
41
        out, err := cmd.Output()
×
42
        if err != nil {
×
43
                var exitErr *exec.ExitError
×
44
                if errors.As(err, &exitErr) {
×
45
                        err = fmt.Errorf("%s", string(exitErr.Stderr))
×
46
                }
×
47
                return "", err
×
48
        }
49
        mod := goMod{}
×
50
        if err = json.Unmarshal(out, &mod); err != nil {
×
51
                return "", fmt.Errorf("failed to unmarshal go.mod: %w", err)
×
52
        }
×
53
        return mod.Module.Path, nil
×
54
}
55

56
// FindCurrentRepo attempts to determine the current repository
57
// though a combination of go/packages and `go mod` commands/tricks.
58
func FindCurrentRepo() (string, error) {
×
59
        // easiest case: existing go module
×
60
        path, err := findGoModulePath()
×
61
        if err == nil {
×
62
                return path, nil
×
63
        }
×
64

65
        // next, check if we've got a package in the current directory
66
        pkgCfg := &packages.Config{
×
67
                Mode: packages.NeedName, // name gives us path as well
×
68
        }
×
69
        pkgs, err := packages.Load(pkgCfg, ".")
×
70
        // NB(directxman12): when go modules are off and we're outside GOPATH and
×
71
        // we don't otherwise have a good guess packages.Load will fabricate a path
×
72
        // that consists of `_/absolute/path/to/current/directory`.  We shouldn't
×
73
        // use that when it happens.
×
74
        if err == nil && len(pkgs) > 0 && len(pkgs[0].PkgPath) > 0 && pkgs[0].PkgPath[0] != '_' {
×
75
                return pkgs[0].PkgPath, nil
×
76
        }
×
77

78
        // otherwise, try to get `go mod init` to guess for us -- it's pretty good
79
        cmd := exec.Command("go", "mod", "init")
×
80
        cmd.Env = append(cmd.Env, os.Environ()...)
×
81
        if _, err := cmd.Output(); err != nil {
×
82
                var exitErr *exec.ExitError
×
83
                if errors.As(err, &exitErr) {
×
84
                        err = fmt.Errorf("%s", string(exitErr.Stderr))
×
85
                }
×
86
                // give up, let the user figure it out
87
                return "", fmt.Errorf("could not determine repository path from module data, "+
×
88
                        "package data, or by initializing a module: %w", err)
×
89
        }
90
        //nolint:errcheck
91
        defer os.Remove("go.mod") // clean up after ourselves
×
92
        return findGoModulePath()
×
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc