github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/debug.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "fmt" 22 "os" 23 "strings" 24 25 "camlistore.org/pkg/cmdmain" 26 ) 27 28 var debugSubModes = map[string]*debugSubMode{ 29 "splits": &debugSubMode{ 30 doc: "Show splits of provided file.", 31 fun: showSplits, 32 }, 33 "mime": &debugSubMode{ 34 doc: "Show MIME type of provided file.", 35 fun: showMIME, 36 }, 37 "exif": &debugSubMode{ 38 doc: "Show EXIF dump of provided file.", 39 fun: showEXIF, 40 }, 41 } 42 43 type debugSubMode struct { 44 doc string 45 fun func(string) 46 } 47 48 type debugCmd struct{} 49 50 func init() { 51 cmdmain.RegisterCommand("debug", func(flags *flag.FlagSet) cmdmain.CommandRunner { 52 return new(debugCmd) 53 }) 54 } 55 56 func (c *debugCmd) Describe() string { 57 return "Show misc meta-info from the given file." 58 } 59 60 func (c *debugCmd) Usage() { 61 var subModes, docs string 62 for k, v := range debugSubModes { 63 subModes += k + "|" 64 docs += fmt.Sprintf(" %s: %s\n", k, v.doc) 65 } 66 subModes = strings.TrimRight(subModes, "|") 67 fmt.Fprintf(os.Stderr, 68 "Usage: camtool [globalopts] debug %s file\n%s", 69 subModes, docs) 70 } 71 72 func (c *debugCmd) RunCommand(args []string) error { 73 if args == nil || len(args) != 2 { 74 return cmdmain.UsageError("Incorrect number of arguments.") 75 } 76 subMode, ok := debugSubModes[args[0]] 77 if !ok { 78 return cmdmain.UsageError(fmt.Sprintf("Invalid submode: %v", args[0])) 79 } 80 subMode.fun(args[1]) 81 return nil 82 }