From 42d49eb0902fbda37c76325727aa13458a649e35 Mon Sep 17 00:00:00 2001 From: William Petit Date: Sun, 25 Jun 2023 11:45:43 -0600 Subject: [PATCH] feat: move client to public package --- internal/agent/agent.go | 2 +- internal/agent/context.go | 2 +- internal/agent/controller/spec/controller.go | 2 +- internal/agent/options.go | 2 +- internal/command/api/agent/count.go | 2 +- internal/command/api/agent/delete.go | 2 +- internal/command/api/agent/get.go | 2 +- internal/command/api/agent/query.go | 2 +- internal/command/api/agent/spec/delete.go | 2 +- internal/command/api/agent/spec/get.go | 2 +- internal/command/api/agent/spec/update.go | 2 +- internal/command/api/agent/update.go | 2 +- pkg/client/alias.go | 23 +++++++++++++++++++ {internal => pkg}/client/client.go | 0 {internal => pkg}/client/delete_agent.go | 2 +- {internal => pkg}/client/delete_agent_spec.go | 3 +-- {internal => pkg}/client/get_agent.go | 5 ++-- {internal => pkg}/client/get_agent_specs.go | 3 +-- {internal => pkg}/client/options.go | 0 {internal => pkg}/client/query_agents.go | 6 ++--- {internal => pkg}/client/register_agent.go | 2 +- {internal => pkg}/client/update_agent.go | 0 {internal => pkg}/client/update_agent_spec.go | 2 +- 23 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 pkg/client/alias.go rename {internal => pkg}/client/client.go (100%) rename {internal => pkg}/client/delete_agent.go (79%) rename {internal => pkg}/client/delete_agent_spec.go (74%) rename {internal => pkg}/client/get_agent.go (63%) rename {internal => pkg}/client/get_agent_specs.go (77%) rename {internal => pkg}/client/options.go (100%) rename {internal => pkg}/client/query_agents.go (94%) rename {internal => pkg}/client/register_agent.go (87%) rename {internal => pkg}/client/update_agent.go (100%) rename {internal => pkg}/client/update_agent_spec.go (85%) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index eccc791..fcedc18 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -6,8 +6,8 @@ import ( "forge.cadoles.com/Cadoles/emissary/internal/agent/metadata" "forge.cadoles.com/Cadoles/emissary/internal/auth/agent" - "forge.cadoles.com/Cadoles/emissary/internal/client" "forge.cadoles.com/Cadoles/emissary/internal/jwk" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" "gitlab.com/wpetit/goweb/logger" diff --git a/internal/agent/context.go b/internal/agent/context.go index a10b409..83a80d1 100644 --- a/internal/agent/context.go +++ b/internal/agent/context.go @@ -3,7 +3,7 @@ package agent import ( "context" - "forge.cadoles.com/Cadoles/emissary/internal/client" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" ) diff --git a/internal/agent/controller/spec/controller.go b/internal/agent/controller/spec/controller.go index 6b2a3d1..87689b5 100644 --- a/internal/agent/controller/spec/controller.go +++ b/internal/agent/controller/spec/controller.go @@ -4,8 +4,8 @@ import ( "context" "forge.cadoles.com/Cadoles/emissary/internal/agent" - "forge.cadoles.com/Cadoles/emissary/internal/client" "forge.cadoles.com/Cadoles/emissary/internal/datastore" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) diff --git a/internal/agent/options.go b/internal/agent/options.go index b1514e2..3d4aa86 100644 --- a/internal/agent/options.go +++ b/internal/agent/options.go @@ -4,7 +4,7 @@ import ( "time" "forge.cadoles.com/Cadoles/emissary/internal/agent/metadata" - "forge.cadoles.com/Cadoles/emissary/internal/client" + "forge.cadoles.com/Cadoles/emissary/pkg/client" ) type Options struct { diff --git a/internal/command/api/agent/count.go b/internal/command/api/agent/count.go index 12bc439..42834a4 100644 --- a/internal/command/api/agent/count.go +++ b/internal/command/api/agent/count.go @@ -3,10 +3,10 @@ package agent import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/delete.go b/internal/command/api/agent/delete.go index b6133eb..3cb230f 100644 --- a/internal/command/api/agent/delete.go +++ b/internal/command/api/agent/delete.go @@ -3,12 +3,12 @@ package agent import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/datastore" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/get.go b/internal/command/api/agent/get.go index 5493200..bba75df 100644 --- a/internal/command/api/agent/get.go +++ b/internal/command/api/agent/get.go @@ -3,11 +3,11 @@ package agent import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/query.go b/internal/command/api/agent/query.go index 5e1ffa0..d763525 100644 --- a/internal/command/api/agent/query.go +++ b/internal/command/api/agent/query.go @@ -3,11 +3,11 @@ package agent import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/datastore" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/spec/delete.go b/internal/command/api/agent/spec/delete.go index a3693f2..e9762e4 100644 --- a/internal/command/api/agent/spec/delete.go +++ b/internal/command/api/agent/spec/delete.go @@ -3,12 +3,12 @@ package spec import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" "forge.cadoles.com/Cadoles/emissary/internal/spec" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/spec/get.go b/internal/command/api/agent/spec/get.go index 0d03693..460f239 100644 --- a/internal/command/api/agent/spec/get.go +++ b/internal/command/api/agent/spec/get.go @@ -3,11 +3,11 @@ package spec import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/internal/command/api/agent/spec/update.go b/internal/command/api/agent/spec/update.go index 2377b6c..fb359c4 100644 --- a/internal/command/api/agent/spec/update.go +++ b/internal/command/api/agent/spec/update.go @@ -4,12 +4,12 @@ import ( "encoding/json" "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" "forge.cadoles.com/Cadoles/emissary/internal/spec" + "forge.cadoles.com/Cadoles/emissary/pkg/client" jsonpatch "github.com/evanphx/json-patch/v5" "github.com/pkg/errors" "github.com/urfave/cli/v2" diff --git a/internal/command/api/agent/update.go b/internal/command/api/agent/update.go index 121b97d..1152819 100644 --- a/internal/command/api/agent/update.go +++ b/internal/command/api/agent/update.go @@ -3,11 +3,11 @@ package agent import ( "os" - "forge.cadoles.com/Cadoles/emissary/internal/client" agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag" "forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr" clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag" "forge.cadoles.com/Cadoles/emissary/internal/format" + "forge.cadoles.com/Cadoles/emissary/pkg/client" "github.com/pkg/errors" "github.com/urfave/cli/v2" ) diff --git a/pkg/client/alias.go b/pkg/client/alias.go new file mode 100644 index 0000000..cad0060 --- /dev/null +++ b/pkg/client/alias.go @@ -0,0 +1,23 @@ +package client + +import ( + "forge.cadoles.com/Cadoles/emissary/internal/agent/metadata" + "forge.cadoles.com/Cadoles/emissary/internal/datastore" + "forge.cadoles.com/Cadoles/emissary/internal/jwk" + "forge.cadoles.com/Cadoles/emissary/internal/spec" +) + +type ( + Spec = spec.Spec + SpecName = spec.Name +) + +type ( + AgentID = datastore.AgentID + Agent = datastore.Agent + AgentStatus = datastore.AgentStatus +) + +type MetadataTuple = metadata.Tuple + +type Key = jwk.Key diff --git a/internal/client/client.go b/pkg/client/client.go similarity index 100% rename from internal/client/client.go rename to pkg/client/client.go diff --git a/internal/client/delete_agent.go b/pkg/client/delete_agent.go similarity index 79% rename from internal/client/delete_agent.go rename to pkg/client/delete_agent.go index 2308cce..8eb68ce 100644 --- a/internal/client/delete_agent.go +++ b/pkg/client/delete_agent.go @@ -8,7 +8,7 @@ import ( "github.com/pkg/errors" ) -func (c *Client) DeleteAgent(ctx context.Context, agentID datastore.AgentID, funcs ...OptionFunc) (datastore.AgentID, error) { +func (c *Client) DeleteAgent(ctx context.Context, agentID AgentID, funcs ...OptionFunc) (AgentID, error) { response := withResponse[struct { AgentID int64 `json:"agentId"` }]() diff --git a/internal/client/delete_agent_spec.go b/pkg/client/delete_agent_spec.go similarity index 74% rename from internal/client/delete_agent_spec.go rename to pkg/client/delete_agent_spec.go index 179eaa8..be479e8 100644 --- a/internal/client/delete_agent_spec.go +++ b/pkg/client/delete_agent_spec.go @@ -4,12 +4,11 @@ import ( "context" "fmt" - "forge.cadoles.com/Cadoles/emissary/internal/datastore" "forge.cadoles.com/Cadoles/emissary/internal/spec" "github.com/pkg/errors" ) -func (c *Client) DeleteAgentSpec(ctx context.Context, agentID datastore.AgentID, name spec.Name, funcs ...OptionFunc) (spec.Name, error) { +func (c *Client) DeleteAgentSpec(ctx context.Context, agentID AgentID, name SpecName, funcs ...OptionFunc) (SpecName, error) { payload := struct { Name spec.Name `json:"name"` }{ diff --git a/internal/client/get_agent.go b/pkg/client/get_agent.go similarity index 63% rename from internal/client/get_agent.go rename to pkg/client/get_agent.go index d95ec36..fd86f3b 100644 --- a/internal/client/get_agent.go +++ b/pkg/client/get_agent.go @@ -4,13 +4,12 @@ import ( "context" "fmt" - "forge.cadoles.com/Cadoles/emissary/internal/datastore" "github.com/pkg/errors" ) -func (c *Client) GetAgent(ctx context.Context, agentID datastore.AgentID, funcs ...OptionFunc) (*datastore.Agent, error) { +func (c *Client) GetAgent(ctx context.Context, agentID AgentID, funcs ...OptionFunc) (*Agent, error) { response := withResponse[struct { - Agent *datastore.Agent `json:"agent"` + Agent *Agent `json:"agent"` }]() path := fmt.Sprintf("/api/v1/agents/%d", agentID) diff --git a/internal/client/get_agent_specs.go b/pkg/client/get_agent_specs.go similarity index 77% rename from internal/client/get_agent_specs.go rename to pkg/client/get_agent_specs.go index 8f36e20..fd42b2c 100644 --- a/internal/client/get_agent_specs.go +++ b/pkg/client/get_agent_specs.go @@ -4,12 +4,11 @@ import ( "context" "fmt" - "forge.cadoles.com/Cadoles/emissary/internal/datastore" "forge.cadoles.com/Cadoles/emissary/internal/spec" "github.com/pkg/errors" ) -func (c *Client) GetAgentSpecs(ctx context.Context, agentID datastore.AgentID, funcs ...OptionFunc) ([]spec.Spec, error) { +func (c *Client) GetAgentSpecs(ctx context.Context, agentID AgentID, funcs ...OptionFunc) ([]Spec, error) { response := withResponse[struct { Specs []*spec.RawSpec `json:"specs"` }]() diff --git a/internal/client/options.go b/pkg/client/options.go similarity index 100% rename from internal/client/options.go rename to pkg/client/options.go diff --git a/internal/client/query_agents.go b/pkg/client/query_agents.go similarity index 94% rename from internal/client/query_agents.go rename to pkg/client/query_agents.go index 8a458d2..4bbd41b 100644 --- a/internal/client/query_agents.go +++ b/pkg/client/query_agents.go @@ -16,8 +16,8 @@ type QueryAgentsOptions struct { Limit *int Offset *int Thumbprints []string - IDs []datastore.AgentID - Statuses []datastore.AgentStatus + IDs []AgentID + Statuses []AgentStatus } func WithQueryAgentsOptions(funcs ...OptionFunc) QueryAgentsOptionFunc { @@ -56,7 +56,7 @@ func WithQueryAgentsStatus(statuses ...datastore.AgentStatus) QueryAgentsOptionF } } -func (c *Client) QueryAgents(ctx context.Context, funcs ...QueryAgentsOptionFunc) ([]*datastore.Agent, int, error) { +func (c *Client) QueryAgents(ctx context.Context, funcs ...QueryAgentsOptionFunc) ([]*Agent, int, error) { options := &QueryAgentsOptions{} for _, fn := range funcs { fn(options) diff --git a/internal/client/register_agent.go b/pkg/client/register_agent.go similarity index 87% rename from internal/client/register_agent.go rename to pkg/client/register_agent.go index a79760a..1a41922 100644 --- a/internal/client/register_agent.go +++ b/pkg/client/register_agent.go @@ -9,7 +9,7 @@ import ( "github.com/pkg/errors" ) -func (c *Client) RegisterAgent(ctx context.Context, key jwk.Key, thumbprint string, meta []metadata.Tuple, funcs ...OptionFunc) (*datastore.Agent, error) { +func (c *Client) RegisterAgent(ctx context.Context, key Key, thumbprint string, meta []MetadataTuple, funcs ...OptionFunc) (*Agent, error) { keySet, err := jwk.PublicKeySet(key) if err != nil { return nil, errors.WithStack(err) diff --git a/internal/client/update_agent.go b/pkg/client/update_agent.go similarity index 100% rename from internal/client/update_agent.go rename to pkg/client/update_agent.go diff --git a/internal/client/update_agent_spec.go b/pkg/client/update_agent_spec.go similarity index 85% rename from internal/client/update_agent_spec.go rename to pkg/client/update_agent_spec.go index 36fce68..86f9c84 100644 --- a/internal/client/update_agent_spec.go +++ b/pkg/client/update_agent_spec.go @@ -10,7 +10,7 @@ import ( "github.com/pkg/errors" ) -func (c *Client) UpdateAgentSpec(ctx context.Context, agentID datastore.AgentID, spc spec.Spec, funcs ...OptionFunc) (*datastore.Spec, error) { +func (c *Client) UpdateAgentSpec(ctx context.Context, agentID AgentID, spc Spec, funcs ...OptionFunc) (Spec, error) { payload := struct { Name spec.Name `json:"name"` Revision int `json:"revision"`