Skip to content

Commit

Permalink
Refactor the common opts bits
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Feb 7, 2024
1 parent 6808058 commit 94e77c2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 64 deletions.
25 changes: 4 additions & 21 deletions internal/commands/waypoint/agent/group.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
package agent

import (
"context"
"fmt"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models"
"github.com/hashicorp/hcp/internal/commands/waypoint/opts"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
"github.com/hashicorp/hcp/internal/pkg/iostreams"
"github.com/hashicorp/hcp/internal/pkg/profile"
"github.com/pkg/errors"
)

type GroupOpts struct {
Ctx context.Context
Profile *profile.Profile
IO iostreams.IOStreams
Output *format.Outputter

WS waypoint_service.ClientService
opts.WaypointOpts

Name string
Description string
}

func NewCmdGroup(ctx *cmd.Context) *cmd.Command {
opts := &GroupOpts{
Ctx: ctx.ShutdownCtx,
Profile: ctx.Profile,
IO: ctx.IO,
Output: ctx.Output,
WS: waypoint_service.New(ctx.HCP, nil),
WaypointOpts: opts.New(ctx),
}

cmd := &cmd.Command{
Expand Down Expand Up @@ -87,17 +76,11 @@ func NewCmdGroupCreate(ctx *cmd.Context, opts *GroupOpts) *cmd.Command {
}

func agentGroupCreate(log hclog.Logger, opts *GroupOpts) error {
resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
ns, err := opts.Namespace()
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

_, err = opts.WS.WaypointServiceCreateAgentGroup(&waypoint_service.WaypointServiceCreateAgentGroupParams{
Expand Down
26 changes: 4 additions & 22 deletions internal/commands/waypoint/agent/queue.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package agent

import (
"context"
"fmt"
"time"

"github.com/go-openapi/strfmt"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models"
"github.com/hashicorp/hcp/internal/commands/waypoint/opts"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
"github.com/hashicorp/hcp/internal/pkg/iostreams"
"github.com/hashicorp/hcp/internal/pkg/profile"
"github.com/pkg/errors"
)

type QueueOpts struct {
Ctx context.Context
Profile *profile.Profile
IO iostreams.IOStreams
Output *format.Outputter

WS waypoint_service.ClientService
opts.WaypointOpts

Group string
Id string
Expand All @@ -35,11 +27,7 @@ type QueueOpts struct {

func NewCmdQueue(ctx *cmd.Context) *cmd.Command {
opts := &QueueOpts{
Ctx: ctx.ShutdownCtx,
Profile: ctx.Profile,
IO: ctx.IO,
Output: ctx.Output,
WS: waypoint_service.New(ctx.HCP, nil),
WaypointOpts: opts.New(ctx),
}

cmd := &cmd.Command{
Expand Down Expand Up @@ -92,17 +80,11 @@ func NewCmdQueue(ctx *cmd.Context) *cmd.Command {
var agentQueueDuration = 60 * time.Second

func agentQueue(log hclog.Logger, opts *QueueOpts) error {
resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
ns, err := opts.Namespace()
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

_, err = opts.WS.WaypointServiceQueueAgentOperation(&waypoint_service.WaypointServiceQueueAgentOperationParams{
Expand Down
25 changes: 4 additions & 21 deletions internal/commands/waypoint/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models"
"github.com/hashicorp/hcp/internal/commands/waypoint/opts"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
"github.com/hashicorp/hcp/internal/pkg/iostreams"
"github.com/hashicorp/hcp/internal/pkg/profile"
waypointagent "github.com/hashicorp/hcp/internal/pkg/waypoint-agent"
"github.com/pkg/errors"
)

type RunOpts struct {
Ctx context.Context
Profile *profile.Profile
IO iostreams.IOStreams
Output *format.Outputter

WS waypoint_service.ClientService
opts.WaypointOpts
Groups []string

ConfigPath string
Expand All @@ -33,11 +26,7 @@ type RunOpts struct {

func NewCmdRun(ctx *cmd.Context) *cmd.Command {
opts := &RunOpts{
Ctx: ctx.ShutdownCtx,
Profile: ctx.Profile,
IO: ctx.IO,
Output: ctx.Output,
WS: waypoint_service.New(ctx.HCP, nil),
WaypointOpts: opts.New(ctx),
}

cmd := &cmd.Command{
Expand Down Expand Up @@ -75,17 +64,11 @@ func agentRun(log hclog.Logger, opts *RunOpts) error {

opts.Groups = cfg.Groups()

resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
ns, err := opts.Namespace()
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

// check the groups!
Expand Down
45 changes: 45 additions & 0 deletions internal/commands/waypoint/opts/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package opts

import (
"context"

"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/iostreams"
"github.com/hashicorp/hcp/internal/pkg/profile"
"github.com/pkg/errors"
)

func New(ctx *cmd.Context) WaypointOpts {
return WaypointOpts{
Ctx: ctx.ShutdownCtx,
Profile: ctx.Profile,
IO: ctx.IO,
Output: ctx.Output,
WS: waypoint_service.New(ctx.HCP, nil),
}
}

type WaypointOpts struct {
WS waypoint_service.ClientService

Ctx context.Context
Profile *profile.Profile
IO iostreams.IOStreams
Output *format.Outputter
}

func (w *WaypointOpts) Namespace() (*models.HashicorpCloudWaypointNamespace, error) {
resp, err := w.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: w.Profile.OrganizationID,
LocationProjectID: w.Profile.ProjectID,
Context: w.Ctx,
}, nil)
if err != nil {
return nil, errors.Wrapf(err, "Unable to access HCP project")
}

return resp.Payload.Namespace, nil
}

0 comments on commit 94e77c2

Please sign in to comment.