aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/acme/main.go7
-rw-r--r--contact.go40
2 files changed, 22 insertions, 25 deletions
diff --git a/cmd/acme/main.go b/cmd/acme/main.go
index 69a72ad..f96ddde 100644
--- a/cmd/acme/main.go
+++ b/cmd/acme/main.go
@@ -57,9 +57,10 @@ func main() {
log.Fatal(err)
}
- con := acme.Contacts{}
- con.Mail(v.Mail)
- con.Phone(v.Phone)
+ con, err := acme.NewContacts(v.Mail, v.Phone)
+ if err != nil {
+ log.Fatal(err)
+ }
log.Println("Register", con)
err = prov.Register(acc, con)
diff --git a/contact.go b/contact.go
index 11061b9..71b4ecd 100644
--- a/contact.go
+++ b/contact.go
@@ -14,18 +14,6 @@ type Contact interface {
encoding.TextMarshaler
}
-type Mail string
-
-func (m Mail) MarshalText() ([]byte, error) {
- return []byte("mailto:" + m), nil
-}
-
-type Phone string
-
-func (ph Phone) MarshalText() ([]byte, error) {
- return []byte("tel:" + ph), nil
-}
-
type Contacts []Contact
func (c *Contacts) UnmarshalJSON(b []byte) error {
@@ -48,18 +36,26 @@ func (c *Contacts) UnmarshalJSON(b []byte) error {
return nil
}
-func (c *Contacts) Mail(email string) error {
+type Mail string
+
+func (m Mail) MarshalText() ([]byte, error) {
+ return []byte("mailto:" + m), nil
+}
+
+type Phone string
+
+func (ph Phone) MarshalText() ([]byte, error) {
+ return []byte("tel:" + ph), nil
+}
+
+func NewContacts(email, phone string) (Contacts, error) {
m, err := mail.ParseAddress(email)
if err != nil {
- return err
+ return nil, err
}
- *c = append(*c, Mail(m.Address))
- return nil
-}
-
-func (c *Contacts) Phone(phone string) error {
- if ph := strings.TrimSpace(phone); ph != "" {
- *c = append(*c, Phone(ph))
+ c := Contacts{Mail(m.Address)}
+ if phone != "" {
+ c = append(c, Phone(phone))
}
- return nil
+ return c, nil
}