Skip to content

Commit f70258b

Browse files
authored
Allow creating SIP objects without a JSON file. (#580)
1 parent 46822ec commit f70258b

File tree

1 file changed

+232
-3
lines changed

1 file changed

+232
-3
lines changed

cmd/lk/sip.go

Lines changed: 232 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ var (
5252
Usage: "Create an inbound SIP Trunk",
5353
Action: createSIPInboundTrunk,
5454
ArgsUsage: RequestDesc[livekit.CreateSIPInboundTrunkRequest](),
55+
Flags: []cli.Flag{
56+
&cli.StringFlag{
57+
Name: "name",
58+
Usage: "Sets a new name for the trunk",
59+
},
60+
&cli.StringSliceFlag{
61+
Name: "numbers",
62+
Usage: "Sets a list of numbers for the trunk",
63+
},
64+
&cli.StringFlag{
65+
Name: "media-enc",
66+
Usage: "Sets media encryption for the trunk",
67+
},
68+
&cli.StringFlag{
69+
Name: "auth-user",
70+
Usage: "Set username for authentication",
71+
},
72+
&cli.StringFlag{
73+
Name: "auth-pass",
74+
Usage: "Set password for authentication",
75+
},
76+
},
5577
},
5678
{
5779
Name: "update",
@@ -105,6 +127,36 @@ var (
105127
Usage: "Create an outbound SIP Trunk",
106128
Action: createSIPOutboundTrunk,
107129
ArgsUsage: RequestDesc[livekit.CreateSIPOutboundTrunkRequest](),
130+
Flags: []cli.Flag{
131+
&cli.StringFlag{
132+
Name: "name",
133+
Usage: "Sets a new name for the trunk",
134+
},
135+
&cli.StringFlag{
136+
Name: "address",
137+
Usage: "Sets a destination address for the trunk",
138+
},
139+
&cli.StringFlag{
140+
Name: "transport",
141+
Usage: "Sets a transport for the trunk",
142+
},
143+
&cli.StringFlag{
144+
Name: "media-enc",
145+
Usage: "Sets media encryption for the trunk",
146+
},
147+
&cli.StringSliceFlag{
148+
Name: "numbers",
149+
Usage: "Sets a list of numbers for the trunk",
150+
},
151+
&cli.StringFlag{
152+
Name: "auth-user",
153+
Usage: "Set username for authentication",
154+
},
155+
&cli.StringFlag{
156+
Name: "auth-pass",
157+
Usage: "Set password for authentication",
158+
},
159+
},
108160
},
109161
{
110162
Name: "update",
@@ -166,6 +218,37 @@ var (
166218
Usage: "Create a SIP Dispatch Rule",
167219
Action: createSIPDispatchRule,
168220
ArgsUsage: RequestDesc[livekit.CreateSIPDispatchRuleRequest](),
221+
Flags: []cli.Flag{
222+
&cli.StringFlag{
223+
Name: "name",
224+
Usage: "Sets a new name for the dispatch rule",
225+
},
226+
&cli.StringSliceFlag{
227+
Name: "trunks",
228+
Usage: "Sets a list of trunks for the dispatch rule",
229+
},
230+
&cli.StringFlag{
231+
Name: "direct",
232+
Usage: "Sets a direct dispatch to a specified room",
233+
},
234+
&cli.StringFlag{
235+
Name: "caller",
236+
Aliases: []string{"individual"},
237+
Usage: "Sets a individual caller dispatch to a new room with a specific prefix",
238+
},
239+
&cli.StringFlag{
240+
Name: "callee",
241+
Usage: "Sets a callee number dispatch to a new room with a specific prefix",
242+
},
243+
&cli.BoolFlag{
244+
Name: "pin",
245+
Usage: "PIN for a dispatch rule",
246+
},
247+
&cli.BoolFlag{
248+
Name: "randomize",
249+
Usage: "Randomize room name, only applies to callee dispatch",
250+
},
251+
},
169252
},
170253
{
171254
Name: "update",
@@ -221,6 +304,14 @@ var (
221304
Name: "room",
222305
Usage: "`ROOM_NAME` to place the call to (overrides json config)",
223306
},
307+
&cli.StringFlag{
308+
Name: "identity",
309+
Usage: "`PARTICIPANT_IDENTITY` to use (overrides json config)",
310+
},
311+
&cli.StringFlag{
312+
Name: "name",
313+
Usage: "`PARTICIPANT_NAME` to use (overrides json config)",
314+
},
224315
&cli.BoolFlag{
225316
Name: "wait",
226317
Usage: "wait for the call to dial (overrides json config)",
@@ -326,6 +417,17 @@ func listUpdateFlag(cmd *cli.Command, setName string) *livekit.ListUpdate {
326417
return &livekit.ListUpdate{Set: val}
327418
}
328419

420+
func listSetFlag(cmd *cli.Command, setName string) ([]string, bool) {
421+
if !cmd.IsSet(setName) {
422+
return nil, false
423+
}
424+
val := cmd.StringSlice(setName)
425+
if len(val) == 1 && val[0] == "" {
426+
val = nil
427+
}
428+
return val, true
429+
}
430+
329431
func createSIPClient(cmd *cli.Command) (*lksdk.SIPClient, error) {
330432
pc, err := loadProjectDetails(cmd)
331433
if err != nil {
@@ -339,7 +441,36 @@ func createSIPInboundTrunk(ctx context.Context, cmd *cli.Command) error {
339441
if err != nil {
340442
return err
341443
}
342-
return createAndPrintReqs(ctx, cmd, nil, cli.CreateSIPInboundTrunk, printSIPInboundTrunkID)
444+
return createAndPrintReqs(ctx, cmd, func(req *livekit.CreateSIPInboundTrunkRequest) error {
445+
if req.Trunk == nil {
446+
req.Trunk = new(livekit.SIPInboundTrunkInfo)
447+
}
448+
p := req.Trunk
449+
if val := cmd.String("name"); val != "" {
450+
p.Name = val
451+
}
452+
if val, ok := listSetFlag(cmd, "numbers"); ok {
453+
p.Numbers = val
454+
}
455+
if val := cmd.String("media-enc"); val != "" {
456+
val = strings.ToUpper(val)
457+
v, ok := livekit.SIPMediaEncryption_value[val]
458+
if !ok {
459+
v, ok = livekit.SIPMediaEncryption_value["SIP_MEDIA_ENCRYPT_"+val]
460+
}
461+
if !ok {
462+
return fmt.Errorf("invalid value for SIP media encryption: %q", val)
463+
}
464+
p.MediaEncryption = livekit.SIPMediaEncryption(v)
465+
}
466+
if val := cmd.String("auth-user"); val != "" {
467+
p.AuthUsername = val
468+
}
469+
if val := cmd.String("auth-pass"); val != "" {
470+
p.AuthPassword = val
471+
}
472+
return nil
473+
}, cli.CreateSIPInboundTrunk, printSIPInboundTrunkID)
343474
}
344475

345476
func updateSIPInboundTrunk(ctx context.Context, cmd *cli.Command) error {
@@ -409,7 +540,50 @@ func createSIPOutboundTrunk(ctx context.Context, cmd *cli.Command) error {
409540
if err != nil {
410541
return err
411542
}
412-
return createAndPrintReqs(ctx, cmd, nil, cli.CreateSIPOutboundTrunk, printSIPOutboundTrunkID)
543+
return createAndPrintReqs(ctx, cmd, func(req *livekit.CreateSIPOutboundTrunkRequest) error {
544+
if req.Trunk == nil {
545+
req.Trunk = new(livekit.SIPOutboundTrunkInfo)
546+
}
547+
p := req.Trunk
548+
if val := cmd.String("name"); val != "" {
549+
p.Name = val
550+
}
551+
if val := cmd.String("address"); val != "" {
552+
p.Address = val
553+
}
554+
if val := cmd.String("transport"); val != "" {
555+
val = strings.ToUpper(val)
556+
v, ok := livekit.SIPTransport_value[val]
557+
if !ok {
558+
v, ok = livekit.SIPTransport_value["SIP_TRANSPORT_"+val]
559+
}
560+
if !ok {
561+
return fmt.Errorf("invalid value for SIP transport: %q", val)
562+
}
563+
p.Transport = livekit.SIPTransport(v)
564+
}
565+
if val := cmd.String("media-enc"); val != "" {
566+
val = strings.ToUpper(val)
567+
v, ok := livekit.SIPMediaEncryption_value[val]
568+
if !ok {
569+
v, ok = livekit.SIPMediaEncryption_value["SIP_MEDIA_ENCRYPT_"+val]
570+
}
571+
if !ok {
572+
return fmt.Errorf("invalid value for SIP media encryption: %q", val)
573+
}
574+
p.MediaEncryption = livekit.SIPMediaEncryption(v)
575+
}
576+
if val, ok := listSetFlag(cmd, "numbers"); ok {
577+
p.Numbers = val
578+
}
579+
if val := cmd.String("auth-user"); val != "" {
580+
p.AuthUsername = val
581+
}
582+
if val := cmd.String("auth-pass"); val != "" {
583+
p.AuthPassword = val
584+
}
585+
return nil
586+
}, cli.CreateSIPOutboundTrunk, printSIPOutboundTrunkID)
413587
}
414588

415589
func updateSIPOutboundTrunk(ctx context.Context, cmd *cli.Command) error {
@@ -658,7 +832,56 @@ func createSIPDispatchRule(ctx context.Context, cmd *cli.Command) error {
658832
if err != nil {
659833
return err
660834
}
661-
return createAndPrintReqs(ctx, cmd, nil, cli.CreateSIPDispatchRule, printSIPDispatchRuleID)
835+
return createAndPrintReqs(ctx, cmd, func(req *livekit.CreateSIPDispatchRuleRequest) error {
836+
if req.DispatchRule == nil {
837+
req.DispatchRule = new(livekit.SIPDispatchRuleInfo)
838+
}
839+
p := req.DispatchRule
840+
if val := cmd.String("name"); val != "" {
841+
p.Name = val
842+
}
843+
if val, ok := listSetFlag(cmd, "trunks"); ok {
844+
p.TrunkIds = val
845+
}
846+
if val := cmd.String("direct"); val != "" {
847+
if p.Rule != nil {
848+
return fmt.Errorf("only one dispatch rule type is allowed")
849+
}
850+
p.Rule = &livekit.SIPDispatchRule{
851+
Rule: &livekit.SIPDispatchRule_DispatchRuleDirect{
852+
DispatchRuleDirect: &livekit.SIPDispatchRuleDirect{
853+
RoomName: val,
854+
},
855+
},
856+
}
857+
}
858+
if val := cmd.String("caller"); val != "" {
859+
if p.Rule != nil {
860+
return fmt.Errorf("only one dispatch rule type is allowed")
861+
}
862+
p.Rule = &livekit.SIPDispatchRule{
863+
Rule: &livekit.SIPDispatchRule_DispatchRuleIndividual{
864+
DispatchRuleIndividual: &livekit.SIPDispatchRuleIndividual{
865+
RoomPrefix: val,
866+
},
867+
},
868+
}
869+
}
870+
if val := cmd.String("callee"); val != "" {
871+
if p.Rule != nil {
872+
return fmt.Errorf("only one dispatch rule type is allowed")
873+
}
874+
p.Rule = &livekit.SIPDispatchRule{
875+
Rule: &livekit.SIPDispatchRule_DispatchRuleCallee{
876+
DispatchRuleCallee: &livekit.SIPDispatchRuleCallee{
877+
RoomPrefix: val,
878+
Randomize: cmd.Bool("randomize"),
879+
},
880+
},
881+
}
882+
}
883+
return nil
884+
}, cli.CreateSIPDispatchRule, printSIPDispatchRuleID)
662885
}
663886

664887
func createSIPDispatchRuleLegacy(ctx context.Context, cmd *cli.Command) error {
@@ -823,6 +1046,12 @@ func createSIPParticipant(ctx context.Context, cmd *cli.Command) error {
8231046
if v := cmd.String("room"); v != "" {
8241047
req.RoomName = v
8251048
}
1049+
if v := cmd.String("identity"); v != "" {
1050+
req.ParticipantIdentity = v
1051+
}
1052+
if v := cmd.String("name"); v != "" {
1053+
req.ParticipantName = v
1054+
}
8261055
if cmd.Bool("wait") {
8271056
req.WaitUntilAnswered = true
8281057
}

0 commit comments

Comments
 (0)