golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/driver/options.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package driver 16 17 import ( 18 "bufio" 19 "flag" 20 "fmt" 21 "io" 22 "os" 23 "strings" 24 25 "github.com/google/pprof/internal/binutils" 26 "github.com/google/pprof/internal/plugin" 27 "github.com/google/pprof/internal/symbolizer" 28 ) 29 30 // setDefaults returns a new plugin.Options with zero fields sets to 31 // sensible defaults. 32 func setDefaults(o *plugin.Options) *plugin.Options { 33 d := &plugin.Options{} 34 if o != nil { 35 *d = *o 36 } 37 if d.Writer == nil { 38 d.Writer = oswriter{} 39 } 40 if d.Flagset == nil { 41 d.Flagset = goFlags{} 42 } 43 if d.Obj == nil { 44 d.Obj = &binutils.Binutils{} 45 } 46 if d.UI == nil { 47 d.UI = &stdUI{r: bufio.NewReader(os.Stdin)} 48 } 49 if d.Sym == nil { 50 d.Sym = &symbolizer.Symbolizer{Obj: d.Obj, UI: d.UI} 51 } 52 return d 53 } 54 55 // goFlags returns a flagset implementation based on the standard flag 56 // package from the Go distribution. It implements the plugin.FlagSet 57 // interface. 58 type goFlags struct{} 59 60 func (goFlags) Bool(o string, d bool, c string) *bool { 61 return flag.Bool(o, d, c) 62 } 63 64 func (goFlags) Int(o string, d int, c string) *int { 65 return flag.Int(o, d, c) 66 } 67 68 func (goFlags) Float64(o string, d float64, c string) *float64 { 69 return flag.Float64(o, d, c) 70 } 71 72 func (goFlags) String(o, d, c string) *string { 73 return flag.String(o, d, c) 74 } 75 76 func (goFlags) BoolVar(b *bool, o string, d bool, c string) { 77 flag.BoolVar(b, o, d, c) 78 } 79 80 func (goFlags) IntVar(i *int, o string, d int, c string) { 81 flag.IntVar(i, o, d, c) 82 } 83 84 func (goFlags) Float64Var(f *float64, o string, d float64, c string) { 85 flag.Float64Var(f, o, d, c) 86 } 87 88 func (goFlags) StringVar(s *string, o, d, c string) { 89 flag.StringVar(s, o, d, c) 90 } 91 92 func (goFlags) StringList(o, d, c string) *[]*string { 93 return &[]*string{flag.String(o, d, c)} 94 } 95 96 func (goFlags) ExtraUsage() string { 97 return "" 98 } 99 100 func (goFlags) Parse(usage func()) []string { 101 flag.Usage = usage 102 flag.Parse() 103 args := flag.Args() 104 if len(args) == 0 { 105 usage() 106 } 107 return args 108 } 109 110 type stdUI struct { 111 r *bufio.Reader 112 } 113 114 func (ui *stdUI) ReadLine(prompt string) (string, error) { 115 os.Stdout.WriteString(prompt) 116 return ui.r.ReadString('\n') 117 } 118 119 func (ui *stdUI) Print(args ...interface{}) { 120 ui.fprint(os.Stderr, args) 121 } 122 123 func (ui *stdUI) PrintErr(args ...interface{}) { 124 ui.fprint(os.Stderr, args) 125 } 126 127 func (ui *stdUI) IsTerminal() bool { 128 return false 129 } 130 131 func (ui *stdUI) SetAutoComplete(func(string) string) { 132 } 133 134 func (ui *stdUI) fprint(f *os.File, args []interface{}) { 135 text := fmt.Sprint(args...) 136 if !strings.HasSuffix(text, "\n") { 137 text += "\n" 138 } 139 f.WriteString(text) 140 } 141 142 // oswriter implements the Writer interface using a regular file. 143 type oswriter struct{} 144 145 func (oswriter) Open(name string) (io.WriteCloser, error) { 146 f, err := os.Create(name) 147 return f, err 148 }