Skip to content

Commit

Permalink
feat: add ci and fix golint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeztk committed Aug 4, 2024
1 parent f041a3c commit 9b7f6b7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Go CI

on:
push:
branches: ['*']
pull_request:
branches: ['*']

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2

- name: Verify dependencies
run: go mod verify

- name: Run go build
run: go build -v ./...

- name: Run go vet
run: go vet ./...

- uses: dominikh/[email protected]
with:
version: "2022.1.3"

- name: Install golint
run: go install golang.org/x/lint/golint@latest

- name: Run golint
run: golint ./...
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# kubectl-cautious

<!-- # TODO: add exhaustive description -->
[![go report](https://goreportcard.com/badge/github.com/fedeztk/kubectl-cautious)](https://goreportcard.com/report/github.com/fedeztk/kubectl-cautious)
![CI/CD](https://github.com/fedeztk/kubectl-cautious/actions/workflows/go.yaml/badge.svg)

Cautiously run kubectl commands, no more accidental deletions!

Expand Down Expand Up @@ -59,4 +60,4 @@ This plugin was inspired by [kubectl-guardrails](https://github.com/theelderbeev

I decided to rewrite this plugin in `go`, adding two main features:
- Support for regexes in context names
- Preservation of standard input for kubectl commands (useful for `apply -f -`)
- Preservation of standard input for kubectl commands (useful for `apply -f -`)
2 changes: 2 additions & 0 deletions cmd/plugin/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/viper"
)

// RootCmd returns the root command for the plugin
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: `cautious [kubectl command]`,
Expand Down Expand Up @@ -55,6 +56,7 @@ at $HOME/.kube/cautious.yaml after running the plugin for the first time. Suppor
return cmd
}

// InitAndExecute initializes the plugin and executes the root
func InitAndExecute() {
if err := RootCmd().Execute(); err != nil {
// log only if the error is not a kubectl error
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ import (
// - name: delete
// dry-run: true

// Config holds the Contexts for the plugin
type Config struct {
Contexts []Context
}

// Context holds the configuration for a specific context
type Context struct {
Name string
Actions []Action
}

// Action represents a kube action and whether it should be a dry-run
type Action struct {
Name string
DryRun bool `mapstructure:"dry-run" yaml:"dry-run"`
}

// ReadConfig reads the cautious.yaml
func ReadConfig() (*Config, error) {
viper.SetConfigName("cautious") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/fedeztk/kubectl-cautious/pkg/config"
)

// ErrKubectl is returned when kubectl returns an error
var ErrKubectl = errors.New("kubectl error")

// RunPlugin runs the plugin with the given configuration and arguments for kubectl
func RunPlugin(conf *config.Config, args []string) error {
kubeconfig, err := getKubeconfigPath()
if err != nil {
Expand Down

0 comments on commit 9b7f6b7

Please sign in to comment.