Skip to content

Commit

Permalink
save GOOS and GOARCH in local engine (#2701)
Browse files Browse the repository at this point in the history
make refactoring easyer
  • Loading branch information
6543 authored Nov 2, 2023
1 parent de53b90 commit c750689
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
17 changes: 8 additions & 9 deletions pipeline/backend/local/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -114,15 +113,15 @@ 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
}

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)
}
Expand All @@ -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"`
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit c750689

Please sign in to comment.