Introduce openConn

This commit is contained in:
JBD
2026-01-21 16:09:29 -08:00
parent 1b15387d7c
commit b42b3f9c83
4 changed files with 17 additions and 18 deletions
+2 -5
View File
@@ -7,8 +7,6 @@ import (
"github.com/google/gar/proto"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
@@ -32,10 +30,9 @@ func init() {
func runInspect(cmd *cobra.Command, args []string) error {
fmt.Printf("Inspecting session: %s\n", inspectSessionID)
// Connect to gRPC server
conn, err := grpc.NewClient(inspectServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := openConn(inspectServerAddr)
if err != nil {
return fmt.Errorf("failed to connect to server: %w", err)
return err
}
defer conn.Close()
+11
View File
@@ -6,6 +6,9 @@ package main
import (
"fmt"
"os"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
@@ -14,3 +17,11 @@ func main() {
os.Exit(1)
}
}
func openConn(server string) (*grpc.ClientConn, error) {
conn, err := grpc.NewClient(server, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
return conn, nil
}
+2 -5
View File
@@ -6,8 +6,6 @@ import (
"github.com/google/gar/proto"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
@@ -44,10 +42,9 @@ func runRegister(cmd *cobra.Command, args []string) error {
fmt.Printf(" Description: %s\n", registerAgentDesc)
}
// Connect to gRPC server
conn, err := grpc.NewClient(registerServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := openConn(inspectServerAddr)
if err != nil {
return fmt.Errorf("failed to connect to server: %w", err)
return err
}
defer conn.Close()
+2 -8
View File
@@ -11,8 +11,6 @@ import (
"github.com/google/gar/proto"
"github.com/google/uuid"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
@@ -73,15 +71,13 @@ func runTrigger(cmd *cobra.Command, args []string) error {
cancel()
}()
// Connect to gRPC server
conn, err := grpc.NewClient(triggerServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := openConn(inspectServerAddr)
if err != nil {
return fmt.Errorf("failed to connect to server: %w", err)
return err
}
defer conn.Close()
client := proto.NewGARServiceClient(conn)
stream, err := client.TriggerSession(ctx, &proto.TriggerSessionRequest{
SessionId: triggerSessionID,
Inputs: inputs,
@@ -105,7 +101,5 @@ func runTrigger(cmd *cobra.Command, args []string) error {
fmt.Printf("[%s] %s\n", resp.State, resp.Output.Data)
}
}
fmt.Println("Session completed successfully")
return nil
}