Skip to content

Commit

Permalink
feat: added --version argumet
Browse files Browse the repository at this point in the history
  • Loading branch information
kutovoys committed Mar 22, 2024
1 parent 4489273 commit de5c8db
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
11 changes: 8 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (

var CLIConfig models.CLI

func Parse() {
kong.Parse(&CLIConfig,
func Parse(version, commit string) {
ctx := kong.Parse(&CLIConfig,
kong.Name("marzban-exporter"),
kong.Description("A command-line application for exporting Marzban metrics."),
kong.Vars{
"version": version,
"commit": commit,
},
)

if err := validate(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
ctx.Exit(2)
}
}

Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"marzban-exporter/api"
"marzban-exporter/config"
Expand All @@ -13,6 +14,11 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
version = "unknown"
commit = "unknown"
)

func init() {
prometheus.MustRegister(
// Node-related metrics
Expand Down Expand Up @@ -59,7 +65,9 @@ func BasicAuthMiddleware(username, password string) func(http.Handler) http.Hand
}

func main() {
config.Parse()
config.Parse(version, commit)

fmt.Println("Marzban Exporter", version)

s := gocron.NewScheduler(time.Local)

Expand Down
42 changes: 31 additions & 11 deletions models/models.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package models

import (
"fmt"

"github.com/alecthomas/kong"
)

type CLI struct {
Port string `name:"metrics-port" help:"Port to listen on" default:"9090" env:"METRICS_PORT"`
ProtectedMetrics bool `name:"metrics-protected" help:"Whether metrics are protected by basic auth" default:"false" env:"METRICS_PROTECTED"`
MetricsUsername string `name:"metrics-username" help:"Username for metrics if protected by basic auth" default:"metricsUser" env:"METRICS_USERNAME"`
MetricsPassword string `name:"metrics-password" help:"Password for metrics if protected by basic auth" default:"MetricsVeryHardPassword" env:"METRICS_PASSWORD"`
UpdateInterval int `name:"update-interval" help:"Interval for metrics update in seconds" default:"30" env:"UPDATE_INTERVAL"`
TimeZone string `name:"timezone" help:"Timezone used in the application" default:"UTC" env:"TIMEZONE"`
InactivityTime int `name:"inactivity-time" help:"Time in minutes after which a user is considered inactive" default:"2" env:"INACTIVITY_TIME"`
BaseURL string `name:"marzban-base-url" help:"Marzban panel base URL" env:"MARZBAN_BASE_URL"`
ApiUsername string `name:"marzban-username" help:"Marzban panel username" env:"MARZBAN_USERNAME" required:""`
ApiPassword string `name:"marzban-password" help:"Marzban panel password" env:"MARZBAN_PASSWORD" required:""`
SocketPath string `name:"marzban-socket" help:"Path to Marzban Unix Domain Socket" env:"MARZBAN_SOCKET"`
Port string `name:"metrics-port" help:"Port to listen on" default:"9090" env:"METRICS_PORT"`
ProtectedMetrics bool `name:"metrics-protected" help:"Whether metrics are protected by basic auth" default:"false" env:"METRICS_PROTECTED"`
MetricsUsername string `name:"metrics-username" help:"Username for metrics if protected by basic auth" default:"metricsUser" env:"METRICS_USERNAME"`
MetricsPassword string `name:"metrics-password" help:"Password for metrics if protected by basic auth" default:"MetricsVeryHardPassword" env:"METRICS_PASSWORD"`
UpdateInterval int `name:"update-interval" help:"Interval for metrics update in seconds" default:"30" env:"UPDATE_INTERVAL"`
TimeZone string `name:"timezone" help:"Timezone used in the application" default:"UTC" env:"TIMEZONE"`
InactivityTime int `name:"inactivity-time" help:"Time in minutes after which a user is considered inactive" default:"2" env:"INACTIVITY_TIME"`
BaseURL string `name:"marzban-base-url" help:"Marzban panel base URL" env:"MARZBAN_BASE_URL"`
ApiUsername string `name:"marzban-username" help:"Marzban panel username" env:"MARZBAN_USERNAME" required:""`
ApiPassword string `name:"marzban-password" help:"Marzban panel password" env:"MARZBAN_PASSWORD" required:""`
SocketPath string `name:"marzban-socket" help:"Path to Marzban Unix Domain Socket" env:"MARZBAN_SOCKET"`
Version VersionFlag `name:"version" help:"Print version information and quit"`
}

type VersionFlag string

func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Println("Marzban Exporter")
fmt.Printf("Version:\t %s\n", vars["version"])
fmt.Printf("Commit:\t %s\n", vars["commit"])
fmt.Printf("GitHub: https://github.com/kutovoys/marzban-exporter\n")
app.Exit(0)
return nil
}

type AuthTokenResponse struct {
Expand Down

0 comments on commit de5c8db

Please sign in to comment.