Skip to content

Commit

Permalink
fix(client, use): format repository name for valid environment variab…
Browse files Browse the repository at this point in the history
…le name

When using the `trdl use group-name-with-dashes 1 stable` command, an invalid script was generated due to non-formated repository name. Now, the repository name is formatted to contain only letters, numbers, and underscores, ensuring it is a valid environment variable name.

All characters that are not letters, numbers, or underscores are replaced with underscores. The repository name is also converted to uppercase.

Signed-off-by: Aleksei Igrychev <[email protected]>
  • Loading branch information
alexey-igrychev committed Jan 16, 2025
1 parent c99e182 commit 6e28b3f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client/pkg/repo/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/werf/trdl/client/pkg/util"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (c Client) prepareSourceScriptFileNameAndData(group, channel, shell string,
backgroundUpdateArgsString := strings.Join(backgroundUpdateArgs, " ")
_ = logPathBackgroundUpdateStderr
trdlBinaryPath := os.Args[0]
trdlUseRepoGroupChannelEnvName := fmt.Sprintf("TRDL_USE_%s_GROUP_CHANNEL", strings.ToUpper(c.repoName))
trdlUseRepoGroupChannelEnvName := fmt.Sprintf("TRDL_USE_%s_GROUP_CHANNEL", formatRepoName(c.repoName))
trdlUseRepoGroupChannelEnvValue := fmt.Sprintf("%s %s", group, channel)

var tmpl string
Expand Down Expand Up @@ -175,3 +176,11 @@ func (c Client) syncSourceScriptFile(group, channel, name string, data []byte) (

return scriptPath, nil
}

// formatRepoName returns a formatted repository name.
// It replaces all non-alphanumeric characters with underscores and converts the result to uppercase.
func formatRepoName(repoName string) string {
re := regexp.MustCompile("[^a-zA-Z0-9_]+")
formattedName := re.ReplaceAllString(repoName, "_")
return strings.ToUpper(formattedName)
}

0 comments on commit 6e28b3f

Please sign in to comment.