11package domain
22
33import (
4- "encoding/json"
54 "errors"
65 "fmt"
7- "net/http"
86 "strings"
97
108 "github.com/MakeNowJust/heredoc"
119 "github.com/loginradius/lr-cli/api"
12- "github.com/loginradius/lr-cli/request"
13-
1410 "github.com/loginradius/lr-cli/cmdutil"
15- "github.com/loginradius/lr-cli/config"
16-
1711 "github.com/spf13/cobra"
1812)
1913
@@ -39,80 +33,47 @@ func NewdomainCmd() *cobra.Command {
3933 Use : "domain" ,
4034 Short : "set domain" ,
4135 Long : `This commmand sets domain` ,
42- Example : heredoc .Doc (`$ lr set domain --domain <domain> --domainmod <domainmodified>
43- domain successfully updated
44- http://localhost;...
36+ Example : heredoc .Doc (`$ lr set domain --domain <domain> --new-domain <new domain>
37+ Domain successfully updated
4538 ` ),
4639 RunE : func (cmd * cobra.Command , args []string ) error {
4740 if opts .Domain == "" {
4841 return & cmdutil.FlagError {Err : errors .New ("`domain` is require argument" )}
4942 }
5043
5144 if opts .DomainMod == "" {
52- return & cmdutil.FlagError {Err : errors .New ("`domainmod ` is require argument" )}
45+ return & cmdutil.FlagError {Err : errors .New ("`new-domain ` is require argument" )}
5346 }
5447
55- var p , err = get ()
48+ p , err := api . GetSites ()
5649 if err != nil {
5750 return err
5851 }
59- domain := strings .ReplaceAll (p .CallbackUrl , (";" + opts .Domain ), (";" + opts .DomainMod ))
52+ if ! strings .Contains (p .Callbackurl , opts .Domain ) {
53+ return & cmdutil.FlagError {Err : errors .New ("Entered Domain Not Found" )}
54+ }
55+ if opts .Domain == "http://localhost" || opts .Domain == "http://127.0.0.1" {
56+ return & cmdutil.FlagError {Err : errors .New ("Cannot Update Default Domains" )}
57+ }
58+ domain := strings .ReplaceAll (p .Callbackurl , opts .Domain , opts .DomainMod )
6059 return set (domain )
6160
6261 },
6362 }
6463
6564 fl := cmd .Flags ()
66- fl .StringVarP (& opts .Domain , "domain" , "d" , "" , "domain name " )
67- fl .StringVarP (& opts .DomainMod , "domainmod " , "m " , "" , "domain modified name " )
65+ fl .StringVarP (& opts .Domain , "domain" , "d" , "" , "Enter Old Domain Value " )
66+ fl .StringVarP (& opts .DomainMod , "new-domain " , "n " , "" , "Enter New Domain Value " )
6867
6968 return cmd
7069}
7170
72- func get () (* domainManagement , error ) {
73- conf := config .GetInstance ()
74- var url string
75- url = conf .AdminConsoleAPIDomain + "/deployment/sites?"
76-
77- var resultResp * domainManagement
78- resp , err := request .Rest (http .MethodGet , url , nil , "" )
79- if err != nil {
80- return nil , err
81- }
82- err = json .Unmarshal (resp , & resultResp )
83- if err != nil {
84- return nil , err
85- }
86-
87- return resultResp , nil
88- }
89-
9071func set (domain string ) error {
91- Match , err := api .Verify (domain )
92- if err != nil {
93- return err
94- }
95- if ! Match {
96- fmt .Println ("Please verify the domain you have entered" )
97- return nil
98- }
99- var url string
100- body , _ := json .Marshal (map [string ]string {
101- "domain" : "http://localhost" ,
102- "production" : domain ,
103- "staging" : "" ,
104- })
105- conf := config .GetInstance ()
106-
107- url = conf .AdminConsoleAPIDomain + "/deployment/sites?"
108-
109- var resultResp Result
110- resp , err := request .Rest (http .MethodPost , url , nil , string (body ))
111- err = json .Unmarshal (resp , & resultResp )
72+ urls := strings .Split (domain , ";" )
73+ err := api .UpdateDomain (urls )
11274 if err != nil {
11375 return err
11476 }
115- fmt .Println ("domain successfully updated" )
116- fmt .Println (resultResp .CallbackUrl )
77+ fmt .Println ("Domain Successfully Updated" )
11778 return nil
11879}
0 commit comments