package main

import (
	"fmt"
	"runtime/debug"
)

func ShowVersion() {
	var buildinfo *debug.BuildInfo
	var ok bool
	buildinfo, ok = debug.ReadBuildInfo()
	if ok {
		// We have build info, display it.
		fmt.Print("Version: ")
		var bs debug.BuildSetting
		for _, bs = range buildinfo.Settings {
			if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
				fmt.Print(bs.Value + " ")
			}
		}
		fmt.Println()
	}
}

func GetVersion() (goversion string, gitcommit string, arch string, goos string) {
	var buildinfo *debug.BuildInfo
	var ok bool
	var results string
	var garch, gos string
	buildinfo, ok = debug.ReadBuildInfo()
	if ok {
		// We have build info, display it.
		goversion := buildinfo.GoVersion

		var bs debug.BuildSetting
		for _, bs = range buildinfo.Settings {
			if bs.Key == "vcs.revision" || bs.Key == "vcs.time" {
				results += bs.Value + " "
			}
			if bs.Key == "GOARCH" {
				garch = bs.Value
			}
			if bs.Key == "GOOS" {
				gos = bs.Value
			}
		}
		return goversion, results, garch, gos
	}
	return "", "", "", ""
}

func GetModules() map[string]string {
	var buildinfo *debug.BuildInfo
	var ok bool
	var results map[string]string = make(map[string]string)
	buildinfo, ok = debug.ReadBuildInfo()
	if ok {
		// We have Module info, return it.
		for _, mod := range buildinfo.Deps {
			results[mod.Path] = mod.Version
		}
	}
	return results
}