Skip to content
Snippets Groups Projects
Commit 918eb4c7 authored by Andre Sterba's avatar Andre Sterba
Browse files

Unify linting for go files

See merge request !258
parent 8efbd30d
No related branches found
No related tags found
2 merge requests!258Unify linting for go files,!247Develop
Pipeline #98278 failed
code-quality:
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golangci/golangci-lint:v1.42-alpine
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golangci/golangci-lint:v1.45-alpine
stage: analyze
script:
# writes golangci-lint output to gl-code-quality-report.json
- apk add --update make jq
- cd controller/
- make ci-lint
artifacts:
reports:
......
golangci-lint run\
--config .ci/.golangci.yml\
--out-format code-climate |\
jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
run:
go: 1.18
concurrency: 4
timeout: 10m
issues-exit-code: 1
# directories to be ignored by linters
skip-dirs:
- forks
- test
- controller/forks
- controller/test
- controller/mocks
skip-dirs-default: true
skip-files:
- http.go
modules-download-mode: readonly
# output settings -> code-climate for GitLab
output:
format: code-climate
......@@ -15,14 +20,16 @@ output:
print-linter-name: true
uniq-by-line: true
path-prefix: ""
# custom settings for linters
linters-settings:
gocyclo:
min-complexity: 15
golint:
min-confidence: 0.8
# enable the specific needed linters
linters:
# enable the specific needed linters
disable-all: true
enable:
- gofmt
......
......@@ -19,12 +19,21 @@ install-tools:
@echo Install development tooling
mkdir -p $(GOSDN_PRG)
go install gotest.tools/gotestsum@v1.7.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.1
@echo Finished installing development tooling
ci-install-tools:
go install gotest.tools/gotestsum@v1.7.0
ci-lint: ci-install-tools
golangci-lint run --config .golangci.yml --out-format code-climate | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
lint: install-tools
./$(TOOLS_DIR)/golangci-lint run --config .golangci.yml | jq
lint-fix: install-tools
./$(TOOLS_DIR)/golangci-lint run --config .golangci.yml --fix | jq
build: pre
$(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/gosdn ./controller/cmd/gosdn
CGO_ENABLED=0 $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/gosdnc ./cli/
......@@ -43,7 +52,6 @@ containerlab-stop:
containerlab-graph:
sudo containerlab graph --topo gosdn.clab.yaml
shell-gosdn:
docker exec -it clab-gosdn_csbi_arista_base-gosdn bash
shell-orchestrator:
......
......@@ -23,7 +23,7 @@ type PndAdapter struct {
endpoint string
}
// NewAdapter creates a PND Adapter. It requires a valid PND UUID and a reachable
// NewPndAdapter creates a PND Adapter. It requires a valid PND UUID and a reachable
// goSDN endpoint.
func NewPndAdapter(id, endpoint string) (*PndAdapter, error) {
pid, err := uuid.Parse(id)
......@@ -56,6 +56,7 @@ func (p *PndAdapter) AddDevice(name string, opts *tpb.TransportOption, sid uuid.
return resp, nil
}
// GetSbiSchemaTree requests a sbi schema tree.
func (p *PndAdapter) GetSbiSchemaTree(sid uuid.UUID) (map[string]*yang.Entry, error) {
resp, err := api.GetSbiSchemaTree(p.Endpoint(), p.ID(), sid)
if err != nil {
......
......@@ -43,7 +43,7 @@ var deviceListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "list all devices in current PND",
Long: `List all orchestrated network devices within the current PND.`,
Long: "List all orchestrated network devices within the current PND.",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := pndAdapter.GetDevices()
......@@ -60,7 +60,7 @@ var deviceListCmd = &cobra.Command{
if err != nil {
return err
}
log.Infof(" SchemaTree: ", tree)
log.Infof(" SchemaTree: %v", tree)
}
return nil
},
......
......@@ -46,19 +46,22 @@ import (
var suggestions []prompt.Suggest
type promptCompleter struct {
// PromptCompleter provides completion for a device
type PromptCompleter struct {
//NOTE: not too sure about this but for now it should be sufficient
deviceId uuid.UUID
deviceID uuid.UUID
yangSchemaCompleterMap map[uuid.UUID]*completer.YangSchemaCompleter
}
func NewPromptCompleter() *promptCompleter {
return &promptCompleter{
// NewPromptCompleter returns a new promptCompleter
func NewPromptCompleter() *PromptCompleter {
return &PromptCompleter{
yangSchemaCompleterMap: make(map[uuid.UUID]*completer.YangSchemaCompleter),
}
}
func (pc *promptCompleter) Run() {
// Run starts the interactive completion
func (pc *PromptCompleter) Run() {
fmt.Println("Welcome to the interactive mode of gosdnc.")
defer fmt.Println("Bye!")
......@@ -99,7 +102,7 @@ func flagVisitor(f *pflag.Flag) {
}
}
func (pc *promptCompleter) cstmCompleter(d prompt.Document) []prompt.Suggest {
func (pc *PromptCompleter) cstmCompleter(d prompt.Document) []prompt.Suggest {
// Start with the cobra 'rootCmd' and walk through it
// Reference: https://github.com/stromland/cobra-prompt
currCmd := rootCmd
......@@ -109,7 +112,6 @@ func (pc *promptCompleter) cstmCompleter(d prompt.Document) []prompt.Suggest {
}
return completionBasedOnCmd(pc, currCmd, inputSplit, d)
}
func removeFlagsFromInputSlice(input []string) []string {
......@@ -122,7 +124,7 @@ func removeFlagsFromInputSlice(input []string) []string {
return r
}
func deviceGetCompletion(c *promptCompleter, d prompt.Document, inputSplit []string) []prompt.Suggest {
func deviceGetCompletion(c *PromptCompleter, d prompt.Document, inputSplit []string) []prompt.Suggest {
inputLen := len(inputSplit)
if inputLen == 2 || inputLen == 3 {
if id, err := uuid.Parse(inputSplit[inputLen-1]); err == nil {
......@@ -143,7 +145,7 @@ func deviceGetCompletion(c *promptCompleter, d prompt.Document, inputSplit []str
}
c.yangSchemaCompleterMap[id] = completer.NewYangSchemaCompleter(schemaTree["Device"], true)
if yc, ok := c.yangSchemaCompleterMap[id]; ok {
c.deviceId = id
c.deviceID = id
return yc.Complete(d)
}
......@@ -153,7 +155,7 @@ func deviceGetCompletion(c *promptCompleter, d prompt.Document, inputSplit []str
return prompt.FilterHasPrefix(getDevices(), d.GetWordBeforeCursor(), true)
}
} else {
if yc, ok := c.yangSchemaCompleterMap[c.deviceId]; ok {
if yc, ok := c.yangSchemaCompleterMap[c.deviceID]; ok {
return yc.Complete(d)
}
return []prompt.Suggest{}
......@@ -177,10 +179,9 @@ func cobraCommandCompletion(currCmd *cobra.Command, d prompt.Document, loaded []
suggestions = append(suggestions, prompt.Suggest{Text: cmd.Name(), Description: cmd.Short})
}
return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true)
}
func completionBasedOnCmd(c *promptCompleter, cmd *cobra.Command, inputSplit []string, d prompt.Document) []prompt.Suggest {
func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []string, d prompt.Document) []prompt.Suggest {
switch cmd {
case pndUseCmd, pndGetCmd:
return cobraCommandCompletion(cmd, d, getPnds())
......
......@@ -6,6 +6,8 @@ import (
"github.com/c-bata/go-prompt"
)
// SortSuggestionByText sorts the suggestion slice, otherwise the order of the slice will be
// random.
func SortSuggestionByText(suggestions []prompt.Suggest) []prompt.Suggest {
// sort the suggestion slice, otherwise the order of the slice will be
// random.
......
......@@ -12,13 +12,14 @@ import (
)
var (
// YangSchemaCompletionSeperator is the seperator for yang schemas
YangSchemaCompletionSeperator = string([]byte{' ', '/'})
)
// The idea of path suggestions for a SBI's YANG schema is heavily inspired by
// gnmic (https://github.com/karimra/gnmic)
// YgotSchemaPathCompleter
// YangSchemaCompleter represtents the YangSchemaCompleter
type YangSchemaCompleter struct {
Cache map[string]*yang.Entry
Entry *yang.Entry
......@@ -105,6 +106,7 @@ func promptFromYangEntry(e *yang.Entry) []prompt.Suggest {
return []prompt.Suggest{}
}
// Complete provides the actual completion
func (c *YangSchemaCompleter) Complete(d prompt.Document) []prompt.Suggest {
p := d.GetWordBeforeCursor()
......
......@@ -15,7 +15,7 @@ install-tools:
@echo Install development tooling
mkdir -p $(GOSDN_PRG)
go install gotest.tools/gotestsum@v1.7.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.1
@echo Finished installing development tooling
ci-install-tools:
......
......@@ -59,6 +59,7 @@ func GetIds(addr string) ([]*ppb.PrincipalNetworkDomain, error) {
return resp.Pnd, nil
}
// GetAllCore requests all PNDs
func GetAllCore(ctx context.Context, addr string) (*pb.GetPndListResponse, error) {
coreClient, err := nbi.CoreClient(addr, dialOptions...)
if err != nil {
......@@ -211,6 +212,7 @@ func Confirm(addr, pnd string, cuids ...string) (*ppb.SetChangeListResponse, err
return CommitConfirm(addr, pnd, changes)
}
// CommitConfirm confirms a commit
func CommitConfirm(addr, pnd string, changes []*ppb.SetChange) (*ppb.SetChangeListResponse, error) {
ctx := context.Background()
client, err := nbi.PndClient(addr, dialOptions...)
......@@ -280,7 +282,7 @@ func GetDevice(addr, pid string, did ...string) (*ppb.GetOndResponse, error) {
return pndClient.GetOnd(ctx, req)
}
// GetSbiSchemaTree
// GetSbiSchemaTree gets the sbi tree for a sbi
func GetSbiSchemaTree(addr string, pid, sid uuid.UUID) (map[string]*yang.Entry, error) {
sbiClient, err := nbi.SbiClient(addr, dialOptions...)
if err != nil {
......@@ -339,6 +341,7 @@ func GetDevices(addr, pid string) (*ppb.GetOndListResponse, error) {
return pndClient.GetOndList(ctx, req)
}
// GetPath requests a specific path
func GetPath(addr, pid, did, path string) (*ppb.GetPathResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
......@@ -355,6 +358,7 @@ func GetPath(addr, pid, did, path string) (*ppb.GetPathResponse, error) {
return pndClient.GetPath(ctx, req)
}
// DeleteDevice deletes a device
func DeleteDevice(addr, pid, did string) (*ppb.DeleteOndResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
......@@ -370,7 +374,7 @@ func DeleteDevice(addr, pid, did string) (*ppb.DeleteOndResponse, error) {
return pndClient.DeleteOnd(ctx, req)
}
// change creates a ChangeRequest for the specified OND. ApiOperations are
// ChangeRequest change creates a ChangeRequest for the specified OND. ApiOperations are
// used to specify the type of the change (update, replace, delete as specified
// in https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-specification.md#34-modifying-state)
// For delete operations the value field needs to contain an empty string.
......@@ -384,6 +388,7 @@ func ChangeRequest(addr, did, pid, path, value string, op ppb.ApiOperation) (*pp
return SendChangeRequest(addr, pid, req)
}
// SendChangeRequest sends a change request
func SendChangeRequest(addr, pid string, req *ppb.ChangeRequest) (*ppb.SetPathListResponse, error) {
pndClient, err := nbi.PndClient(addr, dialOptions...)
if err != nil {
......
......@@ -603,7 +603,7 @@ func (pnd *pndImplementation) requestPlugin(name string, opt *tpb.TransportOptio
return sbi, nil
}
return nil, fmt.Errorf("requestPlugin: received deployment slice was empty.")
return nil, fmt.Errorf("requestPlugin: received deployment slice was empty")
}
// GenericGrpcClient allows to distinguish between the different ygot
......
......@@ -95,7 +95,7 @@ func (oc *OpenConfig) Schema() *ytypes.Schema {
return schema
}
// SchemaTree returns the ygot generated SchemaTree compressed as gzip byte
// SchemaTreeGzip returns the ygot generated SchemaTree compressed as gzip byte
// slice.
func (oc *OpenConfig) SchemaTreeGzip() []byte {
return openconfig.SchemaTreeGzip()
......
......@@ -77,7 +77,6 @@ func init() {
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "access token for private repositories")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level 'debug' or 'trace'")
rootCmd.PersistentFlags().StringVar(&repoBasePath, "repository-base-path", "./models", "path to the repository base path (default is ./models)")
}
// initConfig reads in config file and ENV variables if set.
......
......@@ -64,5 +64,4 @@ func Run(bindAddr string) {
o.Shutdown(ctx)
}()
}
......@@ -165,6 +165,7 @@ func writeCode(path string, code *ygen.GeneratedGoCode, t spb.Type) error {
return nil
}
// Manifest represents a csbi manifest
type Manifest struct {
Name, Author, Version string
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment