Skip to content

Commit

Permalink
resolve client creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShohamBit committed Dec 8, 2024
1 parent 30317ed commit dd19b43
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
20 changes: 10 additions & 10 deletions cmd/traceectl/cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ var disableEventCmd = &cobra.Command{
}

func listEvents(cmd *cobra.Command, args []string) {
var traceeClient client.ServiceClient
if err := traceeClient.NewServiceClient(server); err != nil {
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
}
Expand Down Expand Up @@ -118,8 +118,8 @@ func listEvents(cmd *cobra.Command, args []string) {
}

func getEventDescriptions(cmd *cobra.Command, args []string) {
var traceeClient client.ServiceClient
if err := traceeClient.NewServiceClient(server); err != nil {
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
}
Expand Down Expand Up @@ -161,28 +161,28 @@ func prepareDescription(event *pb.EventDefinition) []string {

}
func enableEvents(cmd *cobra.Command, eventName string) {
var traceeClient client.ServiceClient
if err := traceeClient.NewServiceClient(server); err != nil {
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
}
defer traceeClient.CloseConnection()

_, err := traceeClient.EnableEvent(context.Background(), &pb.EnableEventRequest{Name: eventName})
_, err = traceeClient.EnableEvent(context.Background(), &pb.EnableEventRequest{Name: eventName})
if err != nil {
cmd.PrintErrln("Error enabling event:", err)
return
}
cmd.Printf("Enabled event: %s\n", eventName)
}
func disableEvents(cmd *cobra.Command, eventName string) {
var traceeClient client.ServiceClient
if err := traceeClient.NewServiceClient(server); err != nil {
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
}
defer traceeClient.CloseConnection()
_, err := traceeClient.DisableEvent(context.Background(), &pb.DisableEventRequest{Name: eventName})
_, err = traceeClient.DisableEvent(context.Background(), &pb.DisableEventRequest{Name: eventName})
if err != nil {
cmd.PrintErrln("Error disabling event:", err)
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/traceectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func displayMetrics(cmd *cobra.Command, _ []string) {
}

func displayVersion(cmd *cobra.Command, _ []string) {
var traceeClient client.ServiceClient
if err := traceeClient.NewServiceClient(server); err != nil {
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/traceectl/cmd/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func init() {
}

func streamEvents(cmd *cobra.Command, args []string) {
var traceeClient client.ServiceClient
err := traceeClient.NewServiceClient(server)
traceeClient, err := client.NewServiceClient(server)
if err != nil {
cmd.PrintErrln("Error creating client: ", err)
return
Expand Down
12 changes: 6 additions & 6 deletions cmd/traceectl/pkg/client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type ServiceClient struct {
client pb.TraceeServiceClient
}

func (tc *ServiceClient) NewServiceClient(serverInfo ServerInfo) error {
func NewServiceClient(serverInfo ServerInfo) (*ServiceClient, error) {
conn, err := connectToServer(serverInfo)
if err != nil {
return err
return nil, err
}
tc.conn = conn
tc.client = pb.NewTraceeServiceClient(conn)

return nil
return &ServiceClient{
conn: conn,
client: pb.NewTraceeServiceClient(conn),
}, nil
}
func (tc *ServiceClient) CloseConnection() {
if err := tc.conn.Close(); err != nil {
Expand Down

0 comments on commit dd19b43

Please sign in to comment.