Skip to content

Commit

Permalink
Merge pull request #40 from Phillezi/34-move-communication-with-the-s…
Browse files Browse the repository at this point in the history
…torage-manager-to-separate-client

34 move communication with the storage manager to separate client
  • Loading branch information
Phillezi authored Sep 19, 2024
2 parents 14fda5b + 141a140 commit e73eac9
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 208 deletions.
48 changes: 48 additions & 0 deletions pkg/util/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"errors"
"strings"
)

// GetCommonDomain returns the common domain from multiple domains by removing subdomains.
// It assumes domains provided are hosts without a scheme.
func GetCommonDomain(domains ...string) (string, error) {
if len(domains) == 0 {
return "", errors.New("no domains provided")
}

commonParts := strings.Split(domains[0], ".")

for _, domain := range domains[1:] {
parts := strings.Split(domain, ".")
commonParts = findCommonSuffix(commonParts, parts)

if len(commonParts) == 0 {
return "", errors.New("no common domain found")
}
}

commonDomain := strings.Join(reverse(commonParts), ".")
return commonDomain, nil
}

// findCommonSuffix finds the longest common suffix between two domain part arrays
func findCommonSuffix(parts1, parts2 []string) []string {
i, j := len(parts1)-1, len(parts2)-1
var common []string

for ; i >= 0 && j >= 0 && parts1[i] == parts2[j]; j-- {
common = append(common, parts1[i])
i--
}

return common
}

func reverse(arr []string) (reversed []string) {
for i := len(arr) - 1; i >= 0; i-- {
reversed = append(reversed, arr[i])
}
return reversed
}
23 changes: 23 additions & 0 deletions pkg/v1/auth/browser/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package browser

import (
"fmt"
"os/exec"
"runtime"
)

func Open(url string) error {
var cmd *exec.Cmd
switch {
case runtime.GOOS == "linux":
cmd = exec.Command("xdg-open", url)
case runtime.GOOS == "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
case runtime.GOOS == "darwin":
cmd = exec.Command("open", url)
default:
return fmt.Errorf("unsupported platform")
}
fmt.Printf("Trying to open: %s in web browser\n\n", url)
return cmd.Start()
}
57 changes: 21 additions & 36 deletions pkg/v1/auth/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"os/exec"
"runtime"
"strings"
"sync"
"time"

"github.com/briandowns/spinner"
"github.com/spf13/viper"

"github.com/Phillezi/kthcloud-cli/pkg/v1/auth/browser"
"github.com/Phillezi/kthcloud-cli/pkg/v1/auth/server"
"github.com/Phillezi/kthcloud-cli/pkg/v1/auth/session"
storageclient "github.com/Phillezi/kthcloud-cli/pkg/v1/auth/storage-client"
"github.com/go-resty/resty/v2"
"golang.org/x/exp/rand"
)

type Client struct {
kcBaseURL string
baseURL string
clientID string
clientSecret string
realm string
client *resty.Client
jar http.CookieJar
cookies []*http.Cookie
Session *session.Session
kcBaseURL string
baseURL string
clientID string
clientSecret string
realm string
client *resty.Client
jar http.CookieJar
Session *session.Session
StorageClient *storageclient.Client
}

var (
Expand Down Expand Up @@ -68,14 +68,15 @@ func GetInstance(baseURL, kcBaseURL, clientID, clientSecret, realm string) *Clie
sess = nil
}
instance = &Client{
baseURL: baseURL,
kcBaseURL: kcBaseURL,
clientID: clientID,
clientSecret: clientSecret,
realm: realm,
client: resty.New(),
jar: jar,
Session: sess,
baseURL: baseURL,
kcBaseURL: kcBaseURL,
clientID: clientID,
clientSecret: clientSecret,
realm: realm,
client: client,
jar: jar,
Session: sess,
StorageClient: nil,
}
if sess != nil {
instance.client.SetAuthToken(instance.Session.Token.AccessToken)
Expand All @@ -97,7 +98,7 @@ func (c *Client) Login() (*session.Session, error) {

server.Start()

err := OpenBrowser("http://localhost:3000")
err := browser.Open("http://localhost:3000")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -147,22 +148,6 @@ func (c *Client) fetchOAuthToken(redirectURI, code string) (*http.Response, erro
return resp, nil
}

func OpenBrowser(url string) error {
var cmd *exec.Cmd
switch {
case runtime.GOOS == "linux":
cmd = exec.Command("xdg-open", url)
case runtime.GOOS == "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
case runtime.GOOS == "darwin":
cmd = exec.Command("open", url)
default:
return fmt.Errorf("unsupported platform")
}
fmt.Printf("Trying to open: %s in web browser\n\n", url)
return cmd.Start()
}

func (c *Client) generateKCUrl() string {
redirectURI := fmt.Sprintf("%s/auth/callback", "http://localhost:3000")
state := generateRandomState()
Expand Down
6 changes: 4 additions & 2 deletions pkg/v1/auth/client/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
)

func (c *Client) Deployments() ([]body.DeploymentRead, error) {
if c.Session != nil &&
c.Session.Resources != nil &&
if c.Session == nil {
return nil, errors.New("no active session, log in first")
}
if c.Session.Resources != nil &&
c.Session.Resources.Deployments != nil &&
!c.Session.Resources.Deployments.IsExpired() {
return c.Session.Resources.Deployments.Data, nil
Expand Down
162 changes: 0 additions & 162 deletions pkg/v1/auth/client/storage.go

This file was deleted.

6 changes: 4 additions & 2 deletions pkg/v1/auth/client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
)

func (c *Client) User() (*body.UserRead, error) {
if c.Session != nil &&
c.Session.Resources != nil &&
if c.Session == nil {
return nil, errors.New("no active session, log in first")
}
if c.Session.Resources != nil &&
c.Session.Resources.User != nil &&
!c.Session.Resources.User.IsExpired() {
return c.Session.Resources.User.Data, nil
Expand Down
6 changes: 4 additions & 2 deletions pkg/v1/auth/client/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
)

func (c *Client) Vms() ([]body.VmRead, error) {
if c.Session != nil &&
c.Session.Resources != nil &&
if c.Session == nil {
return nil, errors.New("no active session, log in first")
}
if c.Session.Resources != nil &&
c.Session.Resources.Vms != nil &&
!c.Session.Resources.Vms.IsExpired() {
return c.Session.Resources.Vms.Data, nil
Expand Down
Loading

0 comments on commit e73eac9

Please sign in to comment.