Skip to content

Commit

Permalink
Fix lightweight proxy conflict in github actions (#189)
Browse files Browse the repository at this point in the history
* fix lightweight proxy #183
* split package to lighter proxy #183

Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron authored Jun 26, 2024
1 parent 3af9c22 commit 7a46ebb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 28 deletions.
10 changes: 2 additions & 8 deletions cmd/tenv/tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/config/cmdconst"
configutils "github.com/tofuutils/tenv/v2/config/utils"
"github.com/tofuutils/tenv/v2/versionmanager"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/proxy"
Expand Down Expand Up @@ -96,7 +95,7 @@ func initRootCmd(conf *config.Config, builders map[string]builder.BuilderFunc, g
flags.BoolVarP(&conf.DisplayVerbose, "verbose", "v", false, "verbose output (and set log level to Trace)")

rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newUpdatePathCmd())
rootCmd.AddCommand(newUpdatePathCmd(conf.GithubActions))

tofuParams := subCmdParams{
deprecated: true, // direct use should display a deprecation message
Expand Down Expand Up @@ -187,7 +186,7 @@ func newVersionCmd() *cobra.Command {
}
}

func newUpdatePathCmd() *cobra.Command {
func newUpdatePathCmd(gha bool) *cobra.Command {
return &cobra.Command{
Use: "update-path",
Short: updatePathHelp,
Expand All @@ -199,11 +198,6 @@ func newUpdatePathCmd() *cobra.Command {
return nil
}

gha, err := configutils.GetenvBool(false, cmdconst.GithubActionsEnvName)
if err != nil {
return err
}

execDirPath := filepath.Dir(execPath)
if gha {
pathfilePath := os.Getenv("GITHUB_PATH")
Expand Down
2 changes: 0 additions & 2 deletions config/cmdconst/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ const (
TofuName = "tofu"

CallSubCmd = "call"

GithubActionsEnvName = "GITHUB_ACTIONS"
)
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
)

const (
githubActionsEnvName = "GITHUB_ACTIONS"

archEnvName = "ARCH"
autoInstallEnvName = "AUTO_INSTALL"
defaultConstraint = "DEFAULT_CONSTRAINT"
Expand Down Expand Up @@ -117,6 +119,7 @@ type Config struct {
DisplayVerbose bool
ForceQuiet bool
ForceRemote bool
GithubActions bool
GithubToken string
NoInstall bool
remoteConfLoaded bool
Expand Down Expand Up @@ -161,11 +164,17 @@ func InitConfigFromEnv() (Config, error) {
return Config{}, err
}

gha, err := configutils.GetenvBool(false, githubActionsEnvName)
if err != nil {
return Config{}, err
}

return Config{
Arch: arch,
Atmos: makeRemoteConfig(AtmosRemoteURLEnvName, atmosListURLEnvName, atmosInstallModeEnvName, atmosListModeEnvName, defaultAtmosGithubURL, baseGithubURL),
ForceQuiet: quiet,
ForceRemote: forceRemote,
GithubActions: gha,
GithubToken: configutils.GetenvFallback(tenvTokenEnvName, tofuTokenEnvName),
NoInstall: !autoInstall,
RemoteConfPath: os.Getenv(tenvRemoteConfEnvName),
Expand Down
14 changes: 3 additions & 11 deletions versionmanager/proxy/cmd/proxy.go → pkg/cmdproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ import (
"os/signal"
"strconv"
"strings"

"github.com/tofuutils/tenv/v2/config/cmdconst"
configutils "github.com/tofuutils/tenv/v2/config/utils"
)

var errDelimiter = errors.New("key and value should not contains delimiter")

func Run(execPath string, cmdArgs []string) {
func Run(execPath string, cmdArgs []string, gha bool) {
exitCode := 0
defer func() {
os.Exit(exitCode)
}()

// proxy to selected version
cmd := exec.Command(execPath, cmdArgs...)
done, err := initIO(cmd, execPath, &exitCode)
done, err := initIO(cmd, execPath, &exitCode, gha)
if err != nil {
exitWithErrorMsg(execPath, err, &exitCode)

Expand Down Expand Up @@ -81,12 +78,7 @@ func exitWithErrorMsg(execName string, err error, pExitCode *int) {
*pExitCode = 1
}

func initIO(cmd *exec.Cmd, execName string, pExitCode *int) (func(int), error) {
gha, err := configutils.GetenvBool(false, cmdconst.GithubActionsEnvName)
if err != nil {
return nil, err
}

func initIO(cmd *exec.Cmd, execName string, pExitCode *int, gha bool) (func(int), error) {
cmd.Stdin = os.Stdin
if !gha {
cmd.Stderr = os.Stderr
Expand Down
2 changes: 1 addition & 1 deletion versionmanager/proxy/agnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ func ExecAgnostic(conf *config.Config, builders map[string]builder.BuilderFunc,
os.Exit(1)
}

RunCmd(installPath, detectedVersion, execName, cmdArgs)
RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions)
}
41 changes: 39 additions & 2 deletions versionmanager/proxy/light/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,49 @@
package lightproxy

import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"

"github.com/tofuutils/tenv/v2/config/cmdconst"
proxycmd "github.com/tofuutils/tenv/v2/versionmanager/proxy/cmd"
)

func Exec(execName string) {
proxycmd.Run(cmdconst.TenvName, append([]string{cmdconst.CallSubCmd, execName}, os.Args[1:]...))
cmdArgs := make([]string, len(os.Args)+1)
cmdArgs[0], cmdArgs[1] = cmdconst.CallSubCmd, execName
copy(cmdArgs[2:], os.Args[1:])

// proxy to selected version
cmd := exec.Command(cmdconst.TenvName, cmdArgs...) //nolint
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Start()
if err != nil {
exitWithErrorMsg(execName, err)
}

signalChan := make(chan os.Signal)
go transmitSignal(signalChan, cmd.Process)
signal.Notify(signalChan, os.Interrupt) //nolint

if err = cmd.Wait(); err != nil {
var exitError *exec.ExitError
if ok := errors.As(err, &exitError); ok {
os.Exit(exitError.ExitCode())
}
exitWithErrorMsg(execName, err)
}
}

func exitWithErrorMsg(execName string, err error) {
fmt.Println("Failure during", execName, "call :", err) //nolint
os.Exit(1)
}

func transmitSignal(signalReceiver <-chan os.Signal, process *os.Process) {
for range signalReceiver {
_ = process.Signal(os.Interrupt)
}
}
8 changes: 4 additions & 4 deletions versionmanager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"path/filepath"

"github.com/tofuutils/tenv/v2/config"
cmdproxy "github.com/tofuutils/tenv/v2/pkg/cmdproxy"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
cmdproxy "github.com/tofuutils/tenv/v2/versionmanager/proxy/cmd"
terragruntparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/terragrunt"
)

Expand All @@ -47,9 +47,9 @@ func Exec(conf *config.Config, builderFunc builder.BuilderFunc, gruntParser terr
os.Exit(1)
}

RunCmd(installPath, detectedVersion, execName, cmdArgs)
RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions)
}

func RunCmd(installPath string, detectedVersion string, execName string, cmdArgs []string) {
cmdproxy.Run(filepath.Join(installPath, detectedVersion, execName), cmdArgs)
func RunCmd(installPath string, detectedVersion string, execName string, cmdArgs []string, gha bool) {
cmdproxy.Run(filepath.Join(installPath, detectedVersion, execName), cmdArgs, gha)
}

0 comments on commit 7a46ebb

Please sign in to comment.