save GOOS and GOARCH in local engine (#2701)

make refactoring easyer
This commit is contained in:
6543 2023-11-02 07:58:32 +01:00 committed by GitHub
parent de53b906e8
commit c75068920c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View file

@ -23,7 +23,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/rs/zerolog/log"
@ -55,10 +54,10 @@ func (e *local) setupClone(state *workflowState) error {
log.Info().Msg("no global 'plugin-git' installed, try to download for current workflow")
state.pluginGitBinary = filepath.Join(state.homeDir, "plugin-git")
if runtime.GOOS == "windows" {
if e.os == "windows" {
state.pluginGitBinary += ".exe"
}
return downloadLatestGitPluginBinary(state.pluginGitBinary)
return e.downloadLatestGitPluginBinary(state.pluginGitBinary)
}
// execClone executes a clone-step locally
@ -79,7 +78,7 @@ func (e *local) execClone(ctx context.Context, step *types.Step, state *workflow
log.Warn().Msgf("clone step image '%s' does not match default git clone image. We ignore it and use our plugin-git anyway.", step.Image)
}
rmCmd, err := writeNetRC(step, state)
rmCmd, err := e.writeNetRC(step, state)
if err != nil {
return err
}
@ -88,7 +87,7 @@ func (e *local) execClone(ctx context.Context, step *types.Step, state *workflow
var cmd *exec.Cmd
if rmCmd != "" {
// if we have a netrc injected we have to make sure it's deleted in any case after clone was attempted
if runtime.GOOS == "windows" {
if e.os == "windows" {
pwsh, err := exec.LookPath("powershell.exe")
if err != nil {
return err
@ -114,7 +113,7 @@ func (e *local) execClone(ctx context.Context, step *types.Step, state *workflow
}
// writeNetRC write a netrc file into the home dir of a given workflow state
func writeNetRC(step *types.Step, state *workflowState) (string, error) {
func (e *local) writeNetRC(step *types.Step, state *workflowState) (string, error) {
if step.Environment["CI_NETRC_MACHINE"] == "" {
log.Trace().Msg("no netrc to write")
return "", nil
@ -122,7 +121,7 @@ func writeNetRC(step *types.Step, state *workflowState) (string, error) {
file := filepath.Join(state.homeDir, ".netrc")
rmCmd := fmt.Sprintf("rm \"%s\"", file)
if runtime.GOOS == "windows" {
if e.os == "windows" {
file = filepath.Join(state.homeDir, "_netrc")
rmCmd = fmt.Sprintf("del \"%s\"", file)
}
@ -133,7 +132,7 @@ func writeNetRC(step *types.Step, state *workflowState) (string, error) {
// downloadLatestGitPluginBinary download the latest plugin-git binary based on runtime OS and Arch
// and saves it to dest
func downloadLatestGitPluginBinary(dest string) error {
func (e *local) downloadLatestGitPluginBinary(dest string) error {
type asset struct {
Name string
BrowserDownloadURL string `json:"browser_download_url"`
@ -159,7 +158,7 @@ func downloadLatestGitPluginBinary(dest string) error {
}
for _, at := range rel.Assets {
if strings.Contains(at.Name, runtime.GOOS) && strings.Contains(at.Name, runtime.GOARCH) {
if strings.Contains(at.Name, e.os) && strings.Contains(at.Name, e.arch) {
resp2, err := http.Get(at.BrowserDownloadURL)
if err != nil {
return fmt.Errorf("could not download plugin-git: %w", err)

View file

@ -45,11 +45,15 @@ type local struct {
workflows sync.Map
output io.ReadCloser
pluginGitBinary string
os, arch string
}
// New returns a new local Engine.
func New() types.Engine {
return &local{}
return &local{
os: runtime.GOOS,
arch: runtime.GOARCH,
}
}
func (e *local) Name() string {
@ -64,7 +68,7 @@ func (e *local) Load(context.Context) (*types.EngineInfo, error) {
e.loadClone()
return &types.EngineInfo{
Platform: runtime.GOOS + "/" + runtime.GOARCH,
Platform: e.os + "/" + e.arch,
}, nil
}
@ -149,7 +153,7 @@ func (e *local) execCommands(ctx context.Context, step *types.Step, state *workf
e.output, _ = cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout
if runtime.GOOS == "windows" {
if e.os == "windows" {
// we get non utf8 output from windows so just sanitize it
// TODO: remove hack
e.output = io.NopCloser(transform.NewReader(e.output, unicode.UTF8.NewDecoder().Transformer))