From fbec40f831ac36b7411acde1bb46a1191cf65294 Mon Sep 17 00:00:00 2001 From: na4ma4 Date: Thu, 30 May 2024 13:00:43 +1000 Subject: [PATCH 1/2] added Force method, switched to magefile from Makefile --- .golangci.yml | 145 ++++++++++++++++++++++++++--------- Makefile => Makefile.dontuse | 0 go.mod | 2 +- go.work | 6 ++ go.work.sum | 1 + magefiles/go.mod | 24 ++++++ magefiles/go.sum | 101 ++++++++++++++++++++++++ magefiles/magefile.go | 20 +++++ parse.go | 2 +- parse_shell_test.go | 8 +- parse_test.go | 31 ++++++-- permbits.go | 53 ++++++++++--- permbits_test.go | 36 +++++++++ 13 files changed, 365 insertions(+), 64 deletions(-) rename Makefile => Makefile.dontuse (100%) create mode 100644 go.work create mode 100644 go.work.sum create mode 100644 magefiles/go.mod create mode 100644 magefiles/go.sum create mode 100644 magefiles/magefile.go diff --git a/.golangci.yml b/.golangci.yml index 69b8e53..cc8fbef 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,12 +1,14 @@ -# This code is licensed under the terms of the MIT license. +# This code is licensed under the terms of the MIT license https://opensource.org/license/mit +# Copyright (c) 2021 Marat Reymers -## Golden config for golangci-lint v1.53.3 +## Golden config for golangci-lint v1.58.2 # # This is the best config for golangci-lint based on my experience and opinion. # It is very strict, but not extremely strict. -# Feel free to adopt and change it for your needs. +# Feel free to adapt and change it for your needs. # # Imported from https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322 +# Modified for na4ma4 repositories. # run: @@ -32,6 +34,10 @@ linters-settings: # Such cases aren't reported by default. # Default: false check-type-assertions: true + # List of functions to exclude from checking, where each entry is a single function to exclude. + # See https://github.com/kisielk/errcheck#excluding-functions for details. + exclude-functions: + - (*go.uber.org/zap.Logger).Sync exhaustive: # Program elements to check for exhaustiveness. @@ -41,7 +47,8 @@ linters-settings: - map exhaustruct: - # List of regular expressions to exclude struct packages and names from check. + # List of regular expressions to exclude struct packages and their names from checks. + # Regular expressions must match complete canonical struct package/name/structname. # Default: [] exclude: # std libs @@ -77,6 +84,9 @@ linters-settings: # If lower than 0, disable the check. # Default: 40 statements: 50 + # Ignore comments when counting lines. + # Default false + ignore-comments: true gocognit: # Minimal code complexity to report. @@ -97,21 +107,9 @@ linters-settings: # Default: true skipRecvDeref: false - gomnd: - # List of function patterns to exclude from analysis. - # Values always ignored: `time.Date`, - # `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`, - # `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`. - # Default: [] - ignored-functions: - - os.Chmod - - os.Mkdir - - os.MkdirAll - - os.OpenFile - - os.WriteFile - - prometheus.ExponentialBuckets - - prometheus.ExponentialBucketsRange - - prometheus.LinearBuckets + gomoddirectives: + replace-allow-list: + - k8s.io/kube-openapi gomodguard: blocked: @@ -128,8 +126,8 @@ linters-settings: reason: "satori's package is not maintained" - github.com/gofrs/uuid: recommendations: - - github.com/google/uuid - reason: "gofrs' package is not go module" + - github.com/gofrs/uuid/v5 + reason: "gofrs' package was not go module before v5" govet: # Enable all analyzers. @@ -147,6 +145,36 @@ linters-settings: # Default: false strict: true + inamedparam: + # Skips check for interface methods with only a single parameter. + # Default: false + skip-single-param: true + + mnd: + # List of function patterns to exclude from analysis. + # Values always ignored: `time.Date`, + # `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`, + # `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`. + # Default: [] + ignored-functions: + - args.Error + - flag.Arg + - flag.Duration.* + - flag.Float.* + - flag.Int.* + - flag.Uint.* + - os.Chmod + - os.Mkdir.* + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets.* + - prometheus.LinearBuckets + - tabwriter.NewWriter + - cobra.MinimumNArgs + - cobra.MaximumNArgs + - cobra.ExactArgs + - cobra.RangeArgs + nakedret: # Make an issue if func has more lines of code than this setting, and it has naked returns. # Default: 30 @@ -163,12 +191,33 @@ linters-settings: # Default: false require-specific: true + perfsprint: + # Optimizes into strings concatenation. + # Default: true + strconcat: false + rowserrcheck: # database/sql is always checked # Default: [] packages: - github.com/jmoiron/sqlx + sloglint: + # Enforce not using global loggers. + # Values: + # - "": disabled + # - "all": report all global loggers + # - "default": report only the default slog logger + # Default: "" + no-global: "all" + # Enforce using methods that accept a context. + # Values: + # - "": disabled + # - "all": report all contextless calls + # - "scope": report only if a context exists in the scope of the outermost function + # Default: "" + context: "scope" + tenv: # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. @@ -192,34 +241,38 @@ linters: - asciicheck # checks that your code does not contain non-ASCII identifiers - bidichk # checks for dangerous unicode character sequences - bodyclose # checks whether HTTP response body is closed successfully + - canonicalheader # checks whether net/http.Header uses canonical header + - copyloopvar # detects places where loop variables are copied - cyclop # checks function and package cyclomatic complexity - dupl # tool for code clone detection - durationcheck # checks for two durations multiplied together - errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error - errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13 - - execinquery # checks query string in Query function which reads your Go src files and warning it finds - exhaustive # checks exhaustiveness of enum switch statements - exportloopref # checks for pointers to enclosing loop variables + - fatcontext # detects nested contexts in loops - forbidigo # forbids identifiers - funlen # tool for detection of long functions - gocheckcompilerdirectives # validates go compiler directive comments (//go:) - gochecknoglobals # checks that no global variables exist - gochecknoinits # checks that no init functions are present in Go code + - gochecksumtype # checks exhaustiveness on Go "sum types" - gocognit # computes and checks the cognitive complexity of functions - goconst # finds repeated strings that could be replaced by a constant - gocritic # provides diagnostics that check for bugs, performance and style issues - gocyclo # computes and checks the cyclomatic complexity of functions - godot # checks if comments end in a period - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt - - gomnd # detects magic numbers - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod - gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations - goprintffuncname # checks that printf-like functions are named with f at the end - gosec # inspects source code for security problems + - intrange # finds places where for loops could make use of an integer range - lll # reports long lines - loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap) - makezero # finds slice declarations with non-zero initial length - mirror # reports wrong mirror patterns of bytes/strings usage + - mnd # detects magic numbers - musttag # enforces field tags in (un)marshaled structs - nakedret # finds naked returns in functions greater than a specified function length - nestif # reports deeply nested if statements @@ -229,15 +282,20 @@ linters: - nolintlint # reports ill-formed or insufficient nolint directives - nonamedreturns # reports all named returns - nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL + - perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative - predeclared # finds code that shadows one of Go's predeclared identifiers - promlinter # checks Prometheus metrics naming via promlint + - protogetter # reports direct reads from proto message fields when getters should be used - reassign # checks that package variables are not reassigned - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint - rowserrcheck # checks whether Err of rows is checked successfully + - sloglint # ensure consistent code style when using log/slog + - spancheck # checks for mistakes with OpenTelemetry/Census spans - sqlclosecheck # checks that sql.Rows and sql.Stmt are closed - stylecheck # is a replacement for golint - tenv # detects using os.Setenv instead of t.Setenv since Go1.17 - testableexamples # checks if examples are testable (have an expected output) + - testifylint # checks usage of github.com/stretchr/testify - testpackage # makes you use a separate _test package - tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes - unconvert # removes unnecessary type conversions @@ -253,6 +311,7 @@ linters: #- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega #- godox # detects FIXME, TODO and other comment keywords #- goheader # checks is file header matches to pattern + #- inamedparam # [great idea, but too strict, need to ignore a lot of cases by default] reports interfaces with unnamed method parameters #- interfacebloat # checks the number of methods inside an interface #- ireturn # accept interfaces, return concrete types #- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated @@ -267,9 +326,10 @@ linters: #- depguard # [replaced by gomodguard] checks if package imports are in a list of acceptable packages #- dogsled # checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) #- dupword # [useless without config] checks for duplicate words in the source code + #- err113 # [too strict] checks the errors handling expressions #- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted + #- execinquery # [deprecated] checks query string in Query function which reads your Go src files and warning it finds #- forcetypeassert # [replaced by errcheck] finds forced type assertions - #- goerr113 # [too strict] checks the errors handling expressions #- gofmt # [replaced by goimports] checks whether code was gofmt-ed #- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed #- gosmopolitan # reports certain i18n/l10n anti-patterns in your Go codebase @@ -283,18 +343,6 @@ linters: #- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers #- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines - ## deprecated - #- deadcode # [deprecated, replaced by unused] finds unused code - #- exhaustivestruct # [deprecated, replaced by exhaustruct] checks if all struct's fields are initialized - #- golint # [deprecated, replaced by revive] golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes - #- ifshort # [deprecated] checks that your code uses short syntax for if-statements whenever possible - #- interfacer # [deprecated] suggests narrower interface types - #- maligned # [deprecated, replaced by govet fieldalignment] detects Go structs that would take less memory if their fields were sorted - #- nosnakecase # [deprecated, replaced by revive var-naming] detects snake case of variable naming and function name - #- scopelint # [deprecated, replaced by exportloopref] checks for unpinned variables in go programs - #- structcheck # [deprecated, replaced by unused] finds unused struct fields - #- varcheck # [deprecated, replaced by unused] finds unused global variables and constants - issues: # Maximum count of issues with the same text. @@ -316,5 +364,26 @@ issues: - gosec - noctx - wrapcheck - - source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {" - linters: [ errorlint ] + - linters: [ staticcheck ] + text: "options.AndFrom is deprecated" + - linters: [ staticcheck ] + text: "The component config package has been deprecated and will be removed in a future release." + - path: "cmd/" + linters: + - gochecknoinits + - gochecknoglobals + # Exclude `lll` issues for long lines with `go:generate`. + - linters: + - lll + source: '^//\s*\+kubebuilder:' + - linters: + - godot + source: '^//\s*\+optional' + - path: "api/v1/" + linters: + - gochecknoinits + # Allow dot imports for ginkgo and gomega + - source: ginkgo|gomega + linters: + - revive + text: "should not use dot imports" diff --git a/Makefile b/Makefile.dontuse similarity index 100% rename from Makefile rename to Makefile.dontuse diff --git a/go.mod b/go.mod index 49f2fe0..2b511a3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/na4ma4/go-permbits -go 1.20 +go 1.22.3 diff --git a/go.work b/go.work new file mode 100644 index 0000000..4fa67e5 --- /dev/null +++ b/go.work @@ -0,0 +1,6 @@ +go 1.22.3 + +use ( + . + ./magefiles +) diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..30ecd27 --- /dev/null +++ b/go.work.sum @@ -0,0 +1 @@ +github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU= diff --git a/magefiles/go.mod b/magefiles/go.mod new file mode 100644 index 0000000..e6b6bd0 --- /dev/null +++ b/magefiles/go.mod @@ -0,0 +1,24 @@ +module github.com/na4ma4/go-permbits/magefiles + +go 1.22.3 + +require ( + github.com/dosquad/mage v0.1.3 + github.com/magefile/mage v1.15.0 +) + +require ( + github.com/VividCortex/ewma v1.1.1 // indirect + github.com/cheggaaa/pb/v3 v3.0.4 // indirect + github.com/dosquad/go-giturl v0.0.0-20240529044908-80f368d13ad5 // indirect + github.com/fatih/color v1.9.0 // indirect + github.com/go-resty/resty/v2 v2.13.1 // indirect + github.com/mattn/go-colorable v0.1.4 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mattn/go-runewidth v0.0.7 // indirect + github.com/na4ma4/go-permbits v0.4.0 // indirect + github.com/princjef/mageutil v1.0.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect +) diff --git a/magefiles/go.sum b/magefiles/go.sum new file mode 100644 index 0000000..d46c874 --- /dev/null +++ b/magefiles/go.sum @@ -0,0 +1,101 @@ +github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM= +github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= +github.com/cheggaaa/pb v2.0.7+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= +github.com/cheggaaa/pb/v3 v3.0.4 h1:QZEPYOj2ix6d5oEg63fbHmpolrnNiwjUsk+h74Yt4bM= +github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dosquad/go-giturl v0.0.0-20240529044908-80f368d13ad5 h1:M8PUEhnrl42ljW+9SIB8qVYx+AMuy65abzsr4s18ZIE= +github.com/dosquad/go-giturl v0.0.0-20240529044908-80f368d13ad5/go.mod h1:1p2odnPipR3BY0nOVyL+M88J5u+eDtfAan0V+8HGtBM= +github.com/dosquad/mage v0.1.3 h1:ZgOsO1cu64lL6+1Jr9mM97Qj42vRrUhw4OxK9szRXR8= +github.com/dosquad/mage v0.1.3/go.mod h1:84jtUHTUanfXrQykb/DaBN23S76COUguBicR5B9ycxA= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= +github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= +github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g= +github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0= +github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= +github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI= +github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= +github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/na4ma4/go-permbits v0.4.0 h1:0+oD8hXc7+1FBEx4sGcnQb+3c3ODdjL6sXgz3MSXGSs= +github.com/na4ma4/go-permbits v0.4.0/go.mod h1:zOAErdXXC0T5YbVFtWAmIAZJCQe9738v2SIXRJDKpdc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/princjef/mageutil v1.0.0 h1:1OfZcJUMsooPqieOz2ooLjI+uHUo618pdaJsbCXcFjQ= +github.com/princjef/mageutil v1.0.0/go.mod h1:mkShhaUomCYfAoVvTKRcbAs8YSVPdtezI5j6K+VXhrs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +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/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/VividCortex/ewma.v1 v1.1.1/go.mod h1:TekXuFipeiHWiAlO1+wSS23vTcyFau5u3rxXUSXj710= +gopkg.in/cheggaaa/pb.v2 v2.0.7/go.mod h1:0CiZ1p8pvtxBlQpLXkHuUTpdJ1shm3OqCF1QugkjHL4= +gopkg.in/fatih/color.v1 v1.7.0/go.mod h1:P7yosIhqIl/sX8J8UypY5M+dDpD2KmyfP5IRs5v/fo0= +gopkg.in/mattn/go-colorable.v0 v0.1.0/go.mod h1:BVJlBXzARQxdi3nZo6f6bnl5yR20/tOL6p+V0KejgSY= +gopkg.in/mattn/go-isatty.v0 v0.0.4/go.mod h1:wt691ab7g0X4ilKZNmMII3egK0bTxl37fEn/Fwbd8gc= +gopkg.in/mattn/go-runewidth.v0 v0.0.4/go.mod h1:BmXejnxvhwdaATwiJbB1vZ2dtXkQKZGu9yLFCZb4msQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/magefiles/magefile.go b/magefiles/magefile.go new file mode 100644 index 0000000..8b6115a --- /dev/null +++ b/magefiles/magefile.go @@ -0,0 +1,20 @@ +//go:build mage + +package main + +import ( + "context" + + "github.com/magefile/mage/mg" + + //mage:import + "github.com/dosquad/mage" +) + +// Install update, protoc, format, tidy, lint & test. +func Install(ctx context.Context) { + mg.CtxDeps(ctx, mage.Lint) + mg.CtxDeps(ctx, mage.Test) +} + +var Default = Install diff --git a/parse.go b/parse.go index a52ca6c..c438b0d 100644 --- a/parse.go +++ b/parse.go @@ -56,7 +56,7 @@ func FromString(perms string) (os.FileMode, error) { mode := []parseMode{} apply := parseApplyNone - for idx := 0; idx < len(perms); idx++ { + for idx := range len(perms) { switch perms[idx] { case 'u': mode = append(mode, parseUser) diff --git a/parse_shell_test.go b/parse_shell_test.go index 1c295e9..fdcff7f 100644 --- a/parse_shell_test.go +++ b/parse_shell_test.go @@ -19,8 +19,10 @@ func shellTest(mode string) (os.FileMode, error) { } name := tmpFile.Name() - tmpFile.Close() + defer func() { + _ = os.Remove(name) + }() cmd := exec.Command(`chmod`, "ugo=", name) if err = cmd.Run(); err != nil { @@ -37,10 +39,6 @@ func shellTest(mode string) (os.FileMode, error) { return 0, err } - if err = os.Remove(name); err != nil { - return 0, err - } - return stv.Mode(), nil } diff --git a/parse_test.go b/parse_test.go index d6a1d53..45f4b30 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,6 +1,7 @@ package permbits_test import ( + "io/fs" "os" "testing" @@ -8,6 +9,8 @@ import ( ) func testSymbolicModes(t *testing.T, modeString string, is uint32) { + t.Helper() + mode, err := permbits.FromString(modeString) if err != nil { t.Errorf("[%s] error occurred: %s", modeString, err) @@ -94,6 +97,8 @@ func TestParseReturnError(t *testing.T) { } func testShellChmod(t *testing.T, modeString string) { + t.Helper() + pbMode, err := permbits.FromString(modeString) if err != nil { t.Errorf("[%s] error occurred: %s", modeString, err) @@ -147,7 +152,7 @@ func TestFromString_ResolveSymbolic(t *testing.T) { t.Errorf("permbits.FromString() error = %v", err) } if mode != tt.is { - t.Errorf("permbits.FromString() = %04o, want = %04o", mode, tt.is) + t.Errorf("permbits.FromString() = 0o%04o, want = 0o%04o", mode, tt.is) } }) } @@ -173,12 +178,12 @@ func TestFromString_ResolveSymbolicExample(t *testing.T) { t.Errorf("permbits.FromString() error = %v", err) } if mode != tt.is { - t.Errorf("permbits.FromString() = %04o, want = %04o", mode, tt.is) + t.Errorf("permbits.FromString() = 0o%04o, want = 0o%04o", mode, tt.is) } pbtest := func(mode, match os.FileMode, expect bool) { if v := permbits.Is(mode, match); v != expect { - t.Errorf("permbits.FromString() = %04o to return %t for %04o, but returned %t", mode, expect, match, v) + t.Errorf("permbits.FromString() = 0o%04o to return %t for 0o%04o, but returned %t", mode, expect, match, v) } } @@ -196,14 +201,26 @@ func TestFromString_ResolveSymbolicExample(t *testing.T) { } func TestFromString_ValidParseNoError(t *testing.T) { - tests := []string{ - "u+rg+ro+r", + tests := []struct { + expect fs.FileMode + modeString string + }{ + {0o0444, "u+rg+ro+r"}, + {0o0440, "u+rg+r"}, } for _, tt := range tests { - t.Run(tt, func(t *testing.T) { - if _, err := permbits.FromString(tt); err != nil { + t.Run(tt.modeString, func(t *testing.T) { + mode, err := permbits.FromString(tt.modeString) + if err != nil { t.Errorf("permbits.FromString() error = %v", err) } + + if mode != tt.expect { + t.Errorf("permbits.FromString(): got '0o%04o', want '0o%04o'", + mode, + tt.expect, + ) + } }) } } diff --git a/permbits.go b/permbits.go index 66d8749..c3a9bce 100644 --- a/permbits.go +++ b/permbits.go @@ -1,6 +1,7 @@ package permbits import ( + "math/bits" "os" ) @@ -11,15 +12,15 @@ const ( UserWrite os.FileMode = 0o200 // UserExecute is execute permissions for the user. UserExecute os.FileMode = 0o100 - // UserAll is all permissions for the user. - UserAll os.FileMode = UserRead + UserWrite + UserExecute ) const ( // UserReadWrite is a helper combination of read and write permissions for the user. - UserReadWrite os.FileMode = 0o600 + UserReadWrite os.FileMode = UserRead + UserWrite // UserReadWriteExecute is a helper combination of read, write and execute permissions for the user. - UserReadWriteExecute os.FileMode = 0o700 + UserReadWriteExecute os.FileMode = UserRead + UserWrite + UserExecute + // UserAll is all permissions for user. + UserAll os.FileMode = UserReadWriteExecute ) const ( @@ -29,15 +30,15 @@ const ( GroupWrite os.FileMode = 0o020 // GroupExecute is execute permissions for the group. GroupExecute os.FileMode = 0o010 - // GroupAll is all permissions for the group. - GroupAll os.FileMode = GroupRead + GroupWrite + GroupExecute ) const ( // GroupReadWrite is a helper combination of read and write permissions for the group. - GroupReadWrite os.FileMode = 0o060 + GroupReadWrite os.FileMode = GroupRead + GroupWrite // GroupReadWriteExecute is a helper combination of read, write and execute permissions for the group. - GroupReadWriteExecute os.FileMode = 0o070 + GroupReadWriteExecute os.FileMode = GroupRead + GroupWrite + GroupExecute + // GroupAll is all permissions for groups. + GroupAll os.FileMode = GroupReadWriteExecute ) const ( @@ -47,18 +48,46 @@ const ( OtherWrite os.FileMode = 0o002 // OtherExecute is execute permissions for others. OtherExecute os.FileMode = 0o001 - // OtherAll is all permissions for others. - OtherAll os.FileMode = OtherRead + OtherWrite + OtherExecute ) const ( // OtherReadWrite is a helper combination of read and write permissions for the others. - OtherReadWrite os.FileMode = 0o006 + OtherReadWrite os.FileMode = OtherRead + OtherWrite // OtherReadWriteExecute is a helper combination of read, write and execute permissions for the others. - OtherReadWriteExecute os.FileMode = 0o007 + OtherReadWriteExecute os.FileMode = OtherRead + OtherWrite + OtherExecute + // OtherAll is all permissions for others. + OtherAll os.FileMode = OtherReadWriteExecute ) // Is compares a supplied os.FileMode and returns true if it contains a reference mode. func Is(mode os.FileMode, is os.FileMode) bool { return (mode & is) == is } + +// Force takes a base mode and then a list of wanted modes, and makes sure the result +// is true for all the wanted modes. +// +// It is more efficient to supply a list of individual modes than a combination mode. +func Force(mode os.FileMode, want ...os.FileMode) os.FileMode { + for _, item := range want { + if item&(item-1) != 0 { + // multiple bits set + for i := range bits.Len(uint(item)) { + if v := item & (1 << i); v != 0 { + if !Is(mode, v) { + mode += v + } + } + } + + continue + } + + // single bit set, so just check it and add it if needed. + if !Is(mode, item) { + mode += item + } + } + + return mode +} diff --git a/permbits_test.go b/permbits_test.go index b5d5e8e..e7d4328 100644 --- a/permbits_test.go +++ b/permbits_test.go @@ -1,6 +1,7 @@ package permbits_test import ( + "fmt" "os" "testing" @@ -90,3 +91,38 @@ func TestIs_CompareMultipleModes(t *testing.T) { }) } } + +func TestForce(t *testing.T) { + t.Parallel() + + tests := []struct { + mode os.FileMode + expect os.FileMode + want []os.FileMode + }{ + { + 0o111, 0o711, + []os.FileMode{permbits.UserReadWriteExecute}, + }, + { + 0o111, 0o711, + []os.FileMode{permbits.UserRead, permbits.UserWrite, permbits.UserExecute}, + }, + { + 0o111, 0o171, + []os.FileMode{permbits.GroupRead, permbits.GroupWrite, permbits.GroupExecute}, + }, + } + for _, tt := range tests { + t.Run( + fmt.Sprintf("%04o + %v = %04o", tt.mode, tt.want, tt.expect), + func(t *testing.T) { + t.Parallel() + + if v := permbits.Force(tt.mode, tt.want...); v != tt.expect { + t.Errorf("permbits.Force() got '%04o', want '%04o'", v, tt.expect) + } + }, + ) + } +} From a99f6ba04cfdc174d1a406936db29812f49f12f2 Mon Sep 17 00:00:00 2001 From: na4ma4 Date: Thu, 30 May 2024 13:02:35 +1000 Subject: [PATCH 2/2] updated GitHub workflow --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2085128..3b54c81 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,7 @@ jobs: unit-test: name: "Unit Test" uses: na4ma4/actions/.github/workflows/unit-test.yml@main + secrets: inherit goreleaser: name: "Goreleaser" @@ -15,5 +16,4 @@ jobs: needs: - unit-test uses: na4ma4/actions/.github/workflows/goreleaser.yml@main - secrets: - token: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }} + secrets: inherit