Skip to content

Commit af25270

Browse files
Akash-Patilmohammed786
authored andcommitted
Added get, add, delete SOTT commands (#57)
1 parent bb61c4c commit af25270

8 files changed

Lines changed: 224 additions & 16 deletions

File tree

api/deployment.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ type HostedPageResponse struct {
6464
} `json:"Pages"`
6565
}
6666

67+
type SottResponse struct {
68+
Data []struct {
69+
AuthenticityToken string `json:"AuthenticityToken"`
70+
Comment string `json:"Comment"`
71+
CreatedDate string `json:"CreatedDate"`
72+
DateRange string `json:"DateRange"`
73+
IsEncoded bool `json:"IsEncoded"`
74+
Technology string `json:"Technology"`
75+
} `json:"Data"`
76+
}
77+
6778
type CoreAppData struct {
6879
Apps struct {
6980
Data []SitesReponse `json:"Data"`
@@ -101,6 +112,34 @@ func GetPage() (*HostedPageResponse, error) {
101112
return &resultResp, nil
102113
}
103114

115+
func GetSott() (*SottResponse, error) {
116+
sottUrl := conf.AdminConsoleAPIDomain + "/deployment/sott?"
117+
resp, err := request.Rest(http.MethodGet, sottUrl, nil, "")
118+
if err != nil {
119+
return nil, err
120+
}
121+
var resultResp SottResponse
122+
err = json.Unmarshal(resp, &resultResp)
123+
if err != nil {
124+
return nil, err
125+
}
126+
return &resultResp, nil
127+
}
128+
129+
//This function uses Authenticity token to check if SOTT exists.
130+
func CheckToken(token string) (bool, error) {
131+
Sott, err := GetSott()
132+
if err != nil {
133+
return false, err
134+
}
135+
for i := 0; i < len(Sott.Data); i++ {
136+
if token == Sott.Data[i].AuthenticityToken {
137+
return true, nil
138+
}
139+
}
140+
return false, nil
141+
}
142+
104143
func CheckLoginMethod() error {
105144
res, err := GetSites()
106145
if err != nil {

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/loginradius/lr-cli/cmd/add/customField"
66
"github.com/loginradius/lr-cli/cmd/add/domain"
77
"github.com/loginradius/lr-cli/cmd/add/loginMethod"
8+
"github.com/loginradius/lr-cli/cmd/add/sott"
89

910
"github.com/loginradius/lr-cli/cmd/add/hooks"
1011
"github.com/loginradius/lr-cli/cmd/add/site"
@@ -24,6 +25,9 @@ func NewaddCmd() *cobra.Command {
2425
hooksCmd := hooks.NewHooksCmd()
2526
cmd.AddCommand(hooksCmd)
2627

28+
sottCmd := sott.NewSottCmd()
29+
cmd.AddCommand(sottCmd)
30+
2731
loginMethodCmd := loginMethod.NewloginMethodCmd()
2832
cmd.AddCommand(loginMethodCmd)
2933

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package generateSott
1+
package sott
22

33
import (
44
"encoding/json"
@@ -14,8 +14,6 @@ import (
1414
"github.com/spf13/cobra"
1515
)
1616

17-
var fileName string
18-
1917
type sott struct {
2018
Encoded bool `json:"Encoded"`
2119
FromDate string `json:"FromDate"`
@@ -31,15 +29,14 @@ type Resp struct {
3129
Technology string `json:"Technology"`
3230
}
3331

34-
var url string
35-
36-
func NewgenerateSottCmd() *cobra.Command {
32+
func NewSottCmd() *cobra.Command {
3733
opts := &sott{}
3834
cmd := &cobra.Command{
39-
Use: "generate-sott",
40-
Short: "generates sott",
41-
Long: `This commmand generates sott`,
42-
Example: heredoc.Doc(`$ lr generate-sott -f <FromDate(mm/dd/yyyy)> -t <ToDate(mm/dd/yyyy)>
35+
Use: "sott",
36+
Short: "Adds a sott",
37+
Long: `Use this command to add a sott configured for your app.`,
38+
Example: heredoc.Doc(`$ lr add sott -f <FromDate(mm/dd/yyyy)> -t <ToDate(mm/dd/yyyy)>
39+
Comment(optional): <value>
4340
Select a technology
4441
.....
4542
.....
@@ -75,13 +72,14 @@ func NewgenerateSottCmd() *cobra.Command {
7572

7673
func generate(opts *sott) error {
7774
conf := config.GetInstance()
78-
opts.Comment = ""
75+
fmt.Printf("Comment(optional): ")
76+
fmt.Scanf("%s\n", &opts.Comment)
7977
opts.Encoded = false
8078
opts.Technology = getTech()
8179
if opts.Technology == "" {
8280
return nil
8381
}
84-
url = conf.AdminConsoleAPIDomain + "/deployment/sott?"
82+
url := conf.AdminConsoleAPIDomain + "/deployment/sott?"
8583
body, _ := json.Marshal(opts)
8684
var resultResp Resp
8785
resp, err := request.Rest(http.MethodPost, url, nil, string(body))

cmd/delete/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/loginradius/lr-cli/cmd/delete/domain"
77
"github.com/loginradius/lr-cli/cmd/delete/hooks"
88
"github.com/loginradius/lr-cli/cmd/delete/loginMethod"
9+
"github.com/loginradius/lr-cli/cmd/delete/sott"
910

1011
"github.com/loginradius/lr-cli/cmd/delete/site"
1112
"github.com/loginradius/lr-cli/cmd/delete/social"
@@ -24,6 +25,9 @@ func NewdeleteCmd() *cobra.Command {
2425
hooksCmd := hooks.NewHooksCmd()
2526
cmd.AddCommand((hooksCmd))
2627

28+
sottCmd := sott.NewSottCmd()
29+
cmd.AddCommand((sottCmd))
30+
2731
loginMethodCmd := loginMethod.NewloginMethodCmd()
2832
cmd.AddCommand((loginMethodCmd))
2933

cmd/delete/sott/sott.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package sott
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/prompt"
12+
"github.com/loginradius/lr-cli/request"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var token string
17+
var option bool
18+
var all *bool
19+
20+
type Response struct {
21+
Isdeleted bool `json:"isdeleted"`
22+
}
23+
24+
func NewSottCmd() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "sott",
27+
Short: "Deletes SOTTs",
28+
Long: `Use this command to delete a single or all SOTTs configured to your app.`,
29+
Example: heredoc.Doc(`
30+
$ lr delete sott --token <value> //Pass Authenticity token of SOTT to be deleted as value.
31+
32+
SOTT deleted successfully.
33+
34+
$ lr delete sott --all //Deletes all SOTTs
35+
36+
All SOTTs for your app have been deleted successfully.
37+
38+
`),
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
return deleteSott()
41+
42+
},
43+
}
44+
fl := cmd.Flags()
45+
all = fl.Bool("all", false, "Deletes all SOTT")
46+
fl.StringVarP(&token, "token", "t", "", "Authenticity Token")
47+
return cmd
48+
}
49+
50+
func deleteSott() error {
51+
if token == "--all" {
52+
fmt.Println("Use exactly one of the following flags:")
53+
fmt.Println("--all: Deletes all SOTTs configured to your app.")
54+
fmt.Println("--token: Deletes SOTT with matching Authenticity token.")
55+
return nil
56+
}
57+
err := prompt.Confirm("Are you sure you want to proceed ?", &option)
58+
if err != nil {
59+
return err
60+
}
61+
if !option {
62+
return nil
63+
}
64+
conf := config.GetInstance()
65+
if !*all && token != "" {
66+
checkToken, err := api.CheckToken(token)
67+
if err != nil {
68+
return err
69+
}
70+
if !checkToken {
71+
fmt.Println("SOTT with this Authenticity Token does not exist.")
72+
return nil
73+
}
74+
url := conf.AdminConsoleAPIDomain + "/deployment/sott?" + "authenticityToken=" + token
75+
isDeleted, err := delete(url)
76+
if err != nil {
77+
return err
78+
}
79+
if isDeleted {
80+
fmt.Println("SOTT deleted successfully.")
81+
} else {
82+
fmt.Println("Delete action failed.")
83+
}
84+
} else if *all && token == "" {
85+
url := conf.AdminConsoleAPIDomain + "/deployment/sott/all?"
86+
isDeleted, err := delete(url)
87+
if err != nil {
88+
return err
89+
}
90+
if isDeleted {
91+
fmt.Println("All SOTTs have been deleted successfully.")
92+
} else {
93+
fmt.Println("Delete action failed.")
94+
}
95+
}
96+
return nil
97+
}
98+
99+
func delete(url string) (bool, error) {
100+
resp, err := request.Rest(http.MethodDelete, url, nil, "")
101+
if err != nil {
102+
return false, err
103+
}
104+
var status Response
105+
err = json.Unmarshal(resp, &status)
106+
if err != nil {
107+
return false, err
108+
}
109+
if status.Isdeleted == true {
110+
return true, nil
111+
}
112+
return false, nil
113+
}

cmd/get/get.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
profiles "github.com/loginradius/lr-cli/cmd/get/profile"
1010
"github.com/loginradius/lr-cli/cmd/get/schema"
1111
"github.com/loginradius/lr-cli/cmd/get/site"
12+
"github.com/loginradius/lr-cli/cmd/get/sott"
1213

1314
"github.com/loginradius/lr-cli/cmd/get/domain"
1415
"github.com/loginradius/lr-cli/cmd/get/servertime"
@@ -29,6 +30,9 @@ func NewGetCmd() *cobra.Command {
2930
hooksCmd := hooks.NewHooksCmd()
3031
cmd.AddCommand(hooksCmd)
3132

33+
sottCmd := sott.NewSottCmd()
34+
cmd.AddCommand(sottCmd)
35+
3236
loginMethodCmd := loginMethod.NewloginMethodCmd()
3337
cmd.AddCommand(loginMethodCmd)
3438

cmd/get/sott/sott.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sott
2+
3+
import (
4+
"os"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/loginradius/lr-cli/api"
8+
"github.com/olekukonko/tablewriter"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewSottCmd() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "sott",
15+
Short: "Fetches list of Sott",
16+
Long: `Use this command to fetch the list of Sott's configured to your app.`,
17+
Example: heredoc.Doc(`
18+
$ lr get sott
19+
+--------------------+-------------+--------------------------+---------------+
20+
| AUTHENTICITY TOKEN | TECHNOLOGY | DATE RANGE | COMMENT |
21+
+--------------------+-------------+--------------------------+---------------+
22+
| <value> | android | 2021/8/3 0:0:0 - 2022/8/3| test |
23+
| | | 0:0:0 | |
24+
+--------------------+-------------+--------------------------+---------------+
25+
26+
`),
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
return getSott()
29+
30+
},
31+
}
32+
return cmd
33+
}
34+
35+
func getSott() error {
36+
SottInfo, err := api.GetSott()
37+
if err != nil {
38+
return err
39+
}
40+
numberOfSott := len(SottInfo.Data)
41+
var data [][]string
42+
for i := 0; i < numberOfSott; i++ {
43+
data = append(data, []string{SottInfo.Data[i].AuthenticityToken, SottInfo.Data[i].Technology, SottInfo.Data[i].DateRange, SottInfo.Data[i].Comment})
44+
}
45+
table := tablewriter.NewWriter(os.Stdout)
46+
table.SetHeader([]string{"Authenticity Token", "Technology", "Date Range", "Comment"})
47+
table.AppendBulk(data)
48+
table.Render()
49+
return nil
50+
}

cmd/root/root.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package root
33
import (
44
"github.com/loginradius/lr-cli/cmd/add"
55
"github.com/loginradius/lr-cli/cmd/demo"
6-
"github.com/loginradius/lr-cli/cmd/generateSott"
76
"github.com/loginradius/lr-cli/cmd/resetSecret"
87
"github.com/loginradius/lr-cli/cmd/set"
98
"github.com/loginradius/lr-cli/cmd/version"
@@ -75,8 +74,5 @@ func NewRootCmd() *cobra.Command {
7574
addCmd := add.NewaddCmd()
7675
rootCmd.AddCommand(addCmd)
7776

78-
generateSottCmd := generateSott.NewgenerateSottCmd()
79-
rootCmd.AddCommand(generateSottCmd)
80-
8177
return rootCmd
8278
}

0 commit comments

Comments
 (0)