Skip to content

Commit

Permalink
Added Support for Instance VNC (#472)
Browse files Browse the repository at this point in the history
* Added Support for Instance VNC
  • Loading branch information
uzaxirr authored Oct 1, 2024
1 parent c7bf446 commit d5933aa
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
InstanceCmd.AddCommand(instancePublicIPCmd)
InstanceCmd.AddCommand(instancePasswordCmd)
InstanceCmd.AddCommand(instanceTagCmd)
InstanceCmd.AddCommand(instanceVncCmd)

instanceUpdateCmd.Flags().StringVarP(&notes, "notes", "n", "", "notes stored against the instance")
instanceUpdateCmd.Flags().StringVarP(&reverseDNS, "reverse-dns", "r", "", "the reverse DNS entry for the instance")
Expand Down
59 changes: 59 additions & 0 deletions cmd/instance/instance_vnc.go
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.")
}
},
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -60,14 +62,17 @@ 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
golang.org/x/sys v0.18.0 // indirect
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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
28 changes: 28 additions & 0 deletions pkg/browser/browser.go
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
}

0 comments on commit d5933aa

Please sign in to comment.