dev: use cmd.Context inside custom command (#4496)

This commit is contained in:
Oleksandr Redko 2024-03-13 14:30:37 +02:00 committed by GitHub
parent b923d0fe7e
commit 2a1afc14cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 7 deletions

View File

@ -22,7 +22,7 @@ func main() {
info := createBuildInfo() info := createBuildInfo()
if err := commands.Execute(info); err != nil { if err := commands.Execute(info); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err) _, _ = fmt.Fprintf(os.Stderr, "Failed executing command with error: %v\n", err)
os.Exit(exitcodes.Failure) os.Exit(exitcodes.Failure)
} }
} }

View File

@ -1,7 +1,6 @@
package commands package commands
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -27,7 +26,7 @@ func newCustomCommand(logger logutils.Log) *customCommand {
customCmd := &cobra.Command{ customCmd := &cobra.Command{
Use: "custom", Use: "custom",
Short: "Build a version of golangci-lint with custom linters.", Short: "Build a version of golangci-lint with custom linters",
Args: cobra.NoArgs, Args: cobra.NoArgs,
PreRunE: c.preRunE, PreRunE: c.preRunE,
RunE: c.runE, RunE: c.runE,
@ -54,9 +53,7 @@ func (c *customCommand) preRunE(_ *cobra.Command, _ []string) error {
return nil return nil
} }
func (c *customCommand) runE(_ *cobra.Command, _ []string) error { func (c *customCommand) runE(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
tmp, err := os.MkdirTemp(os.TempDir(), "custom-gcl") tmp, err := os.MkdirTemp(os.TempDir(), "custom-gcl")
if err != nil { if err != nil {
return fmt.Errorf("create temporary directory: %w", err) return fmt.Errorf("create temporary directory: %w", err)
@ -72,7 +69,7 @@ func (c *customCommand) runE(_ *cobra.Command, _ []string) error {
_ = os.RemoveAll(tmp) _ = os.RemoveAll(tmp)
}() }()
err = internal.NewBuilder(c.log, c.cfg, tmp).Build(ctx) err = internal.NewBuilder(c.log, c.cfg, tmp).Build(cmd.Context())
if err != nil { if err != nil {
return fmt.Errorf("build process: %w", err) return fmt.Errorf("build process: %w", err)
} }