Skip to content

Commit

Permalink
refactor: refine all pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Sep 12, 2023
1 parent 0d08c87 commit a301037
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/
build/
coverage.out
tmp/
*.out
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GOSOURCE_PATHS ?= ./...

include go.mk

5 changes: 1 addition & 4 deletions core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package status
package kstatus

import (
"fmt"
Expand Down
5 changes: 1 addition & 4 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

// Package kstatus contains functionality for computing the status
// of Kubernetes resources.
//
Expand Down Expand Up @@ -40,4 +37,4 @@
// )
//
// err := status.Augment(resource)
package status
package kstatus
17 changes: 6 additions & 11 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package status_test
package kstatus

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/testutil"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/yaml"
)

Expand All @@ -34,12 +29,12 @@ status:
- type: Available
status: "True"
`
deployment := testutil.YamlToUnstructured(t, deploymentManifest)
deployment := YamlToUnstructured(t, deploymentManifest)

res, err := status.Compute(deployment)
res, err := Compute(deployment)
assert.NoError(t, err)

assert.Equal(t, status.Status("Current"), res.Status)
assert.Equal(t, Status("Current"), res.Status)
}

func TestExampleAugment(t *testing.T) {
Expand All @@ -63,9 +58,9 @@ status:
- type: Available
status: "True"
`
deployment := testutil.YamlToUnstructured(t, deploymentManifest)
deployment := YamlToUnstructured(t, deploymentManifest)

err := status.Augment(deployment)
err := Augment(deployment)
assert.NoError(t, err)

b, err := yaml.Marshal(deployment.Object)
Expand Down
5 changes: 1 addition & 4 deletions generic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package status
package kstatus

import (
"fmt"
Expand Down
61 changes: 61 additions & 0 deletions go.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# go.mk is a Go project general Makefile, encapsulated some common Target.
# Project repository: https://github.com/elliotxx/go-makefile

APPROOT ?= $(shell basename $(PWD))
GOPKG ?= $(shell go list 2>/dev/null)
GOPKGS ?= $(shell go list ./... 2>/dev/null)
GOSOURCES ?= $(shell find . -type f -name '*.go' ! -path '*Godeps/_workspace*')
# You can also customize GOSOURCE_PATHS, e.g. ./pkg/... ./cmd/...
GOSOURCE_PATHS ?= ././...
COVERAGEOUT ?= coverage.out
COVERAGETMP ?= coverage.tmp


# Go tools
GOFORMATER ?= gofumpt
GOFORMATER_VERSION ?= v0.2.0
GOLINTER ?= golangci-lint
GOLINTER_VERSION ?= v1.52.2


# To generate help information
.DEFAULT_GOAL := help
.PHONY: help
help: ## This help message :)
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' go.mk | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


# If you encounter an error like "panic: permission denied" on MacOS,
# please visit https://github.com/eisenxp/macos-golink-wrapper to find the solution.
.PHONY: test
test: ## Run the tests
go test -gcflags=all=-l -timeout=10m `go list $(GOSOURCE_PATHS)` ${TEST_FLAGS}

.PHONY: cover
cover: ## Generates coverage report
go test -gcflags=all=-l -timeout=10m `go list $(GOSOURCE_PATHS)` -coverprofile $(COVERAGEOUT) ${TEST_FLAGS}

.PHONY: cover-html
cover-html: ## Generates coverage report and displays it in the browser
go tool cover -html=$(COVERAGEOUT)

.PHONY: format
format: ## Format source code
@which $(GOFORMATER) > /dev/null || (echo "Installing $(GOFORMATER)@$(GOFORMATER_VERSION) ..."; go install mvdan.cc/gofumpt@$(GOFORMATER_VERSION) && echo -e "Installation complete!\n")
@for path in $(GOSOURCE_PATHS); do ${GOFORMATER} -l -w -e `echo $${path} | cut -b 3- | rev | cut -b 5- | rev`; done;

.PHONY: lint
lint: ## Lint, will not fix but sets exit code on error
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS)

.PHONY: lint-fix
lint-fix: ## Lint, will try to fix errors and modify code
@which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n")
$(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS) --fix

.PHONY: doc
doc: ## Start the documentation server with godoc
@which godoc > /dev/null || (echo "Installing godoc@latest ..."; go install golang.org/x/tools/cmd/godoc@latest && echo -e "Installation complete!\n")
godoc -http=:6060
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ go 1.19

require (
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.1
k8s.io/apimachinery v0.28.1
sigs.k8s.io/cli-utils v0.35.0
k8s.io/api v0.24.3
k8s.io/apimachinery v0.24.3
sigs.k8s.io/yaml v1.3.0
)

Expand Down
Loading

0 comments on commit a301037

Please sign in to comment.