@@ -2,10 +2,12 @@ package api
22
33import (
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
6466type 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
10372func 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
11986func 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}
0 commit comments