|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + formsapi "google.golang.org/api/forms/v1" |
| 9 | + |
| 10 | + "github.com/steipete/gogcli/internal/outfmt" |
| 11 | + "github.com/steipete/gogcli/internal/ui" |
| 12 | +) |
| 13 | + |
| 14 | +// FormsPublishCmd publishes a form via forms.setPublishSettings. |
| 15 | +type FormsPublishCmd struct { |
| 16 | + FormID string `arg:"" name:"formId" help:"Form ID"` |
| 17 | + Unpublish bool `name:"unpublish" help:"Unpublish the form instead of publishing it"` |
| 18 | + AcceptingResponses bool `name:"accepting-responses" help:"Whether a published form accepts responses" default:"true"` |
| 19 | +} |
| 20 | + |
| 21 | +func (c *FormsPublishCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 22 | + published := !c.Unpublish |
| 23 | + acceptingResponses := c.AcceptingResponses |
| 24 | + if !published { |
| 25 | + acceptingResponses = false |
| 26 | + } |
| 27 | + operation := "forms.publish" |
| 28 | + if !published { |
| 29 | + operation = "forms.unpublish" |
| 30 | + } |
| 31 | + |
| 32 | + return setFormPublishState(ctx, flags, formPublishStateRequest{ |
| 33 | + FormID: c.FormID, |
| 34 | + Published: published, |
| 35 | + AcceptingResponses: acceptingResponses, |
| 36 | + Operation: operation, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +type formPublishStateRequest struct { |
| 41 | + FormID string |
| 42 | + Published bool |
| 43 | + AcceptingResponses bool |
| 44 | + Operation string |
| 45 | +} |
| 46 | + |
| 47 | +func setFormPublishState(ctx context.Context, flags *RootFlags, publishReq formPublishStateRequest) error { |
| 48 | + account, err := requireAccount(flags) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + formID := strings.TrimSpace(normalizeGoogleID(publishReq.FormID)) |
| 53 | + if formID == "" { |
| 54 | + return usage("empty formId") |
| 55 | + } |
| 56 | + |
| 57 | + if dryRunErr := dryRunExit(ctx, flags, publishReq.Operation, map[string]any{ |
| 58 | + "form_id": formID, |
| 59 | + "published": publishReq.Published, |
| 60 | + "accepting_responses": publishReq.AcceptingResponses, |
| 61 | + }); dryRunErr != nil { |
| 62 | + return dryRunErr |
| 63 | + } |
| 64 | + |
| 65 | + svc, err := newFormsService(ctx, account) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + req := &formsapi.SetPublishSettingsRequest{ |
| 71 | + UpdateMask: "publish_state", |
| 72 | + PublishSettings: &formsapi.PublishSettings{ |
| 73 | + PublishState: &formsapi.PublishState{ |
| 74 | + IsPublished: publishReq.Published, |
| 75 | + IsAcceptingResponses: publishReq.AcceptingResponses, |
| 76 | + ForceSendFields: []string{"IsPublished", "IsAcceptingResponses"}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | + resp, err := svc.Forms.SetPublishSettings(formID, req).Context(ctx).Do() |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + form, err := svc.Forms.Get(formID).Context(ctx).Do() |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + responderURI := strings.TrimSpace(form.ResponderUri) |
| 91 | + if outfmt.IsJSON(ctx) { |
| 92 | + return outfmt.WriteJSON(ctx, os.Stdout, map[string]any{ |
| 93 | + "published": publishReq.Published, |
| 94 | + "accepting_responses": publishReq.AcceptingResponses, |
| 95 | + "form_id": formID, |
| 96 | + "responder_uri": responderURI, |
| 97 | + "edit_url": formEditURL(formID), |
| 98 | + "publish_settings": resp.PublishSettings, |
| 99 | + "form": form, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + u := ui.FromContext(ctx) |
| 104 | + u.Out().Printf("published\t%t", publishReq.Published) |
| 105 | + u.Out().Printf("accepting_responses\t%t", publishReq.AcceptingResponses) |
| 106 | + u.Out().Printf("form_id\t%s", formID) |
| 107 | + if responderURI != "" { |
| 108 | + u.Out().Printf("responder_uri\t%s", responderURI) |
| 109 | + } |
| 110 | + u.Out().Printf("edit_url\t%s", formEditURL(formID)) |
| 111 | + return nil |
| 112 | +} |
0 commit comments