Skip to content

Commit 7fa27dc

Browse files
committed
Get the user from the host-binary
Signed-off-by: David Gageot <[email protected]>
1 parent 1bd3813 commit 7fa27dc

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os/user"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func CurrentUser(ctx context.Context) *cobra.Command {
12+
return &cobra.Command{
13+
Use: "current-user",
14+
Short: "Get the current user",
15+
Args: cobra.NoArgs,
16+
RunE: func(*cobra.Command, []string) error {
17+
user, err := user.Current()
18+
if err != nil {
19+
return err
20+
}
21+
fmt.Print(user.Username)
22+
return nil
23+
},
24+
}
25+
}

src/extension/host-binary/cmd/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"github.com/docker/labs-ai-tools-for-devs/pkg/client"
8-
secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
9-
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
10-
"github.com/spf13/cobra"
117
"os"
128
"os/signal"
139
"slices"
1410
"syscall"
11+
12+
"github.com/docker/labs-ai-tools-for-devs/cmd/commands"
13+
"github.com/docker/labs-ai-tools-for-devs/pkg/client"
14+
secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
15+
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
16+
"github.com/spf13/cobra"
1517
)
1618

1719
func main() {
@@ -25,6 +27,7 @@ func main() {
2527
cmd.AddCommand(UnauthorizeApp(ctx))
2628
cmd.AddCommand(ListOAuthApps(ctx))
2729
cmd.AddCommand(DeriveSecret(ctx))
30+
cmd.AddCommand(commands.CurrentUser(ctx))
2831
if err := cmd.Execute(); err != nil {
2932
fmt.Println(err)
3033
os.Exit(1)

src/extension/ui/src/utils/Files.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,18 @@ let user: string | null = null;
3636

3737
export const getUser = async (client: v1.DockerDesktopClient) => {
3838
if (user == null) {
39-
if (client.host.platform === "win32") {
40-
const result = await tryRunImageSync(client, [
41-
"--rm",
42-
"--network=none",
43-
"-e",
44-
"USERNAME",
45-
BUSYBOX,
46-
"/bin/sh",
47-
"-c",
48-
`\"echo $USERNAME\"`,
49-
]);
50-
user = result.trim();
51-
} else {
52-
const result = await tryRunImageSync(client, [
53-
"--rm",
54-
"--network=none",
55-
"-e",
56-
"USER",
57-
BUSYBOX,
58-
"/bin/sh",
59-
"-c",
60-
`'echo $USER'`,
61-
]);
62-
user = result.trim();
63-
}
39+
try {
40+
const result = await client.extension.host?.cli.exec("host-binary", ["current-user"]);
41+
if (result) {
42+
user = result.stdout.trim();
43+
return user;
44+
}
45+
} catch { }
46+
47+
client.desktopUI.toast.error("Unable to get current user");
48+
return ""
6449
}
50+
6551
return user;
6652
};
6753

0 commit comments

Comments
 (0)