cmd

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "go-acc <flags> <packages...>",
	Short: "Receive accurate code coverage reports for Golang (Go)",
	Args:  cobra.MinimumNArgs(1),
	Example: `$ go-acc github.com/some/package
$ go-acc -o my-coverfile.txt github.com/some/package
$ go-acc ./...
$ go-acc $(glide novendor)

You can pass all flags defined by "go test" after "--":
$ go-acc . -- -short -v -failfast
`,
	Run: func(cmd *cobra.Command, args []string) {
		mode := flagx.MustGetString(cmd, "covermode")
		if flagx.MustGetBool(cmd, "verbose") {
			fmt.Println("Flag -v has been deprecated, use `go acc -- -v` instead!")
		}

		payload := "mode: " + mode + "\n"

		var packages []string
		var passthrough []string

		for _, a := range args {
			if len(a) == 0 {
				continue
			}

			if a[0] == '-' || len(passthrough) > 0 {
				passthrough = append(passthrough, a)
				continue
			}

			if len(a) > 4 && a[len(a)-4:] == "/..." {
				var buf bytes.Buffer
				c := exec.Command("go", "list", a)
				c.Stdout = &buf
				c.Stderr = &buf
				cmdx.Must(c.Run(), "unable to run go list")

				var add []string
				for _, s := range strings.Split(buf.String(), "\n") {

					if len(s) > 0 && !strings.HasPrefix(s, "go: ") {
						add = append(add, s)
					}
				}

				packages = append(packages, add...)
			} else {
				packages = append(packages, a)
			}
		}

		files := make([]string, len(packages))
		for k, pkg := range packages {
			files[k] = filepath.Join(os.TempDir(), uuid.New()) + ".cc.tmp"
			var c *exec.Cmd
			ca := append(append(
				[]string{
					"test",
					"-covermode=" + mode,
					"-coverprofile=" + files[k],
					"-coverpkg=" + strings.Join(packages, ","),
				},
				passthrough...),
				pkg)
			c = exec.Command("go", ca...)

			stderr, err := c.StderrPipe()
			if err != nil {
				fatalf("%s", err)
			}
			stdout, err := c.StdoutPipe()
			if err != nil {
				fatalf("%s", err)
			}

			if err := c.Start(); err != nil {
				fatalf("%s", err)
			}

			var wg sync.WaitGroup
			wg.Add(2)
			go scan(&wg, stderr)
			go scan(&wg, stdout)

			if err := c.Wait(); err != nil {
				fatalf("%s", err)
			}

			wg.Wait()
		}

		for _, file := range files {
			if _, err := os.Stat(file); os.IsNotExist(err) {
				continue
			}

			p, err := ioutil.ReadFile(file)
			if err != nil {
				fatalf("%s", err)
			}

			ps := strings.Split(string(p), "\n")
			payload += strings.Join(ps[1:], "\n")
		}

		output, err := cmd.Flags().GetString("output")
		if err != nil {
			fatalf("%s", err)
		}

		if err := ioutil.WriteFile(output, []byte(payload), 0644); err != nil {
			fatalf("%s", err)
		}
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL