-
Notifications
You must be signed in to change notification settings - Fork 167
/
util.go
175 lines (149 loc) · 4.94 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
azdExec "github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/cli/browser"
)
// CmdAnnotations on a command
type CmdAnnotations map[string]string
type Asker func(p survey.Prompt, response interface{}) error
func azurePortalLink(portalUrlBase, subscriptionId, resourceGroupName string) string {
if subscriptionId == "" || resourceGroupName == "" {
return ""
}
return output.WithLinkFormat(fmt.Sprintf(
"%s/#@/resource/subscriptions/%s/resourceGroups/%s/overview",
portalUrlBase,
subscriptionId,
resourceGroupName))
}
func serviceNameWarningCheck(console input.Console, serviceNameFlag string, commandName string) {
if serviceNameFlag == "" {
return
}
fmt.Fprintln(
console.Handles().Stderr,
output.WithWarningFormat("WARNING: The `--service` flag is deprecated and will be removed in a future release."),
)
fmt.Fprintf(console.Handles().Stderr, "Next time use `azd %s <service>`.\n\n", commandName)
}
func getTargetServiceName(
ctx context.Context,
projectManager project.ProjectManager,
importManager *project.ImportManager,
projectConfig *project.ProjectConfig,
commandName string,
targetServiceName string,
allFlagValue bool,
) (string, error) {
if allFlagValue && targetServiceName != "" {
return "", fmt.Errorf("cannot specify both --all and <service>")
}
if !allFlagValue && targetServiceName == "" {
targetService, err := projectManager.DefaultServiceFromWd(ctx, projectConfig)
if errors.Is(err, project.ErrNoDefaultService) {
return "", fmt.Errorf(
"current working directory is not a project or service directory. Specify a service name to %s a service, "+
"or specify --all to %s all services",
commandName,
commandName,
)
} else if err != nil {
return "", err
}
if targetService != nil {
targetServiceName = targetService.Name
}
}
if targetServiceName != "" {
if has, err := importManager.HasService(ctx, projectConfig, targetServiceName); err != nil {
return "", err
} else if !has {
return "", fmt.Errorf("service name '%s' doesn't exist", targetServiceName)
}
}
return targetServiceName, nil
}
// Calculate the total time since t, excluding user interaction time.
func since(t time.Time) time.Duration {
userInteractTime := tracing.InteractTimeMs.Load()
return time.Since(t) - time.Duration(userInteractTime)*time.Millisecond
}
// BrowseUrl allow users to override the default browser from the cmd package with some external implementation.
type browseUrl func(ctx context.Context, console input.Console, url string)
// OverrideBrowser allows users to set their own implementation for browsing urls.
var overrideBrowser browseUrl
func openWithDefaultBrowser(ctx context.Context, console input.Console, url string) {
if overrideBrowser != nil {
overrideBrowser(ctx, console, url)
return
}
cmdRunner := azdExec.NewCommandRunner(nil)
// In Codespaces and devcontainers a $BROWSER environment variable is
// present whose value is an executable that launches the browser when
// called with the form:
// $BROWSER <url>
const BrowserEnvVarName = "BROWSER"
if envBrowser := os.Getenv(BrowserEnvVarName); len(envBrowser) > 0 {
_, err := cmdRunner.Run(ctx, azdExec.RunArgs{
Cmd: envBrowser,
Args: []string{url},
})
if err == nil {
return
}
log.Printf(
"warning: failed to open browser configured by $BROWSER: %s\nTrying with default browser.\n",
err.Error(),
)
}
err := browser.OpenURL(url)
if err == nil {
return
}
log.Printf(
"warning: failed to open default browser: %s\nTrying manual launch.", err.Error(),
)
// wsl manual launch. Trying cmd first, and pwsh second
_, err = cmdRunner.Run(ctx, azdExec.RunArgs{
Cmd: "cmd.exe",
// cmd notes:
// /c -> run command
// /s -> quoted string, use the text within the quotes as it is
// Replace `&` for `^&` -> cmd require to scape the & to avoid using it as a command concat
Args: []string{
"/s", "/c", fmt.Sprintf("start %s", strings.ReplaceAll(url, "&", "^&")),
},
})
if err == nil {
return
}
log.Printf(
"warning: failed to open browser with cmd: %s\nTrying powershell.", err.Error(),
)
_, err = cmdRunner.Run(ctx, azdExec.RunArgs{
Cmd: "powershell.exe",
Args: []string{
"-NoProfile", "-Command", "Start-Process", fmt.Sprintf("\"%s\"", url),
},
})
if err == nil {
return
}
log.Printf("warning: failed to use manual launch: %s\n", err.Error())
console.Message(ctx, fmt.Sprintf("Azd was unable to open the next url. Please try it manually: %s", url))
}
const cReferenceDocumentationUrl = "https://learn.microsoft.com/azure/developer/azure-developer-cli/reference#"