Skip to content

Commit

Permalink
change helper func loc
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Oct 1, 2024
1 parent 9df36e9 commit 15ece21
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion cmd/instance/instance_vnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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"
Expand Down Expand Up @@ -48,7 +49,7 @@ var instanceVncCmd = &cobra.Command{
utility.Info("Opening VNC in your default browser...")

// Open VNC in the browser
err = utility.OpenInBrowser(vnc.URI)
err = browser.OpenInBrowser(vnc.URI)
if err != nil {
utility.Error("Failed to open VNC URL in the browser: %s", err)
} else {
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
}
27 changes: 0 additions & 27 deletions utility/global_struct.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
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 15ece21

Please sign in to comment.