exif-read-tool/main.go: Use go-flags for command-line processing

dustin/add_skipped_tags_tracking
Dustin Oprea 2020-05-18 00:09:14 -04:00
parent d0d0f14dea
commit e1d21a3716
3 changed files with 31 additions and 29 deletions

View File

@ -14,7 +14,6 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
@ -22,6 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"github.com/dsoprea/go-logging" "github.com/dsoprea/go-logging"
"github.com/jessevdk/go-flags"
"github.com/dsoprea/go-exif/v2" "github.com/dsoprea/go-exif/v2"
"github.com/dsoprea/go-exif/v2/common" "github.com/dsoprea/go-exif/v2/common"
@ -36,13 +36,6 @@ var (
mainLogger = log.NewLogger("main.main") mainLogger = log.NewLogger("main.main")
) )
var (
filepathArg = ""
printAsJsonArg = false
printLoggingArg = false
thumbnailOutputFilepathArg = ""
)
type IfdEntry struct { type IfdEntry struct {
IfdPath string `json:"ifd_path"` IfdPath string `json:"ifd_path"`
FqIfdPath string `json:"fq_ifd_path"` FqIfdPath string `json:"fq_ifd_path"`
@ -56,28 +49,33 @@ type IfdEntry struct {
ValueString string `json:"value_string"` ValueString string `json:"value_string"`
} }
type parameters struct {
Filepath string `short:"f" long:"filepath" required:"true" description:"File-path of image"`
PrintAsJson bool `short:"j" long:"json" description:"Print out as JSON"`
IsVerbose bool `short:"v" long:"verbose" description:"Print logging"`
ThumbnailOutputFilepath string `short:"t" long:"thumbnail-output-filepath" description:"File-path to write thumbnail to (if present)"`
}
var (
arguments = new(parameters)
)
func main() { func main() {
defer func() { defer func() {
if state := recover(); state != nil { if errRaw := recover(); errRaw != nil {
err := log.Wrap(state.(error)) err := errRaw.(error)
log.PrintErrorf(err, "Program error.") log.PrintError(err)
os.Exit(1)
os.Exit(-2)
} }
}() }()
flag.StringVar(&filepathArg, "filepath", "", "File-path of image") _, err := flags.Parse(arguments)
flag.BoolVar(&printAsJsonArg, "json", false, "Print JSON") if err != nil {
flag.BoolVar(&printLoggingArg, "verbose", false, "Print logging") os.Exit(-1)
flag.StringVar(&thumbnailOutputFilepathArg, "thumbnail-output-filepath", "", "File-path to write a thumbnail to if found.")
flag.Parse()
if filepathArg == "" {
fmt.Printf("Please provide a file-path for an image.\n")
os.Exit(1)
} }
if printLoggingArg == true { if arguments.IsVerbose == true {
cla := log.NewConsoleLogAdapter() cla := log.NewConsoleLogAdapter()
log.AddAdapter("console", cla) log.AddAdapter("console", cla)
@ -87,7 +85,7 @@ func main() {
log.LoadConfiguration(scp) log.LoadConfiguration(scp)
} }
f, err := os.Open(filepathArg) f, err := os.Open(arguments.Filepath)
log.PanicIf(err) log.PanicIf(err)
data, err := ioutil.ReadAll(f) data, err := ioutil.ReadAll(f)
@ -174,7 +172,8 @@ func main() {
mainLogger.Debugf(nil, "EXIF blob is approximately (%d) bytes.", furthestOffset) mainLogger.Debugf(nil, "EXIF blob is approximately (%d) bytes.", furthestOffset)
if thumbnailOutputFilepathArg != "" { thumbnailOutputFilepath := arguments.ThumbnailOutputFilepath
if thumbnailOutputFilepath != "" {
_, index, err := exif.Collect(im, ti, rawExif) _, index, err := exif.Collect(im, ti, rawExif)
log.PanicIf(err) log.PanicIf(err)
@ -189,17 +188,17 @@ func main() {
if thumbnail == nil { if thumbnail == nil {
mainLogger.Debugf(nil, "No thumbnails found.") mainLogger.Debugf(nil, "No thumbnails found.")
} else { } else {
if printAsJsonArg == false { if arguments.PrintAsJson == false {
fmt.Printf("Writing (%d) bytes for thumbnail: [%s]\n", len(thumbnail), thumbnailOutputFilepathArg) fmt.Printf("Writing (%d) bytes for thumbnail: [%s]\n", len(thumbnail), thumbnailOutputFilepath)
fmt.Printf("\n") fmt.Printf("\n")
} }
err := ioutil.WriteFile(thumbnailOutputFilepathArg, thumbnail, 0644) err := ioutil.WriteFile(thumbnailOutputFilepath, thumbnail, 0644)
log.PanicIf(err) log.PanicIf(err)
} }
} }
if printAsJsonArg == true { if arguments.PrintAsJson == true {
data, err := json.MarshalIndent(entries, "", " ") data, err := json.MarshalIndent(entries, "", " ")
log.PanicIf(err) log.PanicIf(err)

View File

@ -8,6 +8,7 @@ replace github.com/dsoprea/go-logging => ../../go-logging
require ( require (
github.com/dsoprea/go-logging v0.0.0-20200517223158-a10564966e9d github.com/dsoprea/go-logging v0.0.0-20200517223158-a10564966e9d
github.com/golang/geo v0.0.0-20200319012246-673a6f80352d github.com/golang/geo v0.0.0-20200319012246-673a6f80352d
github.com/jessevdk/go-flags v1.4.0 // indirect
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 // indirect golang.org/x/net v0.0.0-20200513185701-a91f0712d120 // indirect
gopkg.in/yaml.v2 v2.3.0 gopkg.in/yaml.v2 v2.3.0
) )

View File

@ -12,6 +12,8 @@ github.com/golang/geo v0.0.0-20190916061304-5b978397cfec h1:lJwO/92dFXWeXOZdoGXg
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/geo v0.0.0-20200319012246-673a6f80352d h1:C/hKUcHT483btRbeGkrRjJz+Zbcj8audldIi9tRJDCc= github.com/golang/geo v0.0.0-20200319012246-673a6f80352d h1:C/hKUcHT483btRbeGkrRjJz+Zbcj8audldIi9tRJDCc=
github.com/golang/geo v0.0.0-20200319012246-673a6f80352d/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= github.com/golang/geo v0.0.0-20200319012246-673a6f80352d/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=