Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge exeutils package into osutils. #2934

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cmd/state-installer/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/events"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/installation/storage"
Expand Down Expand Up @@ -382,8 +381,8 @@ func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.D
an.Event(anaConst.CatInstallerFunnel, "forward-command")

out.Print(fmt.Sprintf("\nRunning '[ACTIONABLE]%s[/RESET]'\n", params.command))
cmd, args := exeutils.DecodeCmd(params.command)
if _, _, err := exeutils.ExecuteAndPipeStd(cmd, args, envSlice(binPath)); err != nil {
cmd, args := osutils.DecodeCmd(params.command)
if _, _, err := osutils.ExecuteAndPipeStd(cmd, args, envSlice(binPath)); err != nil {
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-command-err", err.Error())
return errs.Silence(errs.Wrap(err, "Running provided command failed, error returned: %s", errs.JoinMessage(err)))
}
Expand All @@ -392,7 +391,7 @@ func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.D
an.Event(anaConst.CatInstallerFunnel, "forward-activate")

out.Print(fmt.Sprintf("\nRunning '[ACTIONABLE]state activate %s[/RESET]'\n", params.activate.String()))
if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activate.String()}, envSlice(binPath)); err != nil {
if _, _, err := osutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activate.String()}, envSlice(binPath)); err != nil {
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-err", err.Error())
return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activate.String(), errs.JoinMessage(err)))
}
Expand All @@ -401,7 +400,7 @@ func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.D
an.Event(anaConst.CatInstallerFunnel, "forward-activate-default")

out.Print(fmt.Sprintf("\nRunning '[ACTIONABLE]state activate --default %s[/RESET]'\n", params.activateDefault.String()))
if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activateDefault.String(), "--default"}, envSlice(binPath)); err != nil {
if _, _, err := osutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activateDefault.String(), "--default"}, envSlice(binPath)); err != nil {
an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-default-err", err.Error())
return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activateDefault.String(), errs.JoinMessage(err)))
}
Expand Down Expand Up @@ -478,7 +477,7 @@ func noArgs() bool {
func shouldUpdateInstalledStateTool(stateExePath string) bool {
logging.Debug("Checking if installed state tool is an older version.")

stdout, _, err := exeutils.ExecSimple(stateExePath, []string{"--version", "--output", "json"}, os.Environ())
stdout, _, err := osutils.ExecSimple(stateExePath, []string{"--version", "--output", "json"}, os.Environ())
if err != nil {
logging.Debug("Could not determine state tool version.")
return true // probably corrupted install
Expand Down
7 changes: 3 additions & 4 deletions cmd/state-installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/installmgr"
Expand Down Expand Up @@ -127,7 +126,7 @@ func (i *Installer) Install() (rerr error) {

// Run state _prepare after updates to facilitate anything the new version of the state tool might need to set up
// Yes this is awkward, followup story here: https://www.pivotaltracker.com/story/show/176507898
if stdout, stderr, err := exeutils.ExecSimple(stateExec, []string{"_prepare"}, []string{}); err != nil {
if stdout, stderr, err := osutils.ExecSimple(stateExec, []string{"_prepare"}, []string{}); err != nil {
multilog.Error("_prepare failed after update: %v\n\nstdout: %s\n\nstderr: %s", err, stdout, stderr)
}

Expand Down Expand Up @@ -211,7 +210,7 @@ func detectCorruptedInstallDir(path string) error {
}

func isStateExecutable(name string) bool {
if name == constants.StateCmd+exeutils.Extension || name == constants.StateSvcCmd+exeutils.Extension {
if name == constants.StateCmd+osutils.ExeExtension || name == constants.StateSvcCmd+osutils.ExeExtension {
return true
}
return false
Expand All @@ -223,7 +222,7 @@ func installedOnPath(installRoot, branch string) (bool, string, error) {
}

// This is not using appinfo on purpose because we want to deal with legacy installation formats, which appinfo does not
stateCmd := constants.StateCmd + exeutils.Extension
stateCmd := constants.StateCmd + osutils.ExeExtension

// Check for state.exe in branch, root and bin dir
// This is to handle older state tool versions that gave incompatible input paths
Expand Down
2 changes: 1 addition & 1 deletion cmd/state-installer/test/integration/installer_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (suite *InstallerIntegrationTestSuite) SetupSuite() {
localPayload := filepath.Join(rootPath, "build", "payload", constants.ToplevelInstallArchiveDir)
suite.Require().DirExists(localPayload, "locally generated payload exists")

installerExe := filepath.Join(localPayload, constants.StateInstallerCmd+osutils.ExeExt)
installerExe := filepath.Join(localPayload, constants.StateInstallerCmd+osutils.ExeExtension)
suite.Require().FileExists(installerExe, "locally generated installer exists")

suite.installerExe = installerExe
Expand Down
4 changes: 2 additions & 2 deletions cmd/state-remote-installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/events"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/prompt"
Expand Down Expand Up @@ -201,7 +201,7 @@ func execute(out output.Outputer, prompt prompt.Prompter, cfg *config.Instance,
env := []string{
constants.InstallerNoSubshell + "=true",
}
_, cmd, err := exeutils.ExecuteAndPipeStd(filepath.Join(tmpDir, constants.StateInstallerCmd+exeutils.Extension), args, env)
_, cmd, err := osutils.ExecuteAndPipeStd(filepath.Join(tmpDir, constants.StateInstallerCmd+osutils.ExeExtension), args, env)
if err != nil {
if cmd != nil && cmd.ProcessState.Sys().(syscall.WaitStatus).Exited() {
// The issue happened while running the command itself, meaning the responsibility for conveying the error
Expand Down
4 changes: 2 additions & 2 deletions cmd/state-svc/test/integration/svc_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/svcctl"
"github.com/ActiveState/cli/internal/testhelpers/e2e"
"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
Expand Down Expand Up @@ -192,7 +192,7 @@ func (suite *SvcIntegrationTestSuite) GetNumStateSvcProcesses() int {
for _, p := range procs {
if name, err := p.Name(); err == nil {
name = filepath.Base(name) // just in case an absolute path is returned
if svcName := constants.ServiceCommandName + exeutils.Extension; name == svcName {
if svcName := constants.ServiceCommandName + osutils.ExeExtension; name == svcName {
count++
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/state/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
Expand Down Expand Up @@ -178,7 +177,7 @@ func relaunch(args []string) (int, error) {
return -1, &ErrStateExe{locale.WrapError(err, "err_state_exec")}
}

code, _, err := exeutils.ExecuteAndPipeStd(exec, args[1:], []string{fmt.Sprintf("%s=true", constants.ForwardedStateEnvVarName)})
code, _, err := osutils.ExecuteAndPipeStd(exec, args[1:], []string{fmt.Sprintf("%s=true", constants.ForwardedStateEnvVarName)})
if err != nil {
return code, &ErrExecuteRelaunch{errs.Wrap(err, "Forwarded command after auto-updating failed. Exit code: %d", code)}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/installation/appinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const (
)

var execData = map[executableType]string{
state: constants.StateCmd + osutils.ExeExt,
service: constants.StateSvcCmd + osutils.ExeExt,
installer: constants.StateInstallerCmd + osutils.ExeExt,
executor: constants.StateExecutorCmd + osutils.ExeExt,
state: constants.StateCmd + osutils.ExeExtension,
service: constants.StateSvcCmd + osutils.ExeExtension,
installer: constants.StateInstallerCmd + osutils.ExeExtension,
executor: constants.StateExecutorCmd + osutils.ExeExtension,
}

func newExec(exec executableType) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/installation/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/osutils"
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ func InstallPathFromExecPath() (string, error) {
}

func InstallPathFromReference(dir string) (string, error) {
cmdName := constants.StateCmd + exeutils.Extension
cmdName := constants.StateCmd + osutils.ExeExtension
installPath := filepath.Dir(dir)
binPath, err := BinPathFromInstallPath(installPath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/installation/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/osutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestInstallPathFromReference(t *testing.T) {
err = fileutils.Mkdir(binDir)
require.NoError(t, err)

err = fileutils.Touch(filepath.Join(binDir, constants.StateCmd+exeutils.Extension))
err = fileutils.Touch(filepath.Join(binDir, constants.StateCmd+osutils.ExeExtension))
require.NoError(t, err)

t.Setenv(constants.HomeEnvVarName, home)
Expand Down
5 changes: 2 additions & 3 deletions internal/installmgr/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/locale"
Expand Down Expand Up @@ -46,7 +45,7 @@ func stopSvc(installPath string) error {
}

if fileutils.FileExists(svcExec) {
exitCode, _, err := exeutils.Execute(svcExec, []string{"stop"}, nil)
exitCode, _, err := osutils.Execute(svcExec, []string{"stop"}, nil)
if err != nil {
// We don't return these errors because we want to fall back on killing the process
multilog.Error("Stopping %s returned error: %s", constants.SvcAppName, errs.JoinMessage(err))
Expand Down Expand Up @@ -74,7 +73,7 @@ func stopSvc(installPath string) error {
continue
}

svcName := constants.ServiceCommandName + exeutils.Extension
svcName := constants.ServiceCommandName + osutils.ExeExtension
if n == svcName {
exe, err := p.Exe()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/osutils"
"github.com/thoas/go-funk"
)

Expand Down Expand Up @@ -58,11 +58,11 @@ var lookup = [...]languageData{
},
{
"bash", "Bash", ".sh", true, "", "",
Executable{"bash" + exeutils.Extension, true},
Executable{"bash" + osutils.ExeExtension, true},
},
{
"sh", "Shell", ".sh", true, "", "",
Executable{"sh" + exeutils.Extension, true},
Executable{"sh" + osutils.ExeExtension, true},
},
{
"batch", "Batch", ".bat", false, "", "",
Expand Down
6 changes: 3 additions & 3 deletions internal/legacytray/legacytray.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/ActiveState/cli/internal/app"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/osutils/autostart"
"github.com/shirou/gopsutil/v3/process"
)
Expand All @@ -21,7 +21,7 @@ const stateUpdateDialogCmd = "state-update-dialog"

func DetectAndRemove(path string, cfg *config.Instance) error {
binDir := filepath.Join(path, installation.BinDirName)
trayExec := filepath.Join(binDir, stateTrayCmd+exeutils.Extension)
trayExec := filepath.Join(binDir, stateTrayCmd+osutils.ExeExtension)
if !fileutils.FileExists(trayExec) {
return nil // nothing to do
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func DetectAndRemove(path string, cfg *config.Instance) error {

// Finally, remove state-tray and state-update-dialog executables.
for _, name := range []string{stateTrayCmd, stateUpdateDialogCmd} {
if exec := filepath.Join(binDir, name+exeutils.Extension); fileutils.FileExists(exec) {
if exec := filepath.Join(binDir, name+osutils.ExeExtension); fileutils.FileExists(exec) {
err = os.Remove(exec)
if err != nil {
return errs.Wrap(err, "Unable to remove %s", name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exeutils
package osutils

import (
"bytes"
Expand All @@ -15,7 +15,6 @@ import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/osutils"
)

// Executables will return all the Executables that need to be symlinked in the various provided bin directories
Expand Down Expand Up @@ -133,7 +132,7 @@ func Execute(command string, arg []string, optSetter func(cmd *exec.Cmd) error)
if err != nil {
logging.Debug("Executing command returned error: %v", err)
}
return osutils.CmdExitCode(cmd), cmd, err
return CmdExitCode(cmd), cmd, err
}

// ExecuteAndPipeStd will run the given command and pipe stdin, stdout and stderr
Expand All @@ -157,7 +156,7 @@ func ExecuteAndForget(command string, args []string, opts ...func(cmd *exec.Cmd)
}
}

cmd.SysProcAttr = osutils.SysProcAttrForBackgroundProcess()
cmd.SysProcAttr = SysProcAttrForBackgroundProcess()
if err := cmd.Start(); err != nil {
return nil, errs.Wrap(err, "Could not start %s %v", command, args)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exeutils
package osutils

import (
"fmt"
Expand Down Expand Up @@ -117,4 +117,4 @@ func TestExecuteAndPipeStdExitCode(t *testing.T) {
assert.Equalf(t, tt.wantCode, got, "ExecuteAndPipeStd(%v, %v, %v)", tt.args.command, tt.args.arg, tt.args.env)
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//go:build !windows
// +build !windows

package exeutils
package osutils

const Extension = ""
const ExeExtension = ""

var exts = []string{}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exeutils
package osutils

import (
"os"
Expand All @@ -7,12 +7,11 @@ import (
"github.com/thoas/go-funk"
)

const Extension = ".exe"
const ExeExtension = ".exe"

var exts = []string{".exe"}

func init() {
PATHEXT := os.Getenv("PATHEXT")
exts = funk.Uniq(funk.Map(strings.Split(PATHEXT, string(os.PathListSeparator)), strings.ToLower).([]string)).([]string)
}

6 changes: 3 additions & 3 deletions internal/exeutils/find.go → internal/osutils/find.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exeutils
package osutils

import (
"os"
Expand All @@ -20,7 +20,7 @@ func FindExeOnPATH(executable string) string {

// FindExeOnPATH returns the first path from the PATH env var for which the executable exists
func FilterExesOnPATH(executable string, PATH string, filter func(exe string) bool) []string {
return findExes(executable, PATH, exts, fileutils.TargetExists, filter)
return findExes(executable, PATH, exts, fileutils.TargetExists, filter)
}

func FindExeInside(executable string, PATH string) string {
Expand Down Expand Up @@ -50,7 +50,7 @@ func findExes(executable string, PATH string, exts []string, fileExists func(str
return result
}

func findExe(executable string, PATH string, exts []string,fileExists func(string) bool, filter func(exe string) bool) string {
func findExe(executable string, PATH string, exts []string, fileExists func(string) bool, filter func(exe string) bool) string {
r := findExes(executable, PATH, exts, fileExists, filter)
if len(r) > 0 {
return r[0]
Expand Down
Loading
Loading