From 2997c769913a04320d0e64755a2b64d68a60083f Mon Sep 17 00:00:00 2001 From: Dat Dao Date: Wed, 11 Dec 2024 13:17:13 +0700 Subject: [PATCH] cleanup --- .../controller/atlasschema_controller_test.go | 26 ------------- internal/controller/common.go | 38 ------------------- .../controller/templates/atlas_migration.tmpl | 30 --------------- 3 files changed, 94 deletions(-) delete mode 100644 internal/controller/templates/atlas_migration.tmpl diff --git a/internal/controller/atlasschema_controller_test.go b/internal/controller/atlasschema_controller_test.go index 44611a9..8638b25 100644 --- a/internal/controller/atlasschema_controller_test.go +++ b/internal/controller/atlasschema_controller_test.go @@ -453,32 +453,6 @@ func TestConfigTemplate(t *testing.T) { require.EqualValues(t, expected, buf.String()) } -func TestTemplate_Func_RemoveSpecialChars(t *testing.T) { - var buf bytes.Buffer - tmpl, err := tmpl.New("specialChars").Parse(` - {{- removeSpecialChars .Text -}} - {{- removeSpecialChars .URL -}} -`) - require.NoError(t, err) - var textWithSpecialChars = "a\tb\rc\n" - err = tmpl.ExecuteTemplate(&buf, `specialChars`, struct { - Text string - URL *url.URL - }{ - Text: textWithSpecialChars, - URL: &url.URL{}, - }) - require.NoError(t, err) - require.EqualValues(t, "abc", buf.String()) - // invalid data type - err = tmpl.ExecuteTemplate(&buf, `specialChars`, struct { - Text int - }{ - Text: 0, - }) - require.ErrorContains(t, err, "unsupported type int") -} - func conditionReconciling() *dbv1alpha1.AtlasSchema { return &dbv1alpha1.AtlasSchema{ ObjectMeta: objmeta(), diff --git a/internal/controller/common.go b/internal/controller/common.go index bbd39a3..dc8f144 100644 --- a/internal/controller/common.go +++ b/internal/controller/common.go @@ -16,12 +16,10 @@ package controller import ( "context" - "embed" "errors" "fmt" "regexp" "strings" - "text/template" "time" "ariga.io/atlas-go-sdk/atlasexec" @@ -96,42 +94,6 @@ func NewAtlasExec(dir string, c *Cloud) (AtlasExec, error) { } var ( - //go:embed templates - tmpls embed.FS - tmpl = template.Must(template.New("operator"). - Funcs(template.FuncMap{ - "hclValue": func(s string) string { - if s == "" { - return s - } - return strings.ReplaceAll(strings.ToUpper(s), "-", "_") - }, - "slides": func(s []string) string { - b := &strings.Builder{} - b.WriteRune('[') - for i, v := range s { - if i > 0 { - b.WriteRune(',') - } - fmt.Fprintf(b, "%q", v) - } - b.WriteRune(']') - return b.String() - }, - "removeSpecialChars": func(s interface{}) (string, error) { - r := regexp.MustCompile("[\t\r\n]") - switch s := s.(type) { - case string: - return r.ReplaceAllString(s, ""), nil - case fmt.Stringer: - return r.ReplaceAllString(s.String(), ""), nil - default: - return "", fmt.Errorf("unsupported type %T", s) - } - }, - }). - ParseFS(tmpls, "templates/*.tmpl"), - ) sqlErrRegex = regexp.MustCompile(`sql/migrate: (execute: )?executing statement`) ) diff --git a/internal/controller/templates/atlas_migration.tmpl b/internal/controller/templates/atlas_migration.tmpl deleted file mode 100644 index c67ca23..0000000 --- a/internal/controller/templates/atlas_migration.tmpl +++ /dev/null @@ -1,30 +0,0 @@ -{{- with .Cloud }} -atlas { - cloud { - token = "{{ .Token }}" - {{- if .URL }} - url = "{{ .URL }}" - {{- end }} - {{- if .Repo }} - project = "{{ .Repo }}" - {{- end }} - } -} -{{- end }} -env { - name = atlas.env - url = "{{ removeSpecialChars .URL }}" - dev = "{{ removeSpecialChars .DevURL }}" - migration { - dir = "{{ .DirURL }}" -{{- if .ExecOrder }} - exec_order = {{ hclValue .ExecOrder }} -{{- end }} -{{- if .Baseline }} - baseline = "{{ .Baseline }}" -{{- end }} -{{- if .RevisionsSchema }} - revisions_schema = "{{ .RevisionsSchema }}" -{{- end }} - } -} \ No newline at end of file