cmd

package
v0.0.0-...-3857d48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2018 License: BSD-2-Clause Imports: 22 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:          buildtime.PROGNAME + ` <FILE>`,
	Short:        buildtime.PROGNAME + ` CLI`,
	SilenceUsage: true,
	Args:         cobra.ExactArgs(1),
	Long:         buildtime.PROGNAME + ` is an parallel compression utility with throttling`,
	Example: `  $ echo 'Hello World' | smallz -c -9 - > stdout.gz
  $ smallz -dc stdout.gz
  Hello World
  $ echo 'Hello World' | time smallz -i=1B -c -0 -
  $ smallz -dc stdout.gz
  Hello World`,

	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

		{
			// os.Stderr isn't guaranteed to be thread-safe, wrap in a sync writer.
			// Files are guaranteed to be safe, terminals are not.
			var logWriter io.Writer
			if isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()) {
				logWriter = zerolog.SyncWriter(os.Stderr)
			} else {
				logWriter = os.Stderr
			}

			logFmt, err := config.LogLevelParse(viper.GetString(config.KeyAgentLogFormat))
			if err != nil {
				return errors.Wrap(err, "unable to parse log format")
			}

			if logFmt == config.LogFormatAuto {
				if isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()) {
					logFmt = config.LogFormatHuman
				} else {
					logFmt = config.LogFormatZerolog
				}
			}

			var zlog zerolog.Logger
			switch logFmt {
			case config.LogFormatZerolog:
				zlog = zerolog.New(logWriter).With().Timestamp().Logger()
			case config.LogFormatHuman:
				useColor := viper.GetBool(config.KeyAgentUseColor)
				w := zerolog.ConsoleWriter{
					Out:     logWriter,
					NoColor: !useColor,
				}
				zlog = zerolog.New(w).With().Timestamp().Logger()
			default:
				return fmt.Errorf("unsupported log format: %q")
			}

			log.Logger = zlog

			stdlog.SetFlags(0)
			stdlog.SetOutput(zlog)
		}

		switch logLevel := strings.ToUpper(viper.GetString(config.KeyLogLevel)); logLevel {
		case "DEBUG":
			zerolog.SetGlobalLevel(zerolog.DebugLevel)
		case "INFO":
			zerolog.SetGlobalLevel(zerolog.InfoLevel)
		case "WARN":
			zerolog.SetGlobalLevel(zerolog.WarnLevel)
		case "ERROR":
			zerolog.SetGlobalLevel(zerolog.ErrorLevel)
		case "FATAL":
			zerolog.SetGlobalLevel(zerolog.FatalLevel)
		default:

			return fmt.Errorf("unsupported error level: %q (supported levels: %s)", logLevel,
				strings.Join([]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}, " "))
		}

		go func() {
			if !viper.GetBool(config.KeyGoogleAgentEnable) {
				log.Debug().Msg("gops(1) agent disabled by request")
				return
			}

			log.Debug().Msg("starting gops(1) agent")
			if err := agent.Listen(&agent.Options{}); err != nil {
				log.Fatal().Err(err).Msg("unable to start the gops(1) agent thread")
			}
		}()

		go func() {
			if !viper.GetBool(config.KeyPProfEnable) {
				log.Debug().Msg("pprof endpoint disabled by request")
				return
			}

			pprofPort := viper.GetInt(config.KeyPProfPort)
			log.Debug().Int("pprof-port", pprofPort).Msg("starting pprof endpoing agent")
			if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", pprofPort), nil); err != nil {
				log.Fatal().Err(err).Msg("unable to start the pprof listener")
			}
		}()

		return nil
	},

	RunE: func(cmd *cobra.Command, args []string) error {
		switch {
		case viper.IsSet("decompress") && viper.GetBool("decompress"):
			return decompress(cmd, args)
		case viper.GetBool("compress"):
			return compress(cmd, args)
		default:
			return fmt.Errorf("compress or decompress must be specified")
		}
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL