Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/DefangLabs/defang/src/pkg/setup"
"github.com/DefangLabs/defang/src/pkg/surveyor"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/DefangLabs/defang/src/pkg/timeutils"
"github.com/DefangLabs/defang/src/pkg/track"
"github.com/DefangLabs/defang/src/pkg/types"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
Expand Down Expand Up @@ -857,11 +858,11 @@ var debugCmd = &cobra.Command{
}

now := time.Now()
sinceTs, err := cli.ParseTimeOrDuration(since, now)
sinceTs, err := timeutils.ParseTimeOrDuration(since, now)
if err != nil {
return fmt.Errorf("invalid 'since' time: %w", err)
}
untilTs, err := cli.ParseTimeOrDuration(until, now)
untilTs, err := timeutils.ParseTimeOrDuration(until, now)
if err != nil {
return fmt.Errorf("invalid 'until' time: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/cli/command/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DefangLabs/defang/src/pkg/logs"
"github.com/DefangLabs/defang/src/pkg/modes"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/DefangLabs/defang/src/pkg/timeutils"
"github.com/DefangLabs/defang/src/pkg/track"
"github.com/DefangLabs/defang/src/pkg/types"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
Expand Down Expand Up @@ -569,12 +570,12 @@ func handleLogsCmd(cmd *cobra.Command, args []string) error {
}

now := time.Now()
sinceTs, err := cli.ParseTimeOrDuration(since, now)
sinceTs, err := timeutils.ParseTimeOrDuration(since, now)
if err != nil {
return fmt.Errorf("invalid 'since' duration or time: %w", err)
}
sinceTs = sinceTs.UTC()
untilTs, err := cli.ParseTimeOrDuration(until, now)
untilTs, err := timeutils.ParseTimeOrDuration(until, now)
if err != nil {
return fmt.Errorf("invalid 'until' duration or time: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cli/command/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"path/filepath"

"github.com/DefangLabs/defang/src/pkg/agent/tools"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp"
"github.com/DefangLabs/defang/src/pkg/mcp/tools"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/mark3labs/mcp-go/server"
"github.com/spf13/cobra"
Expand Down
1 change: 1 addition & 0 deletions src/pkg/agent/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package agent
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"strconv"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/cli"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/cli/compose"
"github.com/DefangLabs/defang/src/pkg/login"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/mcp/deployment_info"
"github.com/DefangLabs/defang/src/pkg/modes"
"github.com/DefangLabs/defang/src/pkg/term"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"strings"

"github.com/DefangLabs/defang/src/pkg/agent/common"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/cli/compose"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/modes"
"github.com/DefangLabs/defang/src/pkg/term"
)

func handleDeployTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
func HandleDeployTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
err := common.ProviderNotConfiguredError(*providerId)
if err != nil {
return "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"testing"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/cli/compose"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/modes"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestHandleDeployTool(t *testing.T) {

// Call the function
loader := &client.MockLoader{}
result, err := handleDeployTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)
result, err := HandleDeployTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)

// Verify error expectations
if tt.expectedError != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"errors"
"fmt"

"github.com/DefangLabs/defang/src/pkg/agent/common"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/bufbuild/connect-go"
)

func handleDestroyTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
func HandleDestroyTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
err := common.ProviderNotConfiguredError(*providerId)
if err != nil {
return "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"testing"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/bufbuild/connect-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestHandleDestroyTool(t *testing.T) {

// Call the function
loader := &client.MockLoader{}
result, err := handleDestroyTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)
result, err := HandleDestroyTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)

// Verify error expectations
if tt.expectedError != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type EstimateParams struct {
Region string `json:"region"`
}

func parseEstimateParams(request mcp.CallToolRequest, providerId *cliClient.ProviderID) (EstimateParams, error) {
func ParseEstimateParams(request mcp.CallToolRequest, providerId *cliClient.ProviderID) (EstimateParams, error) {
modeString, err := request.RequireString("deployment_mode")
if err != nil {
modeString = "AFFORDABLE" // Default to AFFORDABLE if not provided
Expand Down Expand Up @@ -50,7 +50,7 @@ func parseEstimateParams(request mcp.CallToolRequest, providerId *cliClient.Prov
}, nil
}

func handleEstimateTool(ctx context.Context, loader cliClient.ProjectLoader, params EstimateParams, cluster string, cli CLIInterface) (string, error) {
func HandleEstimateTool(ctx context.Context, loader cliClient.ProjectLoader, params EstimateParams, cluster string, cli CLIInterface) (string, error) {
term.Debug("Function invoked: loader.LoadProject")
project, err := cli.LoadProject(ctx, loader)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestHandleEstimateTool(t *testing.T) {

// Call the function
loader := &client.MockLoader{}
params, err := parseEstimateParams(request, &providerID)
params, err := ParseEstimateParams(request, &providerID)
if err != nil {
// If parsing params fails, check if this was the expected error
if tt.expectedError != "" {
Expand All @@ -213,7 +213,7 @@ func TestHandleEstimateTool(t *testing.T) {
require.NoError(t, err)
}
}
result, err := handleEstimateTool(t.Context(), loader, params, "test-cluster", mockCLI)
result, err := HandleEstimateTool(t.Context(), loader, params, "test-cluster", mockCLI)

// Verify error expectations
if tt.expectedError != "" {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"strings"

"github.com/DefangLabs/defang/src/pkg/agent/common"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/term"
)

// handleListConfigTool handles the list config tool logic
func handleListConfigTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
// HandleListConfigTool handles the list config tool logic
func HandleListConfigTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
err := common.ProviderNotConfiguredError(*providerId)
if err != nil {
return "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"testing"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestHandleListConfigTool(t *testing.T) {

// Call the function
loader := &client.MockLoader{}
result, err := handleListConfigTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)
result, err := HandleListConfigTool(t.Context(), loader, &tt.providerID, "test-cluster", mockCLI)

// Verify error expectations
if tt.expectedError != "" {
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/mcp/tools/login.go → src/pkg/agent/tools/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"errors"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/auth"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/term"
)

// handleLoginTool handles the login tool logic
func handleLoginTool(ctx context.Context, cluster string, cli CLIInterface) (string, error) {
// HandleLoginTool handles the login tool logic
func HandleLoginTool(ctx context.Context, cluster string, cli CLIInterface) (string, error) {
term.Debug("Function invoked: cli.Connect")
client, err := cli.Connect(ctx, cluster)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestHandleLoginTool(t *testing.T) {

// Call the function
var err error
result, err := handleLoginTool(context.Background(), tt.cluster, mockCLI)
result, err := HandleLoginTool(context.Background(), tt.cluster, mockCLI)
if tt.expectedError != "" {
assert.EqualError(t, err, tt.expectedError)
} else {
Expand Down
52 changes: 22 additions & 30 deletions src/pkg/mcp/tools/logs.go → src/pkg/agent/tools/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,28 @@ import (
cliTypes "github.com/DefangLabs/defang/src/pkg/cli"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/DefangLabs/defang/src/pkg/timeutils"
"github.com/mark3labs/mcp-go/mcp"
)

type LogsParams struct {
DeploymentID string
Since time.Time
Until time.Time
Since string
Until string
}

func parseLogsParams(request mcp.CallToolRequest) (LogsParams, error) {
func ParseLogsParams(request mcp.CallToolRequest) LogsParams {
deploymentId := request.GetString("deployment_id", "")
since, err := request.RequireString("since")
if err != nil {
return LogsParams{}, fmt.Errorf("missing required parameter 'since': %w", err)
}
until, err := request.RequireString("until")
if err != nil {
return LogsParams{}, fmt.Errorf("missing required parameter 'until': %w", err)
}
var sinceTime, untilTime time.Time
if since != "" {
sinceTime, err = time.Parse(time.RFC3339, since)
if err != nil {
return LogsParams{}, fmt.Errorf("invalid parameter 'since', must be in RFC3339 format: %w", err)
}
}
if until != "" {
untilTime, err = time.Parse(time.RFC3339, until)
if err != nil {
return LogsParams{}, fmt.Errorf("invalid parameter 'until', must be in RFC3339 format: %w", err)
}
}
since := request.GetString("since", "")
until := request.GetString("until", "")
return LogsParams{
DeploymentID: deploymentId,
Since: sinceTime,
Until: untilTime,
}, nil
Since: since,
Until: until,
}
}

func handleLogsTool(ctx context.Context, loader cliClient.ProjectLoader, params LogsParams, cluster string, providerId *cliClient.ProviderID, cli CLIInterface) (string, error) {
func HandleLogsTool(ctx context.Context, loader cliClient.ProjectLoader, params LogsParams, cluster string, providerId *cliClient.ProviderID, cli CLIInterface) (string, error) {
term.Debug("Function invoked: loader.LoadProject")
project, err := cli.LoadProject(ctx, loader)
if err != nil {
Expand All @@ -70,10 +52,20 @@ func handleLogsTool(ctx context.Context, loader cliClient.ProjectLoader, params
return "", fmt.Errorf("provider not configured correctly: %w", err)
}

sinceTime, err := timeutils.ParseTimeOrDuration(params.Since, time.Now())
if err != nil {
return "", fmt.Errorf("failed to parse 'since' parameter: %w", err)
}

untilTime, err := timeutils.ParseTimeOrDuration(params.Until, time.Now())
if err != nil {
return "", fmt.Errorf("failed to parse 'until' parameter: %w", err)
}

err = cli.Tail(ctx, provider, project, cliTypes.TailOptions{
Deployment: params.DeploymentID,
Since: params.Since,
Until: params.Until,
Since: sinceTime,
Until: untilTime,
})

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/DefangLabs/defang/src/pkg/agent/common"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/bufbuild/connect-go"
"github.com/mark3labs/mcp-go/mcp"
Expand All @@ -15,7 +15,7 @@ type RemoveConfigParams struct {
Name string
}

func parseRemoveConfigParams(request mcp.CallToolRequest) (RemoveConfigParams, error) {
func ParseRemoveConfigParams(request mcp.CallToolRequest) (RemoveConfigParams, error) {
name, err := request.RequireString("name")
if err != nil || name == "" {
return RemoveConfigParams{}, fmt.Errorf("missing config `name`: %w", err)
Expand All @@ -25,8 +25,8 @@ func parseRemoveConfigParams(request mcp.CallToolRequest) (RemoveConfigParams, e
}, nil
}

// handleRemoveConfigTool handles the remove config tool logic
func handleRemoveConfigTool(ctx context.Context, loader cliClient.ProjectLoader, params RemoveConfigParams, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
// HandleRemoveConfigTool handles the remove config tool logic
func HandleRemoveConfigTool(ctx context.Context, loader cliClient.ProjectLoader, params RemoveConfigParams, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
err := common.ProviderNotConfiguredError(*providerId)
if err != nil {
return "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"testing"

"github.com/DefangLabs/defang/src/pkg/agent/common"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/bufbuild/connect-go"
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestHandleRemoveConfigTool(t *testing.T) {
},
}

params, err := parseRemoveConfigParams(request)
params, err := ParseRemoveConfigParams(request)
if err != nil {
if tt.expectError {
assert.EqualError(t, err, tt.expectedError)
Expand All @@ -168,7 +168,7 @@ func TestHandleRemoveConfigTool(t *testing.T) {

// Call the function
loader := &client.MockLoader{}
result, err := handleRemoveConfigTool(t.Context(), loader, params, &tt.providerID, "test-cluster", mockCLI)
result, err := HandleRemoveConfigTool(t.Context(), loader, params, &tt.providerID, "test-cluster", mockCLI)

// Verify error expectations
if tt.expectError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"fmt"
"strings"

"github.com/DefangLabs/defang/src/pkg/agent/common"
defangcli "github.com/DefangLabs/defang/src/pkg/cli"
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/mcp/common"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/bufbuild/connect-go"
)

func handleServicesTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
func HandleServicesTool(ctx context.Context, loader cliClient.ProjectLoader, providerId *cliClient.ProviderID, cluster string, cli CLIInterface) (string, error) {
err := common.ProviderNotConfiguredError(*providerId)
if err != nil {
return "", err
Expand Down
Loading
Loading