Skip to content

Commit e835954

Browse files
authored
Merge pull request #54 from stuartleeks/sl/progress
Show progress on exec
2 parents 255aad6 + 23efe7f commit e835954

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/pkg/devcontainers/dockerutils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/terminal"
1314
"github.com/stuartleeks/devcontainer-cli/internal/pkg/wsl"
1415
)
1516

@@ -126,6 +127,8 @@ func GetContainerIDForPath(devcontainerPath string) (string, error) {
126127

127128
func ExecInDevContainer(containerIDOrName string, workDir string, args []string) error {
128129

130+
statusWriter := &terminal.UpdatingStatusWriter{}
131+
129132
containerID := ""
130133
devcontainerList, err := ListDevcontainers()
131134
if err != nil {
@@ -151,6 +154,7 @@ func ExecInDevContainer(containerIDOrName string, workDir string, args []string)
151154
return err
152155
}
153156

157+
statusWriter.Printf("Getting mount path")
154158
if workDir == "" {
155159
workDir, err = GetWorkspaceMountPath(localPath)
156160
if err != nil {
@@ -160,18 +164,21 @@ func ExecInDevContainer(containerIDOrName string, workDir string, args []string)
160164

161165
wslPath := localPath
162166
if strings.HasPrefix(wslPath, "\\\\wsl$") && wsl.IsWsl() {
167+
statusWriter.Printf("Converting to WSL path")
163168
wslPath, err = wsl.ConvertWindowsPathToWslPath(wslPath)
164169
if err != nil {
165170
return fmt.Errorf("error converting path: %s", err)
166171
}
167172
}
168173

174+
statusWriter.Printf("Getting user name")
169175
devcontainerJSONPath := path.Join(wslPath, ".devcontainer/devcontainer.json")
170176
userName, err := GetDevContainerUserName(devcontainerJSONPath)
171177
if err != nil {
172178
return err
173179
}
174180

181+
statusWriter.Printf("Checking for SSH_AUTH_SOCK")
175182
sshAuthSockValue, err := getSshAuthSockValue(containerID)
176183
if err != nil {
177184
// output error and continue without SSH_AUTH_SOCK value
@@ -180,9 +187,11 @@ func ExecInDevContainer(containerIDOrName string, workDir string, args []string)
180187
fmt.Println("Continuing without setting SSH_AUTH_SOCK...")
181188
}
182189

190+
statusWriter.Printf("Getting container PATH")
183191
containerPath, err := getContainerEnvVar(containerID, "PATH")
184192
if err == nil {
185193
// Got the PATH
194+
statusWriter.Printf("Getting code server path")
186195
vscodeServerPath, err := getVscodeServerPath(containerID)
187196
if err == nil {
188197
// Got the VS Code server location - add bin subfolder to PATH
@@ -200,13 +209,15 @@ func ExecInDevContainer(containerIDOrName string, workDir string, args []string)
200209
fmt.Println("Continuing without overriding PATH...")
201210
}
202211

212+
statusWriter.Printf("Getting code IPC SOCK")
203213
ipcSock, err := getVscodeIpcSock(containerID)
204214
if err != nil {
205215
ipcSock = ""
206216
fmt.Printf("Warning; Failed to get VS Code IPC SOCK: %s\n", err)
207217
fmt.Println("Continuing without setting VSCODE_IPC_HOOK_CLI...")
208218
}
209219

220+
statusWriter.Printf("Starting exec session\n") // newline to put container shell at start of line
210221
dockerArgs := []string{"exec", "-it", "--workdir", workDir}
211222
if userName != "" {
212223
dockerArgs = append(dockerArgs, "--user", userName)

internal/pkg/terminal/terminal.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package terminal
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// UpdatingStatusWriter overwrites messages on successive writes
9+
type UpdatingStatusWriter struct {
10+
lastMessageLength int
11+
}
12+
13+
func (w *UpdatingStatusWriter) Printf(format string, a ...interface{}) {
14+
// format current message
15+
currentMessage := fmt.Sprintf(format, a...)
16+
// right-pad current message and prefix with carriage return to put at start of line
17+
fmt.Printf("\r%-*s", w.lastMessageLength, currentMessage)
18+
19+
w.lastMessageLength = len(currentMessage)
20+
}
21+
22+
func IsTTY() bool {
23+
fi, err := os.Stdout.Stat()
24+
if err != nil {
25+
return false
26+
}
27+
return fi.Mode()&os.ModeCharDevice != 0
28+
}

0 commit comments

Comments
 (0)