Skip to content

Commit

Permalink
feat(client): export environment variable with a used group and chann…
Browse files Browse the repository at this point in the history
…el in the source script

export TRDL_USE_<REPO>_GROUP_CHANNEL=<GROUP> <CHANNEL>

Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
  • Loading branch information
alexey-igrychev committed Jun 21, 2022
1 parent b09c023 commit 3c083a7
Showing 1 changed file with 49 additions and 44 deletions.
93 changes: 49 additions & 44 deletions client/pkg/repo/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,20 @@ func (c Client) prepareSourceScriptFileNameAndData(group, channel, shell string,
backgroundUpdateArgs = append(backgroundUpdateArgs, "--no-self-update")
}

common := strings.Join(commonArgs, " ") // %[1]s: REPO GROUP CHANNEL
foreground := strings.Join(foregroundUpdateArgs, " ") // %[2]s: REPO GROUP CHANNEL [flag ...]
background := strings.Join(backgroundUpdateArgs, " ") // %[3]s: REPO GROUP CHANNEL [flag ...]
_ = logPathBackgroundUpdateStderr // %[4]s: "*_background_update_stderr.log"
trdlBinaryPath := os.Args[0] // %[5]s: trdl binary path

var content string
commonArgsString := strings.Join(commonArgs, " ")
foregroundUpdateArgsString := strings.Join(foregroundUpdateArgs, " ")
backgroundUpdateArgsString := strings.Join(backgroundUpdateArgs, " ")
_ = logPathBackgroundUpdateStderr
trdlBinaryPath := os.Args[0]
trdlUseRepoGroupChannelEnvName := fmt.Sprintf("TRDL_USE_%s_GROUP_CHANNEL", strings.ToUpper(c.repoName))
trdlUseRepoGroupChannelEnvValue := fmt.Sprintf("%s %s", group, channel)

var tmpl string
var ext string
switch shell {
case "pwsh":
ext, content = pwshSourceScript(common, foreground, background, logPathBackgroundUpdateStderr, trdlBinaryPath)
default: // unix shell
ext, content = unixSourceScript(common, foreground, background, logPathBackgroundUpdateStderr, trdlBinaryPath)
}

name := "source_script"
if ext != "" {
name = strings.Join([]string{name, ext}, ".")
}

data := []byte(fmt.Sprintln(strings.TrimSpace(content)))

return name, data
}

func (c Client) prepareSourceScriptBasename(group, channel, shell string, opts UseSourceOptions) string {
basename := fmt.Sprintf("use_%s_%s_%s", group, channel, shell)

if opts.NoSelfUpdate {
basename += "_" + util.MurmurHash(fmt.Sprintf("%+v", opts))
}

return basename
}

func pwshSourceScript(common, foregroundUpdate, backgroundUpdate, logPathBackgroundUpdateStderr, trdlBinaryPath string) (string, string) {
filenameExt := "ps1"
fileContent := fmt.Sprintf(`
ext = "ps1"
tmpl = `
if (Test-Path %[4]q -PathType Leaf) {
$trdlStderrLog = Get-Content %[4]q
if (!([String]::IsNullOrWhiteSpace($trdlStderrLog))) {
Expand All @@ -97,17 +73,16 @@ if ((Invoke-Expression -Command "%[5]s bin-path %[1]s" 2> $null | Out-String -Ou
$trdlRepoBinPath = %[5]s bin-path %[1]s
}
[System.Environment]::SetEnvironmentVariable('%[6]s','%[7]s',[System.EnvironmentVariableTarget]::Process);
$trdlRepoBinPath = $trdlRepoBinPath.Trim()
$oldPath = [System.Environment]::GetEnvironmentVariable('PATH',[System.EnvironmentVariableTarget]::Process)
$newPath = "$trdlRepoBinPath;$oldPath"
[System.Environment]::SetEnvironmentVariable('Path',$newPath,[System.EnvironmentVariableTarget]::Process);
`, common, foregroundUpdate, backgroundUpdate, logPathBackgroundUpdateStderr, trdlBinaryPath)

return filenameExt, fileContent
}

func unixSourceScript(common, foregroundUpdate, backgroundUpdate, logPathBackgroundUpdateStderr, trdlBinaryPath string) (string, string) {
fileContent := fmt.Sprintf(`
`
default: // unix shell
ext = ""
tmpl = `
if [ -s %[4]q ]; then
echo Previous run of "trdl update" in background generated following errors:
cat %[4]q
Expand All @@ -120,10 +95,40 @@ else
trdl_repo_bin_path="$(%[5]q bin-path %[1]s)"
fi
export %[6]s="%[7]s"
export PATH="$trdl_repo_bin_path${PATH:+:${PATH}}"
`, common, foregroundUpdate, backgroundUpdate, logPathBackgroundUpdateStderr, trdlBinaryPath)
`
}

script := fmt.Sprintf(tmpl,
commonArgsString, // %[1]s: REPO GROUP CHANNEL (common args string)
foregroundUpdateArgsString, // %[2]s: REPO GROUP CHANNEL [flag ...] (foreground update args string)
backgroundUpdateArgsString, // %[3]s: REPO GROUP CHANNEL [flag ...] (background update args string)
logPathBackgroundUpdateStderr, // %[4]s: <path> (background update error file path)
trdlBinaryPath, // %[5]s: <path> (trdl binary path)
trdlUseRepoGroupChannelEnvName, // %[6]s: <env name> (TRDL_USE_<REPO>_GROUP_CHANNEL)
trdlUseRepoGroupChannelEnvValue, // %[7]s: <env value> (TRDL_USE_<REPO>_GROUP_CHANNEL value)
)

name := "source_script"
if ext != "" {
name = strings.Join([]string{name, ext}, ".")
}

return "", fileContent
data := []byte(fmt.Sprintln(strings.TrimSpace(script)))

return name, data
}

func (c Client) prepareSourceScriptBasename(group, channel, shell string, opts UseSourceOptions) string {
basename := fmt.Sprintf("use_%s_%s_%s", group, channel, shell)

if opts.NoSelfUpdate {
basename += "_" + util.MurmurHash(fmt.Sprintf("%+v", opts))
}

return basename
}

func (c Client) syncSourceScriptFile(group, channel, name string, data []byte) (string, error) {
Expand Down

0 comments on commit 3c083a7

Please sign in to comment.