-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
62 lines (54 loc) · 1.68 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cmd
import (
"log"
air "github.com/mehanizm/airtable"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/charlieegan3/tool-airtable-contacts/pkg/airtable"
"github.com/charlieegan3/tool-airtable-contacts/pkg/carddav"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "download data from airtable and upload to carddav server",
Run: func(cmd *cobra.Command, args []string) {
viper.SetConfigName("config.test")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error config file: %s \n", err)
}
// get the latest data
airtableClient := air.NewClient(
viper.GetString("tools.airtable-contacts.airtable.key"),
)
records, err := airtable.Download(
airtableClient,
viper.GetString("tools.airtable-contacts.airtable.base"),
viper.GetString("tools.airtable-contacts.airtable.table"),
viper.GetString("tools.airtable-contacts.airtable.view"),
)
if err != nil {
log.Fatalf("failed to download contacts: %s", err)
}
cardDAVClient := carddav.Client{
URL: viper.GetString("tools.airtable-contacts.carddav.server_url"),
User: viper.GetString("tools.airtable-contacts.carddav.user"),
Password: viper.GetString("tools.airtable-contacts.carddav.password"),
}
// records passed as vcard sync is done on per contact basis
err = carddav.Sync(
cardDAVClient,
records,
viper.GetBool("tools.airtable-contacts.vcard.use_v3"),
viper.GetInt("tools.airtable-contacts.vcard.photo.size"),
)
if err != nil {
log.Fatalf("failed to upload to carddav: %s", err)
}
},
}
func init() {
rootCmd.AddCommand(syncCmd)
}