Skip to content

Commit

Permalink
[CI] add basic static check, complie, test
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
357447923 committed Sep 26, 2023
1 parent 3b6b0a1 commit 07394ad
Show file tree
Hide file tree
Showing 45 changed files with 223 additions and 115 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/CodeQL.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: "CodeQL"

on:
push:
branches: [ "master","develop" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master","develop" ]
schedule:
- cron: '0 17 * * 5'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
# timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
22 changes: 18 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ on:
branches: [ $default-branch, "develop" ]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: '1.19'
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup golangci-lint
uses: golangci/[email protected]
with:
version: v1.52.2
args: --verbose

test:
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -21,15 +37,13 @@ jobs:
run: make test

build:
needs: test
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Set dependencies
run: sudo apt update && sudo apt install musl-tools
- name: Build
run: make build
run: go build -v ./...
48 changes: 48 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
run:
timeout: 5m
linters:
enable:
# Simple linter to check that your code does not contain non-ASCII identifiers.
- asciicheck
# Go linter that checks if package imports are in a list of acceptable packages.
#- depguard
# Checks for pointers to enclosing loop variables.
- exportloopref
# Gofmt checks whether code was gofmt-ed
- gofmt
# Inspects source code for security problems.
- gosec
# Finds naked returns in functions greater than a specified function length.
- nakedret
# Reports ill-formed or insufficient nolint directives.
- nolintlint
# Checks for dangerous unicode character sequences.
- bidichk
disable:
# ignore unused check
- unused
# ignore errcheck
- errcheck
linters-settings:
gosec:
# Available rules: https://github.com/securego/gosec#available-rules
excludes:
- G101 # Look for hard coded credentials
- G204 # Audit use of command execution
- G401 # Detect the usage of DES, RC4, MD5 or SHA1
- G403 # Ensure minimum RSA key length of 2048 bits
- G404 # Insecure random number source (rand)
- G501 # Import blocklist: crypto/md5
- G502 # Import blocklist: crypto/des
- G503 # Import blocklist: crypto/rc4
- G504 # Import blocklist: net/http/cgi
- G505 # Import blocklist: crypto/sha1
- G601 # Implicit memory aliasing of items from a range statement

staticcheck:
# Use default values for all except the SA4006(it will check unused variable). '-' means ignorance
checks: ["all", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022", "-SA4006"]

issues:
# Fix found issues (if it's supported by the linter).
fix: true
15 changes: 7 additions & 8 deletions cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func NewCurveAdm() (*CurveAdm, error) {

rootDir := fmt.Sprintf("%s/.curveadm", home)
curveadm := &CurveAdm{
rootDir: rootDir,
dataDir: path.Join(rootDir, "data"),
pluginDir: path.Join(rootDir, "plugins"),
logDir: path.Join(rootDir, "logs"),
tempDir: path.Join(rootDir, "temp"),
rootDir: rootDir,
dataDir: path.Join(rootDir, "data"),
pluginDir: path.Join(rootDir, "plugins"),
logDir: path.Join(rootDir, "logs"),
tempDir: path.Join(rootDir, "temp"),
httpConfPath: path.Join(rootDir, "http/conf"),
httpLogPath: path.Join(rootDir, "http/logs"),
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (curveadm *CurveAdm) detectVersion() {
}

func (curveadm *CurveAdm) Upgrade() (bool, error) {
if curveadm.config.GetAutoUpgrade() == false {
if !curveadm.config.GetAutoUpgrade() {
return false, nil
}

Expand Down Expand Up @@ -549,8 +549,7 @@ func (curveadm *CurveAdm) PostAudit(id int64, ec error) {
return
}

auditLog := auditLogs[0]
status := auditLog.Status
var status int
errorCode := 0
if ec == nil {
status = comm.AUDIT_STATUS_SUCCESS
Expand Down
2 changes: 1 addition & 1 deletion cli/command/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func genCleanPlaybook(curveadm *cli.CurveAdm,
Configs: dcs,
Options: map[string]interface{}{
comm.KEY_CLEAN_ITEMS: options.only,
comm.KEY_CLEAN_BY_RECYCLE: options.withoutRecycle == false,
comm.KEY_CLEAN_BY_RECYCLE: !options.withoutRecycle,
},
})
}
Expand Down
1 change: 0 additions & 1 deletion cli/command/client/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"

"github.com/dustin/go-humanize"

"github.com/fatih/color"
"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
Expand Down
5 changes: 2 additions & 3 deletions cli/command/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ func readDatabase(filename string) (storage.Cluster, []storage.Service, error) {
switch id {
case CLUSTER_DESCRIPTION:
cluster.Description = value
break
case CLUSTER_TOPOLOGY:
cluster.Topology = value
break
case SERVICE:
items := strings.Split(value, " ")
if len(items) != 2 {
Expand All @@ -166,7 +164,8 @@ func importCluster(storage *storage.Storage, name, dbfile string) error {
}

// insert cluster
if storage.InsertCluster(name, cluster.Description, cluster.Topology); err != nil {
err = storage.InsertCluster(name, cluster.Description, cluster.Topology)
if err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ PowerShell:
`, "curveadm"),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
Expand Down
4 changes: 2 additions & 2 deletions cli/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ func genDeployPlaybook(curveadm *cli.CurveAdm,
role := DEPLOY_FILTER_ROLE[step]
config = curveadm.FilterDeployConfigByRole(config, role)
}
n := len(config)

if DEPLOY_LIMIT_SERVICE[step] > 0 {
n = DEPLOY_LIMIT_SERVICE[step]
n := DEPLOY_LIMIT_SERVICE[step]
config = config[:n]
}

Expand Down
2 changes: 1 addition & 1 deletion internal/configure/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (hc *HostConfig) Build() error {
F("hosts[%d].private_key_file = %s", hc.sequence, privateKeyFile)
}

if hc.GetForwardAgent() == false {
if !hc.GetForwardAgent() {
if !utils.PathExist(privateKeyFile) {
return errno.ERR_PRIVATE_KEY_FILE_NOT_EXIST.
F("%s: no such file", privateKeyFile)
Expand Down
12 changes: 3 additions & 9 deletions internal/configure/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ func ScaleOutClusterPool(old *CurveClusterTopo, dcs []*topology.DeployConfig, po
npools := old.NPools
topo := generateClusterPool(dcs, fmt.Sprintf("pool%d", npools+1), poolset, diskType)
if dcs[0].GetKind() == KIND_CURVEBS {
for _, pool := range topo.LogicalPools {
old.LogicalPools = append(old.LogicalPools, pool)
}
old.LogicalPools = append(old.LogicalPools, topo.LogicalPools...)
for _, newPst := range topo.Poolsets {
isExist := false
for _, oldPst := range old.Poolsets {
Expand All @@ -256,13 +254,9 @@ func ScaleOutClusterPool(old *CurveClusterTopo, dcs []*topology.DeployConfig, po
}
}
} else {
for _, pool := range topo.Pools {
old.Pools = append(old.Pools, pool)
}
}
for _, server := range topo.Servers {
old.Servers = append(old.Servers, server)
old.Pools = append(old.Pools, topo.Pools...)
}
old.Servers = append(old.Servers, topo.Servers...)
old.NPools = old.NPools + 1
}

Expand Down
8 changes: 4 additions & 4 deletions internal/configure/topology/dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func formatName(name string, hostSequence int) string {

func newVariables(m map[string]interface{}) (*variable.Variables, error) {
vars := variable.NewVariables()
if m == nil || len(m) == 0 {
if len(m) == 0 {
return vars, nil
}

Expand Down Expand Up @@ -182,8 +182,8 @@ func (dc *DeployConfig) renderVariables() error {
}
dc.config[k] = realv
build.DEBUG(build.DEBUG_TOPOLOGY,
build.Field{k, v},
build.Field{k, realv})
build.Field{Key: k, Value: v},
build.Field{Key: k, Value: realv})
}
return nil
}
Expand All @@ -192,7 +192,7 @@ func (dc *DeployConfig) convert() error {
// init service config
for k, v := range dc.config {
item := itemset.get(k)
if item == nil || item.exclude == false {
if item == nil || !item.exclude {
dc.serviceConfig[k] = v.(string)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/configure/topology/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func skip(dc *DeployConfig, v Var) bool {
func addVariables(dcs []*DeployConfig, idx int, vars []Var) error {
dc := dcs[idx]
for _, v := range vars {
if skip(dc, v) == true {
if skip(dc, v) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Init(logpath string) {
func List() error {
count := map[int]int{}
for _, e := range elist {
fmt.Printf(color.GreenString("%06d ", e.code))
fmt.Print(color.GreenString("%06d ", e.code))
fmt.Println(color.YellowString("%s", e.description))
count[e.code]++
}
Expand Down
Loading

0 comments on commit 07394ad

Please sign in to comment.