-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathapps.go
958 lines (808 loc) · 24.8 KB
/
apps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/doctl/internal/apps"
"github.com/digitalocean/godo"
multierror "github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
// Apps creates the apps command.
func Apps() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "apps",
Aliases: []string{"app", "a"},
Short: "Display commands for working with apps",
Long: "The subcommands of `doctl app` manage your App Platform apps. For documentation on app specs used by multiple commands, see https://www.digitalocean.com/docs/app-platform/concepts/app-spec.",
},
}
cmd.AddCommand(AppsDev())
create := CmdBuilder(
cmd,
RunAppsCreate,
"create",
"Create an app",
`Create an app with the given app spec.`,
Writer,
aliasOpt("c"),
displayerType(&displayers.Apps{}),
)
AddStringFlag(create, doctl.ArgAppSpec, "", "", `Path to an app spec in JSON or YAML format. Set to "-" to read from stdin.`, requiredOpt())
AddBoolFlag(create, doctl.ArgCommandWait, "", false,
"Boolean that specifies whether to wait for an app to complete before returning control to the terminal")
AddBoolFlag(create, doctl.ArgCommandUpsert, "", false, "Boolean that specifies whether the app should be updated if it already exists")
CmdBuilder(
cmd,
RunAppsGet,
"get <app id>",
"Get an app",
`Get an app with the provided id.
Only basic information is included with the text output format. For complete app details including its app spec, use the JSON format.`,
Writer,
aliasOpt("g"),
displayerType(&displayers.Apps{}),
)
CmdBuilder(
cmd,
RunAppsList,
"list",
"List all apps",
`List all apps.
Only basic information is included with the text output format. For complete app details including the app specs, use the JSON format.`,
Writer,
aliasOpt("ls"),
displayerType(&displayers.Apps{}),
)
update := CmdBuilder(
cmd,
RunAppsUpdate,
"update <app id>",
"Update an app",
`Update the specified app with the given app spec. For more information about app specs, see https://www.digitalocean.com/docs/app-platform/concepts/app-spec`,
Writer,
aliasOpt("u"),
displayerType(&displayers.Apps{}),
)
AddStringFlag(update, doctl.ArgAppSpec, "", "", `Path to an app spec in JSON or YAML format. Set to "-" to read from stdin.`, requiredOpt())
AddBoolFlag(update, doctl.ArgCommandWait, "", false,
"Boolean that specifies whether to wait for an app to complete before returning control to the terminal")
deleteApp := CmdBuilder(
cmd,
RunAppsDelete,
"delete <app id>",
"Deletes an app",
`Deletes an app with the provided id.
This permanently deletes the app and all its associated deployments.`,
Writer,
aliasOpt("d"),
)
AddBoolFlag(deleteApp, doctl.ArgForce, doctl.ArgShortForce, false, "Delete the App without a confirmation prompt")
deploymentCreate := CmdBuilder(
cmd,
RunAppsCreateDeployment,
"create-deployment <app id>",
"Create a deployment",
`Create a deployment for an app.
Creating an app deployment will pull the latest changes from your repository and schedule a new deployment for your app.`,
Writer,
aliasOpt("cd"),
displayerType(&displayers.Deployments{}),
)
AddBoolFlag(deploymentCreate, doctl.ArgAppForceRebuild, "", false, "Force a re-build even if a previous build is eligible for reuse")
AddBoolFlag(deploymentCreate, doctl.ArgCommandWait, "", false,
"Boolean that specifies whether to wait for apps deployment to complete before returning control to the terminal")
CmdBuilder(
cmd,
RunAppsGetDeployment,
"get-deployment <app id> <deployment id>",
"Get a deployment",
`Get a deployment for an app.
Only basic information is included with the text output format. For complete app details including its app specs, use the JSON format.`,
Writer,
aliasOpt("gd"),
displayerType(&displayers.Deployments{}),
)
CmdBuilder(
cmd,
RunAppsListDeployments,
"list-deployments <app id>",
"List all deployments",
`List all deployments for an app.
Only basic information is included with the text output format. For complete app details including the app specs, use the JSON format.`,
Writer,
aliasOpt("lsd"),
displayerType(&displayers.Deployments{}),
)
logs := CmdBuilder(
cmd,
RunAppsGetLogs,
"logs <app id> <component name (defaults to all components)>",
"Get logs",
`Get component logs for a deployment of an app.
Three types of logs are supported and can be configured with --`+doctl.ArgAppLogType+`:
- build
- deploy
- run `,
Writer,
aliasOpt("l"),
)
AddStringFlag(logs, doctl.ArgAppDeployment, "", "", "The deployment ID. Defaults to current deployment.")
AddStringFlag(logs, doctl.ArgAppLogType, "", strings.ToLower(string(godo.AppLogTypeRun)), "The type of logs.")
AddBoolFlag(logs, doctl.ArgAppLogFollow, "f", false, "Follow logs as they are emitted.")
AddIntFlag(logs, doctl.ArgAppLogTail, "", -1, "Number of lines to show from the end of the log.")
CmdBuilder(
cmd,
RunAppsListRegions,
"list-regions",
"List App Platform regions",
`List all regions supported by App Platform including details about their current availability.`,
Writer,
displayerType(&displayers.AppRegions{}),
)
propose := CmdBuilder(
cmd,
RunAppsPropose,
"propose",
"Propose an app spec",
`Reviews and validates an app specification for a new or existing app. The request returns some information about the proposed app, including app cost and upgrade cost. If an existing app ID is specified, the app spec is treated as a proposed update to the existing app.
Only basic information is included with the text output format. For complete app details including an updated app spec, use the JSON format.`,
Writer,
aliasOpt("c"),
displayerType(&displayers.Apps{}),
)
AddStringFlag(propose, doctl.ArgAppSpec, "", "", "Path to an app spec in JSON or YAML format. For more information about app specs, see https://www.digitalocean.com/docs/app-platform/concepts/app-spec", requiredOpt())
AddStringFlag(propose, doctl.ArgApp, "", "", "An optional existing app ID. If specified, the app spec will be treated as a proposed update to the existing app.")
CmdBuilder(
cmd,
RunAppListAlerts,
"list-alerts <app id>",
"List alerts on an app",
`List all alerts associated to an app and its components`,
Writer,
aliasOpt("la"),
displayerType(&displayers.AppAlerts{}),
)
updateAlertDestinations := CmdBuilder(
cmd,
RunAppUpdateAlertDestinations,
"update-alert-destinations <app id> <alert id>",
"Update alert destinations",
`Update alert destinations`,
Writer,
aliasOpt("uad"),
displayerType(&displayers.AppAlerts{}),
)
AddStringFlag(updateAlertDestinations, doctl.ArgAppAlertDestinations, "", "", "Path to an alert destinations file in JSON or YAML format.")
cmd.AddCommand(appsSpec())
cmd.AddCommand(appsTier())
return cmd
}
// RunAppsCreate creates an app.
func RunAppsCreate(c *CmdConfig) error {
specPath, err := c.Doit.GetString(c.NS, doctl.ArgAppSpec)
if err != nil {
return err
}
appSpec, err := apps.ReadAppSpec(os.Stdin, specPath)
if err != nil {
return err
}
upsert, err := c.Doit.GetBool(c.NS, doctl.ArgCommandUpsert)
if err != nil {
return err
}
app, err := c.Apps().Create(&godo.AppCreateRequest{Spec: appSpec})
if err != nil {
if gerr, ok := err.(*godo.ErrorResponse); ok && gerr.Response.StatusCode == 409 && upsert {
notice("App already exists, updating")
apps, err := c.Apps().List()
if err != nil {
return err
}
id, err := getIDByName(apps, appSpec.Name)
if err != nil {
return err
}
app, err = c.Apps().Update(id, &godo.AppUpdateRequest{Spec: appSpec})
if err != nil {
return err
}
} else {
return err
}
}
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}
var errs error
if wait {
apps := c.Apps()
notice("App creation is in progress, waiting for app to be running")
err := waitForActiveDeployment(apps, app.ID, "")
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("app deployment couldn't enter `running` state: %v", err))
if err := c.Display(displayers.Apps{app}); err != nil {
errs = multierror.Append(errs, err)
}
return errs
}
app, _ = c.Apps().Get(app.ID)
}
notice("App created")
return c.Display(displayers.Apps{app})
}
// RunAppsGet gets an app.
func RunAppsGet(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
id := c.Args[0]
app, err := c.Apps().Get(id)
if err != nil {
return err
}
return c.Display(displayers.Apps{app})
}
// RunAppsList lists all apps.
func RunAppsList(c *CmdConfig) error {
apps, err := c.Apps().List()
if err != nil {
return err
}
return c.Display(displayers.Apps(apps))
}
// RunAppsUpdate updates an app.
func RunAppsUpdate(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
id := c.Args[0]
specPath, err := c.Doit.GetString(c.NS, doctl.ArgAppSpec)
if err != nil {
return err
}
appSpec, err := apps.ReadAppSpec(os.Stdin, specPath)
if err != nil {
return err
}
app, err := c.Apps().Update(id, &godo.AppUpdateRequest{Spec: appSpec})
if err != nil {
return err
}
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}
var errs error
if wait {
apps := c.Apps()
notice("App update is in progress, waiting for app to be running")
err := waitForActiveDeployment(apps, app.ID, "")
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("app deployment couldn't enter `running` state: %v", err))
if err := c.Display(displayers.Apps{app}); err != nil {
errs = multierror.Append(errs, err)
}
return errs
}
app, _ = c.Apps().Get(app.ID)
}
notice("App updated")
return c.Display(displayers.Apps{app})
}
// RunAppsDelete deletes an app.
func RunAppsDelete(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
id := c.Args[0]
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
if !force && AskForConfirmDelete("App", 1) != nil {
return errOperationAborted
}
err = c.Apps().Delete(id)
if err != nil {
return err
}
notice("App deleted")
return nil
}
// RunAppsCreateDeployment creates a deployment for an app.
func RunAppsCreateDeployment(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
forceRebuild, err := c.Doit.GetBool(c.NS, doctl.ArgAppForceRebuild)
if err != nil {
return err
}
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}
deployment, err := c.Apps().CreateDeployment(appID, forceRebuild)
if err != nil {
return err
}
var errs error
if wait {
apps := c.Apps()
notice("App deployment is in progress, waiting for deployment to be running")
err := waitForActiveDeployment(apps, appID, deployment.ID)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("app deployment couldn't enter `running` state: %v", err))
if err := c.Display(displayers.Deployments{deployment}); err != nil {
errs = multierror.Append(errs, err)
}
return errs
}
deployment, _ = c.Apps().GetDeployment(appID, deployment.ID)
}
notice("Deployment created")
return c.Display(displayers.Deployments{deployment})
}
func waitForActiveDeployment(apps do.AppsService, appID string, deploymentID string) error {
const maxAttempts = 180
attempts := 0
printNewLineSet := false
for i := 0; i < maxAttempts; i++ {
if attempts != 0 {
fmt.Fprint(os.Stderr, ".")
if !printNewLineSet {
printNewLineSet = true
defer fmt.Fprintln(os.Stderr)
}
}
if deploymentID == "" {
deployments, err := apps.ListDeployments(appID)
if err != nil {
return err
}
// We could find no deployments if both of the following are true:
// - The deployment isn't created in the backend synchronously with the create/update request.
// - This is the first ever deployment of the app.
// Note that if the first is ever true we will do the wrong thing here and test the status of
// a previous deployment, however there's no better way to correlate a deployment with the
// request that triggered it.
if len(deployments) > 0 {
deploymentID = deployments[0].ID
}
} else {
deployment, err := apps.GetDeployment(appID, deploymentID)
if err != nil {
return err
}
allSuccessful := deployment.Progress.SuccessSteps == deployment.Progress.TotalSteps
if allSuccessful {
return nil
}
if deployment.Progress.ErrorSteps > 0 {
return fmt.Errorf("error deploying app (%s) (deployment ID: %s):\n%s", appID, deployment.ID, godo.Stringify(deployment.Progress))
}
}
attempts++
time.Sleep(10 * time.Second)
}
return fmt.Errorf("timeout waiting to app (%s) deployment", appID)
}
// RunAppsGetDeployment gets a deployment for an app.
func RunAppsGetDeployment(c *CmdConfig) error {
if len(c.Args) < 2 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
deploymentID := c.Args[1]
deployment, err := c.Apps().GetDeployment(appID, deploymentID)
if err != nil {
return err
}
return c.Display(displayers.Deployments{deployment})
}
// RunAppsListDeployments lists deployments for an app.
func RunAppsListDeployments(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
deployments, err := c.Apps().ListDeployments(appID)
if err != nil {
return err
}
return c.Display(displayers.Deployments(deployments))
}
// RunAppsGetLogs gets app logs for a given component.
func RunAppsGetLogs(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
var component string
if len(c.Args) >= 2 {
component = c.Args[1]
}
deploymentID, err := c.Doit.GetString(c.NS, doctl.ArgAppDeployment)
if err != nil {
return err
}
if deploymentID == "" {
app, err := c.Apps().Get(appID)
if err != nil {
return err
}
if app.ActiveDeployment != nil {
deploymentID = app.ActiveDeployment.ID
} else if app.InProgressDeployment != nil {
deploymentID = app.InProgressDeployment.ID
} else {
return fmt.Errorf("unable to retrieve logs; no deployment found for app %s", appID)
}
}
logTypeStr, err := c.Doit.GetString(c.NS, doctl.ArgAppLogType)
if err != nil {
return err
}
var logType godo.AppLogType
switch logTypeStr {
case strings.ToLower(string(godo.AppLogTypeBuild)):
logType = godo.AppLogTypeBuild
case strings.ToLower(string(godo.AppLogTypeDeploy)):
logType = godo.AppLogTypeDeploy
case strings.ToLower(string(godo.AppLogTypeRun)):
logType = godo.AppLogTypeRun
default:
return fmt.Errorf("Invalid log type %s", logTypeStr)
}
logFollow, err := c.Doit.GetBool(c.NS, doctl.ArgAppLogFollow)
if err != nil {
return err
}
logTail, err := c.Doit.GetInt(c.NS, doctl.ArgAppLogTail)
if err != nil {
return err
}
logs, err := c.Apps().GetLogs(appID, deploymentID, component, logType, logFollow, logTail)
if err != nil {
return err
}
if logs.LiveURL != "" {
url, err := url.Parse(logs.LiveURL)
if err != nil {
return err
}
schemaFunc := func(message []byte) (io.Reader, error) {
data := struct {
Data string `json:"data"`
}{}
err = json.Unmarshal(message, &data)
if err != nil {
return nil, err
}
r := strings.NewReader(data.Data)
return r, nil
}
token := url.Query().Get("token")
switch url.Scheme {
case "http":
url.Scheme = "ws"
default:
url.Scheme = "wss"
}
listener := c.Doit.Listen(url, token, schemaFunc, c.Out)
err = listener.Start()
if err != nil {
return err
}
} else if len(logs.HistoricURLs) > 0 {
resp, err := http.Get(logs.HistoricURLs[0])
if err != nil {
return err
}
defer resp.Body.Close()
io.Copy(c.Out, resp.Body)
} else {
warn("No logs found for app component")
}
return nil
}
// RunAppsPropose proposes an app spec
func RunAppsPropose(c *CmdConfig) error {
appID, err := c.Doit.GetString(c.NS, doctl.ArgApp)
if err != nil {
return err
}
specPath, err := c.Doit.GetString(c.NS, doctl.ArgAppSpec)
if err != nil {
return err
}
appSpec, err := apps.ReadAppSpec(os.Stdin, specPath)
if err != nil {
return err
}
res, err := c.Apps().Propose(&godo.AppProposeRequest{
Spec: appSpec,
AppID: appID,
})
if err != nil {
// most likely an invalid app spec. The error message would start with "error validating app spec"
return err
}
return c.Display(displayers.AppProposeResponse{Res: res})
}
func appsSpec() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "spec",
Short: "Display commands for working with app specs",
Long: "The subcommands of `doctl app spec` manage your app specs.",
},
}
getCmd := CmdBuilder(cmd, RunAppsSpecGet, "get <app id>", "Retrieve an application's spec", `Use this command to retrieve the latest spec of an app.
Optionally, pass a deployment ID to get the spec of that specific deployment.`, Writer)
AddStringFlag(getCmd, doctl.ArgAppDeployment, "", "", "optional: a deployment ID")
AddStringFlag(getCmd, doctl.ArgFormat, "", "yaml", `the format to output the spec in; either "yaml" or "json"`)
validateCmd := CmdBuilder(cmd, RunAppsSpecValidate, "validate <spec file>", "Validate an application spec", `Use this command to check whether a given app spec (YAML or JSON) is valid.
You may pass - as the filename to read from stdin.`, Writer)
AddBoolFlag(validateCmd, doctl.ArgSchemaOnly, "", false, "Only validate the spec schema and not the correctness of the spec.")
return cmd
}
// RunAppsSpecGet gets the spec for an app
func RunAppsSpecGet(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
deploymentID, err := c.Doit.GetString(c.NS, doctl.ArgAppDeployment)
if err != nil {
return err
}
format, err := c.Doit.GetString(c.NS, doctl.ArgFormat)
if err != nil {
return err
}
var spec *godo.AppSpec
if deploymentID == "" {
app, err := c.Apps().Get(appID)
if err != nil {
return err
}
spec = app.Spec
} else {
deployment, err := c.Apps().GetDeployment(appID, deploymentID)
if err != nil {
return err
}
spec = deployment.Spec
}
switch format {
case "json":
e := json.NewEncoder(c.Out)
e.SetIndent("", " ")
return e.Encode(spec)
case "yaml":
yaml, err := yaml.Marshal(spec)
if err != nil {
return fmt.Errorf("marshaling the spec as yaml: %v", err)
}
_, err = c.Out.Write(yaml)
return err
default:
return fmt.Errorf("invalid spec format %q, must be one of: json, yaml", format)
}
}
// RunAppsSpecValidate validates an app spec file
func RunAppsSpecValidate(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
specPath := c.Args[0]
appSpec, err := apps.ReadAppSpec(os.Stdin, specPath)
if err != nil {
return err
}
schemaOnly, err := c.Doit.GetBool(c.NS, doctl.ArgSchemaOnly)
if err != nil {
return err
}
if schemaOnly {
ymlSpec, err := yaml.Marshal(appSpec)
if err != nil {
return fmt.Errorf("marshaling the spec as yaml: %v", err)
}
_, err = c.Out.Write(ymlSpec)
return err
}
res, err := c.Apps().Propose(&godo.AppProposeRequest{
Spec: appSpec,
})
if err != nil {
// most likely an invalid app spec. The error message would start with "error validating app spec"
return err
}
ymlSpec, err := yaml.Marshal(res.Spec)
if err != nil {
return fmt.Errorf("marshaling the spec as yaml: %v", err)
}
_, err = c.Out.Write(ymlSpec)
return err
}
// RunAppsListRegions lists all app platform regions.
func RunAppsListRegions(c *CmdConfig) error {
regions, err := c.Apps().ListRegions()
if err != nil {
return err
}
return c.Display(displayers.AppRegions(regions))
}
func appsTier() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "tier",
Short: "Display commands for working with app tiers",
Long: "The subcommands of `doctl app tier` retrieve information about app tiers.",
},
}
CmdBuilder(cmd, RunAppsTierList, "list", "List all app tiers", `Use this command to list all the available app tiers.`, Writer)
CmdBuilder(cmd, RunAppsTierGet, "get <tier slug>", "Retrieve an app tier", `Use this command to retrieve information about a specific app tier.`, Writer)
cmd.AddCommand(appsTierInstanceSize())
return cmd
}
// RunAppsTierList lists all app tiers.
func RunAppsTierList(c *CmdConfig) error {
tiers, err := c.Apps().ListTiers()
if err != nil {
return err
}
return c.Display(displayers.AppTiers(tiers))
}
// RunAppsTierGet gets an app tier.
func RunAppsTierGet(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
slug := c.Args[0]
tier, err := c.Apps().GetTier(slug)
if err != nil {
return err
}
return c.Display(displayers.AppTiers([]*godo.AppTier{tier}))
}
func appsTierInstanceSize() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "instance-size",
Short: "Display commands for working with app instance sizes",
Long: "The subcommands of `doctl app tier instance-size` retrieve information about app instance sizes.",
},
}
CmdBuilder(cmd, RunAppsTierInstanceSizeList, "list", "List all app instance sizes", `Use this command to list all the available app instance sizes.`, Writer)
CmdBuilder(cmd, RunAppsTierInstanceSizeGet, "get <instance size slug>", "Retrieve an app instance size", `Use this command to retrieve information about a specific app instance size.`, Writer)
return cmd
}
// RunAppsTierInstanceSizeList lists all app tiers.
func RunAppsTierInstanceSizeList(c *CmdConfig) error {
instanceSizes, err := c.Apps().ListInstanceSizes()
if err != nil {
return err
}
return c.Display(displayers.AppInstanceSizes(instanceSizes))
}
// RunAppsTierInstanceSizeGet gets an app tier.
func RunAppsTierInstanceSizeGet(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
slug := c.Args[0]
instanceSize, err := c.Apps().GetInstanceSize(slug)
if err != nil {
return err
}
return c.Display(displayers.AppInstanceSizes([]*godo.AppInstanceSize{instanceSize}))
}
// RunAppListAlerts gets configured alerts on an app
func RunAppListAlerts(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
alerts, err := c.Apps().ListAlerts(appID)
if err != nil {
return err
}
return c.Display(displayers.AppAlerts(alerts))
}
func RunAppUpdateAlertDestinations(c *CmdConfig) error {
if len(c.Args) < 2 {
return doctl.NewMissingArgsErr(c.NS)
}
appID := c.Args[0]
alertID := c.Args[1]
alertDestinationsPath, err := c.Doit.GetString(c.NS, doctl.ArgAppAlertDestinations)
if err != nil {
return err
}
update, err := readAppAlertDestination(os.Stdin, alertDestinationsPath)
if err != nil {
return err
}
alert, err := c.Apps().UpdateAlertDestinations(appID, alertID, update)
if err != nil {
return err
}
return c.Display(displayers.AppAlerts([]*godo.AppAlert{alert}))
}
func readAppAlertDestination(stdin io.Reader, path string) (*godo.AlertDestinationUpdateRequest, error) {
var alertDestinations io.Reader
if path == "-" {
alertDestinations = stdin
} else {
alertDestinationsFile, err := os.Open(path) // guardrails-disable-line
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("opening app alert destinations: %s does not exist", path)
}
return nil, fmt.Errorf("opening app alert destinations: %w", err)
}
defer alertDestinationsFile.Close()
alertDestinations = alertDestinationsFile
}
byt, err := ioutil.ReadAll(alertDestinations)
if err != nil {
return nil, fmt.Errorf("reading app alert destinations: %w", err)
}
s, err := parseAppAlert(byt)
if err != nil {
return nil, fmt.Errorf("parsing app alert destinations: %w", err)
}
return s, nil
}
func parseAppAlert(destinations []byte) (*godo.AlertDestinationUpdateRequest, error) {
jsonAlertDestinations, err := yaml.YAMLToJSON(destinations)
if err != nil {
return nil, err
}
dec := json.NewDecoder(bytes.NewReader(jsonAlertDestinations))
dec.DisallowUnknownFields()
var alertDestinations godo.AlertDestinationUpdateRequest
if err := dec.Decode(&alertDestinations); err != nil {
return nil, err
}
return &alertDestinations, nil
}
func getIDByName(apps []*godo.App, name string) (string, error) {
for _, app := range apps {
if app.Spec.Name == name {
return app.ID, nil
}
}
return "", fmt.Errorf("app not found")
}