From 332b1f140a1620e3c8943b793992c4215132d34e Mon Sep 17 00:00:00 2001 From: Uzair Ali <72073401+uzaxirr@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:00:21 +0530 Subject: [PATCH] Added Support for Instance VNC --- cmd/instance/instance.go | 1 + cmd/instance/instance_vnc.go | 58 ++++++++++++++++++++++++++++++++++++ go.mod | 7 +++++ utility/global_struct.go | 27 +++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 cmd/instance/instance_vnc.go diff --git a/cmd/instance/instance.go b/cmd/instance/instance.go index a1eb48ce..3ac9251e 100644 --- a/cmd/instance/instance.go +++ b/cmd/instance/instance.go @@ -38,6 +38,7 @@ func init() { InstanceCmd.AddCommand(instancePublicIPCmd) InstanceCmd.AddCommand(instancePasswordCmd) InstanceCmd.AddCommand(instanceTagCmd) + InstanceCmd.AddCommand(instanceVncCmd) instanceUpdateCmd.Flags().StringVarP(¬es, "notes", "n", "", "notes stored against the instance") instanceUpdateCmd.Flags().StringVarP(&reverseDNS, "reverse-dns", "r", "", "the reverse DNS entry for the instance") diff --git a/cmd/instance/instance_vnc.go b/cmd/instance/instance_vnc.go new file mode 100644 index 00000000..32db52cb --- /dev/null +++ b/cmd/instance/instance_vnc.go @@ -0,0 +1,58 @@ +package instance + +import ( + "github.com/civo/cli/common" + "github.com/civo/cli/config" + "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 = utility.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.") + } + }, +} diff --git a/go.mod b/go.mod index 8b6df471..c9153f83 100644 --- a/go.mod +++ b/go.mod @@ -35,12 +35,14 @@ require ( github.com/adhocore/gronx v1.6.5 github.com/google/go-github/v57 v57.0.0 github.com/savioxavier/termlink v1.2.1 + github.com/stretchr/testify v1.8.4 ) require ( github.com/MichaelMure/go-term-text v0.2.7 // indirect github.com/alecthomas/chroma v0.7.1 // indirect github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/disintegration/imaging v1.6.2 // indirect github.com/dlclark/regexp2 v1.1.6 // indirect github.com/eliukblau/pixterm/pkg/ansimage v0.0.0-20191210081756-9fb6cf8c2f75 // indirect @@ -60,7 +62,9 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect golang.org/x/image v0.11.0 // indirect golang.org/x/mod v0.16.0 // indirect golang.org/x/net v0.23.0 // indirect @@ -68,6 +72,7 @@ require ( golang.org/x/text v0.14.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/api v0.29.0 // indirect k8s.io/apimachinery v0.29.0 // indirect k8s.io/klog/v2 v2.110.1 // indirect @@ -75,3 +80,5 @@ require ( sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect ) + +replace github.com/civo/civogo => ../civogo diff --git a/utility/global_struct.go b/utility/global_struct.go index a88e6f3e..9e860444 100644 --- a/utility/global_struct.go +++ b/utility/global_struct.go @@ -1,6 +1,33 @@ package utility +import ( + "fmt" + "os/exec" + "runtime" +) + // ObjecteList contains ID and Name type ObjecteList struct { ID, Name string } + +// 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 +}