Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
[Feature] Add Verison Flag to CLI
Browse files Browse the repository at this point in the history
Added a 'version' variable to store version information
Implemented 'printVersion', 'runVersionCheck' function to print the current version using the '--version' flag

Signed-off-by: HyoBin Kim <[email protected]>
  • Loading branch information
Kim-Hyo-Bin committed Feb 15, 2024
1 parent 1159c53 commit 8c96739
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"go.containerssh.io/libcontainerssh/service"
)

var (
version = "0.5.0"
)

// Main is a helper function to start a standard ContainerSSH instance. It should be used as the outer-most function
// and should never be used as an embedding technique.
func Main() {
Expand All @@ -37,7 +41,7 @@ func Main() {

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

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

Check warning on line 44 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L44

Added line #L44 was not covered by tests

if configFile == "" {
configFile = "config.yaml"
Expand Down Expand Up @@ -81,6 +85,8 @@ func Main() {
runActionLicenses(configuredLogger)
case actionHealthCheck:
runHealthCheck(cfg, configuredLogger)
case actionVersionCheck:
runVersionCheck(configuredLogger)

Check warning on line 89 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L88-L89

Added lines #L88 - L89 were not covered by tests
default:
runContainerSSH(loggerFactory, configuredLogger, cfg, configFile)
}
Expand All @@ -94,7 +100,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)

Check warning on line 108 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L103-L108

Added lines #L103 - L108 were not covered by tests
}
func runActionLicenses(logger log.Logger) {
if err := printLicenses(os.Stdout); err != nil {
logger.Critical(err)
Expand Down Expand Up @@ -142,11 +154,12 @@ func runContainerSSH(
os.Exit(0)
}

func getArguments() (string, bool, bool, bool) {
func getArguments() (string, bool, bool, bool, bool) {

Check warning on line 157 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L157

Added line #L157 was not covered by tests
configFile := ""
actionDumpConfig := false
actionLicenses := false
healthCheck := false
VersionCheck := false

Check warning on line 162 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L162

Added line #L162 was not covered by tests
flag.StringVar(
&configFile,
"config",
Expand All @@ -171,8 +184,14 @@ func getArguments() (string, bool, bool, bool) {
false,
"Run health check",
)
flag.BoolVar(
&VersionCheck,
"version",
false,
"Run version check",
)

Check warning on line 192 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L187-L192

Added lines #L187 - L192 were not covered by tests
flag.Parse()
return configFile, actionDumpConfig, actionLicenses, healthCheck
return configFile, actionDumpConfig, actionLicenses, healthCheck, VersionCheck

Check warning on line 194 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L194

Added line #L194 was not covered by tests
}

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

func printVersion(writer io.Writer) error {
var buffer bytes.Buffer
buffer.WriteString("Containerssh Version:")
buffer.WriteString(version)
buffer.WriteString("\n")
if _, err := writer.Write(buffer.Bytes()); err != nil {
return fmt.Errorf("failed to write Version information (%w)", err)
}
return nil

Check warning on line 335 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L327-L335

Added lines #L327 - L335 were not covered by tests
}

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

Expand Down

0 comments on commit 8c96739

Please sign in to comment.