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
18 changes: 16 additions & 2 deletions internal/cmd/deployment/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,22 @@ func runLogNonInteractive(f *cmdutil.Factory, opts *Options) (err error) {
opts.serviceID = service.ID
}

// When serviceID is available, resolve projectID and environmentID from the service
if opts.serviceID != "" {
if opts.deploymentID != "" && opts.serviceID == "" {
// A deployment ID already identifies its project, service and environment.
deployment, err := f.ApiClient.GetDeployment(context.Background(), opts.deploymentID)
if err != nil {
return fmt.Errorf("failed to get deployment: %w", err)
}
if deployment == nil {
return fmt.Errorf("deployment %s not found", opts.deploymentID)
}
opts.projectID = deployment.ProjectID
opts.serviceID = deployment.ServiceID
if opts.environmentID == "" {
opts.environmentID = deployment.EnvironmentID
}
} else if opts.serviceID != "" {
// When serviceID is available, resolve projectID and environmentID from the service
service, err := f.ApiClient.GetService(context.Background(), opts.serviceID, "", "", "")
if err != nil {
return fmt.Errorf("failed to get service: %w", err)
Expand Down
81 changes: 81 additions & 0 deletions internal/cmd/deployment/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package log_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"

deploymentLog "github.com/zeabur/cli/internal/cmd/deployment/log"
"github.com/zeabur/cli/internal/cmdutil"
"github.com/zeabur/cli/pkg/api"
"github.com/zeabur/cli/pkg/model"
"github.com/zeabur/cli/pkg/printer"
)

// stubClient stubs only the calls a deployment-id lookup should need. Any
// other Client method hits the nil embedded interface and panics.
type stubClient struct {
api.Client

deployment *model.Deployment

gotServiceID, gotEnvironmentID, gotDeploymentID string
}

func (c *stubClient) GetDeployment(_ context.Context, id string) (*model.Deployment, error) {
if c.deployment == nil || c.deployment.ID != id {
return nil, nil
}
return c.deployment, nil
}

func (c *stubClient) GetRuntimeLogs(_ context.Context, serviceID, environmentID, deploymentID string) (model.Logs, error) {
c.gotServiceID = serviceID
c.gotEnvironmentID = environmentID
c.gotDeploymentID = deploymentID
return model.Logs{}, nil
}

func TestLogResolvesServiceFromDeploymentID(t *testing.T) {
t.Parallel()

for _, interactive := range []bool{false, true} {
client := &stubClient{deployment: &model.Deployment{
ID: "6aa50431fbf9c810b64d3a2f",
ProjectID: "6aa50431fbf9c810b64d3a20",
ServiceID: "6aa50431fbf9c810b64d3a21",
EnvironmentID: "6aa50431fbf9c810b64d3a22",
}}
f := &cmdutil.Factory{
ApiClient: client,
Log: zap.NewNop().Sugar(),
Printer: printer.New(),
}
f.Interactive = interactive
f.JSON = true

cmd := deploymentLog.NewCmdLog(f)
cmd.SetArgs([]string{"--deployment-id", "6aa50431fbf9c810b64d3a2f"})
require.NoError(t, cmd.Execute(), "interactive=%v", interactive)

require.Equal(t, "6aa50431fbf9c810b64d3a21", client.gotServiceID, "interactive=%v", interactive)
require.Equal(t, "6aa50431fbf9c810b64d3a22", client.gotEnvironmentID, "interactive=%v", interactive)
require.Equal(t, "6aa50431fbf9c810b64d3a2f", client.gotDeploymentID, "interactive=%v", interactive)
}
}

func TestLogUnknownDeploymentID(t *testing.T) {
t.Parallel()

f := &cmdutil.Factory{
ApiClient: &stubClient{},
Log: zap.NewNop().Sugar(),
Printer: printer.New(),
}

cmd := deploymentLog.NewCmdLog(f)
cmd.SetArgs([]string{"--deployment-id", "6aa50431fbf9c810b64d3a2f"})
require.ErrorContains(t, cmd.Execute(), "not found")
}
Loading