From 9e1add510a0486f739e573d3167e22518b678089 Mon Sep 17 00:00:00 2001 From: Vikas Date: Sun, 30 Jun 2024 12:53:28 +0530 Subject: [PATCH] Add version information; update release workflow --- .github/workflows/release.yml | 1 + config/config.go | 1 + main.go | 27 +++++++++++++++++++-------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b888d65..578dc71 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,3 +29,4 @@ jobs: goarch: ${{ matrix.goarch }} compress_assets: false asset_name: pastepass-${{ matrix.goos }}-${{ matrix.goarch }} + ldflags: -X "github.com/v1k45/pastepass/config.Version=${{ github.ref_name }}" diff --git a/config/config.go b/config/config.go index 05ad3c1..733e910 100644 --- a/config/config.go +++ b/config/config.go @@ -5,4 +5,5 @@ var ( AppName = "PastePass" DBPath = "pastepass.boltdb" ResetDB = false + Version = "0.00-dev" ) diff --git a/main.go b/main.go index fc459b0..4e90836 100644 --- a/main.go +++ b/main.go @@ -2,22 +2,19 @@ package main import ( "flag" - "github.com/v1k45/pastepass/config" + "fmt" "log" "log/slog" "net/http" "time" + "github.com/v1k45/pastepass/config" "github.com/v1k45/pastepass/db" "github.com/v1k45/pastepass/web" ) func main() { - flag.StringVar(&config.ServerAddr, "server-addr", config.ServerAddr, "The server address to listen on") - flag.StringVar(&config.AppName, "app-name", config.AppName, "The name of the application (e.g. ACME PastePass)") - flag.StringVar(&config.DBPath, "db-path", config.DBPath, "The path to the database file") - flag.BoolVar(&config.ResetDB, "reset-db", config.ResetDB, "Reset the database on startup") - flag.Parse() + parseFlags() // Open the database boltdb, err := db.NewDB(config.DBPath, config.ResetDB) @@ -26,9 +23,23 @@ func main() { } go boltdb.DeleteExpiredPeriodically(time.Minute * 5) - slog.Info("starting_server", "server_addr", config.ServerAddr, "app_name", config.AppName, "db_name", config.DBPath) - // Start the web server + slog.Info("starting_server", "server_addr", config.ServerAddr, "app_name", config.AppName, "db_name", config.DBPath) handler := web.NewHandler(boltdb) http.ListenAndServe(config.ServerAddr, handler.Router()) } + +func parseFlags() { + flag.Usage = func() { + fmt.Printf("pastepass - %s\n\n", config.Version) + fmt.Fprintf(flag.CommandLine.Output(), "Usage:\n") + flag.PrintDefaults() + } + + flag.StringVar(&config.ServerAddr, "server-addr", config.ServerAddr, "The server address to listen on") + flag.StringVar(&config.AppName, "app-name", config.AppName, "The name of the application (e.g. ACME PastePass)") + flag.StringVar(&config.DBPath, "db-path", config.DBPath, "The path to the database file") + flag.BoolVar(&config.ResetDB, "reset-db", config.ResetDB, "Reset the database on startup") + + flag.Parse() +}