github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/cmd/rbea_roles_update.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "github.com/emirpasic/gods/sets/hashset" 7 "io/ioutil" 8 "net/http" 9 "os" 10 "strings" 11 ) 12 13 // Default configuration constants 14 const ( 15 RoleURL = "https://raw.githubusercontent.com/mixmaxhq/role-based-email-addresses/master/index.js" 16 RBEARolePath = "pkg/ev/role/rbea_roles.go" 17 ) 18 19 var excludes = hashset.New( 20 "asd", 21 "asdasd", 22 "asdf", 23 ) 24 25 func rbeaRolesUpdate(url, path string) { 26 rolesResp, err := http.Get(url) 27 errPanic(err) 28 defer rolesResp.Body.Close() 29 30 var roles = make([]string, 0) 31 rolesBytes, err := ioutil.ReadAll(rolesResp.Body) 32 errPanic(err) 33 34 rolesBytes = bytes.ReplaceAll(rolesBytes[17:len(rolesBytes)-2], []byte{'\''}, []byte{'"'}) 35 err = json.Unmarshal(rolesBytes, &roles) 36 errPanic(err) 37 38 f, err := os.Create(path) 39 errPanic(err) 40 defer f.Close() 41 42 f.WriteString(generateRoleCode(roles)) 43 } 44 45 func generateRoleCode(roles []string) string { 46 strBuilder := strings.Builder{} 47 strBuilder.WriteString( 48 `package role 49 50 import ( 51 "github.com/emirpasic/gods/sets/hashset" 52 "github.com/go-email-validator/go-email-validator/pkg/ev/contains" 53 "strings" 54 ) 55 56 // RBEARoles returns the list of roles 57 func RBEARoles() []string { 58 return rbeaRoles 59 } 60 61 // NewRBEASetRole forms contains.InSet from roles (https://github.com/mixmaxhq/role-based-email-addresses) 62 func NewRBEASetRole() contains.InSet { 63 RBEARoles := RBEARoles() 64 roles := make([]interface{}, len(RBEARoles)) 65 for i, role := range RBEARoles { 66 roles[i] = strings.ToLower(role) 67 } 68 69 return contains.NewSet(hashset.New(roles...)) 70 } 71 `) 72 73 strBuilder.WriteString( 74 ` 75 var rbeaRoles = []string{ 76 `) 77 for _, role := range roles { 78 if excludes.Contains(role) { 79 continue 80 } 81 82 strBuilder.WriteString("\t\"") 83 strBuilder.WriteString(role) 84 strBuilder.WriteString("\",\n") 85 } 86 strBuilder.WriteString("}\n") 87 88 return strBuilder.String() 89 }