github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/cmdutil/runner.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache-2.0 3 // license that can be found in the LICENSE file. 4 5 package cmdutil 6 7 import ( 8 "sync" 9 10 "github.com/Schaudge/grailbase/pprof" 11 "github.com/Schaudge/grailbase/shutdown" 12 "v.io/x/lib/cmdline" 13 "v.io/x/lib/vlog" 14 ) 15 16 var runnerOnce sync.Once 17 18 // RunnerFunc is an adapter that turns regular functions into cmdline.Runners. 19 type RunnerFunc func(*cmdline.Env, []string) error 20 21 // Run implements the cmdline.Runner interface method by calling f(env, args) 22 // and also ensures that vlog logging is configured and that a flush is done 23 // at the end. 24 func (f RunnerFunc) Run(env *cmdline.Env, args []string) error { 25 runnerOnce.Do(func() { 26 _ = vlog.ConfigureLibraryLoggerFromFlags() 27 pprof.Start() 28 }) 29 err := f(env, args) 30 31 shutdown.Run() 32 vlog.FlushLog() 33 pprof.Write(1) 34 return err 35 }