{"id":15704059,"url":"https://github.com/xsam/go-hybrid","last_synced_at":"2025-05-12T16:34:18.354Z","repository":{"id":46781926,"uuid":"247235767","full_name":"XSAM/go-hybrid","owner":"XSAM","description":"libs and tools for Golang","archived":false,"fork":false,"pushed_at":"2023-05-05T02:31:38.000Z","size":138,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T00:38:46.913Z","etag":null,"topics":["cli","cmdline","golang","hybrid","library","utility"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XSAM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-14T07:59:45.000Z","updated_at":"2024-09-07T01:30:07.000Z","dependencies_parsed_at":"2024-06-20T04:22:23.303Z","dependency_job_id":"25e8dcff-ecd0-413b-bec3-b4efd0d9f966","html_url":"https://github.com/XSAM/go-hybrid","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fgo-hybrid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fgo-hybrid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fgo-hybrid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fgo-hybrid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XSAM","download_url":"https://codeload.github.com/XSAM/go-hybrid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253777477,"owners_count":21962694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","cmdline","golang","hybrid","library","utility"],"created_at":"2024-10-03T20:10:09.293Z","updated_at":"2025-05-12T16:34:18.332Z","avatar_url":"https://github.com/XSAM.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-hybrid\n\n![test \u0026 build](https://github.com/XSAM/go-hybrid/workflows/test%20\u0026%20build/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/XSAM/go-hybrid/badge.svg?branch=master)](https://coveralls.io/github/XSAM/go-hybrid?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/XSAM/go-hybrid)](https://goreportcard.com/report/github.com/XSAM/go-hybrid)\n[![Documentation](https://godoc.org/github.com/XSAM/go-hybrid?status.svg)](https://pkg.go.dev/mod/github.com/XSAM/go-hybrid)\n\nThis repository provides common utility packages for Golang. Such as `log`, `error` and `cmdutil` lib.\n\n- [go-hybrid](#go-hybrid)\n- [Installation](#installation)\n- [Example](#example)\n- [Packages](#packages)\n  - [cmdutil](#cmdutil)\n    - [Flag rules and variables](#flag-rules-and-variables)\n  - [errorw](#errorw)\n  - [log](#log)\n  - [metadata](#metadata)\n  - [builtinutil](#builtinutil)\n\n# Installation\n\n```bash\ngo get github.com/XSAM/go-hybrid\n```\n\n# [Example](_example/)\n\n```\nmake\n./bin/example\n```\n\n# Packages\n\n## [cmdutil](https://pkg.go.dev/github.com/XSAM/go-hybrid/cmdutil)\n\n`cmdutil` offer `ResolveFlagVariable` to register a struct field into [cobra](https://github.com/spf13/cobra) as a flag.\n\nFor example:\n\n```golang\ntype Flag struct {\n\tNumber      int           `flag:\"env\"`\n\tDuration    time.Duration `flag-usage:\"change the duration\"`\n}\n\nvar flag Flag\nvar cmd cobra.Command\n\ncmdutil.ResolveFlagVariable(\u0026cmd, \u0026flag)\n```\n\n`ResolveFlagVariable` will resolve struct through the struct tag. So there is no need to invoke `cmd.PersistentFlags()`, `ResolveFlagVariable` already do that for you.\n\nFurthermore, `ResolveFlagVariable` extends the ability of `cobra`, it can automatically read the environment variable if you like. Simply add `env` to the `flag:\"\"`.\n\nThe priority of the value assignment is:\n\n`flag parameter \u003e environment variable \u003e default value`\n\nFor 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.\n\n### Flag rules and variables\n\nAdd `flag:\"\"` or `flag-usage:\"\"` to the struct tag and let `cmdutil` know that you want to resolve this variable.\n\nUse `flag-usage` to add usage for a flag.\n\n| key  | example             | description                                      |   |\n|------|---------------------|--------------------------------------------------|---|\n| env  | `env`, `env=true`   | read environment variable.                       |   |\n| name | `name=foo`          | not like generated name? use it to overwrite it. |   |\n| flat | `flat`, `flat=true` | ignore prefix name.                               |   |\n\nCurrently supported type: `bool`, `string`, `int`, `int32`, `int64`, `time.Duration`, `[]int`, `[]time.Duration`, `[]string`, `[]bool`, `map[string]string`, `map[string]int`\n\n## [errorw](https://pkg.go.dev/github.com/XSAM/go-hybrid/errorw)\n\n`errrow` is an error type which wraps fields and callstack. It implements `stackTracer`, `causer`, `GRPCStatus` and `zapcore.ObjectMarshaler` interface. \n\nAlso, it uses gRPC status as an API error. So it can be directly returned as a gRPC error.\n\n[grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway/blob/554b3dac4972c2957a8bc8e8ba15a241a6352b93/runtime/errors.go#L16) 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.\n\n## [log](https://pkg.go.dev/github.com/XSAM/go-hybrid/log)\n\n`log` wraps [zap](go.uber.org/zap) as logger. It mainly provides `BgLogger()` and `Logger(ctx context.Context)` to access a concrete logger.\n\nAnd, 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](environment/service.go) for more details.\n\n## [metadata](https://pkg.go.dev/github.com/XSAM/go-hybrid/metadata)\n\nYou 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`.\n\nAlso, you can use `cmdutil.Version` to add `version` command to `cobra`.\n\nYou can check out [Makefile](./Makefile) and learn how to inject these variables.\n\n## [builtinutil](https://pkg.go.dev/github.com/XSAM/go-hybrid/builtinutil)\n\nProviding `UserHomeDir`, `Recovery` and `WrappedGo`\n\n`WrappedGo` wraps a goroutine with a recovery. So you will not worry about forget to recover a goroutine.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxsam%2Fgo-hybrid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxsam%2Fgo-hybrid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxsam%2Fgo-hybrid/lists"}