Skip to content

Commit b152297

Browse files
committed
feat: unify service status table
1 parent 45625e5 commit b152297

33 files changed

+1404
-2711
lines changed

src/cmd/cli/command/commands.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/DefangLabs/defang/src/pkg/clouds/aws"
2424
pcluster "github.com/DefangLabs/defang/src/pkg/cluster"
2525
"github.com/DefangLabs/defang/src/pkg/dryrun"
26+
"github.com/DefangLabs/defang/src/pkg/github"
2627
"github.com/DefangLabs/defang/src/pkg/login"
2728
"github.com/DefangLabs/defang/src/pkg/logs"
2829
"github.com/DefangLabs/defang/src/pkg/mcp"
@@ -127,7 +128,7 @@ func Execute(ctx context.Context) error {
127128
}
128129

129130
if hasTty && !hideUpdate && pkg.RandomIndex(10) == 0 {
130-
if latest, err := GetLatestVersion(ctx); err == nil && isNewer(GetCurrentVersion(), latest) {
131+
if latest, err := github.GetLatestReleaseTag(ctx); err == nil && isNewer(GetCurrentVersion(), latest) {
131132
term.Debug("Latest Version:", latest, "Current Version:", GetCurrentVersion())
132133
fmt.Println("A newer version of the CLI is available at https://github.com/DefangLabs/defang/releases/latest")
133134
if pkg.RandomIndex(10) == 0 && !pkg.GetenvBool("DEFANG_HIDE_HINTS") {
@@ -620,7 +621,7 @@ var getVersionCmd = &cobra.Command{
620621
fmt.Println(GetCurrentVersion())
621622

622623
term.Printc(term.BrightCyan, "Latest CLI: ")
623-
ver, err := GetLatestVersion(cmd.Context())
624+
ver, err := github.GetLatestReleaseTag(cmd.Context())
624625
fmt.Println(ver)
625626

626627
term.Printc(term.BrightCyan, "Defang Fabric: ")
@@ -698,7 +699,7 @@ var configSetCmd = &cobra.Command{
698699
value = strings.TrimSuffix(string(bytes), "\n")
699700
} else if random {
700701
// Generate a random value for the config
701-
value = CreateRandomConfigValue()
702+
value = cli.CreateRandomConfigValue()
702703
term.Info("Generated random value: " + value)
703704
} else {
704705
// Prompt for sensitive value

src/cmd/cli/command/compose.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
1616
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc"
1717
"github.com/DefangLabs/defang/src/pkg/cli/compose"
18+
pcluster "github.com/DefangLabs/defang/src/pkg/cluster"
1819
"github.com/DefangLabs/defang/src/pkg/dryrun"
1920
"github.com/DefangLabs/defang/src/pkg/logs"
2021
"github.com/DefangLabs/defang/src/pkg/modes"
@@ -26,6 +27,19 @@ import (
2627
"github.com/spf13/cobra"
2728
)
2829

30+
const DEFANG_PORTAL_HOST = "portal.defang.io"
31+
const SERVICE_PORTAL_URL = "https://" + DEFANG_PORTAL_HOST + "/service"
32+
33+
func printPlaygroundPortalServiceURLs(serviceInfos []*defangv1.ServiceInfo) {
34+
// We can only show services deployed to the prod1 defang SaaS environment.
35+
if providerID == cliClient.ProviderDefang && cluster == pcluster.DefaultCluster {
36+
term.Info("Monitor your services' status in the defang portal")
37+
for _, serviceInfo := range serviceInfos {
38+
term.Println(" -", SERVICE_PORTAL_URL+"/"+serviceInfo.Service.Name)
39+
}
40+
}
41+
}
42+
2943
var logType = logs.LogTypeAll
3044

3145
func makeComposeUpCmd() *cobra.Command {
@@ -170,7 +184,7 @@ func makeComposeUpCmd() *cobra.Command {
170184
}
171185

172186
// Print the current service states of the deployment
173-
err = printServiceStatesAndEndpoints(deploy.Services)
187+
err = cli.PrintServiceStatesAndEndpoints(ctx, deploy.Services)
174188
if err != nil {
175189
return err
176190
}

src/cmd/cli/command/compose_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package command
22

33
import (
4+
"bytes"
5+
"os"
46
"testing"
7+
8+
cliClient "github.com/DefangLabs/defang/src/pkg/cli/client"
9+
pcluster "github.com/DefangLabs/defang/src/pkg/cluster"
10+
"github.com/DefangLabs/defang/src/pkg/term"
11+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
512
)
613

714
func TestInitializeTailCmd(t *testing.T) {
@@ -14,3 +21,26 @@ func TestInitializeTailCmd(t *testing.T) {
1421
}
1522
})
1623
}
24+
25+
func TestPrintPlaygroundPortalServiceURLs(t *testing.T) {
26+
defaultTerm := term.DefaultTerm
27+
t.Cleanup(func() {
28+
term.DefaultTerm = defaultTerm
29+
})
30+
31+
var stdout, stderr bytes.Buffer
32+
term.DefaultTerm = term.NewTerm(os.Stdin, &stdout, &stderr)
33+
34+
providerID = cliClient.ProviderDefang
35+
cluster = pcluster.DefaultCluster
36+
printPlaygroundPortalServiceURLs([]*defangv1.ServiceInfo{
37+
{
38+
Service: &defangv1.Service{Name: "service1"},
39+
}})
40+
const want = ` * Monitor your services' status in the defang portal
41+
- https://portal.defang.io/service/service1
42+
`
43+
if got := stdout.String(); got != want {
44+
t.Errorf("got %q, want %q", got, want)
45+
}
46+
}

src/cmd/cli/command/deploymentinfo.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/cmd/cli/command/version.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package command
22

33
import (
4-
"context"
5-
"encoding/json"
6-
"fmt"
7-
"os"
84
"strings"
95

10-
"github.com/DefangLabs/defang/src/pkg/http"
11-
"github.com/DefangLabs/defang/src/pkg/term"
126
"golang.org/x/mod/semver"
137
)
148

@@ -36,44 +30,3 @@ func GetCurrentVersion() string {
3630
version, _ := normalizeVersion(RootCmd.Version)
3731
return version
3832
}
39-
40-
type githubError struct {
41-
Message string
42-
Status string
43-
DocumentationUrl string
44-
}
45-
46-
func GetLatestVersion(ctx context.Context) (string, error) {
47-
// Anonymous API request to GitHub are rate limited to 60 requests per hour per IP.
48-
// Check whether the user has set a GitHub token to increase the rate limit. (Copied from the install script.)
49-
githubToken := os.Getenv("GITHUB_TOKEN")
50-
if githubToken == "" {
51-
githubToken = os.Getenv("GH_TOKEN")
52-
}
53-
header := http.Header{}
54-
if githubToken != "" {
55-
header["Authorization"] = []string{"Bearer " + githubToken}
56-
}
57-
resp, err := http.GetWithHeader(ctx, "https://api.github.com/repos/DefangLabs/defang/releases/latest", header)
58-
if err != nil {
59-
return "", err
60-
}
61-
defer resp.Body.Close()
62-
if resp.StatusCode != 200 {
63-
term.Debug(resp.Header)
64-
// The primary rate limit for unauthenticated requests is 60 requests per hour, per IP.
65-
// The API returns a 403 status code when the rate limit is exceeded.
66-
githubError := githubError{Message: resp.Status}
67-
if err := json.NewDecoder(resp.Body).Decode(&githubError); err != nil {
68-
term.Debugf("Failed to decode GitHub response: %v", err)
69-
}
70-
return "", fmt.Errorf("error fetching release info from GitHub: %s", githubError.Message)
71-
}
72-
var release struct {
73-
TagName string `json:"tag_name"`
74-
}
75-
if err = json.NewDecoder(resp.Body).Decode(&release); err != nil {
76-
return "", err
77-
}
78-
return release.TagName, nil
79-
}

src/cmd/cli/command/version_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package command
33
import (
44
"fmt"
55
"net/http"
6-
"net/http/httptest"
76
"testing"
8-
9-
ourHttp "github.com/DefangLabs/defang/src/pkg/http"
107
)
118

129
func TestIsNewer(t *testing.T) {
@@ -67,40 +64,3 @@ func (rt *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
6764
}
6865
return rt.resp, nil
6966
}
70-
71-
func TestGetLatestVersion(t *testing.T) {
72-
if testing.Short() {
73-
t.Skip("skipping GitHub HTTP test in short mode to avoid rate limits.")
74-
}
75-
76-
ctx := t.Context()
77-
78-
const version = "v1.2.3"
79-
rec := httptest.NewRecorder()
80-
rec.Header().Add("Content-Type", "application/json")
81-
rec.WriteString(fmt.Sprintf(`{"tag_name":"%v"}`, version))
82-
response := rec.Result()
83-
84-
client := ourHttp.DefaultClient
85-
t.Cleanup(func() {
86-
ourHttp.DefaultClient = client
87-
response.Body.Close()
88-
})
89-
90-
ourHttp.DefaultClient = &http.Client{Transport: &mockRoundTripper{
91-
method: http.MethodGet,
92-
url: "https://api.github.com/repos/DefangLabs/defang/releases/latest",
93-
resp: response,
94-
}}
95-
96-
v, err := GetLatestVersion(ctx)
97-
if err != nil {
98-
t.Fatalf("GetLatestVersion() error = %v; want nil", err)
99-
}
100-
if v == "" {
101-
t.Fatalf("GetLatestVersion() = %v; want non-empty", v)
102-
}
103-
if v != version {
104-
t.Errorf("GetLatestVersion() = %v; want %v", v, version)
105-
}
106-
}

0 commit comments

Comments
 (0)