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..ba7da019 --- /dev/null +++ b/cmd/instance/instance_vnc.go @@ -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.") + } + }, +} diff --git a/go.mod b/go.mod index 8b6df471..a8ed81b3 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect github.com/briandowns/spinner v1.11.1 github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect - github.com/civo/civogo v0.3.79 + github.com/civo/civogo v0.3.80 github.com/dsnet/compress v0.0.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/google/go-github v17.0.0+incompatible // indirect @@ -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 diff --git a/go.sum b/go.sum index 91167961..30d6b661 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/civo/civogo v0.3.79 h1:Z1MbEG9CsGqSZV7UFBA0xsjk7TBGUPHjW9sM7cS5yZM= github.com/civo/civogo v0.3.79/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= +github.com/civo/civogo v0.3.80 h1:6ZcjiGXIabPhW+R7sdehDotjM1ofnwPMP2Cxpwns0ss= +github.com/civo/civogo v0.3.80/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= @@ -301,10 +303,14 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/pkg/browser/browser.go b/pkg/browser/browser.go new file mode 100644 index 00000000..7ed29524 --- /dev/null +++ b/pkg/browser/browser.go @@ -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 +}