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

[Feature] Add Verison Flag to CLI #560

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"go.containerssh.io/libcontainerssh/service"
)

var (
version = "0.5.0"
Kim-Hyo-Bin marked this conversation as resolved.
Show resolved Hide resolved
)

// 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 @@

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 @@
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 @@
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 @@
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 @@
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 @@
return nil
}

func printVersion(writer io.Writer) error {
var buffer bytes.Buffer
buffer.WriteString("Containerssh Version:")
buffer.WriteString(version)
buffer.WriteString("\n")
Kim-Hyo-Bin marked this conversation as resolved.
Show resolved Hide resolved
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

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
Loading