From 5b3889dfa1916c2d1fa57b5943add43c876cde99 Mon Sep 17 00:00:00 2001 From: James Chacon Date: Fri, 23 Sep 2022 13:17:28 -0700 Subject: [PATCH] Make sansshell service easier to follow. (#165) This was confusing since the server def was stuffed into logging along with the Version var. Put Version with version.go Move generic server parts into server.go --- services/sansshell/server/logging.go | 29 ------------------- services/sansshell/server/server.go | 43 ++++++++++++++++++++++++++++ services/sansshell/server/version.go | 10 +++++++ 3 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 services/sansshell/server/server.go diff --git a/services/sansshell/server/logging.go b/services/sansshell/server/logging.go index 78f6d367..455afc56 100644 --- a/services/sansshell/server/logging.go +++ b/services/sansshell/server/logging.go @@ -19,33 +19,14 @@ package server import ( "context" - "sync" "github.com/go-logr/logr" "github.com/go-logr/stdr" - "google.golang.org/grpc" "google.golang.org/protobuf/types/known/emptypb" - "github.com/Snowflake-Labs/sansshell/services" pb "github.com/Snowflake-Labs/sansshell/services/sansshell" ) -var ( - // Version is the value returned by the Version RPC service. - // This should likely be set as a linker option from external - // input such as a git SHA or RPM version number. - // go build -ldflags="-X github.com/Snowflake-Labs/sansshell/services/sansshell/server.Version=..." - // - // NOTE: This is a var so the linker can set it but in reality it's a const so treat as such. - Version string -) - -// Server is used to implement the gRPC Server -type Server struct { - mu sync.RWMutex - lastVal int32 -} - // SetVerbosity sets the logging level and returns the last value before this was called. func (s *Server) SetVerbosity(ctx context.Context, req *pb.SetVerbosityRequest) (*pb.VerbosityReply, error) { s.mu.Lock() @@ -66,13 +47,3 @@ func (s *Server) GetVerbosity(ctx context.Context, req *emptypb.Empty) (*pb.Verb defer s.mu.RUnlock() return &pb.VerbosityReply{Level: s.lastVal}, nil } - -// Register is called to expose this handler to the gRPC server -func (s *Server) Register(gs *grpc.Server) { - pb.RegisterLoggingServer(gs, s) - pb.RegisterStateServer(gs, s) -} - -func init() { - services.RegisterSansShellService(&Server{}) -} diff --git a/services/sansshell/server/server.go b/services/sansshell/server/server.go new file mode 100644 index 00000000..03df230c --- /dev/null +++ b/services/sansshell/server/server.go @@ -0,0 +1,43 @@ +/* Copyright (c) 2019 Snowflake Inc. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +// Package server implements the sansshell 'Logging' service. +package server + +import ( + "sync" + + "google.golang.org/grpc" + + "github.com/Snowflake-Labs/sansshell/services" + pb "github.com/Snowflake-Labs/sansshell/services/sansshell" +) + +// Server is used to implement the gRPC Server +type Server struct { + mu sync.RWMutex + lastVal int32 +} + +// Register is called to expose this handler to the gRPC server +func (s *Server) Register(gs *grpc.Server) { + pb.RegisterLoggingServer(gs, s) + pb.RegisterStateServer(gs, s) +} + +func init() { + services.RegisterSansShellService(&Server{}) +} diff --git a/services/sansshell/server/version.go b/services/sansshell/server/version.go index 345e15fb..47f16d33 100644 --- a/services/sansshell/server/version.go +++ b/services/sansshell/server/version.go @@ -24,6 +24,16 @@ import ( pb "github.com/Snowflake-Labs/sansshell/services/sansshell" ) +var ( + // Version is the value returned by the Version RPC service. + // This should likely be set as a linker option from external + // input such as a git SHA or RPM version number. + // go build -ldflags="-X github.com/Snowflake-Labs/sansshell/services/sansshell/server.Version=..." + // + // NOTE: This is a var so the linker can set it but in reality it's a const so treat as such. + Version string +) + func (s *Server) Version(ctx context.Context, req *emptypb.Empty) (*pb.VersionResponse, error) { return &pb.VersionResponse{ Version: Version,