github.com/anchore/syft@v1.38.2/cmd/syft/internal/clio_setup_config.go (about)

     1  package internal
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/anchore/clio"
     8  	"github.com/anchore/stereoscope"
     9  	handler "github.com/anchore/syft/cmd/syft/cli/ui"
    10  	"github.com/anchore/syft/cmd/syft/internal/ui"
    11  	"github.com/anchore/syft/internal/bus"
    12  	"github.com/anchore/syft/internal/log"
    13  	"github.com/anchore/syft/internal/redact"
    14  )
    15  
    16  func AppClioSetupConfig(id clio.Identification, out io.Writer) *clio.SetupConfig {
    17  	clioCfg := clio.NewSetupConfig(id).
    18  		WithGlobalConfigFlag().   // add persistent -c <path> for reading an application config from
    19  		WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config
    20  		WithConfigInRootHelp().   // --help on the root command renders the full application config in the help text
    21  		WithUIConstructor(
    22  			// select a UI based on the logging configuration and state of stdin (if stdin is a tty)
    23  			func(cfg clio.Config) (*clio.UICollection, error) {
    24  				noUI := ui.None(out, cfg.Log.Quiet)
    25  				if !cfg.Log.AllowUI(os.Stdin) || cfg.Log.Quiet {
    26  					return clio.NewUICollection(noUI), nil
    27  				}
    28  
    29  				return clio.NewUICollection(
    30  					ui.New(out, cfg.Log.Quiet,
    31  						handler.New(handler.DefaultHandlerConfig()),
    32  					),
    33  					noUI,
    34  				), nil
    35  			},
    36  		).
    37  		WithInitializers(
    38  			func(state *clio.State) error {
    39  				// clio is setting up and providing the bus, redact store, and logger to the application. Once loaded,
    40  				// we can hoist them into the internal packages for global use.
    41  				stereoscope.SetBus(state.Bus)
    42  				bus.Set(state.Bus)
    43  
    44  				redact.Set(state.RedactStore)
    45  
    46  				log.Set(state.Logger)
    47  				stereoscope.SetLogger(state.Logger.Nested("from", "stereoscope"))
    48  				return nil
    49  			},
    50  		).
    51  		WithPostRuns(func(_ *clio.State, _ error) {
    52  			stereoscope.Cleanup()
    53  		})
    54  	return clioCfg
    55  }