-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-exif-date.go
92 lines (79 loc) · 2.02 KB
/
fix-exif-date.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Package main implements the main CLI.
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/denarced/fix-exif-date/lib/fixexif"
"github.com/denarced/fix-exif-date/shared"
)
// CLI for this application.
var CLI struct {
Timezone string `help:"Override local timezone." default:"Local"`
Files []string `arg:"" name:"file" help:"Photos to fix."`
}
func main() {
shared.InitLogging()
shared.Logger.Info().Msg(" ------ Start ----- ")
kong.Parse(&CLI)
location := parseTimezone(CLI.Timezone)
if location == nil {
os.Exit(2)
}
out := &cliOutput{first: true, indent: 4}
for _, each := range CLI.Files {
err := fixexif.FixDate(each, location, out)
if err != nil {
shared.Logger.Error().
Str("filepath", each).
Err(err).
Msg("Failed to fix EXIF date. Quitting.")
os.Exit(1)
}
}
shared.Logger.Info().Msg(" ----- Done ----- ")
}
func parseTimezone(timezone string) *time.Location {
location, err := time.LoadLocation(timezone)
if err != nil {
shared.Logger.Info().Err(err).Msg("Failed to load timezone.")
fmt.Fprintf(os.Stderr, "Invalid timezone location: %s\n", timezone)
return nil
}
return location
}
type cliOutput struct {
first bool
indent int
}
func (v *cliOutput) PrintFile(file string) {
if !v.first {
fmt.Println()
}
v.first = false
fmt.Printf(" -- %s\n", file)
}
func (v *cliOutput) SkipFile() {
fmt.Printf("%sskip\n", strings.Repeat(" ", v.indent))
}
func (v *cliOutput) PrintDates(tag uint16, original, updated string) {
v.printPair(tag, original, updated)
}
func (v *cliOutput) PrintOffsets(tag uint16, original, updated string) {
v.printPair(tag, original, updated)
}
func (v *cliOutput) printPair(tag uint16, first, second string) {
fmt.Printf("%s0x%04x %s -> %s\n", v.createIndent(), tag, first, second)
}
func (v *cliOutput) createIndent() string {
return strings.Repeat(" ", v.indent)
}
func (v *cliOutput) Done(success bool) {
outcome := "ok"
if !success {
outcome = "fail"
}
fmt.Printf("%sdone: %s\n", v.createIndent(), outcome)
}