-
Notifications
You must be signed in to change notification settings - Fork 12
/
social.go
137 lines (112 loc) · 3.4 KB
/
social.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package social
import (
"errors"
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/loginradius/lr-cli/api"
"github.com/loginradius/lr-cli/prompt"
"github.com/spf13/cobra"
)
type socialProvider struct {
Provider string `json:"Provider"`
ProviderKey string `json:"ProviderKey"`
ProviderSecret string `json:"ProviderSecret"`
Scope [1]string `json:"Scope"`
Status bool `json:"status"`
}
type socialProviderList struct {
Data []socialProvider `json:"Data"`
}
type Result struct {
ProviderName string `json:"ProviderName"`
Status bool `json:"status"`
}
type socialProviderList2 struct {
Data []Result `json:"Data"`
}
func NewsocialCmd() *cobra.Command {
var provider string
var on bool
var off bool
cmd := &cobra.Command{
Use: "social",
Short: "Updated the exsiting social provider",
Long: `Use this command to update the configured social login provider.`,
Example: `
$ lr set social -p Google
? API Key: <key>
? API Secret: <secret>
Google updated successfully.
$ lr set social -p Google --disable
Google Disabled Successfully
$ lr set social -p Google --enable
Google Enabled Successfully
`,
RunE: func(cmd *cobra.Command, args []string) error {
return update(provider, on, off)
},
}
cmd.Flags().StringVarP(&provider, "provider", "p", "", "The provider name which you want to update.")
cmd.Flags().BoolVarP(&on, "enable", "e", false, "This Flag is used to enable to field with the default configuration")
cmd.Flags().BoolVarP(&off, "disable", "d", false, "This Flag is used to enable to field with the default configuration")
return cmd
}
func update(provider string, on bool, off bool) error {
activeProv, err := api.GetActiveProviders()
if err != nil {
return err
}
provConfig, ok := activeProv[provider]
if !ok {
return errors.New("Configuration for the selected provider not found.")
}
if on {
if provConfig.Status {
return errors.New(provider + " is already enabled")
} else {
err := api.UpdateProviderStatus(provider, true)
if err != nil {
return err
}
fmt.Println(provider + " Enabled Successfully")
return nil
}
} else if off {
if !provConfig.Status {
return errors.New(provider + " is already disabled")
} else {
err := api.UpdateProviderStatus(provider, false)
if err != nil {
return err
}
fmt.Println(provider + " Disabled Successfully")
return nil
}
}
allProv, err := api.GetAllProviders()
if err != nil {
fmt.Println("Cannot add social login at the momment due to some issue at our end, kindly try after sometime.")
return nil
}
provObj, ok := allProv[strings.ToLower(provider)]
var updateProvObj api.AddProviderSchema
updateProvObj.Data = make([]api.AddProviderObj, 1)
prompt.SurveyAskOne(&survey.Input{
Message: provObj.Options[0].Display + ":",
Default: provConfig.ProviderKey,
}, &updateProvObj.Data[0].ProviderKey, survey.WithValidator(survey.Required))
prompt.SurveyAskOne(&survey.Input{
Message: provObj.Options[1].Display + ":",
Default: provConfig.ProviderSecret,
}, &updateProvObj.Data[0].ProviderSecret, survey.WithValidator(survey.Required))
updateProvObj.Data[0].Provider = provConfig.Provider
updateProvObj.Data[0].Scope = provConfig.Scope
updateProvObj.Data[0].Status = provConfig.Status
err = api.AddSocialProvider(updateProvObj)
if err != nil {
return err
}
fmt.Println(provider + " updated successfully.")
return nil
}