Skip to content

Commit

Permalink
feat: fix global and scoped flag parser
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <marko@mkungla.dev>
  • Loading branch information
mkungla committed Dec 1, 2024
1 parent cb7d79a commit e4ed124
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
module github.com/happy-sdk/happy

go 1.22
go 1.22.0

toolchain go1.23.3

require (
github.com/happy-sdk/happy/pkg/branding v0.1.1
github.com/happy-sdk/happy/pkg/cli/ansicolor v0.2.1
github.com/happy-sdk/happy/pkg/devel/testutils v0.7.0
github.com/happy-sdk/happy/pkg/options v0.2.0
github.com/happy-sdk/happy/pkg/options v0.2.1
github.com/happy-sdk/happy/pkg/scheduling/cron v0.4.1
github.com/happy-sdk/happy/pkg/settings v0.3.1
github.com/happy-sdk/happy/pkg/strings/humanize v0.2.0
github.com/happy-sdk/happy/pkg/strings/slug v0.1.0
github.com/happy-sdk/happy/pkg/strings/textfmt v0.3.2
github.com/happy-sdk/happy/pkg/vars v0.12.0
github.com/happy-sdk/happy/pkg/version v0.1.3
golang.org/x/sys v0.20.0
golang.org/x/text v0.15.0
golang.org/x/sys v0.27.0
golang.org/x/text v0.20.0
)

require (
github.com/happy-sdk/happy/pkg/strings/bexp v1.4.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/mod v0.22.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/happy-sdk/happy/pkg/cli/ansicolor v0.2.1 h1:qAvMYJfoPOqKV+UI5Xl0VhsKT
github.com/happy-sdk/happy/pkg/cli/ansicolor v0.2.1/go.mod h1:31cuN6nBnI7cWiNoJG951P6aZvDje0NACQHWbsszuhI=
github.com/happy-sdk/happy/pkg/devel/testutils v0.7.0 h1:Tyym7OiArjuKnKRIAITioGjuobvhFUMsIGg0bZ58CUs=
github.com/happy-sdk/happy/pkg/devel/testutils v0.7.0/go.mod h1:K/VIXChL6yiZTWsYrrl3lauYUAQL1cMaIJK5aPvG/xs=
github.com/happy-sdk/happy/pkg/options v0.2.0 h1:F2RMMZ4/7qm0qRq2hcJk3xBkdRdbDPj/+rB0NHwP8k8=
github.com/happy-sdk/happy/pkg/options v0.2.0/go.mod h1:Nz8Px0uN11BFKWWeWxuG/GtxgSx3ag0pIh0Wzah7l1c=
github.com/happy-sdk/happy/pkg/options v0.2.1 h1:Y5IyLd9ePB+RMsxX1O/gh+BYyOtMBaMl351NgIdMOa0=
github.com/happy-sdk/happy/pkg/options v0.2.1/go.mod h1:fydvXc271ftnYxxN5rNqp9F5IUE1vZH+fag4gJy1kZw=
github.com/happy-sdk/happy/pkg/scheduling/cron v0.4.1 h1:d8W6QGH+G6o9UjNXevjo/Iu9vMCP7IG5ppd29AfqrGk=
github.com/happy-sdk/happy/pkg/scheduling/cron v0.4.1/go.mod h1:9k/p1Zh3YwW8/pcEffP06p/imhqlbRCcdwXCMKkDoFY=
github.com/happy-sdk/happy/pkg/settings v0.3.1 h1:lAiVUtEUnvnr+AjQg0bo4Rjp/y+/qn6UFRjX+B62DQ8=
Expand All @@ -22,9 +22,9 @@ github.com/happy-sdk/happy/pkg/vars v0.12.0 h1:mfJ9ai8J4dZEgiJbKksLsNMvkYlgkhE65
github.com/happy-sdk/happy/pkg/vars v0.12.0/go.mod h1:XPUQNv4TCux9QR7PqxAuyhHhvUJx5iNiNaFxbsqgzh4=
github.com/happy-sdk/happy/pkg/version v0.1.3 h1:jjGuvRWHCTw9NecuAi74HkVn0hOV31JoO09sQaU8AeU=
github.com/happy-sdk/happy/pkg/version v0.1.3/go.mod h1:4xuHwdl5mp9TKqg7yXpQP9psVoC+i/tGtyxxhTXppmQ=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
10 changes: 10 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/happy-sdk/happy v0.19.0/go.mod h1:8pND3rDvQoP+zhm3Dd9qRzMGOWmMG3emRTAZv+rDjdU=
github.com/happy-sdk/happy v0.23.0/go.mod h1:OjnEjUhw5mmRKDoMHRsi2JkukNWsABeM+4G87CbgtH0=
github.com/happy-sdk/happy v0.24.0/go.mod h1:84kGSaM9COejEOwj62LUKc8afm5rH4hha8MKxGtB4jY=
Expand All @@ -11,10 +12,19 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
9 changes: 7 additions & 2 deletions pkg/vars/varflag/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package varflag

import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -145,7 +147,7 @@ func (f *Common) Hide() {
f.hidden = true
}

// IsGlobal reports whether this flag is global.
// Global reports whether this flag is global.
// By default all flags are global flags. You can mark flag non-global
// by calling .BelongsTo(cmdname string).
func (f *Common) Global() bool {
Expand Down Expand Up @@ -324,7 +326,8 @@ func (f *Common) parseArgs(args []string, read func([]vars.Variable) error) (err
// what was before the flag including flag it self

// default is global
f.global = f.command == "" || f.command == "/"

f.global = f.command == "" || f.command == "/" || f.command == filepath.Base(os.Args[0])
if len(pargs) < poses[0] {
return nil
}
Expand All @@ -342,6 +345,8 @@ func (f *Common) parseArgs(args []string, read func([]vars.Variable) error) (err
opts++
if opts > 1 {
cmd = arg
// Global flags have to be set before any cmnd args
f.global = false
}
// found portential command
if len(cmd) > 0 && (f.command == "*" || cmd == f.command) {
Expand Down
24 changes: 22 additions & 2 deletions sdk/app/internal/application/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Runtime struct {
engine *engine.Engine

tmplogger logging.Logger
execlvl logging.Level

initStartedAt time.Time
initTook time.Duration
Expand Down Expand Up @@ -218,7 +219,9 @@ func (rt *Runtime) boot() (err error) {

rt.sess.Dispatch(rt.sessionReadyEvent)
rt.sessionReadyEvent = nil
rt.sess.Log().LogDepth(1, logging.LevelDebug, "application booted", slog.String("took", bootTook))
if rt.execlvl == logging.LevelQuiet || rt.execlvl < logging.LevelDebug {
rt.sess.Log().LogDepth(1, logging.LevelDebug, "application booted", slog.String("took", bootTook))
}
return nil
}

Expand All @@ -234,7 +237,10 @@ func (rt *Runtime) Start() {
}

rt.startedAt = rt.sess.Time(time.Now())
rt.sess.Log().LogDepth(1, logging.LevelDebug, "starting application", slog.Time("started.at", rt.startedAt))
if rt.execlvl == logging.LevelQuiet || rt.execlvl < logging.LevelDebug {
rt.sess.Log().LogDepth(1, logging.LevelDebug, "starting application", slog.Time("started.at", rt.startedAt))
}

if rt.engine != nil {
if err := rt.engine.Stats().Set("app.started.at", rt.startedAt.Format(time.RFC3339)); err != nil {
rt.sess.Log().Error("failed to set app started at", slog.String("err", err.Error()))
Expand Down Expand Up @@ -289,6 +295,9 @@ func (rt *Runtime) Start() {
rt.Exit(1)
return
}
if rt.execlvl < logging.LevelQuiet {
rt.sess.Log().SetLevel(rt.execlvl)
}

if err != nil {
rt.Exit(1)
Expand Down Expand Up @@ -324,7 +333,14 @@ func (rt *Runtime) executeBeforeActions() error {
rt.recover(r, "before actions failed")
}
}()
if rt.execlvl < logging.LevelQuiet {
execlvl := rt.sess.Log().Level()
rt.sess.Log().SetLevel(rt.execlvl)
rt.execlvl = execlvl
}

internal.Log(rt.sess.Log(), "executing before actions")

if rt.sess.Log().Level() < logging.LevelDebug {
// Settings table
settingstbl := textfmt.Table{
Expand Down Expand Up @@ -405,6 +421,10 @@ func (rt *Runtime) ExitCh() <-chan ShutDown {
return rt.exitCh
}

func (rt *Runtime) SetExecLogLevel(lvl logging.Level) {
rt.execlvl = lvl
}

func (rt *Runtime) Exit(code int) {
rt.log(0, internal.LogLevelHappy, "shutting down", slog.Int("exit.code", code))

Expand Down
26 changes: 22 additions & 4 deletions sdk/app/internal/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type Initializer struct {
log *logging.QueueLogger

// user defined logger or default logger for runtime
logger logging.Logger
logger logging.Logger
execlvl logging.Level

opts *options.Options
settings settings.Settings
Expand Down Expand Up @@ -90,7 +91,9 @@ func New(s settings.Settings, rt *application.Runtime, log *logging.QueueLogger)
createdAt: time.Now(),
rt: rt,
defaults: &defaults{},
execlvl: logging.LevelQuiet,
}

init.log.LogDepth(3, logging.LevelDebug, "initializing", slog.String("pid", fmt.Sprint(init.pid)))
init.initialize()
return init
Expand Down Expand Up @@ -386,6 +389,8 @@ func (init *Initializer) Finalize() (err error) {
init.rt.InitStats(init.createdAt, took)

session.Log().LogDepth(1, logging.LevelDebug, "initialization completed", slog.String("took", took.String()))

init.rt.SetExecLogLevel(init.execlvl)
return nil
}

Expand Down Expand Up @@ -682,13 +687,26 @@ func (init *Initializer) configureLogger() (err error) {
tslocStr = "Local"
timestampFormat = "15:04:05"
}

if init.cmd != nil {
if init.cmd.Flag("system-debug").Var().Bool() {
lvl = internal.LogLevelHappy
if init.cmd.Flag("system-debug").Global() {
lvl = internal.LogLevelHappy
} else {
init.execlvl = internal.LogLevelHappy
}
} else if init.cmd.Flag("debug").Var().Bool() {
lvl = logging.LevelDebug
if init.cmd.Flag("debug").Global() {
lvl = logging.LevelDebug
} else {
init.execlvl = logging.LevelDebug
}
} else if init.cmd.Flag("verbose").Var().Bool() {
lvl = logging.LevelInfo
if init.cmd.Flag("verbose").Global() {
lvl = logging.LevelInfo
} else {
init.execlvl = logging.LevelInfo
}
}
}

Expand Down

0 comments on commit e4ed124

Please sign in to comment.