Skip to content

Commit

Permalink
Merge pull request #108 from xmidt-org/hotfix/code-style
Browse files Browse the repository at this point in the history
Hotfix/code style
  • Loading branch information
johnabass authored Nov 22, 2024
2 parents 6449714 + df37545 commit 5559fff
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: xmidt-org/shared-go/.github/workflows/ci.yml@59f5d322b0ee953245334530336f8e6503cacb65 # v4.4.27
with:
copyright-skip: true
style-skip: true
style-skip: false
release-type: library
yaml-lint-skip: false
secrets: inherit
22 changes: 11 additions & 11 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var (
stringType = reflect.TypeOf("")

levelType = reflect.TypeOf(zapcore.Level(0))
levelPtrType = reflect.PtrTo(levelType)
levelPtrType = reflect.PointerTo(levelType)
atomicLevelType = reflect.TypeOf(zap.AtomicLevel{})
atomicLevelPtrType = reflect.PtrTo(atomicLevelType)
atomicLevelPtrType = reflect.PointerTo(atomicLevelType)

levelEncoderType = reflect.TypeOf(zapcore.LevelEncoder(nil))
timeEncoderType = reflect.TypeOf(zapcore.TimeEncoder(nil))
Expand Down Expand Up @@ -79,15 +79,15 @@ func decodeNameEncoder(text string) (ne zapcore.NameEncoder, err error) {
//
// The to type may be one of:
//
// zapcore.Level
// *zapcore.Level
// zap.AtomicLevel
// *zap.AtomicLevel
// zapcore.LevelEncoder
// zapcore.TimeEncoder
// zapcore.DurationEncoder
// zapcore.CallerEncoder
// zapcore.NameEncoder
// zapcore.Level
// *zapcore.Level
// zap.AtomicLevel
// *zap.AtomicLevel
// zapcore.LevelEncoder
// zapcore.TimeEncoder
// zapcore.DurationEncoder
// zapcore.CallerEncoder
// zapcore.NameEncoder
//
// The UnmarshalText method of the to type is used to do the conversion.
//
Expand Down
6 changes: 0 additions & 6 deletions fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ func WithLogger(options ...zap.Option) fx.Option {
// SyncOnShutdown adds an fx lifecycle hook that invokes Sync on the application's logger.
// Generally, this option should be placed as an fx.Invoke last in the set of options.
// That ensures that log entries from other lifecycle OnStop hooks are written to log sinks.
//
// fx.New(
// // all other options come first ...
//
// sallust.SyncOnShutdown(),
// )
func SyncOnShutdown() fx.Option {
return fx.Invoke(
func(logger *zap.Logger, lifecycle fx.Lifecycle) {
Expand Down
50 changes: 15 additions & 35 deletions permissions.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
package sallust

import (
"errors"
"fmt"
"io/fs"
"strconv"
)

var (
// ErrInvalidPermissions is returned by ParsePermissions to indicate a bad permissions value.
ErrInvalidPermissions = errors.New("Invalid permissions")
)

func accumulate(v byte, factor int, perms *fs.FileMode) (ok bool) {
if ok = v >= '0' && v <= '7'; ok {
*perms += (fs.FileMode(int(v-'0') * factor))
}

return
}

// ParsePermissions parses a nix-style file permissions value. The value must be a 3-digit
// octal integer with an optional leading zero (0). The empty string is considered to be 000.
func ParsePermissions(v string) (perms fs.FileMode, err error) {
switch {
// allows for an unset configuration value, which means just take the underlying defaults
case len(v) == 0:
return

case len(v) < 3:
fallthrough

case len(v) > 4:
fallthrough

// if 4 characters, the first character must be a zero (0)
case len(v) == 4 && v[0] != '0':
err = ErrInvalidPermissions

case !accumulate(v[len(v)-1], 1, &perms):
err = ErrInvalidPermissions

case !accumulate(v[len(v)-2], 8, &perms):
err = ErrInvalidPermissions

case !accumulate(v[len(v)-3], 64, &perms):
err = ErrInvalidPermissions
// do nothing. allow an empty string to map to zero perms

case len(v) == 3 || (len(v) == 4 && v[0] == '0'):
var raw uint64
raw, err = strconv.ParseUint(v, 8, 32)
if err == nil {
perms = fs.FileMode(raw)
} else {
err = fmt.Errorf("Invalid permissions [%s]: %s", v, err)
}

default:
err = fmt.Errorf("Invalid permissions [%s]: incorrect length", v)
}

return
Expand Down
2 changes: 1 addition & 1 deletion permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *ParsePermissionsTestSuite) TestInvalid() {
for _, testCase := range testCases {
suite.Run(testCase, func() {
_, err := ParsePermissions(testCase)
suite.ErrorIs(err, ErrInvalidPermissions)
suite.Error(err)
})
}
}
Expand Down
14 changes: 0 additions & 14 deletions sallusthttp/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ type Middleware struct {

// Decorate is a middleware function for augmenting request contexts with
// loggers. If next is nil, then this function decorates http.DefaultServeMux.
//
// This function may be used with gorilla/mux, e.g.:
//
// var m Middleware
// m.Builders.Add(Named("myHandler"), DefaultFields)
// r := mux.NewRouter()
// r.UseMiddleware(m.Decorate)
// r.Handle("/", MyHandler{})
//
// Similarly, it can be used with packages like justinas/alice:
//
// var m Middleware
// m.Builders.Add(Named("myHandler"), DefaultFields)
// alice.New(m.Decorate).Then(MyHandler{})
func (m Middleware) Decorate(next http.Handler) http.Handler {
// keep a similar behavior to justinas/alice:
if next == nil {
Expand Down

0 comments on commit 5559fff

Please sign in to comment.