Skip to content

Support some commands offline #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,6 @@ var RootCmd = &cobra.Command{
}

client, err = cli.Connect(cmd.Context(), getCluster())
if cli.IsNetworkError(err) {
return fmt.Errorf("unable to connect to Defang server %q; please check network settings and try again", cluster)
}

if v, err := client.GetVersions(cmd.Context()); err == nil {
version := cmd.Root().Version // HACK to avoid circular dependency with RootCmd
Expand All @@ -379,6 +376,11 @@ var RootCmd = &cobra.Command{
return nil
}

// Check whether Connect failed with a networking error
Copy link
Collaborator

@commit111 commit111 Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relocation of the check may cause commands which require a Fabric/network connection but are not marked with authNeeded annotation to exit way later and show the raw network connection error instead of our nice, user-friendly error when there is no internet.

An example would be defang generate, which requires internet but not necessarily authNeeded. So when a user runs this without internet, they may see the unable to resolve host error or something similar as opposed to unable to connect to Defang server when the call to Fabric occurs later on.

According to @edwardrf, a solution would be to avoid mixing up/overloading the meaning of the when auth is needed versus when Fabric/network is needed, perhaps by adding another flag called fabricNeeded or networkNeeded

if cli.IsNetworkError(err) {
return fmt.Errorf("unable to connect to Defang server %q; please check network settings and try again", cluster)
}

if err = client.CheckLoginAndToS(cmd.Context()); err != nil {
if nonInteractive {
return err
Expand Down Expand Up @@ -1280,7 +1282,7 @@ func determineProviderID(ctx context.Context, loader cliClient.Loader) (string,
if projectName != "" && !RootCmd.PersistentFlags().Changed("provider") { // If user manually selected auto provider, do not load from remote
resp, err := client.GetSelectedProvider(ctx, &defangv1.GetSelectedProviderRequest{Project: projectName})
if err != nil {
term.Warnf("Unable to get selected provider: %v", err)
term.Debugf("Unable to get selected provider: %v", err)
} else if resp.Provider != defangv1.Provider_PROVIDER_UNSPECIFIED {
providerID.SetValue(resp.Provider)
return "stored preference", nil
Expand All @@ -1293,7 +1295,7 @@ func determineProviderID(ctx context.Context, loader cliClient.Loader) (string,
// Save the selected provider to the fabric
if projectName != "" {
if err := client.SetSelectedProvider(ctx, &defangv1.SetSelectedProviderRequest{Project: projectName, Provider: providerID.Value()}); err != nil {
term.Warnf("Unable to save selected provider to defang server: %v", err)
term.Debugf("Unable to save selected provider to defang server: %v", err)
} else {
term.Printf("%v is now the default provider for project %v and will auto-select next time if no other provider is specified. Use --provider=auto to reselect.", providerID, projectName)
}
Expand Down