Skip to content

Commit d4367ef

Browse files
committed
fix(cmd/rofl): Properly normalize ORC filenames
1 parent 0bc78bb commit d4367ef

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cmd/rofl/common/manifest.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package common
22

33
import (
44
"fmt"
5+
"regexp"
6+
"strings"
57

68
"github.com/spf13/cobra"
79

@@ -10,6 +12,14 @@ import (
1012
cliConfig "github.com/oasisprotocol/cli/config"
1113
)
1214

15+
var (
16+
// orcFilenameDisallowedChars is a regexp matching characters that are not allowed in filenames.
17+
orcFilenameDisallowedChars = regexp.MustCompile("[^a-zA-Z0-9-]")
18+
19+
// orcFilenameRepeatedChars is a regexp matching repeats of dash characters in filenames.
20+
orcFilenameRepeatedChars = regexp.MustCompile("(-){2,}")
21+
)
22+
1323
// ManifestOptions configures the manifest options.
1424
type ManifestOptions struct {
1525
// NeedAppID specifies whether a configured app ID is required in the manifest.
@@ -98,5 +108,9 @@ func MaybeLoadManifestAndSetNPA(cfg *cliConfig.Config, npa *common.NPASelection,
98108

99109
// GetOrcFilename generates a filename based on the project name and deployment.
100110
func GetOrcFilename(manifest *rofl.Manifest, deploymentName string) string {
101-
return fmt.Sprintf("%s.%s.orc", manifest.Name, deploymentName)
111+
normalizedName := strings.ToLower(manifest.Name)
112+
normalizedName = strings.TrimSpace(normalizedName)
113+
normalizedName = orcFilenameDisallowedChars.ReplaceAllString(normalizedName, "-")
114+
normalizedName = orcFilenameRepeatedChars.ReplaceAllString(normalizedName, "$1")
115+
return fmt.Sprintf("%s.%s.orc", normalizedName, deploymentName)
102116
}

cmd/rofl/common/manifest_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/oasisprotocol/cli/build/rofl"
9+
)
10+
11+
func TestGetOrcFilename(t *testing.T) {
12+
require := require.New(t)
13+
14+
for _, tc := range []struct {
15+
name string
16+
deployment string
17+
expected string
18+
}{
19+
{"rofl-scheduler", "mainnet", "rofl-scheduler.mainnet.orc"},
20+
{"ROFL Scheduler", "mainnet", "rofl-scheduler.mainnet.orc"},
21+
{" This is a test ", "mainnet", "this-is-a-test.mainnet.orc"},
22+
} {
23+
manifest := &rofl.Manifest{Name: tc.name}
24+
fn := GetOrcFilename(manifest, "mainnet")
25+
require.Equal(tc.expected, fn)
26+
}
27+
}

0 commit comments

Comments
 (0)