Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Commit 0f74f2d

Browse files
authored
Release v0.0.1 (#4)
* Add version subcommand (add cicd for testing and code quality and version command #1) * Add gcp deploy for go codebase (#2)
1 parent 1f75d45 commit 0f74f2d

File tree

10 files changed

+975
-1
lines changed

10 files changed

+975
-1
lines changed

.github/workflows/cicd.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: CI-CD
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- development
8+
pull_request:
9+
branches:
10+
- main
11+
- development
12+
13+
jobs:
14+
unit-testing:
15+
name: 🧪 unit testing
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
go-version: ['1.21', '1.22']
20+
21+
steps:
22+
- name: Checkout code into go module directory
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Go ${{ matrix.go-version }}
26+
uses: actions/setup-go@v4
27+
with:
28+
go-version: ${{ matrix.go-version }}
29+
id: Go
30+
31+
- name: Get dependencies
32+
run: |
33+
go mod download
34+
35+
- name: Test
36+
run: |
37+
export APP_ENV=test
38+
go test ./... -v -coverprofile profile.cov -coverpkg=./...
39+
go tool cover -func profile.cov
40+
41+
- name: Parse code-coverage value
42+
run: |
43+
codeCoverage=$(go tool cover -func=profile.cov | grep total | awk '{print $3}')
44+
codeCoverage=${codeCoverage%?}
45+
echo $codeCoverage:
46+
47+
code_quality:
48+
name: 🎖Code Quality️
49+
runs-on: ubuntu-latest
50+
steps:
51+
- name: Checkout code
52+
uses: actions/checkout@v4
53+
54+
- name: Install Go 1.21
55+
uses: actions/setup-go@v4
56+
with:
57+
go-version: 1.21
58+
59+
- name: golangci-lint
60+
uses: golangci/golangci-lint-action@v3
61+
with:
62+
version: v1.57.2
63+
args: --timeout=9m

.golangci.yml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
linters-settings:
2+
dupl:
3+
threshold: 100
4+
exhaustive:
5+
default-signifies-exhaustive: false
6+
funlen:
7+
lines: 100
8+
statements: 50
9+
gci:
10+
sections:
11+
- prefix(kops.dev)
12+
goconst:
13+
min-len: 2
14+
min-occurrences: 2
15+
gocritic:
16+
enabled-tags:
17+
- diagnostic
18+
- experimental
19+
- opinionated
20+
- performance
21+
- style
22+
disabled-checks:
23+
- dupImport # https://github.com/go-critic/go-critic/issues/845
24+
- ifElseChain
25+
- octalLiteral
26+
- whyNoLint
27+
- wrapperFunc
28+
gocyclo:
29+
min-complexity: 10
30+
goimports:
31+
local-prefixes: kops.dev
32+
golint:
33+
min-confidence: 0
34+
gomnd:
35+
checks:
36+
- argument
37+
- case
38+
- condition
39+
- return
40+
govet:
41+
enable:
42+
- shadow
43+
lll:
44+
line-length: 140
45+
maligned:
46+
suggest-new: true
47+
misspell:
48+
locale: US
49+
nolintlint:
50+
allow-unused: false # report any unused nolint directives
51+
require-explanation: true # require an explanation for nolint directives
52+
require-specific: true # require nolint directives to be specific about which linter is being skipped
53+
54+
linters:
55+
# please, do not use `enable-all`: it's deprecated and will be removed soon.
56+
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
57+
disable-all: true
58+
enable:
59+
- asciicheck
60+
- bodyclose
61+
- dogsled
62+
- dupl
63+
- errcheck
64+
- exhaustive
65+
- exportloopref
66+
- funlen
67+
- gochecknoglobals
68+
- gochecknoinits
69+
- gocognit
70+
- goconst
71+
- gocritic
72+
- gocyclo
73+
- godot
74+
- goerr113
75+
- gofmt
76+
- goimports
77+
- gomnd
78+
- goprintffuncname
79+
- gosec
80+
- gosimple
81+
- govet
82+
- ineffassign
83+
- lll
84+
- misspell
85+
- nakedret
86+
- nestif
87+
- noctx
88+
- nolintlint
89+
- prealloc
90+
- revive
91+
- rowserrcheck
92+
- staticcheck
93+
- stylecheck
94+
- unconvert
95+
- unparam
96+
- unused
97+
- whitespace
98+
- wsl
99+
100+
# don't enable:
101+
# - godox # Disabling because we need TODO lines at this stage of project.
102+
# - testpackage # We also need to do unit test for unexported functions. And adding _internal in all files is cumbersome.
103+
104+
105+
service:
106+
golangci-lint-version: 1.58.x
107+
108+
issues:
109+
# exclude-use-default: false
110+
# exclude-use-default: false # By default, golangci-lint does not enforce comments on exported types. We want it.
111+
# Excluding configuration per-path, per-linter, per-text and per-source
112+
exclude-rules:
113+
- path: _test\.go
114+
linters:
115+
- gomnd
116+
- dupl
117+
- goconst

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
# KOPS Command Line Tool
22

33
The Kops-cli provides the functionality to deploy your applications seamlessly on
4-
cloud using kops - simplified devops.
4+
cloud using kops - simplified devops.
5+
6+
To install the cli app run the following command
7+
```
8+
go install kops.dev@latest //to get the latest release for kops cli
9+
```
10+
11+
or clone the repo and build binary
12+
```bash
13+
git clone https://github.com/kops-dev/kops-cli
14+
15+
go build -o kops.dev .
16+
```

deploy.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
11+
"gofr.dev/pkg/gofr"
12+
13+
"kops.dev/internal/models"
14+
)
15+
16+
const (
17+
gcp = "GCP"
18+
executableName = "main"
19+
)
20+
21+
var (
22+
errDepKeyNotProvided = errors.New("KOPS_DEPLOYMENT_KEY not provided, please download the key form https://kops.dev")
23+
errCloudProviderNotRecognized = errors.New("cloud provider in KOPS_DEPLOYMENT_KEY is not provided or supported")
24+
)
25+
26+
func Deploy(ctx *gofr.Context) (interface{}, error) {
27+
var deploy models.Deploy
28+
29+
keyFile := os.Getenv("KOPS_DEPLOYMENT_KEY")
30+
if keyFile == "" {
31+
return nil, errDepKeyNotProvided
32+
}
33+
34+
f, err := os.ReadFile(filepath.Clean(keyFile))
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
err = json.Unmarshal(f, &deploy)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
// build binaries for the current working directory
45+
err = buildBinary()
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
// create and build docker image
51+
err = createGoDockerFile()
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
tag := ctx.Param("tag")
57+
if tag == "" {
58+
tag = "latest"
59+
}
60+
61+
image := filepath.Clean(deploy.ServiceName + ":" + tag)
62+
63+
err = replaceInputOutput(exec.Command("docker", "build", "-t", image, ".")).Run()
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
// check what cloud provider is
69+
switch deploy.CloudProvider {
70+
case gcp:
71+
err = deployGCP(&deploy, image)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
return "Successfully deployed!", nil
77+
default:
78+
return nil, errCloudProviderNotRecognized
79+
}
80+
}
81+
82+
func buildBinary() error {
83+
fmt.Println("Creating binary for the project")
84+
85+
output, err := exec.Command("sh", "-c", "CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "+executableName+" .").CombinedOutput()
86+
if err != nil {
87+
fmt.Println("error occurred while creating binary!", output)
88+
89+
return err
90+
}
91+
92+
fmt.Println("Binary created successfully")
93+
94+
return nil
95+
}
96+
97+
func createGoDockerFile() error {
98+
content := `FROM alpine:latest
99+
RUN apk add --no-cache tzdata ca-certificates
100+
COPY main ./main
101+
RUN chmod +x /main
102+
EXPOSE 8000
103+
CMD ["/main"]`
104+
105+
fi, _ := os.Stat("Dockerfile")
106+
if fi != nil {
107+
fmt.Println("Dockerfile present, using already created dockerfile")
108+
return nil
109+
}
110+
111+
file, err := os.Create("Dockerfile")
112+
if err != nil {
113+
return err
114+
}
115+
116+
defer file.Close()
117+
118+
if _, err = file.WriteString(content); err != nil {
119+
return err
120+
}
121+
122+
fmt.Println("Dockerfile created successfully!")
123+
124+
return nil
125+
}

0 commit comments

Comments
 (0)