Skip to content

Commit

Permalink
Merge pull request #11 from SaaShup/add_platform_login
Browse files Browse the repository at this point in the history
Add platform login
  • Loading branch information
dzove855 authored Sep 13, 2024
2 parents ee57790 + fdf31e5 commit 4edb6db
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ netbox-vm-agent.yml
paashup-cli
test.yml
pkg
Taskfile.yml
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Build
using docker:
```bash
docker run -it --rm -v ./:/go golang:1.22 go build -buildvcs=false .
docker run -it --rm -v ./:/go golang:1.22 go build -buildvcs=false -ldflags "-X main.version=<VERSION> -X main.PLATFORM_URL=<PLATFORM_URL> -X main.PLATFORM_PUB_KEY=<PUB_KEY>".
```

## Installation
Expand Down Expand Up @@ -56,19 +56,28 @@ Manage platforms within your paashup environment.
paashup-cli platform account create user1 password123
```

- **Login in Platform**
```bash
paashup-cli platform login <username> <password>
```

- **Initialize a Platform**

```bash
paashup-cli platform init <username> <password>
paashup-cli platform init
```

Initializes a platform. Example:

```bash
paashup-cli platform init user1 password123
paashup-cli platform init
```

- **List Platforms**

```bash
paashup-cli platform ls
```
#### 2. Netbox Commands

Manage Netbox configurations for your paashup environment.
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ func main() {
Usage: "Initialize a platform\nExample: paashup-cli platform init <email> <password>\n",
Action: platformInit,
},
{
Name: "login",
Usage: "Login inside the platform\nExample: paashup-cli platform login <email> <password>\n",
Action: platformLogin,
},
{
Name: "logout",
Usage: "Logout from the platform\nExample: paashup-cli platform logout\n",
Action: platformLogout,
},
{
Name: "list",
Aliases: []string{"ls", "ps"},
Usage: "List all platforms\nExample: paashup-cli platform list\n",
Action: platformList,
},
},
},
}
Expand Down
143 changes: 128 additions & 15 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"fmt"
"log"
"encoding/json"
"io/ioutil"
"os"
"net/http"
)

func platformCreateAccount(c *cli.Context) error {
Expand Down Expand Up @@ -39,39 +42,149 @@ func platformCreateAccount(c *cli.Context) error {
return nil
}

func platformInit(c *cli.Context) error {
func platformLogout(c *cli.Context) error {
var configpath string

if c.Args().Len() != 2 {
fmt.Println("Please provide a username and password")
cli.ShowAppHelpAndExit(c, 1)
if os.Getenv("XDG_CONFIG_HOME") == "" {
configpath = os.Getenv("HOME") + "/.config/paashup-cli/"
} else {
configpath = os.Getenv("XDG_CONFIG_HOME") + "/paashup-cli/"
}

client, err := supabase.NewClient(PLATFORM_URL, PLATFORM_PUB_KEY, nil)
if _, err := os.Stat(configpath + "platform.token"); os.IsNotExist(err) {
log.Fatal("You are not logged in!")
}

if err != nil {
log.Fatal("Could not connect to platform!")
os.Remove(configpath + "platform.token")

fmt.Println("Logged out successfully!")
return nil
}

func platformReadLogin(c *cli.Context) (string, error) {
var configpath string

if os.Getenv("XDG_CONFIG_HOME") == "" {
configpath = os.Getenv("HOME") + "/.config/paashup-cli/"
} else {
configpath = os.Getenv("XDG_CONFIG_HOME") + "/paashup-cli/"
}

client.SignInWithEmailPassword(c.Args().First(), c.Args().Get(c.Args().Len()-1))

data, _, err := client.From("netbox").Insert(map[string]interface{}{}, true, "", "", "exact").Execute()
data, err := ioutil.ReadFile(configpath + "platform.token")

if err != nil {
log.Fatal("Could not init project")
return "", err
}

var response []struct {
return string(data), nil
}

type PlatformList struct {
Data []struct {
Id int `json:"id"`
Created_at string `json:"created_at"`
Name string `json:"name"`
User_id string `json:"user_id"`
} `json:"data"`
}

func platformList(c *cli.Context) error {
token, err := platformReadLogin(c)

if err != nil {
log.Fatal("You are not logged in!")
}
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/functions/v1/paashup-list", PLATFORM_URL), nil)

if err != nil {
log.Fatal("could not create request")
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}

defer res.Body.Close()
var data PlatformList
body, _ := ioutil.ReadAll(res.Body)
if err := json.Unmarshal(body, &data); err != nil { // Parse []byte to the go struct pointer
return err
}

for _, project := range data.Data {
fmt.Printf("https://%s.paashup.cloud\n", project.Name)
}
return nil
}

func platformLogin(c *cli.Context) error {
client, _ := supabase.NewClient(PLATFORM_URL, PLATFORM_PUB_KEY, nil)

if c.Args().Len() != 2 {
fmt.Println("Please provide a username and password")
cli.ShowAppHelpAndExit(c, 1)
}

data, err := client.SignInWithEmailPassword(c.Args().First(), c.Args().Get(c.Args().Len()-1))

if err := json.Unmarshal(data, &response); err != nil { // Parse []byte to the go struct pointer
log.Fatal(err)
if err != nil {
log.Fatal("Could not login to the platform!")
}

fmt.Printf("Your paashup url is https://%s.paashup.com\n", response[0].Name)
var configpath string

if os.Getenv("XDG_CONFIG_HOME") == "" {
configpath = os.Getenv("HOME") + "/.config/paashup-cli/"
} else {
configpath = os.Getenv("XDG_CONFIG_HOME") + "/paashup-cli/"
}

if _, err := os.Stat(configpath); os.IsNotExist(err) {
os.MkdirAll(configpath, 0755)
}
if _, err := os.Stat(configpath + "platform.token"); os.IsNotExist(err) {
os.Create(configpath + "platform.token")
}

_ = ioutil.WriteFile(configpath+"platform.token", []byte(data.AccessToken), 0644)

fmt.Println("Logged in successfully!")

return nil
}

func platformInit(c *cli.Context) error {
token, err := platformReadLogin(c)

if err != nil {
log.Fatal("You are not logged in!")
}
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/functions/v1/paashup-init", PLATFORM_URL), nil)

if err != nil {
log.Fatal("could not create request")
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}

defer res.Body.Close()
var data PlatformList
body, _ := ioutil.ReadAll(res.Body)
if err := json.Unmarshal(body, &data); err != nil { // Parse []byte to the go struct pointer
return err
}

for _, project := range data.Data {
fmt.Printf("https://%s.paashup.cloud\n", project.Name)
}
return nil

}

0 comments on commit 4edb6db

Please sign in to comment.