Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add Verison Flag to CLI #560

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
60 changes: 56 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -37,7 +38,7 @@ func Main() {

logger = logger.WithLabel("module", "core")

configFile, actionDumpConfig, actionLicenses, actionHealthCheck := getArguments()
configFile, actionDumpConfig, actionLicenses, actionHealthCheck, actionVersionCheck := getArguments()

if configFile == "" {
configFile = "config.yaml"
Expand Down Expand Up @@ -81,6 +82,8 @@ func Main() {
runActionLicenses(configuredLogger)
case actionHealthCheck:
runHealthCheck(cfg, configuredLogger)
case actionVersionCheck:
runVersionCheck(configuredLogger)
default:
runContainerSSH(loggerFactory, configuredLogger, cfg, configFile)
}
Expand All @@ -94,7 +97,13 @@ func runHealthCheck(cfg config.AppConfig, logger log.Logger) {
logger.Info(message.NewMessage(message.MCoreHealthCheckSuccessful, "Health check successful."))
os.Exit(0)
}

func runVersionCheck(logger log.Logger) {
if err := printVersion(os.Stdout); err != nil {
logger.Critical(err)
os.Exit(1)
}
os.Exit(0)
}
func runActionLicenses(logger log.Logger) {
if err := printLicenses(os.Stdout); err != nil {
logger.Critical(err)
Expand Down Expand Up @@ -142,11 +151,12 @@ func runContainerSSH(
os.Exit(0)
}

func getArguments() (string, bool, bool, bool) {
func getArguments() (string, bool, bool, bool, bool) {
configFile := ""
actionDumpConfig := false
actionLicenses := false
healthCheck := false
VersionCheck := false
flag.StringVar(
&configFile,
"config",
Expand All @@ -171,8 +181,14 @@ func getArguments() (string, bool, bool, bool) {
false,
"Run health check",
)
flag.BoolVar(
&VersionCheck,
"version",
false,
"Run version check",
)
flag.Parse()
return configFile, actionDumpConfig, actionLicenses, healthCheck
return configFile, actionDumpConfig, actionLicenses, healthCheck, VersionCheck
}

func startServices(cfg config.AppConfig, loggerFactory log.LoggerFactory) error {
Expand Down Expand Up @@ -305,6 +321,42 @@ func healthCheck(cfg config.AppConfig, logger log.Logger) error {
return nil
}

func printVersion(writer io.Writer) error {
var buffer bytes.Buffer
var libcontainersshVersion, buildRevision, buildTime, buildArch, buildmodified string
bi, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Errorf("read build info %t", ok)
}
fmt.Printf("%+v\n", bi)
for _, dep := range bi.Deps {
if dep.Path == "go.containerssh.io/libcontainerssh" {
libcontainersshVersion = dep.Version
break
}
}
for _, setting := range bi.Settings {
if setting.Key == "vcs.revision" {
buildRevision = setting.Value
} else if setting.Key == "vcs.time" {
buildTime = setting.Value
} else if setting.Key == "GOARCH" {
buildArch = setting.Value
} else if setting.Key == "modified" {
buildmodified = setting.Value
}
}
stringGolangVersion := bi.GoVersion + " - " + buildArch + "\n"
stringLibcontainersshVersion := fmt.Sprintf("libcontainerssh version : %s\n", libcontainersshVersion)
stringBuildRevision := fmt.Sprintf("build revision: %s(%s) {{%s}}\n", buildRevision, buildTime, buildmodified)
stringConcateBuildInfo := stringGolangVersion + stringLibcontainersshVersion + stringBuildRevision
buffer.WriteString(stringConcateBuildInfo)
if _, err := writer.Write(buffer.Bytes()); err != nil {
return fmt.Errorf("failed to write Version information (%w)", err)
}
Kim-Hyo-Bin marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func printLicenses(writer io.Writer) error {
var buffer bytes.Buffer

Expand Down