Skip to content

Commit d5c474f

Browse files
Akash-Patilmohammed786
authored andcommitted
Added set site command and Refactored code
1 parent f3c12b0 commit d5c474f

8 files changed

Lines changed: 203 additions & 33 deletions

File tree

api/account.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"strconv"
7+
8+
"github.com/loginradius/lr-cli/request"
9+
)
10+
11+
type SitesToken struct {
12+
APIVersion string `json:"ApiVersion"`
13+
AppID int32 `json:"AppId"`
14+
AppName string `json:"AppName"`
15+
Authenticated bool `json:"authenticated"`
16+
XSign string `json:"xsign"`
17+
XToken string `json:"xtoken"`
18+
}
19+
20+
func SetSites(appid int) (*SitesToken, error) {
21+
switchapp := conf.AdminConsoleAPIDomain + "/account/switchapp?appid=" + strconv.Itoa(appid)
22+
switchResp, err := request.Rest(http.MethodGet, switchapp, nil, "")
23+
var switchRespObj SitesToken
24+
err = json.Unmarshal(switchResp, &switchRespObj)
25+
if err != nil {
26+
return nil, err
27+
}
28+
return &switchRespObj, nil
29+
}

api/auth.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package api
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
58
"net/http"
69
"time"
710

11+
"github.com/loginradius/lr-cli/cmdutil"
812
"github.com/loginradius/lr-cli/config"
913
"github.com/loginradius/lr-cli/request"
1014
)
@@ -62,3 +66,74 @@ func AuthValidateToken() (*ValidateTokenResp, error) {
6266
}
6367
return &resObj, nil
6468
}
69+
70+
type AppID struct {
71+
CurrentAppId int `json:"currentAppId"`
72+
}
73+
74+
func CurrentID() (*AppID, error) {
75+
conf := config.GetInstance()
76+
config := conf.AdminConsoleAPIDomain + "/auth/config?"
77+
var currentAppId AppID
78+
resp, err := request.Rest(http.MethodGet, config, nil, "")
79+
if err != nil {
80+
return nil, err
81+
}
82+
err = json.Unmarshal(resp, &currentAppId)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return &currentAppId, nil
88+
}
89+
90+
func SitesBasic(tokens *SitesToken) error {
91+
conf := config.GetInstance()
92+
var newToken SitesToken
93+
client := &http.Client{}
94+
basic := conf.AdminConsoleAPIDomain + "/auth/basicsettings?"
95+
req, err := http.NewRequest(http.MethodGet, basic, nil)
96+
if err != nil {
97+
log.Printf("Could not make request % -v", err)
98+
}
99+
req.Header.Add("x-is-loginradius--sign", tokens.XSign)
100+
req.Header.Add("x-is-loginradius--token", tokens.XToken)
101+
req.Header.Add("x-is-loginradius-ajax", "true")
102+
req.Header.Add("Content-Type", "application/json; charset=utf-8")
103+
req.Header.Add("Origin", conf.DashboardDomain)
104+
resp, err := client.Do(req)
105+
if err != nil {
106+
log.Printf("%s", err.Error())
107+
}
108+
109+
defer resp.Body.Close()
110+
bodyBytes, err := ioutil.ReadAll(resp.Body)
111+
if err != nil {
112+
fmt.Print(err.Error())
113+
}
114+
//obtaining the new tokens
115+
err = json.Unmarshal(bodyBytes, &newToken)
116+
if err != nil {
117+
return err
118+
}
119+
result := LoginResponse{
120+
APIVersion: tokens.APIVersion,
121+
AppName: tokens.AppName,
122+
AppID: tokens.AppID,
123+
Authenticated: true,
124+
XSign: newToken.XSign, //switching tokens
125+
XToken: newToken.XToken,
126+
}
127+
resObj, err := json.Marshal(result)
128+
err = cmdutil.DeleteFiles()
129+
if err != nil {
130+
return err
131+
}
132+
err = cmdutil.StoreCreds(resObj)
133+
if err != nil {
134+
return err
135+
}
136+
137+
return nil
138+
139+
}

api/deployment.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"encoding/json"
5+
56
"net/http"
67
"time"
78

@@ -143,3 +144,18 @@ func AppInfo() (*CoreAppData, error) {
143144
}
144145
return &App, nil
145146
}
147+
148+
func CheckApp(appid int) (bool, error) {
149+
AppInfo, err := AppInfo()
150+
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
157+
}
158+
}
159+
return false, nil
160+
161+
}

cmd/get/site/site.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
package site
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"net/http"
75

86
"github.com/MakeNowJust/heredoc"
97
"github.com/loginradius/lr-cli/api"
10-
"github.com/loginradius/lr-cli/config"
11-
"github.com/loginradius/lr-cli/request"
128
"github.com/spf13/cobra"
139
)
1410

15-
type AppID struct {
16-
CurrentAppId int `json:"currentAppId"`
17-
}
18-
1911
var all *bool
2012
var active *bool
2113
var appid *int
@@ -63,7 +55,7 @@ func getSite() error {
6355
numberOfApps := len(AppInfo.Apps.Data)
6456

6557
if *active && (!*all && *appid == -1) {
66-
currentID, err := currentID()
58+
currentID, err := api.CurrentID()
6759
if err != nil {
6860
return err
6961
}
@@ -105,23 +97,6 @@ func getSite() error {
10597
return nil
10698
}
10799

108-
func currentID() (*AppID, error) {
109-
conf := config.GetInstance()
110-
config := conf.AdminConsoleAPIDomain + "/auth/config?"
111-
var currentAppId AppID
112-
resp, err := request.Rest(http.MethodGet, config, nil, "")
113-
if err != nil {
114-
return nil, err
115-
}
116-
err = json.Unmarshal(resp, &currentAppId)
117-
if err != nil {
118-
return nil, err
119-
}
120-
121-
return &currentAppId, nil
122-
123-
}
124-
125100
func Output(AppInfo *api.CoreAppData, i int) {
126101
fmt.Println(" App Name: ", AppInfo.Apps.Data[i].Appname)
127102
fmt.Println(" App ID: ", AppInfo.Apps.Data[i].Appid)

cmd/logout/logout.go

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

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"net/http"
87
"os"
98
"os/user"
10-
"path"
119
"path/filepath"
1210
"time"
1311

@@ -36,7 +34,7 @@ func logout() error {
3634
dirName := filepath.Join(user.HomeDir, ".lrcli")
3735
_, err := os.Stat(dirName)
3836
if os.IsNotExist(err) {
39-
fmt.Println("You are already been logged out")
37+
fmt.Println("You have already been logged out")
4038
return nil
4139
} else {
4240
cmdutil.Openbrowser(conf.HubPageDomain + "/auth.aspx?action=logout&return_url=http://localhost:8089/postLogout")
@@ -46,10 +44,7 @@ func logout() error {
4644
RouteName: "/postLogout",
4745
})
4846
tempServer.Server.ListenAndServe()
49-
dir, err := ioutil.ReadDir(dirName)
50-
for _, d := range dir {
51-
os.RemoveAll(path.Join([]string{dirName, d.Name()}...))
52-
}
47+
err := cmdutil.DeleteFiles()
5348
if err != nil {
5449
return err
5550
}

cmd/set/set.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/loginradius/lr-cli/cmd/set/accountPassword"
55
"github.com/loginradius/lr-cli/cmd/set/domain"
66
"github.com/loginradius/lr-cli/cmd/set/email"
7+
"github.com/loginradius/lr-cli/cmd/set/site"
78
"github.com/loginradius/lr-cli/cmd/set/theme"
89

910
"github.com/spf13/cobra"
@@ -17,6 +18,9 @@ func NewsetCmd() *cobra.Command {
1718
Long: `This commmand acts as a base command for set subcommands`,
1819
}
1920

21+
siteCmd := site.NewSiteCmd()
22+
cmd.AddCommand(siteCmd)
23+
2024
themeCmd := theme.NewThemeCmd()
2125
cmd.AddCommand(themeCmd)
2226

cmd/set/site/site.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package site
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/loginradius/lr-cli/api"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var appid int
12+
13+
func NewSiteCmd() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "site",
16+
Short: "Enables switching between sites",
17+
Long: heredoc.Doc(`
18+
This command changes switches sites based on the App ID entered by the user.
19+
`),
20+
Example: heredoc.Doc(`
21+
$ lr set site --appid <appid> # To fetch app id use lr get site --all
22+
23+
Your site has been changed
24+
25+
`),
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
return setSite()
28+
},
29+
}
30+
fl := cmd.Flags()
31+
fl.IntVarP(&appid, "appid", "i", -1, "Switches the site")
32+
33+
return cmd
34+
}
35+
36+
func setSite() error {
37+
checkApp, err := api.CheckApp(appid)
38+
if err != nil {
39+
return err
40+
}
41+
if !checkApp {
42+
fmt.Println("There is no site with this AppID.")
43+
return nil
44+
}
45+
currentID, err := api.CurrentID()
46+
if err != nil {
47+
return err
48+
}
49+
if currentID.CurrentAppId == appid {
50+
fmt.Println("You are already using this site")
51+
return nil
52+
}
53+
switchRespObj, err := api.SetSites(appid)
54+
err = api.SitesBasic(switchRespObj)
55+
if err != nil {
56+
return err
57+
}
58+
59+
fmt.Println("Your site has been changed")
60+
61+
return nil
62+
}

cmdutil/lrUtils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"os/user"
12+
"path"
1213
"path/filepath"
1314
"runtime"
1415
)
@@ -108,3 +109,16 @@ func ThemeConstants(theme string) (ThemeType, ThemeType) {
108109
}
109110
return auths[theme], profiles[theme]
110111
}
112+
113+
func DeleteFiles() error {
114+
user, _ := user.Current()
115+
dirName := filepath.Join(user.HomeDir, ".lrcli")
116+
dir, err := ioutil.ReadDir(dirName)
117+
for _, d := range dir {
118+
os.RemoveAll(path.Join([]string{dirName, d.Name()}...))
119+
}
120+
if err != nil {
121+
return err
122+
}
123+
return nil
124+
}

0 commit comments

Comments
 (0)