mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
e8381db778
* Update Kubernetes to v1.21.0 * Update to golang v1.16.2 * Update dependent modules to track with upstream * Switch to upstream flannel * Track changes to upstream cloud-controller-manager and FeatureGates Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
// Copyright 2020 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package git
|
|
|
|
import (
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"sigs.k8s.io/kustomize/api/filesys"
|
|
"sigs.k8s.io/kustomize/api/internal/utils"
|
|
)
|
|
|
|
// Arbitrary, but non-infinite, timeout for running commands.
|
|
const defaultDuration = 27 * time.Second
|
|
|
|
// gitRunner runs the external git binary.
|
|
type gitRunner struct {
|
|
gitProgram string
|
|
duration time.Duration
|
|
dir filesys.ConfirmedDir
|
|
}
|
|
|
|
// newCmdRunner returns a gitRunner if it can find the binary.
|
|
// It also creats a temp directory for cloning repos.
|
|
func newCmdRunner() (*gitRunner, error) {
|
|
gitProgram, err := exec.LookPath("git")
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "no 'git' program on path")
|
|
}
|
|
dir, err := filesys.NewTmpConfirmedDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &gitRunner{
|
|
gitProgram: gitProgram,
|
|
duration: defaultDuration,
|
|
dir: dir,
|
|
}, nil
|
|
}
|
|
|
|
// run a command with a timeout.
|
|
func (r gitRunner) run(args ...string) error {
|
|
//nolint: gosec
|
|
cmd := exec.Command(r.gitProgram, args...)
|
|
cmd.Dir = r.dir.String()
|
|
return utils.TimedCall(
|
|
cmd.String(),
|
|
r.duration,
|
|
func() error {
|
|
_, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return errors.Wrapf(err, "git cmd = '%s'", cmd.String())
|
|
}
|
|
return err
|
|
})
|
|
}
|