
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
go.wamod.dev/warning
warning
package provides mechanisms for capturing diagnostics using context.Context
.
It allows to easily capture warning without modifying existing function signatures.
warning are captured using a Collector
, which is attached to the context.Context
.
The package offers the following functionalities:
To use this package in your Go project, you can import it using:
import "go.wamod.dev/warning"
To start capturing warning create a new Collector
:
collector := warning.NewCollector()
defer collector.Close() // close before exiting
Attach this collector to your context:
ctx := warning.Attach(context.Background(), collector)
Then, you can write warning to the context:
warning.Warnf(ctx, "this is a warning")
Finally, capture all of warning back from the collector:
wrrs, err := warning.ReadAll(collector)
This example demonstrates how to use the Filter
function to filter warning:
// filter warnings
ctx = warning.Filter(ctx, func(wrr warning.Warning) bool {
return !strings.HasPrefix(wrr.Warn(), "ignore:")
})
// This warning will be captured
warning.Warnf(ctx, "capture: warning 1")
// This warning will be ignored
warning.Warnf(ctx, "ignore: warning 2")
Transform each written warning.
ctx = warning.Map(ctx, func(wrr warning.Warning) warning.Warning {
return warning.New(strings.ToUpper(wrr.Warn()))
})
// This warning will be transformed to "WARNING 1"
warning.Warnf(ctx, "warning 1")
Combine all written warning into a single warning.
// create a custom warning
type multiWarn struct {
details []string
}
func (w *multiWarn) Warn() string {
return strings.Join(w.details, ", ")
}
// reduce warnings
ctx, flush := warning.Reduce(ctx, func(acc *multiWarn*, wrr warning.Warning) *multiWarn {
if acc == nil {
acc = new(multiWarn)
}
acc.details = append(acc.details, wrr.Warn())
return acc
})
// flush on exit
defer flush()
// Write warnings
warning.Warnf(ctx, "warning 1")
warning.Warnf(ctx, "warning 2")
warning.Warnf(ctx, "warning 3")
// The captured warning will be:
// &multiWarn{"warning 1", "warning 2", "warning 3"}
It does not modify the warning or the context but is useful for side effects like logging.
ctx = warning.Tap(ctx, func(wrr warning.Warning) {
slog.Warn(wrr.Warn())
})
// Now every new warning will be logged using `slog`
warning.Warnf(ctx, "this is a warning")
warning.Warnf(ctx, "this is another warning")
Thank you for your interest in contributing to the warning
Go library! We welcome and appreciate any contributions, whether they be bug reports, feature requests, or code changes.
If you've found a bug, please create an issue describing the problem, including any relevant error messages and a minimal reproduction of the issue.
warning
is licensed under the MIT License.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.