Don't repeat yourself, every piece of knowledge must have a single, unambiguous, authoritative representation within a system. |
---|
jiusanzhou / x Goto Github PK
View Code? Open in Web Editor NEW:rocket: Utility library for Go
License: Other
:rocket: Utility library for Go
License: Other
We expect to do:
example -y
but we need to do:
example -y true
cleanupFuncs := make([]func(), 0)
defer func() {
// no error, no need to cleanup
if retErr == nil || isContextError(retErr) {
return
}
for i := len(cleanupFuncs) - 1; i >= 0; i-- {
cleanupFuncs[i]()
}
}()
WithHomeDir function return wrong path
v1.1.0-alpha.0.6+84c76d1142ea4d
auto increase Z
Proposal:
X.Y
Z
Z
alpha
or beta
ignore those steps.type Main struct {
CmdA
}
// Run at the Main command register the run
func (m Main) Run() error {
}
I can’t remember anything about the issue about
use string for time.Duration
cli.Command
Command.RunWithErr(func() error {
return nil
})
// if err is not nil, auto print error msg
Proposal:
OneOf()
// only do while we has defined a config command
if len(cfos) == 0 {
return
}
// parse flags while onchanged, before call custom onchanged
WithConfigChanged(func(o, n interface{}) {
c.ParseFlags(os.Args)
})(cfopts)
cmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
cmd.Flags().BoolP("version", "v", false, "version for "+cmd.Name())
Components:
Inprised:
Marshal
var data, err = x.Marshal(object, MarshalJSON) // must affter a type
// or
var data, err = x.Marshal(object).JSON()
Unmarshal
var err = x.Unmarshal(data, &object, func(data []byte) MarshalType {
return x.MarshalJSON
})
// default offer MarshalType getter function
// from []byte, from filename
// x.MarshalTypeFromFileName("")
If we dont add opts: "-"
to the complex struct on config, we will get error in installing flags, but without any output.
So, we have 2 methods to improve this:
At most time as I coding, I need to load config from Kubernetes ConfigMap
We need to re-call code bellow after call c.Option:
// if c.config is not nil we can load value
// from parent or root
PreRun(func(cmd *Command, args ...string) {
// if we are not a root cmd, and set the config and with root config
// we try to load config
// but what about the ...
if !cmd.IsRoot() && cmd.configv != nil && cmd.root.configobj != nil {
v, ok := cmd.root.configobj.Get(c.Name())
if ok {
// FIXME:
b, err := json.Marshal(v)
// ignore error
if err != nil {
return
}
json.Unmarshal(b, cmd.configv)
// re parse flag
cmd.ParseFlags(os.Args)
}
}
})(c)
// we must do it at init time.
// load set flag
c.initFlags()
_cli_ config new -f toml
add enum type of options args and auto generate help information.
Example:
--layout The output format (list, table, or grid)
stat
like mertics
?
While we use opts package, we want some fields or all of them can load from a configuration file.
Registe configuration file loader while exits filed Config
:
type SomeOptions struct {
Config string `opts:"config,short=c"`
}
Configuration file loader
Must offer a multi extesion supported configuration file loader:
.yaml
, .json
m .toml
Unmarshal to Options which can be over by command line arguments.
Auto detect Go prog version from:
Parse value from env for option:
type Config {
Port String `opts:"env[=MY_PORT]"`
}
Proposal:
.env
file.env
parent directoryComponents:
Projects:
Proposal:
go run ./version/gen -v
Just print the version tag out like v1.0.0
[ERROR] parse opts error: field 'Config' slices cannot have default values
opts
in tag.env
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.