Giter VIP home page Giter VIP logo

x's Introduction

Utility library for Go

go.zoe.im/x

Build Status Go Report Card codecov GoDoc On Sourcegraph PRs Welcome

Don't repeat yourself, every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

x's People

Contributors

jiusanzhou avatar renovate-bot avatar

Stargazers

 avatar

Watchers

 avatar  avatar

x's Issues

feature: add clean function register for grance run

	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]()
		}
	}()

version: gen semver version Z

v1.1.0-alpha.0.6+84c76d1142ea4d

auto increase Z

Proposal:

  • we must set tag the base X.Y
  • parse current Z
  • we auto increase the version Z
  • after release we need to tag the source code with the new version
  • or we found the code status clear, auto tag the code
  • if we get alpha or beta ignore those steps.

cli.Run handle with error

  • x.cli: add method for cli.Command
Command.RunWithErr(func() error {
  return nil
})

// if err is not nil, auto print error msg

cli: auto load config wather not correct

// 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)

cli: version flags simple

cmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
cmd.Flags().BoolP("version", "v", false, "version for "+cmd.Name())

Add Marshal and Unmarshal common methods

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("")

opts: improve the config load flag error

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:

  1. log out the error
  2. implement the nested struct flags parser

cli: c.Option can not parse flags from config and load config auto.

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: configruation file auto load

While we use opts package, we want some fields or all of them can load from a configuration file.

Proposal

  • 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:

    • auto handle with extension like: .yaml, .jsonm .toml
    • unmarshal to optisons
  • Unmarshal to Options which can be over by command line arguments.

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.