-
Notifications
You must be signed in to change notification settings - Fork 2
release 0.6 (#48) #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kooksee
wants to merge
1
commit into
master
Choose a base branch
from
release/0.6
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
package assert | ||
package assert_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pubgo/funk/errors" | ||
"github.com/pubgo/funk/assert" | ||
"github.com/pubgo/funk/recovery" | ||
) | ||
|
||
func init() { | ||
assert.EnablePrintStack = true | ||
} | ||
|
||
func TestCheckNil(t *testing.T) { | ||
var a *int | ||
|
||
defer func() { | ||
errors.Debug(errors.Parse(recover())) | ||
}() | ||
defer recovery.DebugPrint() | ||
|
||
Assert(a == nil, "ok") | ||
assert.Assert(a == nil, "ok") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,41 +2,39 @@ package assert | |
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"runtime/debug" | ||
|
||
"github.com/pubgo/funk/errors" | ||
"github.com/pubgo/funk/try" | ||
) | ||
|
||
func Must(err error, args ...interface{}) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
panic(errors.WrapStack(errors.Wrap(err, fmt.Sprint(args...)))) | ||
must(err, args...) | ||
} | ||
|
||
func MustFn(errFn func() error, args ...interface{}) { | ||
err := try.Try(errFn) | ||
err := try(errFn) | ||
if err == nil { | ||
return | ||
} | ||
|
||
panic(errors.WrapStack(errors.Wrap(err, fmt.Sprint(args...)))) | ||
must(err, args...) | ||
} | ||
|
||
func MustF(err error, msg string, args ...interface{}) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
panic(errors.WrapStack(errors.Wrap(err, fmt.Sprintf(msg, args...)))) | ||
must(err, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
func Must1[T any](ret T, err error) T { | ||
func Must1[T any](ret T, err error, args ...any) T { | ||
if err != nil { | ||
panic(errors.WrapStack(err)) | ||
must(err, args...) | ||
} | ||
|
||
return ret | ||
|
@@ -47,18 +45,18 @@ func Exit(err error, args ...interface{}) { | |
return | ||
} | ||
|
||
errors.Debug(errors.WrapStack(errors.Wrap(err, fmt.Sprint(args...)))) | ||
slog.Error("os exit with error", "err", err, "msg", fmt.Sprint(args...)) | ||
debug.PrintStack() | ||
os.Exit(1) | ||
} | ||
|
||
func ExitFn(errFn func() error, args ...interface{}) { | ||
err := try.Try(errFn) | ||
err := try(errFn) | ||
if err == nil { | ||
return | ||
} | ||
|
||
errors.Debug(errors.WrapStack(errors.Wrap(err, fmt.Sprint(args...)))) | ||
slog.Error("os exit with error func", "err", err, "msg", fmt.Sprint(args...)) | ||
debug.PrintStack() | ||
os.Exit(1) | ||
} | ||
|
@@ -68,14 +66,14 @@ func ExitF(err error, msg string, args ...interface{}) { | |
return | ||
} | ||
|
||
errors.Debug(errors.WrapStack(errors.Wrapf(err, msg, args...))) | ||
slog.Error("os exit with error format", "err", err, "msg", fmt.Sprintf(msg, args...)) | ||
debug.PrintStack() | ||
os.Exit(1) | ||
} | ||
|
||
func Exit1[T any](ret T, err error) T { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
errors.Debug(errors.WrapStack(err)) | ||
slog.Error("os exit with error unwrap", "err", err) | ||
debug.PrintStack() | ||
os.Exit(1) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package assert | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"runtime/debug" | ||
|
||
"github.com/pubgo/funk/stack" | ||
) | ||
|
||
func messageFromMsgAndArgs(msgAndArgs ...any) string { | ||
if len(msgAndArgs) == 0 { | ||
return "" | ||
} | ||
|
||
if len(msgAndArgs) == 1 { | ||
if msgAsStr, ok := msgAndArgs[0].(string); ok { | ||
return msgAsStr | ||
} | ||
return fmt.Sprintf("%+v", msgAndArgs[0]) | ||
} | ||
|
||
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) | ||
} | ||
|
||
func must(err error, messageArgs ...any) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
message := messageFromMsgAndArgs(messageArgs...) | ||
if message == "" { | ||
message = err.Error() | ||
} else { | ||
message = fmt.Sprintf("msg:%s err:%s", message, err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
if EnablePrintStack { | ||
slog.Error(message) | ||
debug.PrintStack() | ||
} | ||
|
||
panic(message) | ||
} | ||
|
||
func try(fn func() error) (gErr error) { | ||
if fn == nil { | ||
gErr = fmt.Errorf("[fn] is nil") | ||
return | ||
} | ||
|
||
defer func() { | ||
if gErr != nil { | ||
gErr = fmt.Errorf("stack:%s, err:%w", stack.CallerWithFunc(fn).String(), gErr) | ||
} | ||
}() | ||
|
||
defer func() { | ||
if err := recover(); err != nil { | ||
gErr = fmt.Errorf("%v", err) | ||
} | ||
}() | ||
|
||
gErr = fn() | ||
return | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cliutils | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func IsHelp() bool { | ||
help := strings.TrimSpace(os.Args[len(os.Args)-1]) | ||
if strings.HasSuffix(help, "--help") || strings.HasSuffix(help, "-h") { | ||
return true | ||
} | ||
return false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package envcmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/urfave/cli/v3" | ||
|
||
"github.com/pubgo/funk/config" | ||
"github.com/pubgo/funk/env" | ||
"github.com/pubgo/funk/pretty" | ||
"github.com/pubgo/funk/recovery" | ||
) | ||
|
||
func New() *cli.Command { | ||
return &cli.Command{ | ||
Name: "envs", | ||
Usage: "show all envs", | ||
Action: func(ctx context.Context, command *cli.Command) error { | ||
defer recovery.Exit() | ||
|
||
env.Reload() | ||
|
||
fmt.Println("config path:", config.GetConfigPath()) | ||
envs := config.LoadEnvConfigMap(config.GetConfigPath()) | ||
for name, cfg := range envs { | ||
envData := env.Get(name) | ||
if envData != "" { | ||
cfg.Default = envData | ||
} | ||
} | ||
|
||
pretty.Println(envs) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment here to explain what
must
does, as it's not immediately obvious from the name.