Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Add health subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
duck8823 committed Nov 13, 2018
1 parent 28ccdf3 commit 894b9a5
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/duck8823/duci/application"
"github.com/duck8823/duci/application/semaphore"
"github.com/duck8823/duci/application/service/docker"
"github.com/duck8823/duci/infrastructure/logger"
"github.com/duck8823/duci/presentation/router"
"github.com/google/uuid"
Expand All @@ -29,8 +30,15 @@ func main() {
}
configCmd.PersistentFlags().StringVarP(&configPath, "config", "c", application.DefaultConfigurationPath, "configuration file path")

healthCmd := &cobra.Command{
Use: "health",
Short: "Health check",
Run: healthCmd,
}
healthCmd.PersistentFlags().StringVarP(&configPath, "config", "c", application.DefaultConfigurationPath, "configuration file path")

rootCmd := &cobra.Command{Use: "duci"}
rootCmd.AddCommand(serverCmd, configCmd)
rootCmd.AddCommand(serverCmd, configCmd, healthCmd)

if err := rootCmd.Execute(); err != nil {
logger.Errorf(uuid.New(), "Failed to execute command.\n%+v", err)
Expand Down Expand Up @@ -72,6 +80,24 @@ func configCmd(cmd *cobra.Command, _ []string) {
}
}

func healthCmd(cmd *cobra.Command, _ []string) {
readConfiguration(cmd)

dockerService, err := docker.New()
if err != nil {
logger.Errorf(uuid.New(), "Failed to set configuration.\n%+v", err)
os.Exit(1)
}

if err := dockerService.Status(); err != nil {
logger.Errorf(uuid.New(), "Unhealthy.\n%s", err)
os.Exit(1)
} else {
logger.Info(uuid.New(), "Healthy.")
os.Exit(0)
}
}

func readConfiguration(cmd *cobra.Command) {
configFilePath := cmd.Flag("config").Value.String()
if !exists(configFilePath) && configFilePath == application.DefaultConfigurationPath {
Expand Down

0 comments on commit 894b9a5

Please sign in to comment.