Skip to content

Commit

Permalink
Added Support for Instance VNC
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Sep 26, 2024
1 parent c7bf446 commit 332b1f1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
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
58 changes: 58 additions & 0 deletions cmd/instance/instance_vnc.go
Original file line number Diff line number Diff line change
@@ -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.")
}
},
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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,18 +62,23 @@ 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
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
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
27 changes: 27 additions & 0 deletions utility/global_struct.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 332b1f1

Please sign in to comment.