Skip to content

Commit

Permalink
update flags
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed May 3, 2024
1 parent 340d559 commit dece13a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ builds:
- darwin_amd64
- darwin_arm64
- windows_amd64
ldflags:
- -s -w

archives:
- format: tar.gz
Expand Down
50 changes: 50 additions & 0 deletions Cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"flag"
"log/slog"
"os"
)

var version string

func printVersion() {
if version == "" {
version = "development"
}

println("amock version " + version)
}

var DebugValue = false

var versionFlag = flag.Bool("version", false, "Print the current version and exit")
var helpFlag = flag.Bool("help", false, "Print help message and exit")
var debugFlag = flag.Bool("debug", false, "Enable debug logging")

func parseFlags() {
flag.BoolVar(versionFlag, "v", false, "Print the current version and exit")
flag.BoolVar(debugFlag, "d", false, "Enable debug logging")
flag.BoolVar(helpFlag, "h", false, "Print help message and exit")
DebugValue = *debugFlag

flag.Parse()

if *versionFlag {
printVersion()
os.Exit(0)
}

if *helpFlag || len(flag.Args()) >= 0 && flag.Arg(0) == "help" {
println("Usage:\n\tamock [host:port] [flags]")
println("\n[host:port] - (optional) The host and port to bind the server to")
println("\nFlags: (optional)")
flag.PrintDefaults()
os.Exit(0)
}

if DebugValue {
LogLevel.Set(slog.LevelDebug)
slog.SetLogLoggerLevel(LogLevel.Level())
}
}
15 changes: 0 additions & 15 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"log"
"log/slog"
"net/http"
Expand Down Expand Up @@ -35,20 +34,6 @@ func Debug(msg string, args ...any) {
slog.Debug(gchalk.Dim(msg), args...)
}

var Verbose = false
var verboseFlag = flag.Bool("verbose", false, "Enable verbose logging")

func InitLogger() {
flag.BoolVar(verboseFlag, "v", false, "Shorthand for `--verbose`")
flag.Parse()
Verbose = *verboseFlag

if Verbose {
LogLevel.Set(slog.LevelDebug)
slog.SetLogLoggerLevel(LogLevel.Level())
}
}

func LogRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var config *Config
var db Database

func init() {
InitLogger()
parseFlags()

Debug("Creating database from config...")

config, _ = parseConfigFiles(ConfigPaths...)
Expand Down Expand Up @@ -131,10 +132,14 @@ func StartServer() {

for _, route := range Routes {
_, err := fmt.Fprintln(writer, gchalk.Bold(RequestMethodColor(route.Method, false))+"\t"+url+route.Path+"\t"+gchalk.Dim("[entity: "+gchalk.WithItalic().Bold(strings.Split(route.Path, "/")[1])+"]"))
writer.Flush()
if err != nil {
Error("Error writing to tabwriter", "error", err)
}

err = writer.Flush()
if err != nil {
Error("Error flushing", "error", err)
}
}
fmt.Println("")

Expand Down

0 comments on commit dece13a

Please sign in to comment.