diff --git a/internal/cmd/acl.go b/internal/cmd/acl.go index 7a5e07c..c1c2e41 100644 --- a/internal/cmd/acl.go +++ b/internal/cmd/acl.go @@ -30,11 +30,10 @@ func getACLConfig() *coral.Command { command.Flags().BoolVar(&asJson, "json", false, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { @@ -99,11 +98,10 @@ func setACLConfig() *coral.Command { return err } - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { diff --git a/internal/cmd/auth_filter.go b/internal/cmd/auth_filter.go index 8510542..d7731f4 100644 --- a/internal/cmd/auth_filter.go +++ b/internal/cmd/auth_filter.go @@ -40,11 +40,10 @@ func listAuthFilterCommand() *coral.Command { command.Flags().Uint64Var(&authMethodID, "auth-method-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := &api.ListAuthFiltersRequest{} @@ -102,11 +101,10 @@ func createAuthFilterCommand() *coral.Command { } } - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { @@ -158,11 +156,10 @@ func deleteAuthFilterCommand() *coral.Command { command.Flags().Uint64Var(&authFilterID, "auth-filter-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := &api.DeleteAuthFilterRequest{ AuthFilterId: authFilterID, diff --git a/internal/cmd/auth_key.go b/internal/cmd/auth_key.go index 79a6663..430c220 100644 --- a/internal/cmd/auth_key.go +++ b/internal/cmd/auth_key.go @@ -46,11 +46,10 @@ func createAuthkeysCommand() *coral.Command { command.Flags().StringVar(&expiry, "expiry", "180d", "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { @@ -104,11 +103,10 @@ func deleteAuthKeyCommand() *coral.Command { command.Flags().Uint64Var(&authKeyId, "id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - grpcClient, c, err := target.createGRPCClient() + grpcClient, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := api.DeleteAuthKeyRequest{AuthKeyId: authKeyId} if _, err := grpcClient.DeleteAuthKey(context.Background(), connect.NewRequest(&req)); err != nil { @@ -138,11 +136,10 @@ func listAuthkeysCommand() *coral.Command { command.Flags().Uint64Var(&tailnetID, "tailnet-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { diff --git a/internal/cmd/auth_method.go b/internal/cmd/auth_method.go index b29f488..23d9e1c 100644 --- a/internal/cmd/auth_method.go +++ b/internal/cmd/auth_method.go @@ -34,11 +34,10 @@ func listAuthMethods() *coral.Command { command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) resp, err := client.ListAuthMethods(context.Background(), connect.NewRequest(&api.ListAuthMethodsRequest{})) @@ -97,11 +96,10 @@ func createOIDCAuthMethodCommand() *coral.Command { command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := &api.CreateAuthMethodRequest{ Type: "oidc", diff --git a/internal/cmd/derp_map.go b/internal/cmd/derp_map.go index 127b1b3..8fb5c16 100644 --- a/internal/cmd/derp_map.go +++ b/internal/cmd/derp_map.go @@ -38,11 +38,10 @@ func getDERPMap() *coral.Command { command.Flags().BoolVar(&asJson, "json", false, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) resp, err := client.GetDERPMap(context.Background(), connect.NewRequest(&api.GetDERPMapRequest{})) @@ -95,11 +94,10 @@ func setDERPMap() *coral.Command { command.Flags().StringVar(&file, "file", "", "") command.RunE = func(command *coral.Command, args []string) error { - grpcClient, c, err := target.createGRPCClient() + grpcClient, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) rawJson, err := ioutil.ReadFile(file) if err != nil { diff --git a/internal/cmd/dns.go b/internal/cmd/dns.go index ab87e34..4192746 100644 --- a/internal/cmd/dns.go +++ b/internal/cmd/dns.go @@ -25,11 +25,10 @@ func getDNSConfig() *coral.Command { command.Flags().Uint64Var(&tailnetID, "tailnet-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { @@ -84,11 +83,10 @@ func setDNSConfig() *coral.Command { command.Flags().BoolVarP(&overrideLocalDNS, "override-local-dns", "", false, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { diff --git a/internal/cmd/funcs.go b/internal/cmd/funcs.go index 14a6298..e4f7ecf 100644 --- a/internal/cmd/funcs.go +++ b/internal/cmd/funcs.go @@ -6,7 +6,6 @@ import ( "github.com/bufbuild/connect-go" api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1" apiconnect "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1/ionscalev1connect" - "io" ) func findTailnet(client apiconnect.IonscaleServiceClient, tailnet string, tailnetID uint64) (*api.Tailnet, error) { @@ -46,9 +45,3 @@ func findAuthMethod(client apiconnect.IonscaleServiceClient, authMethod string, return nil, fmt.Errorf("requested auth method not found or you are not authorized for this tailnet") } - -func safeClose(c io.Closer) { - if c != nil { - _ = c.Close() - } -} diff --git a/internal/cmd/machine.go b/internal/cmd/machine.go index 4111fc6..bd26c47 100644 --- a/internal/cmd/machine.go +++ b/internal/cmd/machine.go @@ -41,11 +41,10 @@ func deleteMachineCommand() *coral.Command { command.Flags().Uint64Var(&machineID, "machine-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := api.DeleteMachineRequest{MachineId: machineID} if _, err := client.DeleteMachine(context.Background(), connect.NewRequest(&req)); err != nil { @@ -73,11 +72,10 @@ func expireMachineCommand() *coral.Command { command.Flags().Uint64Var(&machineID, "machine-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := api.ExpireMachineRequest{MachineId: machineID} if _, err := client.ExpireMachine(context.Background(), connect.NewRequest(&req)); err != nil { @@ -108,11 +106,10 @@ func listMachinesCommand() *coral.Command { command.Flags().Uint64Var(&tailnetID, "tailnet-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { @@ -160,11 +157,10 @@ func getMachineRoutesCommand() *coral.Command { command.Flags().Uint64Var(&machineID, "machine-id", 0, "") command.RunE = func(command *coral.Command, args []string) error { - grpcClient, c, err := target.createGRPCClient() + grpcClient, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) req := api.GetMachineRoutesRequest{MachineId: machineID} resp, err := grpcClient.GetMachineRoutes(context.Background(), connect.NewRequest(&req)) @@ -199,11 +195,10 @@ func setMachineRoutesCommand() *coral.Command { command.Flags().StringSliceVar(&allowedIps, "allowed-ips", []string{}, "") command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) var prefixes []netaddr.IPPrefix for _, r := range allowedIps { diff --git a/internal/cmd/tailnet.go b/internal/cmd/tailnet.go index 23667c2..ea5ae24 100644 --- a/internal/cmd/tailnet.go +++ b/internal/cmd/tailnet.go @@ -40,11 +40,10 @@ func listTailnetsCommand() *coral.Command { command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) resp, err := client.ListTailnets(context.Background(), connect.NewRequest(&api.ListTailnetRequest{})) @@ -80,11 +79,10 @@ func createTailnetsCommand() *coral.Command { command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) resp, err := client.CreateTailnet(context.Background(), connect.NewRequest(&api.CreateTailnetRequest{Name: name})) @@ -121,11 +119,10 @@ func deleteTailnetCommand() *coral.Command { command.RunE = func(command *coral.Command, args []string) error { - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { return err } - defer safeClose(c) tailnet, err := findTailnet(client, tailnetName, tailnetID) if err != nil { diff --git a/internal/cmd/target.go b/internal/cmd/target.go index 65622b2..5758fb5 100644 --- a/internal/cmd/target.go +++ b/internal/cmd/target.go @@ -5,7 +5,6 @@ import ( "github.com/jsiebens/ionscale/pkg/client/ionscale" api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1/ionscalev1connect" "github.com/muesli/coral" - "io" ) const ( @@ -26,14 +25,14 @@ func (t *Target) prepareCommand(cmd *coral.Command) { cmd.Flags().StringVar(&t.systemAdminKey, "admin-key", "", "If specified, the given value will be used as the key to generate a Bearer token for the call. This can also be specified via the IONSCALE_ADMIN_KEY environment variable.") } -func (t *Target) createGRPCClient() (api.IonscaleServiceClient, io.Closer, error) { +func (t *Target) createGRPCClient() (api.IonscaleServiceClient, error) { addr := t.getAddr() skipVerify := t.getInsecureSkipVerify() systemAdminKey := t.getSystemAdminKey() auth, err := ionscale.LoadClientAuth(systemAdminKey) if err != nil { - return nil, nil, err + return nil, err } return ionscale.NewClient(auth, addr, skipVerify) diff --git a/internal/cmd/version.go b/internal/cmd/version.go index 372c09a..7cf8de4 100644 --- a/internal/cmd/version.go +++ b/internal/cmd/version.go @@ -27,7 +27,7 @@ Client: Git Revision: %s `, clientVersion, clientRevision) - client, c, err := target.createGRPCClient() + client, err := target.createGRPCClient() if err != nil { fmt.Printf(` Server: @@ -35,7 +35,6 @@ Server: `, err) return } - defer safeClose(c) resp, err := client.GetVersion(context.Background(), connect.NewRequest(&api.GetVersionRequest{})) if err != nil { diff --git a/pkg/client/ionscale/client.go b/pkg/client/ionscale/client.go index c765b28..c679e47 100644 --- a/pkg/client/ionscale/client.go +++ b/pkg/client/ionscale/client.go @@ -6,11 +6,10 @@ import ( "fmt" "github.com/bufbuild/connect-go" api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1/ionscalev1connect" - "io" "net/http" ) -func NewClient(clientAuth ClientAuth, serverURL string, insecureSkipVerify bool) (api.IonscaleServiceClient, io.Closer, error) { +func NewClient(clientAuth ClientAuth, serverURL string, insecureSkipVerify bool) (api.IonscaleServiceClient, error) { tlsConfig := &tls.Config{ InsecureSkipVerify: insecureSkipVerify, } @@ -22,7 +21,7 @@ func NewClient(clientAuth ClientAuth, serverURL string, insecureSkipVerify bool) } interceptors := connect.WithInterceptors(NewAuthenticationInterceptor(clientAuth)) - return api.NewIonscaleServiceClient(client, serverURL, interceptors), nil, nil + return api.NewIonscaleServiceClient(client, serverURL, interceptors), nil } func NewAuthenticationInterceptor(clientAuth ClientAuth) connect.UnaryInterceptorFunc {