ver

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TemplateFileFieldPrefix = "{$"
	TemplateFileFieldSuffix = "$}"
)
View Source
const (
	PackageJsonFilename = "package.json"
)
View Source
const (
	VersionInfoYamlFilename = "version-info.yml"
)

Variables

View Source
var AllInputs = []InputSpec{
	InputSpec{
		Name:        "version-info",
		Description: fmt.Sprintf("Read project and version information from an existing %v file in the current or parent directories.", VersionInfoYamlFilename),
		Parameters: []string{
			"depth:{levels}\tLimit depth of directories to scan. Set to 0 for current directory, only. Default is 10.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			maxDepth := 10

			if params["depth"] != "" {
				md, err := strconv.Atoi(params["depth"])

				if err != nil {
					log.Errorf("Failed to parse 'maxDepth' parameter: %v", params["depth"])
					return err
				}

				maxDepth = md
			}

			ver, err := TryReadFromVersionInfoYAML(maxDepth)

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "directory-name",
		Description: "Use the working directory's name as project name.",
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromWorkingDirectory()

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "build-host",
		Description: "Use the current machine's name and time as build host and timestamp.",
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromBuildHost()

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "consul-build-id",
		Description: "Retrieves a build number based on the build revision. Use this for non-numeric revisions, like Git.",
		Parameters: []string{
			"url:{consulurl}\tThe connection URL to consul, e.g. http://consul:8500/dc1 or http://:token@consul:8500/dc1.",
			"root:{key}\tThe base KV path for the project, e.g. builds/myproject.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {
			return RetrieveBuildFromConsul(params["url"], params["root"], vi)
		},
	},
	InputSpec{
		Name:        "npm",
		Description: fmt.Sprintf("Read project and version information from an existing %v file in the current directory.", PackageJsonFilename),
		Parameters: []string{
			"name:false\tIgnore the project name.",
			"desc:false\tIgnore the project description.",
			"license:false\tIgnore the project license information.",
			"version:false\tIgnore the project version number.",
			"author:false\tIgnore the project author information.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromPackageJSON(
				params["name"] == "false",
				params["version"] == "false",
				params["desc"] == "false",
				params["author"] == "false",
				params["license"] == "false",
			)

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "konsorten",
		Description: "Use marvin + konsorten default author information.",
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromKonsortenDefaults()

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "teamcity",
		Description: "Read project name, version, and revision information from TeamCity environment variables.",
		Parameters: []string{
			"build:false\tIgnore the build number.",
			"rev:false\tIgnore the revision number.",
			"name:false\tIgnore the project name.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromTeamCity(
				params["build"] == "false",
				params["rev"] == "false",
				params["name"] == "false",
			)

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "gitlab-ci",
		Description: "Read project name and revision information from GitLab CI environment variables.",
		Parameters: []string{
			"rev:false\tIgnore the revision number.",
			"name:false\tIgnore the project name.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromGitlabCI(
				params["rev"] == "false",
				params["name"] == "false",
			)

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "git",
		Description: "Read revision information from current directory's Git repository.",
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromGit()

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "env-vars",
		Description: "Read build information from environment variables.",
		Parameters: []string{
			"name:{envVarName}\tRead the project name from the specified environment variable {envVarName}. Ignored if missing or empty.",
			"desc:{envVarName}\tRead the project description from the specified environment variable {envVarName}. Ignored if missing or empty.",
			"rev:{envVarName}\tRead the build revision from the specified environment variable {envVarName}. Ignored if missing or empty.",
			"build:{envVarName}\tRead the build ID from the specified environment variable {envVarName}. Ignored if missing or empty.",
			"buildHost:{envVarName}\tRead the build host name from the specified environment variable {envVarName}. Ignored if missing or empty.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			ver, err := TryReadFromEnvironmentVariables(params)

			if err != nil {
				return err
			}

			vi.CopyMissingFrom(ver)

			return nil
		},
	},
	InputSpec{
		Name:        "limit-revision",
		Description: "Limits the length of the revision string.",
		Parameters: []string{
			"length:{int}\tThe number of characters to limit the revision string, e.g. 7 for Git short revision.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {
			l, err := strconv.Atoi(params["length"])

			if err != nil {
				return err
			}

			vi.LimitRevision(l)

			return nil
		},
	},
}
View Source
var AllOutputs = []OutputSpec{
	OutputSpec{
		Name:        "template",
		Description: "Renders a template file and writes the result into a file dropping the last extension, e.g. myfile.c.template becomes myfile.c. Takes the relative file path as parameter.",
		Parameters: []string{
			"file:{path}\tPath to the template file.",
			"mode:{filemode}\tFile mode to use for the file (Linux and macOS). Typical values are 644 for a regular file and 755 for an executable file.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {

			f := params["file"]

			if f == "" {
				return fmt.Errorf("No template file specified; missing 'file' parameter")
			}

			// parse the file mode
			var fm os.FileMode = 0644

			fmStr := params["mode"]

			if fmStr != "" {
				fmParsed, err := strconv.ParseUint(fmStr, 8, 32)

				if err != nil {
					return fmt.Errorf("Failed to parse file mode: %v: %v", fmStr, err)
				}

				fm = os.FileMode(fmParsed)
			}

			return vi.WriteTemplateFile(f, fm)
		},
	},
	OutputSpec{
		Name:        "update-npm",
		Description: fmt.Sprintf("Updates an existing NPM %v file in the current directory.", PackageJsonFilename),
		Action: func(vi *VersionInformation, params map[string]string) error {
			return UpdatePackageJSON(vi)
		},
	},
	OutputSpec{
		Name:        "update-json",
		Description: "Updates an existing JSON document.",
		Parameters: []string{
			"file:{path}\tPath to the JSON file.",
			"indent:{chars}\tCharacters to use for indent, like four space characters.",
			"!{path}:{value}\tSet the value at {path} to {value}. All template file fields are supported. Strings have to be quoted, e.g. '\"{$.Author$}\"' or '{$.Author | asQuoted$}'. Missing objects are created automatically.",
			"!{path}:$null$\tSets the value at {path} to null.",
			"!{path}:$delete$\tDeletes the value at {path}.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {
			f := params["file"]

			if f == "" {
				return fmt.Errorf("No JSON file specified; missing 'file' parameter")
			}

			actions := make(UpdateActions)

			for k, v := range params {
				if strings.HasPrefix(k, "!") {
					actions[k[1:]] = v
				}
			}

			return UpdateJsonFile(f, actions, vi, params["indent"])
		},
	},
	OutputSpec{
		Name:        "update-xml",
		Description: "Updates an existing XML document.",
		Parameters: []string{
			"file:{path}\tPath to the XML file.",
			"indent:{chars}\tCharacters to use for indent, like four space characters.",
			"!{xpath}:{value}\tSet the value at {xpath} to {value}. The target element/attribute must exist. All template file fields are supported.",
			"!{xpath}:$create$\tCreates a new element or attribute from {xpath}. The parent element must exist.",
			"!{xpath}:$ensure$\tIf missing, creates a new element or attribute from {xpath}. The parent element must exist.",
			"!{xpath}:$delete$\tDeletes the value at {xpath}.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {
			f := params["file"]

			if f == "" {
				return fmt.Errorf("No XML file specified; missing 'file' parameter")
			}

			actions := make(UpdateActions)

			for k, v := range params {
				if strings.HasPrefix(k, "!") {
					actions[k[1:]] = v
				}
			}

			return UpdateXmlFile(f, actions, vi, params["indent"])
		},
	},
	OutputSpec{
		Name:        "update-regex",
		Description: "Updates an existing text document.",
		Parameters: []string{
			"file:{path}\tPath to the text file.",
			"posix:true\tRestricts the regular expression to POSIX ERE (egrep) syntax.",
			"!{regex}:{replace}\tReplaces all matches of {regex} with {replace}. All template file fields are supported.",
		},
		Action: func(vi *VersionInformation, params map[string]string) error {
			f := params["file"]

			if f == "" {
				return fmt.Errorf("No text file specified; missing 'file' parameter")
			}

			actions := make(UpdateActions)

			for k, v := range params {
				if strings.HasPrefix(k, "!") {
					actions[k[1:]] = v
				}
			}

			return UpdateRegexFile(f, actions, params["posix"] == "true", vi)
		},
	},
	OutputSpec{
		Name:        "go-syso",
		Description: fmt.Sprintf("Writes the version information to be embedded into an Go executable (%v). Supported on Windows, only.", goSysoFilename),
		Action: func(vi *VersionInformation, params map[string]string) error {
			return WriteGoSysoFile(vi)
		},
	},
	OutputSpec{
		Name:        "teamcity",
		Description: "Writes the version number back to TeamCity.",
		Action: func(vi *VersionInformation, params map[string]string) error {
			return vi.WriteToTeamCity()
		},
	},
}

Functions

func GetTemplateFileFunctionsMap

func GetTemplateFileFunctionsMap() template.FuncMap

func IsGitlabCIAvailable

func IsGitlabCIAvailable() bool

func IsTeamCityAvailable

func IsTeamCityAvailable() bool

func RenderTemplate

func RenderTemplate(templateContent string, templateName string, vi *VersionInformation) (string, error)

func RetrieveBuildFromConsul

func RetrieveBuildFromConsul(consulUrl string, kvProjectRoot string, vi *VersionInformation) error

func RunCurrentDirectory

func RunCurrentDirectory() error

func UpdateJsonFile

func UpdateJsonFile(filePath string, updates UpdateActions, vi *VersionInformation, indent string) error

func UpdatePackageJSON

func UpdatePackageJSON(vi *VersionInformation) error

func UpdateRegexFile

func UpdateRegexFile(filename string, matchMap UpdateActions, usePosix bool, vi *VersionInformation) error

func UpdateXmlFile

func UpdateXmlFile(filePath string, updates UpdateActions, vi *VersionInformation, indent string) error

func WriteGoSysoFile

func WriteGoSysoFile(vi *VersionInformation) error

Types

type InputAction

type InputAction = func(vi *VersionInformation, params map[string]string) error

type InputSpec

type InputSpec struct {
	Name        string
	Description string
	Parameters  []string
	Action      InputAction
}

func GetInputSpec

func GetInputSpec(name string) *InputSpec

type ItemDocumentation

type ItemDocumentation []ItemDocumentationEntry

func GetTemplateFileFields

func GetTemplateFileFields() ItemDocumentation

func GetTemplateFileFunctions

func GetTemplateFileFunctions() ItemDocumentation

func (ItemDocumentation) Len

func (s ItemDocumentation) Len() int

func (ItemDocumentation) Less

func (s ItemDocumentation) Less(i, j int) bool

func (ItemDocumentation) Sort

func (s ItemDocumentation) Sort()

func (ItemDocumentation) Swap

func (s ItemDocumentation) Swap(i, j int)

type ItemDocumentationEntry

type ItemDocumentationEntry struct {
	Name        string
	Description string
}

type OutputAction

type OutputAction = func(vi *VersionInformation, params map[string]string) error

type OutputSpec

type OutputSpec struct {
	Name        string
	Description string
	Parameters  []string
	Action      OutputAction
}

func GetOutputSpec

func GetOutputSpec(name string) *OutputSpec

type UpdateActions

type UpdateActions map[string]string

type VersionInfoAction

type VersionInfoAction struct {
	Name       string
	Parameters map[string]string
}

type VersionInfoActions

type VersionInfoActions struct {
	Inputs  []VersionInfoAction
	Outputs []VersionInfoAction
}

func ReadVersionInfoActions

func ReadVersionInfoActions() (*VersionInfoActions, error)

type VersionInformation

type VersionInformation struct {
	Author      string
	Email       string
	Website     string
	Name        string
	Description string
	License     string

	Major  int
	Minor  int
	Hotfix int
	Build  int

	Revision string

	BuildTimestamp int
	BuildHost      string
}

func MakeVersionInformation

func MakeVersionInformation() *VersionInformation

func TryReadFromBuildHost

func TryReadFromBuildHost() (*VersionInformation, error)

func TryReadFromEnvironmentVariables

func TryReadFromEnvironmentVariables(varMap map[string]string) (*VersionInformation, error)

func TryReadFromGit

func TryReadFromGit() (*VersionInformation, error)

func TryReadFromGitlabCI

func TryReadFromGitlabCI(ignoreRevision bool, ignoreProjectName bool) (*VersionInformation, error)

func TryReadFromKonsortenDefaults

func TryReadFromKonsortenDefaults() (*VersionInformation, error)

func TryReadFromPackageJSON

func TryReadFromPackageJSON(ignoreName bool, ignoreVersion bool, ignoreDescription bool, ignoreAuthor bool, ignoreLicense bool) (*VersionInformation, error)

func TryReadFromTeamCity

func TryReadFromTeamCity(ignoreBuildNumber bool, ignoreRevision bool, ignoreName bool) (*VersionInformation, error)

func TryReadFromVersionInfoYAML

func TryReadFromVersionInfoYAML(maxDepth int) (*VersionInformation, error)

func TryReadFromWorkingDirectory

func TryReadFromWorkingDirectory() (*VersionInformation, error)

func (*VersionInformation) CopyMissingFrom

func (vi *VersionInformation) CopyMissingFrom(copy *VersionInformation)

func (*VersionInformation) IsValid

func (vi *VersionInformation) IsValid() (ok bool, errors []string)

func (*VersionInformation) LimitRevision

func (vi *VersionInformation) LimitRevision(length int)

func (*VersionInformation) SemVerString

func (vi *VersionInformation) SemVerString() string

func (*VersionInformation) SetSemVersion

func (vi *VersionInformation) SetSemVersion(semVerString string)

func (*VersionInformation) String

func (vi *VersionInformation) String() string

func (*VersionInformation) VersionString

func (vi *VersionInformation) VersionString() string

func (*VersionInformation) VersionTitle

func (vi *VersionInformation) VersionTitle() string

func (*VersionInformation) WriteTemplateFile

func (vi *VersionInformation) WriteTemplateFile(templateFilePath string, fileMode os.FileMode) error

func (*VersionInformation) WriteToTeamCity

func (vi *VersionInformation) WriteToTeamCity() error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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