forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(buildtool): add the gofixpath subcommand (ooni#1484)
This subcommand executes another command ensuring a PATH lookup for this subcommand resolves "go" as the version of Go specified in the `GOVERSION` file. This is a building block for ooni/probe#2664.
- Loading branch information
1 parent
c1ea612
commit e435d33
Showing
6 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Ensures that ./internal/cmd/buildtool gofixpath {command} [arguments] downloads the correct | ||
# version of go and executes {command} with [arguments] with "go" being the right version. | ||
|
||
name: gofixpath | ||
on: | ||
push: | ||
branches: | ||
- "release/**" | ||
- "fullbuild" | ||
- "gofixpathbuild" | ||
- "master" | ||
tags: | ||
- "v*" | ||
pull_request: | ||
schedule: | ||
- cron: "17 7 * * *" | ||
|
||
jobs: | ||
build_with_specific_go_version: | ||
strategy: | ||
matrix: | ||
goversion: ["1.19", "1.20", "1.21"] # when releasing check whether we need to update this array | ||
system: [ubuntu-latest, macos-latest] | ||
runs-on: "${{ matrix.system }}" | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: "${{ matrix.goversion }}" | ||
|
||
- run: go run ./internal/cmd/buildtool gofixpath -- go run ./internal/cmd/buildtool generic miniooni |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/apex/log" | ||
"github.com/ooni/probe-cli/v3/internal/cmd/buildtool/internal/buildtoolmodel" | ||
"github.com/ooni/probe-cli/v3/internal/must" | ||
"github.com/ooni/probe-cli/v3/internal/runtimex" | ||
"github.com/ooni/probe-cli/v3/internal/shellx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// gofixpathSubcommand returns the gofixpath [cobra.Command]. | ||
func gofixpathSubcommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "gofixpath", | ||
Short: "Executes a command ensuring the expected version of Go comes first in PATH lookup", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
gofixpathMain(&buildDeps{}, args...) | ||
}, | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
} | ||
|
||
// gofixpathMain ensures the correct version of Go is in path, otherwise | ||
// installs such a version, configure the PATH correctly, and then executes | ||
// whatever argument passed to the command with the correct PATH. | ||
func gofixpathMain(deps buildtoolmodel.Dependencies, args ...string) { | ||
// create empty environment | ||
envp := &shellx.Envp{} | ||
|
||
// install and configure the correct go version if needed | ||
if !golangCorrectVersionCheckP("GOVERSION") { | ||
// read the version of Go we would like to use | ||
expected := string(must.FirstLineBytes(must.ReadFile("GOVERSION"))) | ||
|
||
// install the wrapper command | ||
packageName := fmt.Sprintf("golang.org/dl/go%s@latest", expected) | ||
must.Run(log.Log, "go", "install", "-v", packageName) | ||
|
||
// run the wrapper to download the distribution | ||
gobinproxy := filepath.Join( | ||
string(must.FirstLineBytes(must.RunOutput(log.Log, "go", "env", "GOPATH"))), | ||
"bin", | ||
fmt.Sprintf("go%s", expected), | ||
) | ||
must.Run(log.Log, gobinproxy, "download") | ||
|
||
// add the path to the SDK binary dir | ||
// | ||
// Note: because gomobile wants to execute "go" we must provide the | ||
// path to a directory that contains a command named "go" and we cannot | ||
// just use the gobinproxy binary | ||
sdkbinpath := filepath.Join( | ||
string(must.FirstLineBytes(must.RunOutput(log.Log, gobinproxy, "env", "GOROOT"))), | ||
"bin", | ||
) | ||
|
||
// prepend to PATH | ||
envp.Append("PATH", cdepsPrependToPath(sdkbinpath)) | ||
} | ||
|
||
// create shellx configuration | ||
config := &shellx.Config{ | ||
Logger: log.Log, | ||
Flags: shellx.FlagShowStdoutStderr, | ||
} | ||
|
||
// create argv | ||
argv := runtimex.Try1(shellx.NewArgv(args[0], args[1:]...)) // safe because cobra.MinimumNArgs(1) | ||
|
||
// execute the child command | ||
runtimex.Try0(shellx.RunEx(config, argv, envp)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.17.11 |