go-hybrid

module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: BSD-3-Clause

README

go-hybrid

Coverage Status Go Report Card Documentation

This repository provides common utility packages for Golang. Such as log, error and cmdutil lib.

Installation

go get github.com/XSAM/go-hybrid

Example

make
./bin/example

Packages

cmdutil

cmdutil offer ResolveFlagVariable to register a struct field into cobra as a flag.

For example:

type Flag struct {
	Number      int           `flag:"env"`
	Duration    time.Duration `flag-usage:"change the duration"`
}

var flag Flag
var cmd cobra.Command

cmdutil.ResolveFlagVariable(&cmd, &flag)

ResolveFlagVariable will resolve struct through the struct tag. So there is no need to invoke cmd.PersistentFlags(), ResolveFlagVariable already do that for you.

Furthermore, ResolveFlagVariable extends the ability of cobra, it can automatically read the environment variable if you like. Simply add env to the flag:"".

The priority of the value assignment is:

flag parameter > environment variable > default value

For instance, setting a variable though a flag parameter and an environment variable at the same time, the variable value will be the flag parameter value.

Flag rules and variables

Add flag:"" or flag-usage:"" to the struct tag and let cmdutil know that you want to resolve this variable.

Use flag-usage to add usage for a flag.

key example description
env env, env=true read environment variable.
name name=foo not like generated name? use it to overwrite it.
flat flat, flat=true ignore prefix name.

Currently supported type: bool, string, int, int32, int64, time.Duration, []int, []time.Duration, []string, []bool, map[string]string, map[string]int

errorw

errrow is an error type which wraps fields and callstack. It implements stackTracer, causer, GRPCStatus and zapcore.ObjectMarshaler interface.

Also, it uses gRPC status as an API error. So it can be directly returned as a gRPC error.

grpc-gateway provides an approach that converting a gRPC error code into the corresponding HTTP response status. Therefore, you can use it even you want HTTP status codes.

log

log wraps zap as logger. It mainly provides BgLogger() and Logger(ctx context.Context) to access a concrete logger.

And, it provides customized preset config to control the log output style, such as JSON and Text style. You can use environment package to switch it. Check this file for more details.

metadata

You can inject some const variables relevant to the program itself, such as gitVersion, gitCommit, gitBranch and buildTime. Then you can fetch these variables from metadata.AppInfo.

Also, you can use cmdutil.Version to add version command to cobra.

You can check out Makefile and learn how to inject these variables.

builtinutil

Providing UserHomeDir, Recovery and WrappedGo

WrappedGo wraps a goroutine with a recovery. So you will not worry about forget to recover a goroutine.

Directories

Path Synopsis
log

Jump to

Keyboard shortcuts

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