Skip to content
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

Add golangci + more detailed output to assign, object and remove cmds #43

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
name: Build Warrant CLI
name: Warrant CLI

on:
push:
branches: [master]
pull_request:
branches: [master]
permissions:
contents: read
pull-requests: read
jobs:
build:
ci:
runs-on: ubuntu-latest
steps:
- name: Setup Go Env
Expand All @@ -17,6 +20,10 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Verify Go dependencies
run: go mod verify
- name: Run unit tests
run: go test -v ./...
- name: Build CLI
run: make build
working-directory: cmd/warrant
Expand All @@ -26,3 +33,22 @@ jobs:
distribution: goreleaser
version: latest
args: check
golangci:
runs-on: ubuntu-latest
steps:
- name: Setup Go env
uses: actions/setup-go@v4
with:
go-version: "^1.21.0"
cache: false
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: -v --timeout=5m
only-new-issues: false
install-mode: "binary"
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release Warrant CLI
name: Warrant CLI

on:
push:
Expand Down
91 changes: 91 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
linters:
enable-all: true
disable:
# Deprecated:
- deadcode
- exhaustivestruct
- golint
- ifshort
- interfacer
- maligned
- nosnakecase
- scopelint
- structcheck
- varcheck

# Should review/fix:
# - cyclop
- depguard
- dupl
- dupword
# - errorlint
# - exhaustive
- exhaustruct
# - forcetypeassert
- forbidigo
- funlen
- gci
- gochecknoglobals
# - gochecknoinits
# - gocognit
# - gocritic
# - gocyclo
# - godot
- godox
- goerr113
- gofumpt
- gomnd
# - gosec
- interfacebloat
- ireturn
# - mirror
# - nestif
# - nilerr
# - nilnil
- nlreturn
# - noctx
# - nonamedreturns
# - paralleltest
# - reassign
# Revive needs config:
- revive
- stylecheck
# - tagalign
# - testpackage
# - unconvert
- unparam
- varnamelen
- wrapcheck
- wsl
linters-settings:
goheader:
template: |-
Copyright {{YEAR-RANGE}} Forerunner Labs, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
lll:
line-length: 270
nestif:
min-complexity: 40
cyclop:
max-complexity: 100
gocognit:
min-complexity: 150
gocyclo:
min-complexity: 80
maintidx:
under: 10
issues:
new-from-rev: 46ca9cb0f3fb283113388fcabd762b8c55eda2be
max-issues-per-linter: 0
max-same-issues: 0
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/warrant-dev/warrant-go/v5 v5.2.0
github.com/warrant-dev/warrant-go/v5 v5.3.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/warrant-dev/warrant-go/v5 v5.2.0 h1:5khdIdGhKLN8JeDg5jDj3oGcSpSoh9Q8kWMgD2FL7I8=
github.com/warrant-dev/warrant-go/v5 v5.2.0/go.mod h1:00jaOr9wwpFFqPf8Ol19d38eXNyuDJMMdvyO8lINdIY=
github.com/warrant-dev/warrant-go/v5 v5.3.0 h1:7DipZOCw7EpLffBOzNg+VVoO9klUQTd+zEvpP056XII=
github.com/warrant-dev/warrant-go/v5 v5.3.0/go.mod h1:00jaOr9wwpFFqPf8Ol19d38eXNyuDJMMdvyO8lINdIY=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
15 changes: 14 additions & 1 deletion internal/cmd/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ warrant assign user:56 member role:admin 'domain == warrant.dev'`,
if err != nil {
return err
}
fmt.Println("Created warrant")
fmt.Printf("assigned %s\n", warrantAsString(warrantSpec))

return nil
},
}

func warrantAsString(w *warrant.WarrantParams) string {
subject := fmt.Sprintf("%s:%s", w.Subject.ObjectType, w.Subject.ObjectId)
if w.Subject.Relation != "" {
subject = fmt.Sprintf("%s#%s", subject, w.Subject.Relation)
}
s := fmt.Sprintf("%s %s %s:%s", subject, w.Relation, w.ObjectType, w.ObjectId)
if w.Policy != "" {
s = fmt.Sprintf("%s %s", s, w.Policy)
}

return s
}
35 changes: 31 additions & 4 deletions internal/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -75,23 +76,49 @@ warrant check user:56 member role:admin --assert true`,
return err
}

checkSpecString, err := checkSpecAsString(&checkSpec.WarrantCheck)
if err != nil {
return err
}

if assertFlagVal != "" {
// Assert
if checkResult == assertVal {
fmt.Println(termenv.String(printer.Checkmark + " passed").Foreground(printer.Green))
fmt.Printf("%s %s\n", termenv.String(printer.Checkmark, fmt.Sprintf("assert %t", assertVal)).Foreground(printer.Green), checkSpecString)
} else {
fmt.Println(termenv.String(printer.Cross + " failed").Foreground(printer.Red))
fmt.Printf("%s %s\n", termenv.String(printer.Cross, fmt.Sprintf("assert %t", assertVal)).Foreground(printer.Red), checkSpecString)
os.Exit(1)
}
} else {
// Check
if checkResult {
fmt.Println(termenv.String(printer.Checkmark + " true").Foreground(printer.Green))
fmt.Printf("%s %s\n", termenv.String(printer.Checkmark, "true").Foreground(printer.Green), checkSpecString)
} else {
fmt.Println(termenv.String(printer.Cross + " false").Foreground(printer.Red))
fmt.Printf("%s %s\n", termenv.String(printer.Cross, "false").Foreground(printer.Red), checkSpecString)
}
}

return nil
},
}

func checkSpecAsString(w *warrant.WarrantCheck) (string, error) {
// TODO: should also handle subject relation if present
s := fmt.Sprintf(
"%s:%s %s %s:%s",
w.Subject.GetObjectType(),
w.Subject.GetObjectId(),
w.Relation,
w.Object.GetObjectType(),
w.Object.GetObjectId(),
)
if len(w.Context) > 0 {
bytes, err := json.Marshal(w.Context)
if err != nil {
return "", err
}
s = fmt.Sprintf("%s '%s'", s, string(bytes))
}

return s, nil
}
63 changes: 29 additions & 34 deletions internal/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,52 @@ import (
"github.com/warrant-dev/warrant-cli/internal/reader"
)

var listEnvs bool

func init() {
envCmd.AddCommand(listEnvCmd)
envCmd.Flags().BoolVarP(&listEnvs, "list", "l", false, "list all configured environments")

envCmd.AddCommand(addEnvCmd)
envCmd.AddCommand(removeEnvCmd)
envCmd.AddCommand(switchEnvCmd)

rootCmd.AddCommand(envCmd)
}

var envCmd = &cobra.Command{
Use: "env",
Short: "Get the name of the current active environment",
Long: "Get the name of the current active environment.",
Short: "List configured environment(s)",
Long: "List configured environment(s), including the current active environment.",
Example: `
warrant env`,
warrant env
warrant env --list`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfigOrExit()
fmt.Println(config.ActiveEnvironment)

return nil
},
}
if listEnvs {
if len(config.Environments) == 1 {
fmt.Println(config.ActiveEnvironment)
return nil
}

var listEnvCmd = &cobra.Command{
Use: "list",
Short: "List all configured environments",
Long: "List all configured environments, including the current active environment denoted by a * prefix.",
Example: `
warrant list`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
config := GetConfigOrExit()
envs := make([]string, 0, len(config.Environments))
for k := range config.Environments {
envs = append(envs, k)
}
sort.Strings(envs)
for _, env := range envs {
if env == config.ActiveEnvironment {
fmt.Println(termenv.String("* " + env).Bold())
} else {
fmt.Println(" " + env)
}
}

if len(config.Environments) == 1 {
fmt.Println(config.ActiveEnvironment)
return nil
}

envs := make([]string, 0, len(config.Environments))
for k := range config.Environments {
envs = append(envs, k)
}
sort.Strings(envs)
for _, env := range envs {
if env == config.ActiveEnvironment {
fmt.Println(termenv.String("* " + env).Bold())
} else {
fmt.Println(" " + env)
}
}

fmt.Println(config.ActiveEnvironment)
return nil
},
}
Expand Down Expand Up @@ -132,8 +127,8 @@ warrant remove test`,

var switchEnvCmd = &cobra.Command{
Use: "switch <envName>",
Short: "Switch to the given environment",
Long: "Switch to the given environment, provided it exists in config.",
Short: "Switch to a given environment",
Long: "Switch to a given environment, provided it exists in config.",
Example: `
warrant switch prod`,
Args: cobra.ExactArgs(1),
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ warrant init`,
return err
}

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

return nil
},
Expand Down
Loading