diff --git a/cmd/gen-vcard.go b/cmd/gen-vcard.go new file mode 100644 index 000000000..a5d268210 --- /dev/null +++ b/cmd/gen-vcard.go @@ -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 +} diff --git a/go.mod b/go.mod index 278292631..098902900 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6fbc8c7a4..7682d0720 100644 --- a/go.sum +++ b/go.sum @@ -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=