Skip to content

Commit 2b9898f

Browse files
Akash-Patilmohammed786
authored andcommitted
Added add site command
1 parent ce92134 commit 2b9898f

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

api/account.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"net/http"
66
"strconv"
7+
"time"
78

89
"github.com/loginradius/lr-cli/request"
910
)
@@ -27,3 +28,48 @@ func SetSites(appid int) (*SitesToken, error) {
2728
}
2829
return &switchRespObj, nil
2930
}
31+
32+
type AccountPayment struct {
33+
Data struct {
34+
Order []struct {
35+
Totalamount int `json:"TotalAmount"`
36+
Recurringprofileid interface{} `json:"RecurringProfileId"`
37+
Basediscount int `json:"BaseDiscount"`
38+
Promotionaldiscount int `json:"PromotionalDiscount"`
39+
Initialamount int `json:"InitialAmount"`
40+
Tax int `json:"Tax"`
41+
UUID interface{} `json:"Uuid"`
42+
Invoiceno int `json:"InvoiceNo"`
43+
Createddate time.Time `json:"CreatedDate"`
44+
Lastmodifieddate time.Time `json:"LastModifiedDate"`
45+
Isactive bool `json:"IsActive"`
46+
Isdeleted bool `json:"IsDeleted"`
47+
Orderid int `json:"OrderId"`
48+
Paymentdetail struct {
49+
Stripecustomerid string `json:"StripeCustomerId"`
50+
Stripepaymentmethodid string `json:"StripePaymentMethodId"`
51+
} `json:"PaymentDetail"`
52+
Orderdetails []interface{} `json:"OrderDetails"`
53+
} `json:"Order"`
54+
Carddetails struct {
55+
Expmonth int `json:"expMonth"`
56+
Expyear int `json:"expYear"`
57+
Last4 string `json:"last4"`
58+
} `json:"cardDetails"`
59+
} `json:"data"`
60+
Sharedsiteownerdata interface{} `json:"sharedSiteOwnerData"`
61+
}
62+
63+
func PaymentInfo() (*AccountPayment, error) {
64+
payment := conf.AdminConsoleAPIDomain + "/account/accountpaymentdetail?"
65+
paymentResp, err := request.Rest(http.MethodGet, payment, nil, "")
66+
if err != nil {
67+
return nil, err
68+
}
69+
var paymentRespObj AccountPayment
70+
err = json.Unmarshal(paymentResp, &paymentRespObj)
71+
if err != nil {
72+
return nil, err
73+
}
74+
return &paymentRespObj, nil
75+
}

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package add
33
import (
44
"github.com/loginradius/lr-cli/cmd/add/account"
55
"github.com/loginradius/lr-cli/cmd/add/domain"
6+
"github.com/loginradius/lr-cli/cmd/add/site"
67
"github.com/loginradius/lr-cli/cmd/add/social"
78

89
"github.com/spf13/cobra"
@@ -16,6 +17,9 @@ func NewaddCmd() *cobra.Command {
1617
Long: `This commmand acts as a base command for add subcommands`,
1718
}
1819

20+
siteCmd := site.NewSiteCmd()
21+
cmd.AddCommand(siteCmd)
22+
1923
domainCmd := domain.NewdomainCmd()
2024
cmd.AddCommand(domainCmd)
2125

cmd/add/site/site.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package site
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/loginradius/lr-cli/api"
10+
"github.com/loginradius/lr-cli/config"
11+
"github.com/loginradius/lr-cli/request"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var AppName string
16+
var Domain string
17+
var PlanName string
18+
var planOption string
19+
var AppsInfo *api.CoreAppData
20+
21+
type AddAppResponse struct {
22+
AppId int `json:"appId"`
23+
}
24+
25+
func NewSiteCmd() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Use: "site",
28+
Short: "Adds a site",
29+
Long: heredoc.Doc(`
30+
This command enables user to add a site depending on the subscribed plan.
31+
`),
32+
Example: heredoc.Doc(`
33+
$ lr add site
34+
35+
Your site has been added
36+
`),
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
return addSite()
39+
},
40+
}
41+
return cmd
42+
}
43+
func addSite() error {
44+
checkInput := input()
45+
if !checkInput {
46+
fmt.Println("Please enter the input paramaters properly.")
47+
return nil
48+
}
49+
checkPlan, err := plans()
50+
if err != nil {
51+
return err
52+
}
53+
if !checkPlan {
54+
fmt.Println("Please upgrade your plan to add more sites. ")
55+
return nil
56+
}
57+
err = add()
58+
if err != nil {
59+
return err
60+
}
61+
fmt.Println("Your site has been added.")
62+
return nil
63+
}
64+
65+
func input() bool {
66+
fmt.Printf("Enter the App Name: ")
67+
fmt.Scanf("%s", &AppName)
68+
if AppName == "" {
69+
fmt.Println("App Name is a required entry")
70+
return false
71+
}
72+
fmt.Printf("Enter the Domain: ")
73+
fmt.Scanf("%s", &Domain)
74+
if Domain == "" {
75+
fmt.Println("Domain is a required entry")
76+
return false
77+
}
78+
plan := map[string]string{
79+
"1": "free",
80+
"2": "developer",
81+
"3": "business",
82+
}
83+
fmt.Println("To select a plan, choose a correponding number from the following options: ")
84+
fmt.Println("1 - Free plan")
85+
fmt.Println("2 - Developer plan")
86+
fmt.Println("3 - Developer Pro plan")
87+
fmt.Printf("Option: ")
88+
fmt.Scanf("%s", &planOption)
89+
if planOption == "" {
90+
fmt.Println("Plan is a required entry")
91+
return false
92+
}
93+
PlanName = plan[planOption]
94+
return true
95+
96+
}
97+
98+
func plans() (bool, error) {
99+
AppsInfo, err := api.AppInfo()
100+
if err != nil {
101+
return false, err
102+
}
103+
if len(AppsInfo.Apps.Data) > 1 {
104+
return true, nil
105+
}
106+
if AppsInfo.Apps.Data[0].Productplan.Name != "free" { //case for 1 App
107+
return true, nil
108+
}
109+
return false, nil
110+
}
111+
112+
func add() error {
113+
conf := config.GetInstance()
114+
paymentInfo, err := api.PaymentInfo()
115+
if err != nil {
116+
return err
117+
}
118+
newApp := conf.AdminConsoleAPIDomain + "/auth/create-new-app?"
119+
body, _ := json.Marshal(map[string]string{
120+
"appName": AppName,
121+
"domain": Domain,
122+
"paymentMethodId": paymentInfo.Data.Order[0].Paymentdetail.Stripepaymentmethodid,
123+
"planName": PlanName,
124+
})
125+
resp, err := request.Rest(http.MethodPost, newApp, nil, string(body))
126+
if err != nil {
127+
return err
128+
}
129+
var App AddAppResponse
130+
err = json.Unmarshal(resp, &App)
131+
if err != nil {
132+
return err
133+
}
134+
switchRespObj, err := api.SetSites(App.AppId)
135+
if err != nil {
136+
return err
137+
}
138+
err = api.SitesBasic(switchRespObj)
139+
if err != nil {
140+
return err
141+
}
142+
return nil
143+
144+
}

0 commit comments

Comments
 (0)