version

package
v0.82.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:     "version",
	Short:   "Generate version file",
	Aliases: []string{"v"},
	Args:    cobra.NoArgs,
	Run: func(c *cobra.Command, args []string) {
		r, err := git.PlainOpen(".")
		if err != nil {
			panic(err)
		}
		head, err := r.Head()
		if err != nil {
			panic(err)
		}
		var gitTreeState string
		if FlagSetGitClean && FlagSetGitDirty {
			log.Fatalln("can't use --set-git-clean and --set-git-dirty together")
		}
		if FlagSetGitClean {
			gitTreeState = "clean"
		}
		if FlagSetGitDirty {
			gitTreeState = "dirty"
		}
		if gitTreeState == "" {
			w, err := r.Worktree()
			if err != nil {
				panic(err)
			}
			s, err := w.Status()
			if err != nil {
				panic(err)
			}
			if s.IsClean() {
				gitTreeState = "clean"
			} else {
				gitTreeState = "dirty"
			}
		}
		gitRef := head.Name().Short()
		if FlagSetGitRef != "" {
			gitRef = FlagSetGitRef
		}
		t := time.Now()
		var data []byte
		extra := map[string]string{}
		if len(FlagExtra) != 0 {
			for _, e := range FlagExtra {
				e2 := strings.SplitN(e, "=", 2)
				if len(e2) != 2 {
					log.Fatalf("extra argument must be \"key=val\" not \"%s\"\n", e)
				}
				extra[e2[0]] = e2[1]
			}
		}
		v := VersionJSON{
			GitRef:                 gitRef,
			GitCommit:              head.Hash().String(),
			GitTreeState:           gitTreeState,
			BuildTimestampUnix:     int(t.Unix()),
			BuildTimestampRFC3339:  t.Format(time.RFC3339),
			BuildTimestampUnixDate: t.Format(time.UnixDate),
			Extra:                  extra,
		}
		if FlagPretty {
			data, err = json.MarshalIndent(v, "", "  ")
		} else {
			data, err = json.Marshal(v)
		}
		if err != nil {
			panic(err)
		}
		fmt.Println(string(data))
	},
}
View Source
var FlagExtra []string
View Source
var FlagPretty bool
View Source
var FlagSetGitClean bool
View Source
var FlagSetGitDirty bool
View Source
var FlagSetGitRef string

Functions

This section is empty.

Types

type VersionJSON

type VersionJSON struct {
	GitRef                 string            `json:"git_ref"`
	GitCommit              string            `json:"git_commit"`
	GitTreeState           string            `json:"git_tree_state"`
	BuildTimestampUnix     int               `json:"build_timestamp_unix"`
	BuildTimestampRFC3339  string            `json:"build_timestamp_rfc3339"`
	BuildTimestampUnixDate string            `json:"build_timestamp_unixdate"`
	Extra                  map[string]string `json:"extra"`
}

Jump to

Keyboard shortcuts

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