Created
May 21, 2020 16:30
-
-
Save meeDamian/33eb0034fa612683509a20173aef6917 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"strings" | |
"github.com/BurntSushi/toml" | |
"github.com/google/uuid" | |
) | |
const ( | |
Package = "lukechilds/company-contacts" | |
vBegin = "BEGIN:VCARD" | |
vVersion = "VERSION:3.0" | |
vKindOrg = "KIND:org" | |
vKindGroup = "KIND:group" | |
vNote = "NOTE:Generated by " + Package | |
vEnd = "END:VCARD" | |
) | |
type ( | |
Company struct { | |
ID, | |
Name, | |
Logo, | |
Color, | |
Website string | |
LogoPadding int | |
Phone interface{} | |
Email interface{} | |
vcard []string | |
} | |
Companies map[string]Company | |
) | |
func (c Company) UUID() string { | |
b := []byte(fmt.Sprintf("%s:%s", Package, c.ID)) | |
// Convert last 16 bytes into UUID | |
uid, err := uuid.FromBytes(b[len(b)-16:]) | |
if err != nil { | |
panic(err) | |
} | |
return uid.URN() | |
} | |
func unFuck(fuck interface{}) string { | |
if cuss, ok := fuck.(string); ok { | |
return cuss | |
} | |
return "" | |
} | |
func unClusterFuck(clusterFuck interface{}) (clusterCuss []string) { | |
if cuss := unFuck(clusterFuck); cuss != "" { | |
return []string{cuss} | |
} | |
if fucks, ok := clusterFuck.([]interface{}); ok { | |
for _, fuck := range fucks { | |
clusterCuss = append(clusterCuss, unFuck(fuck)) | |
} | |
} | |
return | |
} | |
func (c Company) ToVCard() string { | |
v := []string{ | |
vBegin, | |
vVersion, | |
vKindOrg, | |
} | |
v = append(v, fmt.Sprintf("FN:%s", c.Name)) | |
v = append(v, fmt.Sprintf("URL:%s", c.Website)) | |
for _, phone := range unClusterFuck(c.Phone) { | |
v = append(v, fmt.Sprintf("TEL;VALUE=uri;TYPE=cell:tel:%s", phone)) | |
} | |
for _, email := range unClusterFuck(c.Email) { | |
v = append(v, fmt.Sprintf("EMAIL;TYPE=INTERNET;TYPE=WORK:%s", email)) | |
} | |
logo, err := ioutil.ReadFile(fmt.Sprintf("logos/%s", c.Logo)) | |
if err != nil { | |
panic(err) | |
} | |
logoMinimized := regexp.MustCompile(`[\n\t]|[\s\p{Zs}]{2,}`).ReplaceAllString(string(logo), "") | |
v = append(v, fmt.Sprintf("LOGO:data:image/svg+xml;utf8,%s", logoMinimized)) | |
v = append(v, fmt.Sprintf("PHOTO:data:image/svg+xml;utf8,%s", logoMinimized)) | |
v = append(v, fmt.Sprintf("CATEGORIES:Company Logos")) | |
v = append(v, fmt.Sprintf("UID:%s", c.UUID())) | |
return strings.Join(append(v, vNote, vEnd), "\n") | |
} | |
func main() { | |
_ = os.MkdirAll("dist/individual-vcards", os.ModePerm) | |
var companies Companies | |
if _, err := toml.DecodeFile("companies.toml", &companies); err != nil { | |
panic(err) | |
} | |
f, err := os.Create("dist/company-contacts.vcf") | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
var group []string | |
for id, company := range companies { | |
company.ID = id | |
group = append(group, fmt.Sprintf("MEMBER:%s", company.UUID())) | |
vcard := company.ToVCard() | |
err := ioutil.WriteFile( | |
fmt.Sprintf("dist/individual-vcards/%s.vcf", id), | |
[]byte(vcard), | |
os.ModePerm, | |
) | |
if err != nil { | |
panic(err) | |
} | |
if _, err := f.WriteString(fmt.Sprintf("%s\n\n", vcard)); err != nil { | |
panic(err) | |
} | |
} | |
_, err = f.WriteString(fmt.Sprintf("\n\n%s\n%s", strings.Join( | |
append([]string{ | |
vBegin, | |
vVersion, | |
vKindGroup, | |
fmt.Sprintf("FN:Company Logos"), | |
}, group...), "\n"), | |
vEnd)) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment