Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gen-vcard command #1090

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions cmd/gen-vcard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"github.com/emersion/go-vcard"
"github.com/spf13/cobra"
"log"
"os"
"strings"
)

type GenVCardCmdOptions struct {
Output string
Tel string
Fullname string
Title string
Org string
Email string
Kind string
}

func init() {
opts := &GenVCardCmdOptions{}
genVCardCmd := NewGenVCardCmd(opts)

fl := genVCardCmd.Flags()
fl.StringVarP(&opts.Output, "output", "o", "/dev/stdout", "Output file")
fl.StringVar(&opts.Tel, "tel", "", "The phone number of the contact (E164 or international format)")
fl.StringVar(&opts.Fullname, "fullname", "", "Name of the contact")
fl.StringVar(&opts.Title, "title", "", "Title of the contact")
fl.StringVar(&opts.Org, "org", "", "Organization of the contact")
fl.StringVar(&opts.Email, "email", "", "Email of the contact")
fl.StringVar(&opts.Kind, "kind", string(vcard.KindIndividual), "Kind of the contact: 'application', 'individual', 'group', 'location' or 'organization'; 'x-*' values may be used for experimental purposes.")

rootCmd.AddCommand(genVCardCmd)
}

func NewGenVCardCmd(opts *GenVCardCmdOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "gen-vcard",
Example: "phoneinfoga gen-vcard -n +33678345233 --name \"John Doe\" -o cards.vcf",
Short: "Generate vcard v4 file for a given phone number",
Run: func(cmd *cobra.Command, args []string) {
destFile, err := os.Create(opts.Output)
if err != nil {
log.Fatal(err)
}
defer destFile.Close()

enc := vcard.NewEncoder(destFile)

card := vcard.Card{}
card.SetValue(vcard.FieldFormattedName, opts.Fullname)
card.SetValue(vcard.FieldName, strings.Join(strings.Split(opts.Fullname, " "), ";"))
card.SetValue(vcard.FieldTelephone, opts.Tel)
card.SetValue(vcard.FieldOrganization, opts.Org)
card.SetValue(vcard.FieldTitle, opts.Title)
card.SetValue(vcard.FieldEmail, opts.Email)
card.SetValue(vcard.FieldKind, opts.Kind)

vcard.ToV4(card)
err = enc.Encode(card)
if err != nil {
log.Fatal(err)
}
},
}
return cmd
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/emersion/go-vcard v0.0.0-20220507122617-d4056df0ec4a
github.com/fatih/color v1.10.0
github.com/gin-gonic/gin v1.6.3
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/emersion/go-vcard v0.0.0-20220507122617-d4056df0ec4a h1:cltZpe6s0SJtqK5c/5y2VrIYi8BAtDM6qjmiGYqfTik=
github.com/emersion/go-vcard v0.0.0-20220507122617-d4056df0ec4a/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
Expand Down