Skip to content

Commit

Permalink
add task setup to taskfile and lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Sep 15, 2023
1 parent f88b6d5 commit 61e4018
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 59 deletions.
59 changes: 56 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ tasks:
desc: Formatting and linting
dir: "{{.SRC_DIR}}"
cmds:
- gofmt -d .
- gofumpt -d .
- go vet ./...
- golangci-lint run

lintfix:
desc: Fix formatting and linting
dir: "{{.SRC_DIR}}"
cmds:
- gofmt -w .
- gofumpt -w .
- go mod tidy
- golangci-lint run --fix

setup:
desc: Setup linter, formatter, etc. for local testing and CI
cmds:
- cmd: echo "Installing development tools..."
silent: true
- task: install-changie
- task: install-gofumpt
- task: install-golangci-lint
- cmd: echo "Development tools installed!"
silent: true
- task: workspace

test:
desc: Run tests
dir: "{{.SRC_DIR}}"
Expand All @@ -40,7 +52,48 @@ tasks:
desc: Setup workspace for cli & opslevel-go development
dir: "{{.SRC_DIR}}"
cmds:
- cmd: echo "Setting up opslevel-go workspace..."
silent: true
- git submodule update --init
- go work init || exit 0
- go work use . submodules/opslevel-go
- echo "Workspace ready!"
- cmd: echo "opslevel-go workspace ready!"
silent: true

# internal (not directly called) tasks

go-install-tool:
desc: go install '{{.GO_TOOL}}' and set GOBIN if not set
internal: true
silent: true
vars:
IS_TOOL_INSTALLED:
sh: which {{.GO_TOOL}} > /dev/null || echo "1"
cmds:
- test -z "{{.IS_TOOL_INSTALLED}}" || echo "Installing {{.GO_TOOL}}..."
- test -z "{{.IS_TOOL_INSTALLED}}" || go install {{.GO_TOOL_PATH}}
- test -n $(go env GOBIN) || go env -w GOBIN=$(go env GOPATH)/bin
- echo " '{{.GO_TOOL}}' is installed."
requires:
vars: [GO_TOOL, GO_TOOL_PATH]

install-changie:
desc: go install "changie"
internal: true
cmds:
- task: go-install-tool
vars: { GO_TOOL: "changie", GO_TOOL_PATH: "github.com/miniscruff/changie@latest" }

install-gofumpt:
desc: go install "gofumpt"
internal: true
cmds:
- task: go-install-tool
vars: { GO_TOOL: "gofumpt", GO_TOOL_PATH: "mvdan.cc/gofumpt@latest" }

install-golangci-lint:
desc: go install "golangci-lint"
internal: true
cmds:
- task: go-install-tool
vars: { GO_TOOL: "golangci-lint", GO_TOOL_PATH: "github.com/golangci/golangci-lint/cmd/golangci-lint@latest" }
10 changes: 5 additions & 5 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ service:
- '{"environment": .spec.template.metadata.labels.environment}'
tools:
- '{"category": "other", "environment": "production", "displayName": "my-cool-tool", "url": .metadata.annotations."example.com/my-cool-tool"} | if .url then . else empty end'
# find annotations with format: opslevel.com/tools.<category>.<displayname>: <url>
# find annotations with format: opslevel.com/tools.<category>.<displayname>: <url>
- '.metadata.annotations | to_entries | map(select(.key | startswith("opslevel.com/tools"))) | map({"category": .key | split(".")[2], "displayName": .key | split(".")[3], "url": .value})'
# OR find annotations with format: opslevel.com/tools.<category>.<environment>.<displayname>: <url>
# OR find annotations with format: opslevel.com/tools.<category>.<environment>.<displayname>: <url>
# - '.metadata.annotations | to_entries | map(select(.key | startswith("opslevel.com/tools"))) | map({"category": .key | split(".")[2], "environment": .key | split(".")[3], "displayName": .key | split(".")[4], "url": .value})'
repositories: # attach repositories to the service using the opslevel repo alias - IE github.com:hashicorp/vault
- '{"name": "My Cool Repo", "directory": "/", "repo": .metadata.annotations.repo} | if .repo then . else empty end'
# if just the alias is returned as a single string we'll build the name for you and set the directory to "/"
- .metadata.annotations.repo
# find annotations with format: opslevel.com/repo.<displayname>.<repo.subpath.dots.turned.to.forwardslash>: <opslevel repo alias>
# find annotations with format: opslevel.com/repo.<displayname>.<repo.subpath.dots.turned.to.forwardslash>: <opslevel repo alias>
- '.metadata.annotations | to_entries | map(select(.key | startswith("opslevel.com/repos"))) | map({"name": .key | split(".")[2], "directory": .key | split(".")[3:] | join("/"), "repo": .value})'
collect:
- selector: # This limits what data we look at in Kubernetes
Expand Down Expand Up @@ -148,7 +148,7 @@ func init() {

func getSample(simple bool) string {
var sample []byte
if simple == true {
if simple {
sample = configSimple
} else {
sample = configSample
Expand All @@ -166,5 +166,5 @@ func getSample(simple bool) string {
if encodeErr != nil {
return encodeErr.Error()
}
return string(b.Bytes())
return b.String()
}
14 changes: 10 additions & 4 deletions src/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
}

func runPreview(cmd *cobra.Command, args []string) {
var samples = 5
samples := 5
if len(args) > 0 {
if parsedSamples, err := strconv.Atoi(args[0]); err == nil {
samples = parsedSamples
Expand All @@ -51,7 +51,9 @@ func runPreview(cmd *cobra.Command, args []string) {
samples = servicesCount
}

if IsTextOutput() { fmt.Print("The following data was found in your Kubernetes cluster ...\n\n") }
if IsTextOutput() {
fmt.Print("The following data was found in your Kubernetes cluster ...\n\n")
}
if len(services) == 0 {
fmt.Printf("[]\n")
} else {
Expand All @@ -61,11 +63,15 @@ func runPreview(cmd *cobra.Command, args []string) {
}
fmt.Printf("%s\n", string(prettyJSON))
if samples < servicesCount {
if IsTextOutput() { fmt.Printf("\nShowing %v / %v resources\n", samples, servicesCount) }
if IsTextOutput() {
fmt.Printf("\nShowing %v / %v resources\n", samples, servicesCount)
}
}
}

if IsTextOutput() { fmt.Println("\nIf you're happy with the above data you can reconcile it with OpsLevel by running:\n\n OPSLEVEL_API_TOKEN=XXX kubectl opslevel service import\n\nOtherwise, please adjust the config file and rerun this command") }
if IsTextOutput() {
fmt.Println("\nIf you're happy with the above data you can reconcile it with OpsLevel by running:\n\n OPSLEVEL_API_TOKEN=XXX kubectl opslevel service import\n\nOtherwise, please adjust the config file and rerun this command")
}
}

func sample(data []common.ServiceRegistration, samples int) []common.ServiceRegistration {
Expand Down
12 changes: 6 additions & 6 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"fmt"
"github.com/go-resty/resty/v2"
"os"
"runtime"
"strings"
"time"

"github.com/go-resty/resty/v2"

"github.com/opslevel/opslevel-go/v2023"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -118,7 +119,7 @@ func setupLogging() {
}

func setupOutput() {
outputFormat := strings.ToLower(viper.GetString("output"))
outputFormat = strings.ToLower(viper.GetString("output"))
if outputFormat != "json" {
outputFormat = "text"
}
Expand All @@ -140,10 +141,9 @@ func setupConcurrency() {
// setupAPIToken evaluates several API token sources and sets the preferred token based on precedence.
//
// Precedence:
// 1. --api-token
// 2. --api-token-path
// 3. OL_APITOKEN
//
// 1. --api-token
// 2. --api-token-path
// 3. OL_APITOKEN
func setupAPIToken() {
const key = "api-token"

Expand Down
4 changes: 2 additions & 2 deletions src/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func containsAllTags(tagAssigns []opslevel.TagInput, serviceTags []opslevel.Tag)
}
}
for _, value := range found {
if value == false {
if !value {
return false
}
}
Expand All @@ -229,7 +229,7 @@ func assignTags(client *opslevel.Client, registration ServiceRegistration, servi
if registration.TagAssigns == nil {
return
}
if containsAllTags(registration.TagAssigns, service.Tags.Nodes) == false {
if !containsAllTags(registration.TagAssigns, service.Tags.Nodes) {
tags := map[string]string{}
for _, tagAssign := range registration.TagAssigns {
tags[tagAssign.Key] = tagAssign.Value
Expand Down
4 changes: 2 additions & 2 deletions src/common/jqparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (parser *JQParser) ParseMulti(field string, data []byte) *JQResponseMulti {
}

func (resp *JQResponse) Unmarshal() {
//fmt.Printf("Unmarshaling '%s'\n", string(resp.Bytes))
// fmt.Printf("Unmarshaling '%s'\n", string(resp.Bytes))
if string(resp.Bytes) == "" {
resp.Type = Empty
return
Expand Down Expand Up @@ -138,7 +138,7 @@ func (resp *JQResponse) Unmarshal() {
}

func (resp *JQResponseMulti) Unmarshal() {
//fmt.Printf("Unmarshaling '%s'\n", string(resp.Bytes))
// fmt.Printf("Unmarshaling '%s'\n", string(resp.Bytes))
if string(resp.Bytes) == "[]" {
resp.Objects = nil
return
Expand Down
33 changes: 12 additions & 21 deletions src/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,14 @@ func (s *ServiceRegistration) mergeData(o ServiceRegistration) {
if s.Framework == "" {
s.Framework = o.Framework
}
for _, alias := range o.Aliases {
s.Aliases = append(s.Aliases, alias)
}
s.Aliases = append(s.Aliases, o.Aliases...)
s.Aliases = removeDuplicates(s.Aliases)
for _, tag := range removeOverlappedKeys(s.TagAssigns, o.TagAssigns) {
s.TagAssigns = append(s.TagAssigns, tag)
}
for _, tag := range o.TagCreates {
s.TagCreates = append(s.TagCreates, tag)
}
s.TagAssigns = append(s.TagAssigns, removeOverlappedKeys(s.TagAssigns, o.TagAssigns)...)
s.TagCreates = append(s.TagCreates, o.TagCreates...)
s.TagAssigns = removeDuplicatesFromTagInputList(s.TagAssigns)
s.TagAssigns = removeOverlappedKeys(s.TagAssigns, s.TagCreates)
for _, tool := range o.Tools {
s.Tools = append(s.Tools, tool)
}
for _, repo := range o.Repositories {
s.Repositories = append(s.Repositories, repo)
}
s.Tools = append(s.Tools, o.Tools...)
s.Repositories = append(s.Repositories, o.Repositories...)
}

func parseField(field string, filter string, resources []byte) *JQResponseMulti {
Expand Down Expand Up @@ -106,7 +96,7 @@ func contains(item opslevel.TagInput, data []opslevel.TagInput) bool {
func removeDuplicatesFromTagInputList(data []opslevel.TagInput) []opslevel.TagInput {
unique := []opslevel.TagInput{}
for _, entry := range data {
if contains(entry, unique) == false {
if !contains(entry, unique) {
unique = append(unique, entry)
}
}
Expand Down Expand Up @@ -155,7 +145,7 @@ func removeOverlappedKeys(source []opslevel.TagInput, check []opslevel.TagInput)
break
}
}
if foundMatch == false {
if !foundMatch {
output = append(output, tagAssign)
}
}
Expand Down Expand Up @@ -342,9 +332,11 @@ func getRepositories(index int, data []*JQResponseMulti) []opslevel.ServiceRepos
return output
}

var StartArray []byte = []byte(`[`)
var EndArray []byte = []byte(`]`)
var JoinItem []byte = []byte(`,`)
var (
StartArray []byte = []byte(`[`)
EndArray []byte = []byte(`]`)
JoinItem []byte = []byte(`,`)
)

func joinResourceBytes(resources [][]byte) []byte {
var output []byte
Expand Down Expand Up @@ -382,7 +374,6 @@ func anyIsTrue(resourceIndex int, filters []*JQResponseMulti) bool {
}
}
return false

}

func FilterResources(selector k8sutils.KubernetesSelector, resources [][]byte) [][]byte {
Expand Down
7 changes: 2 additions & 5 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"errors"
"fmt"

"github.com/opslevel/kubectl-opslevel/k8sutils"
Expand All @@ -10,9 +9,7 @@ import (
"github.com/spf13/viper"
)

var (
ConfigCurrentVersion = "1.2.0"
)
var ConfigCurrentVersion = "1.2.0"

type TagRegistrationConfig struct {
Assign []string `json:"assign"` // JQ expressions that return a single string or a map[string]string
Expand Down Expand Up @@ -61,7 +58,7 @@ func New() (*Config, error) {
v := &ConfigVersion{}
viper.Unmarshal(&v)
if v.Version != ConfigCurrentVersion {
return nil, errors.New(fmt.Sprintf("Supported config version is '%s' but found '%s' | Please update config file or create a new sample with `kubectl opslevel config sample`", ConfigCurrentVersion, v.Version))
return nil, fmt.Errorf("Supported config version is '%s' but found '%s' | Please update config file or create a new sample with `kubectl opslevel config sample`", ConfigCurrentVersion, v.Version)
}

c := &Config{}
Expand Down
17 changes: 8 additions & 9 deletions src/jq/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os/exec"
"strings"
Expand Down Expand Up @@ -72,17 +71,17 @@ func (jq *JQ) Commandline() string {
func (jq *JQ) Run(json []byte) ([]byte, *JQError) {
var stderr bytes.Buffer
ctx, cancel := context.WithTimeout(context.Background(), jq.timeout)
//fmt.Printf("Exec: `jq %s`\n", strings.Join(jq.options, " "))
// fmt.Printf("Exec: `jq %s`\n", strings.Join(jq.options, " "))
cmd := exec.CommandContext(ctx, "jq", jq.options...)
cmd.Stdin = bytes.NewBuffer(json)
cmd.Stderr = &stderr
cmd.Env = make([]string, 0)
defer cancel()
out, err := cmd.Output()
if err != nil {
//fmt.Println("Got Error on JQ Execution")
//fmt.Println(err.Error())
//fmt.Println(string(stderr.Bytes()))
// fmt.Println("Got Error on JQ Execution")
// fmt.Println(err.Error())
// fmt.Println(string(stderr.Bytes()))
// TODO: printing out that it couldn't find JQ binary
if err.Error() == "exit status 2" {
return nil, &JQError{Message: jq.Commandline(), Type: BadOptions}
Expand All @@ -94,9 +93,9 @@ func (jq *JQ) Run(json []byte) ([]byte, *JQError) {
return nil, &JQError{Message: string(json), Type: BadJSON}
}
if err.Error() == "exit status 5" {
return nil, &JQError{Message: string(stderr.Bytes()), Type: BadExcution}
return nil, &JQError{Message: stderr.String(), Type: BadExcution}
}
return nil, &JQError{Message: string(stderr.Bytes()), Type: BadExcution}
return nil, &JQError{Message: stderr.String(), Type: BadExcution}
}
return out, nil
}
Expand Down Expand Up @@ -129,11 +128,11 @@ func NewWithOptions(filter string, timeout time.Duration, options []JQOpt) JQ {
opts = append(opts, fmt.Sprintf("--%s", opt.Name))
}
}
opts = append(opts, fmt.Sprintf("%s", filter))
opts = append(opts, filter)
jq := &JQ{
options: opts,
timeout: timeout,
writer: ioutil.Discard,
writer: io.Discard,
}
return *jq
}
1 change: 1 addition & 0 deletions src/k8sutils/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (c *KubernetesController) processNextItem() bool {

return true
}

func (c *KubernetesController) mainloop() {
for c.processNextItem() {
}
Expand Down
Loading

0 comments on commit 61e4018

Please sign in to comment.