mirror of https://github.com/dsoprea/go-exif.git
exif-read-tool/main.go: Use go-flags for command-line processing
parent
d0d0f14dea
commit
e1d21a3716
|
@ -14,7 +14,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
|
@ -22,6 +21,7 @@ import (
|
|||
"io/ioutil"
|
||||
|
||||
"github.com/dsoprea/go-logging"
|
||||
"github.com/jessevdk/go-flags"
|
||||
|
||||
"github.com/dsoprea/go-exif/v2"
|
||||
"github.com/dsoprea/go-exif/v2/common"
|
||||
|
@ -36,13 +36,6 @@ var (
|
|||
mainLogger = log.NewLogger("main.main")
|
||||
)
|
||||
|
||||
var (
|
||||
filepathArg = ""
|
||||
printAsJsonArg = false
|
||||
printLoggingArg = false
|
||||
thumbnailOutputFilepathArg = ""
|
||||
)
|
||||
|
||||
type IfdEntry struct {
|
||||
IfdPath string `json:"ifd_path"`
|
||||
FqIfdPath string `json:"fq_ifd_path"`
|
||||
|
@ -56,28 +49,33 @@ type IfdEntry struct {
|
|||
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() {
|
||||
defer func() {
|
||||
if state := recover(); state != nil {
|
||||
err := log.Wrap(state.(error))
|
||||
log.PrintErrorf(err, "Program error.")
|
||||
os.Exit(1)
|
||||
if errRaw := recover(); errRaw != nil {
|
||||
err := errRaw.(error)
|
||||
log.PrintError(err)
|
||||
|
||||
os.Exit(-2)
|
||||
}
|
||||
}()
|
||||
|
||||
flag.StringVar(&filepathArg, "filepath", "", "File-path of image")
|
||||
flag.BoolVar(&printAsJsonArg, "json", false, "Print JSON")
|
||||
flag.BoolVar(&printLoggingArg, "verbose", false, "Print logging")
|
||||
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)
|
||||
_, err := flags.Parse(arguments)
|
||||
if err != nil {
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if printLoggingArg == true {
|
||||
if arguments.IsVerbose == true {
|
||||
cla := log.NewConsoleLogAdapter()
|
||||
log.AddAdapter("console", cla)
|
||||
|
||||
|
@ -87,7 +85,7 @@ func main() {
|
|||
log.LoadConfiguration(scp)
|
||||
}
|
||||
|
||||
f, err := os.Open(filepathArg)
|
||||
f, err := os.Open(arguments.Filepath)
|
||||
log.PanicIf(err)
|
||||
|
||||
data, err := ioutil.ReadAll(f)
|
||||
|
@ -174,7 +172,8 @@ func main() {
|
|||
|
||||
mainLogger.Debugf(nil, "EXIF blob is approximately (%d) bytes.", furthestOffset)
|
||||
|
||||
if thumbnailOutputFilepathArg != "" {
|
||||
thumbnailOutputFilepath := arguments.ThumbnailOutputFilepath
|
||||
if thumbnailOutputFilepath != "" {
|
||||
_, index, err := exif.Collect(im, ti, rawExif)
|
||||
log.PanicIf(err)
|
||||
|
||||
|
@ -189,17 +188,17 @@ func main() {
|
|||
if thumbnail == nil {
|
||||
mainLogger.Debugf(nil, "No thumbnails found.")
|
||||
} else {
|
||||
if printAsJsonArg == false {
|
||||
fmt.Printf("Writing (%d) bytes for thumbnail: [%s]\n", len(thumbnail), thumbnailOutputFilepathArg)
|
||||
if arguments.PrintAsJson == false {
|
||||
fmt.Printf("Writing (%d) bytes for thumbnail: [%s]\n", len(thumbnail), thumbnailOutputFilepath)
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(thumbnailOutputFilepathArg, thumbnail, 0644)
|
||||
err := ioutil.WriteFile(thumbnailOutputFilepath, thumbnail, 0644)
|
||||
log.PanicIf(err)
|
||||
}
|
||||
}
|
||||
|
||||
if printAsJsonArg == true {
|
||||
if arguments.PrintAsJson == true {
|
||||
data, err := json.MarshalIndent(entries, "", " ")
|
||||
log.PanicIf(err)
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ replace github.com/dsoprea/go-logging => ../../go-logging
|
|||
require (
|
||||
github.com/dsoprea/go-logging v0.0.0-20200517223158-a10564966e9d
|
||||
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
|
||||
gopkg.in/yaml.v2 v2.3.0
|
||||
)
|
||||
|
|
|
@ -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-20200319012246-673a6f80352d h1:C/hKUcHT483btRbeGkrRjJz+Zbcj8audldIi9tRJDCc=
|
||||
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/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=
|
||||
|
|
Loading…
Reference in New Issue