|
| 1 | +package site |
| 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/request" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var AppName string |
| 16 | +var Domain string |
| 17 | +var PlanName string |
| 18 | +var planOption string |
| 19 | +var AppsInfo *api.CoreAppData |
| 20 | + |
| 21 | +type AddAppResponse struct { |
| 22 | + AppId int `json:"appId"` |
| 23 | +} |
| 24 | + |
| 25 | +func NewSiteCmd() *cobra.Command { |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "site", |
| 28 | + Short: "Adds a site", |
| 29 | + Long: heredoc.Doc(` |
| 30 | + This command enables user to add a site depending on the subscribed plan. |
| 31 | + `), |
| 32 | + Example: heredoc.Doc(` |
| 33 | + $ lr add site |
| 34 | +
|
| 35 | + Your site has been added |
| 36 | + `), |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + return addSite() |
| 39 | + }, |
| 40 | + } |
| 41 | + return cmd |
| 42 | +} |
| 43 | +func addSite() error { |
| 44 | + checkInput := input() |
| 45 | + if !checkInput { |
| 46 | + fmt.Println("Please enter the input paramaters properly.") |
| 47 | + return nil |
| 48 | + } |
| 49 | + checkPlan, err := plans() |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + if !checkPlan { |
| 54 | + fmt.Println("Please upgrade your plan to add more sites. ") |
| 55 | + return nil |
| 56 | + } |
| 57 | + err = add() |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + fmt.Println("Your site has been added.") |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func input() bool { |
| 66 | + fmt.Printf("Enter the App Name: ") |
| 67 | + fmt.Scanf("%s", &AppName) |
| 68 | + if AppName == "" { |
| 69 | + fmt.Println("App Name is a required entry") |
| 70 | + return false |
| 71 | + } |
| 72 | + fmt.Printf("Enter the Domain: ") |
| 73 | + fmt.Scanf("%s", &Domain) |
| 74 | + if Domain == "" { |
| 75 | + fmt.Println("Domain is a required entry") |
| 76 | + return false |
| 77 | + } |
| 78 | + plan := map[string]string{ |
| 79 | + "1": "free", |
| 80 | + "2": "developer", |
| 81 | + "3": "business", |
| 82 | + } |
| 83 | + fmt.Println("To select a plan, choose a correponding number from the following options: ") |
| 84 | + fmt.Println("1 - Free plan") |
| 85 | + fmt.Println("2 - Developer plan") |
| 86 | + fmt.Println("3 - Developer Pro plan") |
| 87 | + fmt.Printf("Option: ") |
| 88 | + fmt.Scanf("%s", &planOption) |
| 89 | + if planOption == "" { |
| 90 | + fmt.Println("Plan is a required entry") |
| 91 | + return false |
| 92 | + } |
| 93 | + PlanName = plan[planOption] |
| 94 | + return true |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +func plans() (bool, error) { |
| 99 | + AppsInfo, err := api.AppInfo() |
| 100 | + if err != nil { |
| 101 | + return false, err |
| 102 | + } |
| 103 | + if len(AppsInfo.Apps.Data) > 1 { |
| 104 | + return true, nil |
| 105 | + } |
| 106 | + if AppsInfo.Apps.Data[0].Productplan.Name != "free" { //case for 1 App |
| 107 | + return true, nil |
| 108 | + } |
| 109 | + return false, nil |
| 110 | +} |
| 111 | + |
| 112 | +func add() error { |
| 113 | + conf := config.GetInstance() |
| 114 | + paymentInfo, err := api.PaymentInfo() |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + newApp := conf.AdminConsoleAPIDomain + "/auth/create-new-app?" |
| 119 | + body, _ := json.Marshal(map[string]string{ |
| 120 | + "appName": AppName, |
| 121 | + "domain": Domain, |
| 122 | + "paymentMethodId": paymentInfo.Data.Order[0].Paymentdetail.Stripepaymentmethodid, |
| 123 | + "planName": PlanName, |
| 124 | + }) |
| 125 | + resp, err := request.Rest(http.MethodPost, newApp, nil, string(body)) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + var App AddAppResponse |
| 130 | + err = json.Unmarshal(resp, &App) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + switchRespObj, err := api.SetSites(App.AppId) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + err = api.SitesBasic(switchRespObj) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + return nil |
| 143 | + |
| 144 | +} |
0 commit comments