This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.89 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/pkg/errors"
"github.com/tj/go-dropbox"
)
func main() {
// Check if config is provided as command line argument.
if len(os.Args) < 2 {
log.Fatal("please provide config file as first argument")
}
// Read configuration file.
configFile := os.Args[1]
config, err := LoadConfig(configFile)
if err != nil {
log.Fatalf("loading config from '%s' failed: %s", configFile, err)
}
// Build filename for this run using current time.
filenameFull := fmt.Sprintf(config.FilePath, time.Now().Format("2006-01-02-15-04-05"))
log.Printf("capturing -> %s", filenameFull)
// Initialize camera and capture image with it.
camera := NewCamera(config.CaptureCommand, config.CaptureCommandArgs)
if err := camera.Capture(filenameFull); err != nil {
log.Fatalf("capturing image failed: %s", err)
}
// Prepare custom function that is used for uploading images to Dropbox.
dropboxUploadFunc := prepareDropboxUploadFunc(config)
log.Printf("uploading <- %s", filenameFull)
// Run uploader to get image uploaded to Dropbox.
dropboxUploader := NewDropboxUploader(dropboxUploadFunc)
if err := dropboxUploader.Upload(filenameFull); err != nil {
log.Fatalf("uploading to Dropbox failed: %s", err)
}
// Share the happiness!
log.Printf("done! %s", filenameFull)
}
// prepareDropboxUploadFunc initializes the uploader library with correct settings.
func prepareDropboxUploadFunc(config *Config) UploadFunc {
return func(name string, file io.Reader) error {
dbx := dropbox.NewFiles(dropbox.NewConfig(config.DropboxToken))
uploadInput := &dropbox.UploadInput{
Path: fmt.Sprintf("/%s", name),
Mode: dropbox.WriteModeAdd,
AutoRename: false,
Mute: true,
Reader: file,
}
if _, err := dbx.Upload(uploadInput); err != nil {
return errors.Wrap(err, "uploading file to Dropbox failed")
}
return nil
}
}