Skip to content

Commit db3bf71

Browse files
committed
Refactor Sites Info Logic to improve cli performance
Reduced Redundant Code Reduced API Calls
1 parent ac005af commit db3bf71

30 files changed

Lines changed: 232 additions & 470 deletions

File tree

api/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type SitesToken struct {
1818
XToken string `json:"xtoken"`
1919
}
2020

21-
func SetSites(appid int) (*SitesToken, error) {
22-
switchapp := conf.AdminConsoleAPIDomain + "/account/switchapp?appid=" + strconv.Itoa(appid)
21+
func SetSites(appid int64) (*SitesToken, error) {
22+
switchapp := conf.AdminConsoleAPIDomain + "/account/switchapp?appid=" + strconv.FormatInt(appid, 10)
2323
switchResp, err := request.Rest(http.MethodGet, switchapp, nil, "")
2424
var switchRespObj SitesToken
2525
err = json.Unmarshal(switchResp, &switchRespObj)

api/auth.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func AuthValidateToken() (*ValidateTokenResp, error) {
6868
}
6969

7070
type AppID struct {
71-
CurrentAppId int `json:"currentAppId"`
71+
CurrentAppId int64 `json:"currentAppId"`
7272
}
7373

7474
func CurrentID() (*AppID, error) {
@@ -103,7 +103,7 @@ func SitesBasic(tokens *SitesToken) error {
103103
req.Header.Add("Origin", conf.DashboardDomain)
104104
resp, err := client.Do(req)
105105
if err != nil {
106-
log.Printf("%s", err.Error())
106+
fmt.Printf("%s", err.Error())
107107
}
108108

109109
defer resp.Body.Close()
@@ -129,7 +129,11 @@ func SitesBasic(tokens *SitesToken) error {
129129
if err != nil {
130130
return err
131131
}
132-
err = cmdutil.StoreCreds(resObj)
132+
err = cmdutil.WriteFile("token.json", resObj)
133+
if err != nil {
134+
return err
135+
}
136+
_, err = GetAppsInfo()
133137
if err != nil {
134138
return err
135139
}

api/deployment.go

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package api
22

33
import (
44
"encoding/json"
5+
"errors"
56

67
"net/http"
78
"time"
89

10+
"github.com/loginradius/lr-cli/cmdutil"
911
"github.com/loginradius/lr-cli/request"
1012
)
1113

@@ -17,7 +19,7 @@ type SitesReponse struct {
1719
Callbackurl string `json:"CallbackUrl"`
1820
Devdomain string `json:"DevDomain"`
1921
Ismobile bool `json:"IsMobile"`
20-
Appid int `json:"AppId"`
22+
Appid int64 `json:"AppId"`
2123
Key string `json:"Key"`
2224
Secret string `json:"Secret"`
2325
Role string `json:"Role"`
@@ -63,57 +65,22 @@ type HostedPageResponse struct {
6365

6466
type CoreAppData struct {
6567
Apps struct {
66-
Data []struct {
67-
Appname string `json:"AppName"`
68-
Customername interface{} `json:"CustomerName"`
69-
Webtechnology int `json:"WebTechnology"`
70-
Domain string `json:"Domain"`
71-
Callbackurl string `json:"CallbackUrl"`
72-
Devdomain string `json:"DevDomain"`
73-
Ismobile bool `json:"IsMobile"`
74-
Appid int `json:"AppId"`
75-
Key string `json:"Key"`
76-
Secret string `json:"Secret"`
77-
Role string `json:"Role"`
78-
Iswelcomeemailenabled bool `json:"IsWelcomeEmailEnabled"`
79-
Ishttps bool `json:"Ishttps"`
80-
Interfaceid int `json:"InterfaceId"`
81-
Recurlyaccountcode interface{} `json:"RecurlyAccountCode"`
82-
Userlimit int `json:"UserLimit"`
83-
Domainlimit int `json:"DomainLimit"`
84-
Datecreated time.Time `json:"DateCreated"`
85-
Datemodified time.Time `json:"DateModified"`
86-
Status bool `json:"Status"`
87-
Profilephoto string `json:"ProfilePhoto"`
88-
Apiversion string `json:"ApiVersion"`
89-
Israasenabled bool `json:"IsRaasEnabled"`
90-
Privacypolicy interface{} `json:"PrivacyPolicy"`
91-
Termsofservice interface{} `json:"TermsOfService"`
92-
Ownerid string `json:"OwnerId"`
93-
Productplan struct {
94-
Name string `json:"Name"`
95-
Expirytime time.Time `json:"ExpiryTime"`
96-
Billingcycle interface{} `json:"BillingCycle"`
97-
Fromdate time.Time `json:"FromDate"`
98-
} `json:"ProductPlan"`
99-
} `json:"Data"`
68+
Data []SitesReponse `json:"Data"`
10069
} `json:"apps"`
10170
}
10271

10372
func GetSites() (*SitesReponse, error) {
10473

105-
url := conf.AdminConsoleAPIDomain + "/deployment/sites?"
106-
107-
var resultResp SitesReponse
108-
resp, err := request.Rest(http.MethodGet, url, nil, "")
74+
var siteInfo SitesReponse
75+
data, err := cmdutil.ReadFile("currentSite.json")
10976
if err != nil {
110-
return nil, err
77+
return nil, errors.New("Please Login to execute this command")
11178
}
112-
err = json.Unmarshal(resp, &resultResp)
79+
err = json.Unmarshal(data, &siteInfo)
11380
if err != nil {
11481
return nil, err
11582
}
116-
return &resultResp, nil
83+
return &siteInfo, nil
11784
}
11885

11986
func GetPage() (*HostedPageResponse, error) {
@@ -131,31 +98,71 @@ func GetPage() (*HostedPageResponse, error) {
13198
return &resultResp, nil
13299
}
133100

134-
func AppInfo() (*CoreAppData, error) {
135-
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
136-
AppData, err := request.Rest(http.MethodGet, coreAppData, nil, "")
101+
func UpdateDomain(domains string) error {
102+
var url string
103+
body, _ := json.Marshal(map[string]string{
104+
"domain": "http://localhost",
105+
"production": domains,
106+
"staging": "",
107+
})
108+
109+
url = conf.AdminConsoleAPIDomain + "/deployment/sites?"
110+
_, err := request.Rest(http.MethodPost, url, nil, string(body))
137111
if err != nil {
138-
return nil, err
112+
return err
139113
}
140-
var App CoreAppData
141-
err = json.Unmarshal(AppData, &App)
114+
115+
// Updating to current site
116+
var siteInfo SitesReponse
117+
data, err := cmdutil.ReadFile("currentSite.json")
142118
if err != nil {
143-
return nil, err
119+
return err
144120
}
145-
return &App, nil
121+
err = json.Unmarshal(data, &siteInfo)
122+
if err != nil {
123+
return err
124+
}
125+
siteInfo.Callbackurl = domains
126+
sInfo, _ := json.Marshal(siteInfo)
127+
_ = cmdutil.WriteFile("currentSite.json", sInfo)
128+
129+
return nil
146130
}
147131

148-
func CheckApp(appid int) (bool, error) {
149-
AppInfo, err := AppInfo()
132+
func GetAppsInfo() (map[int64]SitesReponse, error) {
133+
var Apps CoreAppData
134+
data, err := cmdutil.ReadFile("siteInfo.json")
150135
if err != nil {
151-
return false, err
152-
}
153-
numberOfApps := len(AppInfo.Apps.Data)
154-
for i := 0; i < numberOfApps; i++ {
155-
if appid == AppInfo.Apps.Data[i].Appid {
156-
return true, nil
136+
coreAppData := conf.AdminConsoleAPIDomain + "/auth/core-app-data?"
137+
data, err = request.Rest(http.MethodGet, coreAppData, nil, "")
138+
if err != nil {
139+
return nil, err
140+
}
141+
err = json.Unmarshal(data, &Apps)
142+
if err != nil {
143+
return nil, err
157144
}
145+
return storeSiteInfo(Apps), nil
158146
}
159-
return false, nil
147+
var siteInfo map[int64]SitesReponse
148+
err = json.Unmarshal(data, &siteInfo)
149+
return siteInfo, nil
150+
}
160151

152+
func storeSiteInfo(data CoreAppData) map[int64]SitesReponse {
153+
siteInfo := make(map[int64]SitesReponse, len(data.Apps.Data))
154+
for _, app := range data.Apps.Data {
155+
siteInfo[app.Appid] = app
156+
}
157+
obj, _ := json.Marshal(siteInfo)
158+
cmdutil.WriteFile("siteInfo.json", obj)
159+
currentId, err := CurrentID()
160+
if err == nil {
161+
site, ok := siteInfo[currentId.CurrentAppId]
162+
if ok {
163+
obj, _ := json.Marshal(site)
164+
cmdutil.WriteFile("currentSite.json", obj)
165+
}
166+
}
167+
return siteInfo
161168
}

api/securityConfiguration.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/loginradius/lr-cli/cmdutil"
8+
"github.com/loginradius/lr-cli/request"
9+
)
10+
11+
type ResetResponse struct {
12+
Secret string `json:"Secret"`
13+
XSign string `json:"xsign"`
14+
XToken string `json:"xtoken"`
15+
}
16+
17+
func ResetSecret() error {
18+
19+
// Restting the Secret
20+
changeURL := conf.AdminConsoleAPIDomain + "/security-configuration/api-credentials/change?"
21+
resp, err := request.Rest(http.MethodGet, changeURL, nil, "")
22+
if err != nil {
23+
return err
24+
}
25+
var resObj ResetResponse
26+
err = json.Unmarshal(resp, &resObj) //store reset response
27+
if err != nil {
28+
return err
29+
}
30+
31+
// Update Sceret in site info
32+
var siteInfo SitesReponse
33+
data, err := cmdutil.ReadFile("currentSite.json")
34+
if err != nil {
35+
return err
36+
}
37+
err = json.Unmarshal(data, &siteInfo)
38+
if err != nil {
39+
return err
40+
}
41+
siteInfo.Secret = resObj.Secret
42+
sInfo, _ := json.Marshal(siteInfo)
43+
_ = cmdutil.WriteFile("currentSite.json", sInfo)
44+
45+
// Updating XSign and Xtoken
46+
var loginInfo LoginResponse
47+
data, err = cmdutil.ReadFile("token.json")
48+
if err != nil {
49+
return err
50+
}
51+
err = json.Unmarshal(data, &loginInfo)
52+
if err != nil {
53+
return err
54+
}
55+
loginInfo.XSign = resObj.XSign
56+
loginInfo.XToken = resObj.XToken
57+
lInfo, _ := json.Marshal(loginInfo)
58+
_ = cmdutil.WriteFile("token.json", lInfo)
59+
return nil
60+
61+
}

cmd/add/account/account.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,7 @@ func NewaccountCmd() *cobra.Command {
6767
func add(Account account) error {
6868
Account.Password = cmdutil.GeneratePassword()
6969

70-
var resObj *cmdutil.APICred
71-
res, err := cmdutil.GetAPICreds()
72-
if err == nil {
73-
resObj = &cmdutil.APICred{
74-
Key: res.Key,
75-
Secret: res.Secret,
76-
}
77-
} else {
78-
resp, err := api.GetSites()
79-
if err != nil {
80-
return err
81-
}
82-
resObj = &cmdutil.APICred{
83-
Key: resp.Key,
84-
Secret: resp.Secret,
85-
}
86-
cmdutil.StoreAPICreds(resObj)
87-
}
70+
resObj, err := api.GetSites()
8871

8972
url := config.GetInstance().LoginRadiusAPIDomain + "/identity/v2/manage/account?apikey=" + resObj.Key + "&apisecret=" + resObj.Secret
9073
body, _ := json.Marshal(Account)

cmd/add/domain/domain.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package domain
22

33
import (
4-
"encoding/json"
54
"errors"
65
"fmt"
7-
"net/http"
86
"strings"
97

108
"github.com/MakeNowJust/heredoc"
119
"github.com/loginradius/lr-cli/api"
12-
"github.com/loginradius/lr-cli/request"
1310

1411
"github.com/loginradius/lr-cli/cmdutil"
15-
"github.com/loginradius/lr-cli/config"
1612

1713
"github.com/spf13/cobra"
1814
)
@@ -65,19 +61,7 @@ func NewdomainCmd() *cobra.Command {
6561

6662
func add(allDomains string, newDomain string) error {
6763
domain := allDomains + ";" + newDomain
68-
var url string
69-
body, _ := json.Marshal(map[string]string{
70-
"domain": "http://localhost",
71-
"production": domain,
72-
"staging": "",
73-
})
74-
conf := config.GetInstance()
75-
76-
url = conf.AdminConsoleAPIDomain + "/deployment/sites?"
77-
78-
var resultResp Result
79-
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
80-
err = json.Unmarshal(resp, &resultResp)
64+
err := api.UpdateDomain(domain)
8165
if err != nil {
8266
return err
8367
}

cmd/add/site/site.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var planOption string
1919
var AppsInfo *api.CoreAppData
2020

2121
type AddAppResponse struct {
22-
AppId int `json:"appId"`
22+
AppId int64 `json:"appId"`
2323
}
2424

2525
func NewSiteCmd() *cobra.Command {
@@ -96,15 +96,17 @@ func input() bool {
9696
}
9797

9898
func plans() (bool, error) {
99-
AppsInfo, err := api.AppInfo()
99+
AppsInfo, err := api.GetAppsInfo()
100100
if err != nil {
101101
return false, err
102102
}
103-
if len(AppsInfo.Apps.Data) > 1 {
103+
if len(AppsInfo) > 1 {
104104
return true, nil
105105
}
106-
if AppsInfo.Apps.Data[0].Productplan.Name != "free" { //case for 1 App
107-
return true, nil
106+
for _, app := range AppsInfo {
107+
if app.Productplan.Name != "free" { //case for 1 App
108+
return true, nil
109+
}
108110
}
109111
return false, nil
110112
}

0 commit comments

Comments
 (0)