11package client
22
33import (
4+ "errors"
45 "fmt"
56 "io"
67 "net/http"
@@ -10,6 +11,9 @@ import (
1011 "github.com/Cloverhound/webex-cli/internal/config"
1112)
1213
14+ // ErrDryRun is returned when a write operation is intercepted by --dry-run mode.
15+ var ErrDryRun = errors .New ("dry run: no changes made" )
16+
1317// Do executes an HTTP request. On a 401, it attempts to refresh the token and retry once.
1418func Do (req * Request ) ([]byte , int , error ) {
1519 body , status , err := doOnce (req )
@@ -83,6 +87,20 @@ func doOnce(req *Request) ([]byte, int, error) {
8387 }
8488 }
8589
90+ // Dry-run: intercept write operations before making the HTTP call
91+ if config .DryRun () && isWriteMethod (req .method ) {
92+ fmt .Fprintf (os .Stderr , "[DRY RUN] %s %s\n " , req .method , url )
93+ for k , v := range httpReq .Header {
94+ if k != "Authorization" {
95+ fmt .Fprintf (os .Stderr , "[DRY RUN] %s: %s\n " , k , strings .Join (v , ", " ))
96+ }
97+ }
98+ if req .bodyRaw != "" {
99+ fmt .Fprintf (os .Stderr , "[DRY RUN] Body: %s\n " , req .bodyRaw )
100+ }
101+ return nil , 0 , ErrDryRun
102+ }
103+
86104 resp , err := http .DefaultClient .Do (httpReq )
87105 if err != nil {
88106 return nil , 0 , fmt .Errorf ("executing request: %w" , err )
@@ -111,3 +129,12 @@ func truncate(s string, n int) string {
111129 }
112130 return s [:n ] + "..."
113131}
132+
133+ // isWriteMethod returns true for HTTP methods that modify data.
134+ func isWriteMethod (method string ) bool {
135+ switch method {
136+ case http .MethodPost , http .MethodPut , http .MethodDelete , http .MethodPatch :
137+ return true
138+ }
139+ return false
140+ }
0 commit comments