Skip to content

Commit 87c7666

Browse files
authored
Add golangci + more detailed output to assign, object and remove cmds (#43)
* Add golangci + more detailed output to assign, object and remove cmds * Update check cmd output * Update golangci config * Simplify env cmd * Implement objecttype apply cmd * Update to warrant-go v5.3.0 * Rebase with master
1 parent f84a40f commit 87c7666

File tree

12 files changed

+264
-58
lines changed

12 files changed

+264
-58
lines changed

.github/workflows/build.yaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
name: Build Warrant CLI
1+
name: Warrant CLI
22

33
on:
44
push:
55
branches: [master]
66
pull_request:
77
branches: [master]
8+
permissions:
9+
contents: read
10+
pull-requests: read
811
jobs:
9-
build:
12+
ci:
1013
runs-on: ubuntu-latest
1114
steps:
1215
- name: Setup Go Env
@@ -17,6 +20,10 @@ jobs:
1720
uses: actions/checkout@v3
1821
with:
1922
fetch-depth: 2
23+
- name: Verify Go dependencies
24+
run: go mod verify
25+
- name: Run unit tests
26+
run: go test -v ./...
2027
- name: Build CLI
2128
run: make build
2229
working-directory: cmd/warrant
@@ -26,3 +33,22 @@ jobs:
2633
distribution: goreleaser
2734
version: latest
2835
args: check
36+
golangci:
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Setup Go env
40+
uses: actions/setup-go@v4
41+
with:
42+
go-version: "^1.21.0"
43+
cache: false
44+
- name: Checkout
45+
uses: actions/checkout@v3
46+
with:
47+
fetch-depth: 0
48+
- name: Run golangci-lint
49+
uses: golangci/golangci-lint-action@v3
50+
with:
51+
version: latest
52+
args: -v --timeout=5m
53+
only-new-issues: false
54+
install-mode: "binary"

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Release Warrant CLI
1+
name: Warrant CLI
22

33
on:
44
push:

.golangci.yaml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
linters:
2+
enable-all: true
3+
disable:
4+
# Deprecated:
5+
- deadcode
6+
- exhaustivestruct
7+
- golint
8+
- ifshort
9+
- interfacer
10+
- maligned
11+
- nosnakecase
12+
- scopelint
13+
- structcheck
14+
- varcheck
15+
16+
# Should review/fix:
17+
# - cyclop
18+
- depguard
19+
- dupl
20+
- dupword
21+
# - errorlint
22+
# - exhaustive
23+
- exhaustruct
24+
# - forcetypeassert
25+
- forbidigo
26+
- funlen
27+
- gci
28+
- gochecknoglobals
29+
# - gochecknoinits
30+
# - gocognit
31+
# - gocritic
32+
# - gocyclo
33+
# - godot
34+
- godox
35+
- goerr113
36+
- gofumpt
37+
- gomnd
38+
# - gosec
39+
- interfacebloat
40+
- ireturn
41+
# - mirror
42+
# - nestif
43+
# - nilerr
44+
# - nilnil
45+
- nlreturn
46+
# - noctx
47+
# - nonamedreturns
48+
# - paralleltest
49+
# - reassign
50+
# Revive needs config:
51+
- revive
52+
- stylecheck
53+
# - tagalign
54+
# - testpackage
55+
# - unconvert
56+
- unparam
57+
- varnamelen
58+
- wrapcheck
59+
- wsl
60+
linters-settings:
61+
goheader:
62+
template: |-
63+
Copyright {{YEAR-RANGE}} Forerunner Labs, Inc.
64+
65+
Licensed under the Apache License, Version 2.0 (the "License");
66+
you may not use this file except in compliance with the License.
67+
You may obtain a copy of the License at
68+
69+
http://www.apache.org/licenses/LICENSE-2.0
70+
71+
Unless required by applicable law or agreed to in writing, software
72+
distributed under the License is distributed on an "AS IS" BASIS,
73+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74+
See the License for the specific language governing permissions and
75+
limitations under the License.
76+
lll:
77+
line-length: 270
78+
nestif:
79+
min-complexity: 40
80+
cyclop:
81+
max-complexity: 100
82+
gocognit:
83+
min-complexity: 150
84+
gocyclo:
85+
min-complexity: 80
86+
maintidx:
87+
under: 10
88+
issues:
89+
new-from-rev: 46ca9cb0f3fb283113388fcabd762b8c55eda2be
90+
max-issues-per-linter: 0
91+
max-same-issues: 0

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/pkg/errors v0.9.1
88
github.com/spf13/cobra v1.7.0
99
github.com/spf13/viper v1.17.0
10-
github.com/warrant-dev/warrant-go/v5 v5.2.0
10+
github.com/warrant-dev/warrant-go/v5 v5.3.0
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
199199
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
200200
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
201201
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
202-
github.com/warrant-dev/warrant-go/v5 v5.2.0 h1:5khdIdGhKLN8JeDg5jDj3oGcSpSoh9Q8kWMgD2FL7I8=
203-
github.com/warrant-dev/warrant-go/v5 v5.2.0/go.mod h1:00jaOr9wwpFFqPf8Ol19d38eXNyuDJMMdvyO8lINdIY=
202+
github.com/warrant-dev/warrant-go/v5 v5.3.0 h1:7DipZOCw7EpLffBOzNg+VVoO9klUQTd+zEvpP056XII=
203+
github.com/warrant-dev/warrant-go/v5 v5.3.0/go.mod h1:00jaOr9wwpFFqPf8Ol19d38eXNyuDJMMdvyO8lINdIY=
204204
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
205205
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
206206
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

internal/cmd/assign.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,21 @@ warrant assign user:56 member role:admin 'domain == warrant.dev'`,
4646
if err != nil {
4747
return err
4848
}
49-
fmt.Println("Created warrant")
49+
fmt.Printf("assigned %s\n", warrantAsString(warrantSpec))
5050

5151
return nil
5252
},
5353
}
54+
55+
func warrantAsString(w *warrant.WarrantParams) string {
56+
subject := fmt.Sprintf("%s:%s", w.Subject.ObjectType, w.Subject.ObjectId)
57+
if w.Subject.Relation != "" {
58+
subject = fmt.Sprintf("%s#%s", subject, w.Subject.Relation)
59+
}
60+
s := fmt.Sprintf("%s %s %s:%s", subject, w.Relation, w.ObjectType, w.ObjectId)
61+
if w.Policy != "" {
62+
s = fmt.Sprintf("%s %s", s, w.Policy)
63+
}
64+
65+
return s
66+
}

internal/cmd/check.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package cmd
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"os"
2021
"strconv"
@@ -75,23 +76,49 @@ warrant check user:56 member role:admin --assert true`,
7576
return err
7677
}
7778

79+
checkSpecString, err := checkSpecAsString(&checkSpec.WarrantCheck)
80+
if err != nil {
81+
return err
82+
}
83+
7884
if assertFlagVal != "" {
7985
// Assert
8086
if checkResult == assertVal {
81-
fmt.Println(termenv.String(printer.Checkmark + " passed").Foreground(printer.Green))
87+
fmt.Printf("%s %s\n", termenv.String(printer.Checkmark, fmt.Sprintf("assert %t", assertVal)).Foreground(printer.Green), checkSpecString)
8288
} else {
83-
fmt.Println(termenv.String(printer.Cross + " failed").Foreground(printer.Red))
89+
fmt.Printf("%s %s\n", termenv.String(printer.Cross, fmt.Sprintf("assert %t", assertVal)).Foreground(printer.Red), checkSpecString)
8490
os.Exit(1)
8591
}
8692
} else {
8793
// Check
8894
if checkResult {
89-
fmt.Println(termenv.String(printer.Checkmark + " true").Foreground(printer.Green))
95+
fmt.Printf("%s %s\n", termenv.String(printer.Checkmark, "true").Foreground(printer.Green), checkSpecString)
9096
} else {
91-
fmt.Println(termenv.String(printer.Cross + " false").Foreground(printer.Red))
97+
fmt.Printf("%s %s\n", termenv.String(printer.Cross, "false").Foreground(printer.Red), checkSpecString)
9298
}
9399
}
94100

95101
return nil
96102
},
97103
}
104+
105+
func checkSpecAsString(w *warrant.WarrantCheck) (string, error) {
106+
// TODO: should also handle subject relation if present
107+
s := fmt.Sprintf(
108+
"%s:%s %s %s:%s",
109+
w.Subject.GetObjectType(),
110+
w.Subject.GetObjectId(),
111+
w.Relation,
112+
w.Object.GetObjectType(),
113+
w.Object.GetObjectId(),
114+
)
115+
if len(w.Context) > 0 {
116+
bytes, err := json.Marshal(w.Context)
117+
if err != nil {
118+
return "", err
119+
}
120+
s = fmt.Sprintf("%s '%s'", s, string(bytes))
121+
}
122+
123+
return s, nil
124+
}

internal/cmd/env.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,52 @@ import (
2424
"github.com/warrant-dev/warrant-cli/internal/reader"
2525
)
2626

27+
var listEnvs bool
28+
2729
func init() {
28-
envCmd.AddCommand(listEnvCmd)
30+
envCmd.Flags().BoolVarP(&listEnvs, "list", "l", false, "list all configured environments")
31+
2932
envCmd.AddCommand(addEnvCmd)
3033
envCmd.AddCommand(removeEnvCmd)
3134
envCmd.AddCommand(switchEnvCmd)
35+
3236
rootCmd.AddCommand(envCmd)
3337
}
3438

3539
var envCmd = &cobra.Command{
3640
Use: "env",
37-
Short: "Get the name of the current active environment",
38-
Long: "Get the name of the current active environment.",
41+
Short: "List configured environment(s)",
42+
Long: "List configured environment(s), including the current active environment.",
3943
Example: `
40-
warrant env`,
44+
warrant env
45+
warrant env --list`,
4146
Args: cobra.NoArgs,
4247
RunE: func(cmd *cobra.Command, args []string) error {
4348
config := GetConfigOrExit()
44-
fmt.Println(config.ActiveEnvironment)
4549

46-
return nil
47-
},
48-
}
50+
if listEnvs {
51+
if len(config.Environments) == 1 {
52+
fmt.Println(config.ActiveEnvironment)
53+
return nil
54+
}
4955

50-
var listEnvCmd = &cobra.Command{
51-
Use: "list",
52-
Short: "List all configured environments",
53-
Long: "List all configured environments, including the current active environment denoted by a * prefix.",
54-
Example: `
55-
warrant list`,
56-
Args: cobra.NoArgs,
57-
RunE: func(cmd *cobra.Command, args []string) error {
58-
config := GetConfigOrExit()
56+
envs := make([]string, 0, len(config.Environments))
57+
for k := range config.Environments {
58+
envs = append(envs, k)
59+
}
60+
sort.Strings(envs)
61+
for _, env := range envs {
62+
if env == config.ActiveEnvironment {
63+
fmt.Println(termenv.String("* " + env).Bold())
64+
} else {
65+
fmt.Println(" " + env)
66+
}
67+
}
5968

60-
if len(config.Environments) == 1 {
61-
fmt.Println(config.ActiveEnvironment)
6269
return nil
6370
}
6471

65-
envs := make([]string, 0, len(config.Environments))
66-
for k := range config.Environments {
67-
envs = append(envs, k)
68-
}
69-
sort.Strings(envs)
70-
for _, env := range envs {
71-
if env == config.ActiveEnvironment {
72-
fmt.Println(termenv.String("* " + env).Bold())
73-
} else {
74-
fmt.Println(" " + env)
75-
}
76-
}
77-
72+
fmt.Println(config.ActiveEnvironment)
7873
return nil
7974
},
8075
}
@@ -132,8 +127,8 @@ warrant remove test`,
132127

133128
var switchEnvCmd = &cobra.Command{
134129
Use: "switch <envName>",
135-
Short: "Switch to the given environment",
136-
Long: "Switch to the given environment, provided it exists in config.",
130+
Short: "Switch to a given environment",
131+
Long: "Switch to a given environment, provided it exists in config.",
137132
Example: `
138133
warrant switch prod`,
139134
Args: cobra.ExactArgs(1),

internal/cmd/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ warrant init`,
3939
return err
4040
}
4141

42-
fmt.Println("Creating ~/.warrant.json")
42+
fmt.Println("creating ~/.warrant.json")
4343
envMap := make(map[string]config.Environment)
4444
envMap[envName] = *env
4545
newConfig := config.Config{
@@ -50,7 +50,7 @@ warrant init`,
5050
if err != nil {
5151
return err
5252
}
53-
fmt.Println("Setup complete")
53+
fmt.Println("setup complete")
5454

5555
return nil
5656
},

0 commit comments

Comments
 (0)