Skip to content

Commit

Permalink
feat:get version cmd + endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LeVraiBaptiste committed Dec 9, 2024
1 parent 54a3312 commit 20dd242
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/ctl/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

flags "stamus-ctl/internal/handlers"
config "stamus-ctl/internal/handlers/config"
handlers "stamus-ctl/internal/handlers/config"
)

// Command
Expand All @@ -34,6 +35,35 @@ Example: get scirius`,
return cmd
}

func versionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Get config version",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
versionHandler()
return nil
},
}
// Flags
flags.Config.AddAsFlag(cmd, false)
return cmd
}

// Handlers
func versionHandler() error {
// Get properties
conf, err := flags.Config.GetValue()
if err != nil {
return err
}
// Get the version
version := handlers.GetVersion(conf.(string))
// Print the version
fmt.Println(version)
return nil
}

// Subcommands
func getContentCmd() *cobra.Command {
// Command
Expand Down
1 change: 1 addition & 0 deletions cmd/ctl/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ func ConfigCmd() *cobra.Command {
// Add Commands
cmd.AddCommand(getCmd())
cmd.AddCommand(setCmd())
cmd.AddCommand(versionCmd())
return cmd
}
24 changes: 24 additions & 0 deletions cmd/daemon/run/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,27 @@ func getHandler(c *gin.Context) {
}
c.JSON(200, groupedValues)
}

type VersionResponse string

// versionHandler godoc
// @Summary Get configuration version
// @Description Retrieves configuration version for a given project.
// @Tags config
// @Produce json
// @Param get query pkg.Config true "Config to get version from"
// @Success 200 {object} VersionResponse "Configuration version retrieved successfully"
// @Failure 404 {object} pkg.ErrorResponse "Bad request with explanation"
// @Failure 500 {object} pkg.ErrorResponse "Internal server error with explanation"
// @Router /config [get]
func getVersionHandler(c *gin.Context) {
// Extract request body
var req pkg.Config
if err := c.BindQuery(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

version := handlers.GetVersion(req.Value)
c.JSON(200, version)
}
1 change: 1 addition & 0 deletions cmd/daemon/run/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ func NewConfig(router *gin.RouterGroup) {
router.POST("/config", setHandler)
router.POST("/config/list", getConfigListHandler)
router.GET("/config", getHandler)
router.GET("/config/version", getVersionHandler)
}
15 changes: 15 additions & 0 deletions internal/handlers/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import (
"strings"
)

func GetVersion(config string) string {
// File
if !app.IsCtl() {
config = app.GetConfigsFolder(config)
}
filePath := filepath.Join(config, "version")

// Read the file content
content, err := os.ReadFile(filePath)
if err != nil {
return "Could not read the version file"
}
return string(content)
}

// Get the grouped config values
// Essentially, this function reads the config values file and groups the values
func GetGroupedConfig(conf string, args []string, reload bool) (map[string]interface{}, error) {
Expand Down

0 comments on commit 20dd242

Please sign in to comment.