Skip to content

Commit

Permalink
Go Code format checker and formatter
Browse files Browse the repository at this point in the history
Also formatted the files which dont adhere to go formats. Build will
fail if there exists any formatting issue.
Formatting is supported for bookworm as well.
  • Loading branch information
faraazbrcm committed Jan 19, 2024
1 parent 07a64ab commit e61ae37
Show file tree
Hide file tree
Showing 37 changed files with 2,061 additions and 1,975 deletions.
23 changes: 22 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ export PATH := $(PATH):$(GOPATH)/bin
INSTALL := /usr/bin/install
DBDIR := /var/run/redis/sonic-db/
GO ?= /usr/local/go/bin/go
GOROOT ?= $(shell $(GO) env GOROOT)
TOP_DIR := $(abspath ..)
MGMT_COMMON_DIR := $(TOP_DIR)/sonic-mgmt-common
BUILD_DIR := build/bin
FORMAT_CHECK = $(BUILD_DIR)/.formatcheck
FORMAT_LOG = $(BUILD_DIR)/go_format.log
# Find all .go files excluding vendor, build, and patches files
GO_FILES := $(shell find . -type f -name '*.go' ! -path './vendor/*' ! -path './build/*' ! -path './patches/*' ! -path './proto/*' ! -path './swsscommon/*')
export CVL_SCHEMA_PATH := $(MGMT_COMMON_DIR)/build/cvl/schema
export GOBIN := $(abspath $(BUILD_DIR))
export PATH := $(PATH):$(GOBIN):$(shell dirname $(GO))
Expand Down Expand Up @@ -61,7 +66,7 @@ go-deps: $(GO_DEPS)
go-deps-clean:
$(RM) -r vendor

sonic-gnmi: $(GO_DEPS)
sonic-gnmi: $(GO_DEPS) $(FORMAT_CHECK)
ifeq ($(CROSS_BUILD_ENVIRON),y)
$(GO) build -o ${GOBIN}/telemetry -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/telemetry
ifneq ($(ENABLE_DIALOUT_VALUE),0)
Expand Down Expand Up @@ -136,6 +141,22 @@ clean:
$(RM) -r build
$(RM) -r vendor

# File target that generates a diff file if formatting is incorrect
$(FORMAT_CHECK): $(GO_FILES)
@echo "Checking Go file formatting..."
@echo $(GO_FILES)
mkdir -p $(@D)
@$(GOROOT)/bin/gofmt -l $(GO_FILES) > $(FORMAT_LOG)
@if [ -s $(FORMAT_LOG) ]; then \
cat $(FORMAT_LOG); \
echo "Formatting issues found. Please run 'gofmt -w <file>' on the above files and commit the changes."; \
exit 1; \
else \
echo "All files are properly formatted."; \
rm -f $(FORMAT_LOG); \
fi
touch $@

install:
$(INSTALL) -D $(BUILD_DIR)/telemetry $(DESTDIR)/usr/sbin/telemetry
ifneq ($(ENABLE_DIALOUT_VALUE),0)
Expand Down
7 changes: 3 additions & 4 deletions common_utils/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package common_utils

const GNMI_WORK_PATH = "/tmp"
package common_utils

const GNMI_WORK_PATH = "/tmp"
6 changes: 2 additions & 4 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"sync/atomic"
)


// AuthInfo holds data about the authenticated user
type AuthInfo struct {
// Username
User string
User string
AuthEnabled bool
// Roles
Roles []string
Expand All @@ -37,6 +36,7 @@ const requestContextKey contextkey = 0
var requestCounter uint64

type CounterType int

const (
GNMI_GET CounterType = iota
GNMI_GET_FAIL
Expand Down Expand Up @@ -89,7 +89,6 @@ func (c CounterType) String() string {

var globalCounters [COUNTER_SIZE]uint64


// GetContext function returns the RequestContext object for a
// gRPC request. RequestContext is maintained as a context value of
// the request. Creates a new RequestContext object is not already
Expand Down Expand Up @@ -125,4 +124,3 @@ func IncCounter(cnt CounterType) {
atomic.AddUint64(&globalCounters[cnt], 1)
SetMemCounters(&globalCounters)
}

5 changes: 2 additions & 3 deletions common_utils/shareMem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// Use share memory to dump GNMI internal counters,
// GNMI server and gnmi_dump should use memKey to access the share memory,
// memSize is 1024 bytes, so we can support 128 counters
// memMode is 0x380, this value is O_RDWR|IPC_CREAT,
// memMode is 0x380, this value is O_RDWR|IPC_CREAT,
// O_RDWR means: Owner can write and read the file, everyone else can't.
// IPC_CREAT means: Create a shared memory segment if a shared memory identifier does not exist for memKey.
var (
memKey = 7749
memKey = 7749
memSize = 1024
memMode = 0x380
)
Expand Down Expand Up @@ -61,4 +61,3 @@ func GetMemCounters(counters *[int(COUNTER_SIZE)]uint64) error {
}
return nil
}

6 changes: 3 additions & 3 deletions dialout/dialout_client/dialout_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"crypto/tls"
"errors"
"fmt"
spb "github.com/sonic-net/sonic-gnmi/proto"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config"
"github.com/Workiva/go-datastructures/queue"
"github.com/go-redis/redis"
log "github.com/golang/glog"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ygot/ygot"
spb "github.com/sonic-net/sonic-gnmi/proto"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down
3 changes: 1 addition & 2 deletions dialout/dialout_client/dialout_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"testing"
"time"

gclient "github.com/openconfig/gnmi/client/gnmi"
sds "github.com/sonic-net/sonic-gnmi/dialout/dialout_server"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config"
gclient "github.com/openconfig/gnmi/client/gnmi"
)

var clientTypes = []string{gclient.Type}
Expand Down Expand Up @@ -326,7 +326,6 @@ func serverOp(t *testing.T, sop ServerOp) {
}
}

//
func TestGNMIDialOutPublish(t *testing.T) {

fileName := "../../testdata/COUNTERS_PORT_NAME_MAP.txt"
Expand Down
2 changes: 1 addition & 1 deletion dialout/dialout_client_cli/dialout_client_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package main
import (
"crypto/tls"
"flag"
dc "github.com/sonic-net/sonic-gnmi/dialout/dialout_client"
log "github.com/golang/glog"
gpb "github.com/openconfig/gnmi/proto/gnmi"
dc "github.com/sonic-net/sonic-gnmi/dialout/dialout_client"
"golang.org/x/net/context"
"os"
"os/signal"
Expand Down
2 changes: 1 addition & 1 deletion dialout/dialout_server/dialout_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package dialout_server
import (
"errors"
"fmt"
spb "github.com/sonic-net/sonic-gnmi/proto"
log "github.com/golang/glog"
"github.com/google/gnxi/utils"
gpb "github.com/openconfig/gnmi/proto/gnmi"
spb "github.com/sonic-net/sonic-gnmi/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
Expand Down
2 changes: 1 addition & 1 deletion gnmi_server/basicAuth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gnmi

import (
"github.com/sonic-net/sonic-gnmi/common_utils"
"github.com/golang/glog"
"github.com/sonic-net/sonic-gnmi/common_utils"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down
45 changes: 45 additions & 0 deletions gnmi_server/clear_neighbor_dummy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gnmi

//This file contains dummy tests for the sake of coverage and will be removed later

import (
"testing"
"time"

spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
"golang.org/x/net/context"
)

func TestDummyClearNeighbor(t *testing.T) {
// Start server
s := createServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()

// Run Client
client := createClient(t, 8081)
sc := spb_gnoi.NewSonicServiceClient(client)
req := &spb_gnoi.ClearNeighborsRequest{
Input: &spb_gnoi.ClearNeighborsRequest_Input{},
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sc.ClearNeighbors(ctx, req)
}

func TestDummyCopyConfig(t *testing.T) {
// Start server
s := createServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()

// Run Client
client := createClient(t, 8081)
sc := spb_gnoi.NewSonicServiceClient(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req := &spb_gnoi.CopyConfigRequest{
Input: &spb_gnoi.CopyConfigRequest_Input{},
}
sc.CopyConfig(ctx, req)
}
2 changes: 1 addition & 1 deletion gnmi_server/clientCertAuth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gnmi

import (
"github.com/sonic-net/sonic-gnmi/common_utils"
"github.com/golang/glog"
"github.com/sonic-net/sonic-gnmi/common_utils"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
Expand Down
20 changes: 10 additions & 10 deletions gnmi_server/client_subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"github.com/Workiva/go-datastructures/queue"
log "github.com/golang/glog"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
)

// Client contains information about a subscribe client that has connected to the server.
Expand All @@ -28,9 +28,9 @@ type Client struct {
q *queue.PriorityQueue
subscribe *gnmipb.SubscriptionList
// Wait for all sub go routine to finish
w sync.WaitGroup
fatal bool
logLevel int
w sync.WaitGroup
fatal bool
logLevel int
}

// Syslog level for error
Expand All @@ -44,8 +44,8 @@ var connectionManager *ConnectionManager
func NewClient(addr net.Addr) *Client {
pq := queue.NewPriorityQueue(1, false)
return &Client{
addr: addr,
q: pq,
addr: addr,
q: pq,
logLevel: logLevelError,
}
}
Expand All @@ -58,7 +58,7 @@ func (c *Client) setConnectionManager(threshold int) {
if connectionManager != nil && threshold == connectionManager.GetThreshold() {
return
}
connectionManager = &ConnectionManager {
connectionManager = &ConnectionManager{
connections: make(map[string]struct{}),
threshold: threshold,
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func (c *Client) Run(stream gnmipb.GNMI_SubscribeServer) (err error) {
return grpc.Errorf(codes.Unimplemented, "Empty target data not supported")
} else if target == "OTHERS" {
dc, err = sdc.NewNonDbClient(paths, prefix)
} else if ((target == "EVENTS") && (mode == gnmipb.SubscriptionList_STREAM)) {
} else if (target == "EVENTS") && (mode == gnmipb.SubscriptionList_STREAM) {
dc, err = sdc.NewEventClient(paths, prefix, c.logLevel)
} else if _, ok, _, _ := sdc.IsTargetDb(target); ok {
dc, err = sdc.NewDbClient(paths, prefix)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (c *Client) send(stream gnmipb.GNMI_SubscribeServer, dc sdc.Client) error {
c.errors++
return err
}
val = &v;
val = &v
default:
log.V(1).Infof("Unknown data type %v for %s in queue", items[0], c)
c.errors++
Expand Down
16 changes: 8 additions & 8 deletions gnmi_server/connection_manager.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package gnmi

import (
"sync"
"time"
log "github.com/golang/glog"
"net"
"regexp"
log "github.com/golang/glog"
"sync"
"time"

"github.com/go-redis/redis"
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config"
Expand All @@ -16,9 +16,9 @@ const table = "TELEMETRY_CONNECTIONS"
var rclient *redis.Client

type ConnectionManager struct {
connections map[string]struct{}
mu sync.RWMutex
threshold int
connections map[string]struct{}
mu sync.RWMutex
threshold int
}

func (cm *ConnectionManager) GetThreshold() int {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (cm *ConnectionManager) PrepareRedis() {
}

func (cm *ConnectionManager) Add(addr net.Addr, query string) (string, bool) {
cm.mu.RLock() // reading
cm.mu.RLock() // reading
if len(cm.connections) >= cm.threshold && cm.threshold != 0 { // 0 is defined as no threshold
log.V(1).Infof("Cannot add another client connection as threshold is already at limit")
cm.mu.RUnlock()
Expand All @@ -73,7 +73,7 @@ func (cm *ConnectionManager) Add(addr net.Addr, query string) (string, bool) {
return key, true
}

func (cm *ConnectionManager) Remove(key string) (bool) {
func (cm *ConnectionManager) Remove(key string) bool {
cm.mu.RLock() // reading
_, exists := cm.connections[key]
cm.mu.RUnlock()
Expand Down
11 changes: 6 additions & 5 deletions gnmi_server/constants_native.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// +build !gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = false
//go:build !gnmi_native_write
// +build !gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = false
11 changes: 6 additions & 5 deletions gnmi_server/constants_native_write.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// +build gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = true
//go:build gnmi_native_write
// +build gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = true
11 changes: 6 additions & 5 deletions gnmi_server/constants_translib.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// +build !gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = false
//go:build !gnmi_translib_write
// +build !gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = false
11 changes: 6 additions & 5 deletions gnmi_server/constants_translib_write.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// +build gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = true
//go:build gnmi_translib_write
// +build gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = true
Loading

0 comments on commit e61ae37

Please sign in to comment.