-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Support for Instance VNC (#472)
* Added Support for Instance VNC
- Loading branch information
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package instance | ||
|
||
import ( | ||
"github.com/civo/cli/common" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/pkg/browser" | ||
"github.com/civo/cli/utility" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var instanceVncCmd = &cobra.Command{ | ||
Use: "vnc", | ||
Example: "civo instance vnc INSTANCE-ID/NAME", | ||
Args: cobra.MinimumNArgs(1), | ||
Short: "Enable and access VNC on an instance", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
utility.EnsureCurrentRegion() | ||
|
||
// Create the API client | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
utility.Error("Failed to connect to Civo's API: %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Set the region if specified by the user | ||
if common.RegionSet != "" { | ||
client.Region = common.RegionSet | ||
} | ||
|
||
// Locate the instance | ||
instance, err := client.FindInstance(args[0]) | ||
if err != nil { | ||
utility.Error("Unable to find instance with ID/Name '%s': %s", args[0], err) | ||
os.Exit(1) | ||
} | ||
|
||
// Enable VNC for the instance | ||
vnc, err := client.GetInstanceVnc(instance.ID) | ||
if err != nil { | ||
utility.Error("Failed to enable VNC on instance '%s': %s", instance.Hostname, err) | ||
os.Exit(1) | ||
} | ||
|
||
// Display VNC details | ||
utility.Info("VNC has been successfully enabled for instance: %s", instance.Hostname) | ||
utility.Info("VNC URL: %s", vnc.URI) | ||
utility.Info("Opening VNC in your default browser...") | ||
|
||
// Open VNC in the browser | ||
err = browser.OpenInBrowser(vnc.URI) | ||
if err != nil { | ||
utility.Error("Failed to open VNC URL in the browser: %s", err) | ||
} else { | ||
utility.Info("VNC session is now active. You can access your instance's graphical interface.") | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package browser | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
// OpenInBrowser attempts to open the specified URL in the default browser. | ||
// Returns an error if the command fails or the platform is unsupported. | ||
func OpenInBrowser(url string) error { | ||
var err error | ||
switch runtime.GOOS { | ||
case "linux": | ||
err = exec.Command("xdg-open", url).Start() | ||
case "windows": | ||
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() | ||
case "darwin": | ||
err = exec.Command("open", url).Start() | ||
default: | ||
err = fmt.Errorf("unsupported platform") | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to open URL: %w", err) | ||
} | ||
return nil | ||
} |