From c75068920cbd93f93931b0c1d6fbb3ed889a8746 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 2 Nov 2023 07:58:32 +0100 Subject: [PATCH] save GOOS and GOARCH in local engine (#2701) make refactoring easyer --- pipeline/backend/local/clone.go | 17 ++++++++--------- pipeline/backend/local/local.go | 10 +++++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pipeline/backend/local/clone.go b/pipeline/backend/local/clone.go index 5ad899548..f67c8d6b9 100644 --- a/pipeline/backend/local/clone.go +++ b/pipeline/backend/local/clone.go @@ -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) diff --git a/pipeline/backend/local/local.go b/pipeline/backend/local/local.go index 5543fed0e..89ef203ce 100644 --- a/pipeline/backend/local/local.go +++ b/pipeline/backend/local/local.go @@ -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))