diff --git a/cmd/uncloud/service/rm.go b/cmd/uncloud/service/rm.go index 85aee389..4aa36d9d 100644 --- a/cmd/uncloud/service/rm.go +++ b/cmd/uncloud/service/rm.go @@ -3,7 +3,10 @@ package service import ( "context" "fmt" + "github.com/docker/cli/cli/streams" + "github.com/docker/compose/v2/pkg/progress" "github.com/spf13/cobra" + "os" "uncloud/internal/cli" ) @@ -40,10 +43,12 @@ func rm(ctx context.Context, uncli *cli.CLI, opts rmOptions) error { defer client.Close() for _, s := range opts.services { - if err = client.RemoveService(ctx, s); err != nil { - return fmt.Errorf("remove service %q: %w", s, err) - } - fmt.Printf("Service %q removed.\n", s) + err = progress.RunWithTitle(ctx, func(ctx context.Context) error { + if err = client.RemoveService(ctx, s); err != nil { + return fmt.Errorf("remove service '%s': %w", s, err) + } + return nil + }, streams.NewOut(os.Stdout), "Removing service "+s) } return nil diff --git a/internal/api/container.go b/internal/api/container.go index 177306bc..1d6ca21e 100644 --- a/internal/api/container.go +++ b/internal/api/container.go @@ -18,6 +18,10 @@ type Container struct { types.Container } +func (c *Container) Name() string { + return c.Names[0][1:] // Remove leading slash. +} + // ServiceID returns the ID of the service this container belongs to. func (c *Container) ServiceID() string { return c.Labels[LabelServiceID] diff --git a/internal/cli/client/container.go b/internal/cli/client/container.go index 8e1ac7fd..0b386251 100644 --- a/internal/cli/client/container.go +++ b/internal/cli/client/container.go @@ -229,7 +229,7 @@ func (cli *Client) InspectContainer(ctx context.Context, serviceID, containerID } for _, c := range svc.Containers { - if c.Container.ID == containerID || c.Container.Names[0] == containerID { + if c.Container.ID == containerID || c.Container.Name() == containerID { ctr = c } } @@ -254,7 +254,7 @@ func (cli *Client) StartContainer(ctx context.Context, serviceID, containerID st ctx = proxyToMachine(ctx, machine.Machine) pw := progress.ContextWriter(ctx) - eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) + eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name) pw.Event(progress.StartingEvent(eventID)) if err = cli.Docker.StartContainer(ctx, ctr.Container.ID, container.StartOptions{}); err != nil { @@ -281,7 +281,7 @@ func (cli *Client) StopContainer( ctx = proxyToMachine(ctx, machine.Machine) pw := progress.ContextWriter(ctx) - eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) + eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name) pw.Event(progress.StoppingEvent(eventID)) if err = cli.Docker.StopContainer(ctx, ctr.Container.ID, opts); err != nil { @@ -308,7 +308,7 @@ func (cli *Client) RemoveContainer( ctx = proxyToMachine(ctx, machine.Machine) pw := progress.ContextWriter(ctx) - eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Names[0], machine.Machine.Name) + eventID := fmt.Sprintf("Container %s on %s", ctr.Container.Name(), machine.Machine.Name) pw.Event(progress.RemovingEvent(eventID)) if err = cli.Docker.RemoveContainer(ctx, ctr.Container.ID, opts); err != nil { diff --git a/test/e2e/service_test.go b/test/e2e/service_test.go index 1f0e3913..6e1fd049 100644 --- a/test/e2e/service_test.go +++ b/test/e2e/service_test.go @@ -13,12 +13,12 @@ import ( "uncloud/internal/ucind" ) -func TestService(t *testing.T) { +func TestRunService(t *testing.T) { t.Parallel() - clusterName := "ucind-test.service" + clusterName := "ucind-test.run-service" ctx := context.Background() - c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 1}, true) + c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 3}, true) cli, err := c.Machines[0].Connect(ctx) require.NoError(t, err) @@ -59,17 +59,6 @@ func TestService(t *testing.T) { err = cli.RemoveContainer(ctx, svcName, ctr.ID, container.RemoveOptions{}) require.ErrorIs(t, err, client.ErrNotFound) }) -} - -func TestRunService(t *testing.T) { - t.Parallel() - - clusterName := "ucind-test.run-service" - ctx := context.Background() - c, _ := createTestCluster(t, clusterName, ucind.CreateClusterOptions{Machines: 3}, true) - - cli, err := c.Machines[0].Connect(ctx) - require.NoError(t, err) t.Run("1 replica", func(t *testing.T) { t.Parallel()