<commit_before>/*
Copyright 2015 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
	"flag"
	"fmt"
	"log"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"github.com/gravitational/version"
	"github.com/gravitational/version/pkg/tool"
)

// pkg is the path to the package the tool will create linker flags for.
var pkg = flag.String("pkg", "", "root package path")

// versionPackage is the path to this version package.
// It is used to access version information attributes during link time.
// This flag is useful when the version package is custom-vendored and has a different package path.
var versionPackage = flag.String("verpkg", "github.com/gravitational/version", "path to the version package")

var compatMode = flag.Bool("compat", false, "generate linker flags using go1.4 syntax")

// semverPattern defines a regexp pattern to modify the results of `git describe` to be semver-complaint.
var semverPattern = regexp.MustCompile(`(.+)-([0-9]{1,})-g([0-9a-f]{14})$`)

// goVersionPattern defines a regexp pattern to parse versions of the `go tool`.
var goVersionPattern = regexp.MustCompile(`go([1-9])\.(\d+)(?:.\d+)*`)

func main() {
	if err := run(); err != nil {
		log.Fatalln(err)
	}
}

func run() error {
	log.SetFlags(0)
	flag.Parse()
	if *pkg == "" {
		return fmt.Errorf("-pkg required")
	}

	goVersion, err := goToolVersion()
	if err != nil {
		return fmt.Errorf("failed to determine go tool version: %v\n", err)
	}

	info, err := getVersionInfo(*pkg)
	if err != nil {
		return fmt.Errorf("failed to determine version information: %v\n", err)
	}

	var linkFlags []string
	linkFlag := func(key, value string) string {
		if goVersion <= 14 || *compatMode {
			return fmt.Sprintf("-X %s.%s %s", *versionPackage, key, value)
		} else {
			return fmt.Sprintf("-X %s.%s=%s", *versionPackage, key, value)
		}
	}

	// Determine the values of version-related variables as commands to the go linker.
	if info.GitCommit != "" {
		linkFlags = append(linkFlags, linkFlag("gitCommit", info.GitCommit))
		linkFlags = append(linkFlags, linkFlag("gitTreeState", info.GitTreeState))
	}
	if info.Version != "" {
		linkFlags = append(linkFlags, linkFlag("version", info.Version))
	}

	fmt.Printf("%s", strings.Join(linkFlags, " "))<commit_msg>add option to print just tag, set current directory for package flag<commit_after>/*
Copyright 2015 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"github.com/gravitational/version"
	"github.com/gravitational/version/pkg/tool"
)

// pkg is the path to the package the tool will create linker flags for.
var pkg = flag.String("pkg", "", "root package path")

// versionPackage is the path to this version package.
// It is used to access version information attributes during link time.
// This flag is useful when the version package is custom-vendored and has a different package path.
var versionPackage = flag.String("verpkg", "github.com/gravitational/version", "path to the version package")

var compatMode = flag.Bool("compat", false, "generate linker flags using go1.4 syntax")

var tagOnly = flag.Bool("tag", false, "print just tag only")

// semverPattern defines a regexp pattern to modify the results of `git describe` to be semver-complaint.
var semverPattern = regexp.MustCompile(`(.+)-([0-9]{1,})-g([0-9a-f]{14})$`)

// goVersionPattern defines a regexp pattern to parse versions of the `go tool`.
var goVersionPattern = regexp.MustCompile(`go([1-9])\.(\d+)(?:.\d+)*`)

func main() {
	if err := run(); err != nil {
		log.Fatalln(err)
	}
}

func run() error {
	log.SetFlags(0)
	flag.Parse()
	if *pkg == "" {
		dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
		if err != nil {
			return err
		}
		*pkg = dir
	}

	goVersion, err := goToolVersion()
	if err != nil {
		return fmt.Errorf("failed to determine go tool version: %v\n", err)
	}

	info, err := getVersionInfo(*pkg)
	if err != nil {
		return fmt.Errorf("failed to determine version information: %v\n", err)
	}

	// print just tag and return
	if *tagOnly {
		fmt.Printf(info.Version)
		return nil
	}

	var linkFlags []string
	linkFlag := func(key, value string) string {
		if goVersion <= 14 || *compatMode {
			return fmt.Sprintf("-X %s.%s %s", *versionPackage, key, value)
		}
		return fmt.Sprintf("-X %s.%s=%s", *versionPackage, key, value)
	}

	// Determine the values of version-related variables as commands to the go linker.
	if info.GitCommit != "" {
		linkFlags = append(linkFlags, linkFlag("gitCommit", info.GitCommit))
		linkFlags = append(linkFlags, linkFlag("gitTreeState", info.GitTreeState))
	}
	if info.Version != "" {
		linkFlags = append(linkFlags, linkFlag("version", info.Version))
	}

	fmt.Printf("%s", strings.Join(linkFlags, " "))
{'fraction_non_alphanumeric': 0.13020203764462096, 'fraction_numerical': 0.007770678639267829, 'mean_word_length': 4.153024911032029, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1031864', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1798, 'n_words': 646}