|
| 1 | +package calling |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Cloverhound/webex-cli/internal/audio" |
| 10 | + "github.com/Cloverhound/webex-cli/internal/config" |
| 11 | + "github.com/Cloverhound/webex-cli/internal/output" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + registerAnnouncementUpload() |
| 17 | + registerAnnouncementUploadLocation() |
| 18 | + registerAnnouncementUpdate() |
| 19 | + registerAnnouncementUpdateLocation() |
| 20 | +} |
| 21 | + |
| 22 | +// registerAnnouncementCmd is a helper that registers one announcement upload/update command. |
| 23 | +func registerAnnouncementCmd(use, short, long string, method string, |
| 24 | + buildURL func(orgID, locationID, announcementID string) string, |
| 25 | + needsLocation, needsAnnouncement bool) { |
| 26 | + |
| 27 | + var filePath string |
| 28 | + var name string |
| 29 | + var orgID string |
| 30 | + var locationID string |
| 31 | + var announcementID string |
| 32 | + |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: use, |
| 35 | + Short: short, |
| 36 | + Long: long, |
| 37 | + RunE: func(c *cobra.Command, args []string) error { |
| 38 | + f, err := os.Open(filePath) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("opening audio file: %w", err) |
| 41 | + } |
| 42 | + defer f.Close() |
| 43 | + |
| 44 | + url := buildURL(orgID, locationID, announcementID) |
| 45 | + |
| 46 | + parts := []audio.MultipartPart{ |
| 47 | + { |
| 48 | + FieldName: "name", |
| 49 | + ContentType: "text/plain", |
| 50 | + Data: strings.NewReader(name), |
| 51 | + }, |
| 52 | + { |
| 53 | + FieldName: "file", |
| 54 | + FileName: filepath.Base(filePath), |
| 55 | + ContentType: "audio/wav", |
| 56 | + Data: f, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + body, statusCode, err := audio.UploadMultipart(method, url, parts) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + return output.Print(body, statusCode) |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + cmd.Flags().StringVar(&filePath, "file", "", "Path to WAV file to upload") |
| 70 | + cmd.MarkFlagRequired("file") |
| 71 | + cmd.Flags().StringVar(&name, "name", "", "Announcement name") |
| 72 | + cmd.MarkFlagRequired("name") |
| 73 | + cmd.Flags().StringVar(&orgID, "org-id", "", "Organization ID") |
| 74 | + if needsLocation { |
| 75 | + cmd.Flags().StringVar(&locationID, "location-id", "", "Location ID") |
| 76 | + cmd.MarkFlagRequired("location-id") |
| 77 | + } |
| 78 | + if needsAnnouncement { |
| 79 | + cmd.Flags().StringVar(&announcementID, "announcement-id", "", "Announcement ID") |
| 80 | + cmd.MarkFlagRequired("announcement-id") |
| 81 | + } |
| 82 | + |
| 83 | + announcementRepositoryCmd.AddCommand(cmd) |
| 84 | +} |
| 85 | + |
| 86 | +func registerAnnouncementUpload() { |
| 87 | + registerAnnouncementCmd( |
| 88 | + "upload-binary-greeting", |
| 89 | + "Upload Binary Announcement Greeting", |
| 90 | + `Upload an announcement greeting (WAV) at the organization level. |
| 91 | +
|
| 92 | +Uses multipart/form-data with a "name" text field and a "file" binary part. |
| 93 | +
|
| 94 | +Examples: |
| 95 | + webex calling announcement-repository upload-binary-greeting --file greeting.wav --name "Main Greeting" |
| 96 | + webex calling announcement-repository upload-binary-greeting --file greeting.wav --name "Main Greeting" --dry-run`, |
| 97 | + "POST", |
| 98 | + func(orgID, _, _ string) string { |
| 99 | + url := config.CallingBaseURL + "/telephony/config/announcements" |
| 100 | + if orgID != "" { |
| 101 | + url += "?orgId=" + orgID |
| 102 | + } |
| 103 | + return url |
| 104 | + }, |
| 105 | + false, false, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +func registerAnnouncementUploadLocation() { |
| 110 | + registerAnnouncementCmd( |
| 111 | + "upload-binary-greeting-2", |
| 112 | + "Upload Binary Announcement Greeting at Location", |
| 113 | + `Upload an announcement greeting (WAV) at a specific location. |
| 114 | +
|
| 115 | +Uses multipart/form-data with a "name" text field and a "file" binary part. |
| 116 | +
|
| 117 | +Examples: |
| 118 | + webex calling announcement-repository upload-binary-greeting-2 --file greeting.wav --name "Lobby Greeting" --location-id <loc-id> |
| 119 | + webex calling announcement-repository upload-binary-greeting-2 --file greeting.wav --name "Lobby Greeting" --location-id <loc-id> --dry-run`, |
| 120 | + "POST", |
| 121 | + func(orgID, locationID, _ string) string { |
| 122 | + url := config.CallingBaseURL + "/telephony/config/locations/" + locationID + "/announcements" |
| 123 | + if orgID != "" { |
| 124 | + url += "?orgId=" + orgID |
| 125 | + } |
| 126 | + return url |
| 127 | + }, |
| 128 | + true, false, |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +func registerAnnouncementUpdate() { |
| 133 | + registerAnnouncementCmd( |
| 134 | + "update-binary-greeting", |
| 135 | + "Update Binary Announcement Greeting", |
| 136 | + `Update an existing announcement greeting (WAV) at the organization level. |
| 137 | +
|
| 138 | +Uses multipart/form-data with a "name" text field and a "file" binary part. |
| 139 | +
|
| 140 | +Examples: |
| 141 | + webex calling announcement-repository update-binary-greeting --file greeting.wav --name "Updated Greeting" --announcement-id <ann-id> |
| 142 | + webex calling announcement-repository update-binary-greeting --file greeting.wav --name "Updated Greeting" --announcement-id <ann-id> --dry-run`, |
| 143 | + "PUT", |
| 144 | + func(orgID, _, announcementID string) string { |
| 145 | + url := config.CallingBaseURL + "/telephony/config/announcements/" + announcementID |
| 146 | + if orgID != "" { |
| 147 | + url += "?orgId=" + orgID |
| 148 | + } |
| 149 | + return url |
| 150 | + }, |
| 151 | + false, true, |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +func registerAnnouncementUpdateLocation() { |
| 156 | + registerAnnouncementCmd( |
| 157 | + "update-binary-greeting-2", |
| 158 | + "Update Binary Announcement Greeting at Location", |
| 159 | + `Update an existing announcement greeting (WAV) at a specific location. |
| 160 | +
|
| 161 | +Uses multipart/form-data with a "name" text field and a "file" binary part. |
| 162 | +
|
| 163 | +Examples: |
| 164 | + webex calling announcement-repository update-binary-greeting-2 --file greeting.wav --name "Updated Greeting" --location-id <loc-id> --announcement-id <ann-id>`, |
| 165 | + "PUT", |
| 166 | + func(orgID, locationID, announcementID string) string { |
| 167 | + url := config.CallingBaseURL + "/telephony/config/locations/" + locationID + "/announcements/" + announcementID |
| 168 | + if orgID != "" { |
| 169 | + url += "?orgId=" + orgID |
| 170 | + } |
| 171 | + return url |
| 172 | + }, |
| 173 | + true, true, |
| 174 | + ) |
| 175 | +} |
0 commit comments