GithubHelp home page GithubHelp logo

xsam / go-hybrid Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 2.0 138 KB

libs and tools for Golang

License: BSD 3-Clause "New" or "Revised" License

Makefile 1.82% Go 98.18%
golang utility library cli hybrid cmdline

go-hybrid's Introduction

go-hybrid

test & build 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
make
./bin/example

Packages

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

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 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.

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.

Providing UserHomeDir, Recovery and WrappedGo

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

go-hybrid's People

Contributors

xsam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

deamwork isgasho

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.