forked from cloudflare/wrangler-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
977 lines (923 loc) · 41.1 KB
/
main.rs
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#![cfg_attr(feature = "strict", deny(warnings))]
extern crate text_io;
extern crate tokio;
use std::env;
use std::path::Path;
use std::str::FromStr;
use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
use exitfailure::ExitFailure;
use url::Url;
use wrangler::commands;
use wrangler::commands::kv::key::KVMetaData;
use wrangler::installer;
use wrangler::preview::{HttpMethod, PreviewOpt};
use wrangler::settings;
use wrangler::settings::global_user::GlobalUser;
use wrangler::settings::toml::TargetType;
use wrangler::terminal::{emoji, interactive, message, styles};
use wrangler::version::background_check_for_updates;
fn main() -> Result<(), ExitFailure> {
env_logger::init();
let latest_version_receiver = background_check_for_updates();
if let Ok(me) = env::current_exe() {
// If we're actually running as the installer then execute our
// self-installation, otherwise just continue as usual.
if me
.file_stem()
.and_then(|s| s.to_str())
.expect("executable should have a filename")
.starts_with("wrangler-init")
{
installer::install();
}
}
run()?;
if let Ok(latest_version) = latest_version_receiver.try_recv() {
let latest_version = styles::highlight(latest_version.to_string());
let new_version_available = format!(
"A new version of Wrangler ({}) is available!",
latest_version
);
let update_message = "You can learn more about updating here:".to_string();
let update_docs_url =
styles::url("https://developers.cloudflare.com/workers/quickstart#updating-the-cli");
message::billboard(&format!(
"{}\n{}\n{}",
new_version_available, update_message, update_docs_url
));
}
Ok(())
}
#[allow(clippy::cognitive_complexity)]
fn run() -> Result<(), failure::Error> {
// Define commonly used arguments and arg groups up front for consistency
// The args below are for KV Subcommands
let kv_binding_arg = Arg::with_name("binding")
.help("The binding of the namespace this action applies to")
.short("b")
.long("binding")
.value_name("BINDING NAME")
.takes_value(true);
let kv_namespace_id_arg = Arg::with_name("namespace-id")
.help("The id of the namespace this action applies to")
.short("n")
.long("namespace-id")
.value_name("ID")
.takes_value(true);
let kv_namespace_specifier_group = ArgGroup::with_name("namespace-specifier")
.args(&["binding", "namespace-id"])
.required(true);
// This arg is for any action that uses environments (e.g. KV subcommands, publish)
let environment_arg = Arg::with_name("env")
.help("Environment to use")
.short("e")
.long("env")
.takes_value(true)
.value_name("ENVIRONMENT NAME");
let secret_name_arg = Arg::with_name("name")
.help("Name of the secret variable")
.short("n")
.long("name")
.required(true)
.takes_value(true)
.index(1)
.value_name("VAR_NAME");
let verbose_arg = Arg::with_name("verbose")
.long("verbose")
.takes_value(false)
.help("toggle verbose output");
let silent_verbose_arg = verbose_arg.clone().hidden(true);
let matches = App::new(format!("{}{} wrangler", emoji::WORKER, emoji::SPARKLES))
.version(env!("CARGO_PKG_VERSION"))
.author("The Wrangler Team <[email protected]>")
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::VersionlessSubcommands)
.subcommand(
SubCommand::with_name("kv:namespace")
.about(&*format!(
"{} Interact with your Workers KV Namespaces",
emoji::FILES
))
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("create")
.about("Create a new namespace")
.arg(environment_arg.clone())
.arg(
Arg::with_name("binding")
.help("The binding for your new namespace")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.about("Delete namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("list")
.about("List all namespaces on your Cloudflare account")
.arg(silent_verbose_arg.clone())
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("kv:key")
.about(&*format!(
"{} Individually manage Workers KV key-value pairs",
emoji::KEY
))
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg(silent_verbose_arg.clone())
.subcommand(
SubCommand::with_name("put")
.about("Put a key-value pair into a namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(
Arg::with_name("key")
.help("Key to write value to")
.required(true)
.index(1)
)
.arg(
Arg::with_name("value")
.help("Value for key")
.required(true)
.index(2)
)
.arg(
Arg::with_name("expiration-ttl")
.help("Number of seconds for which the entries should be visible before they expire. At least 60. Takes precedence over 'expiration' option")
.short("t")
.long("ttl")
.value_name("SECONDS")
.takes_value(true)
)
.arg(
Arg::with_name("expiration")
.help("Number of seconds since the UNIX epoch, indicating when the key-value pair should expire")
.short("x")
.long("expiration")
.takes_value(true)
.value_name("SECONDS")
)
.arg(
Arg::with_name("path")
.help("The value passed in is a path to a file; open and upload its contents")
.short("p")
.long("path")
.takes_value(false)
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("get")
.about("Get a key's value from a namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(
Arg::with_name("key")
.help("Key whose value to get")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.about("Delete a key and its value from a namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(
Arg::with_name("key")
.help("Key whose value to delete")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("list")
.about("List all keys in a namespace. Produces JSON output")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(
Arg::with_name("prefix")
.help("The prefix for filtering listed keys")
.short("p")
.long("prefix")
.value_name("STRING")
.takes_value(true),
)
.arg(silent_verbose_arg.clone())
)
)
.subcommand(
SubCommand::with_name("kv:bulk")
.about(&*format!(
"{} Interact with multiple Workers KV key-value pairs at once",
emoji::BICEP
))
.arg(silent_verbose_arg.clone())
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("put")
.about("Upload multiple key-value pairs to a namespace")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.arg(
Arg::with_name("path")
.help("the JSON file of key-value pairs to upload, in form [{\"key\":..., \"value\":...}\"...]")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.arg(kv_binding_arg.clone())
.arg(kv_namespace_id_arg.clone())
.group(kv_namespace_specifier_group.clone())
.arg(environment_arg.clone())
.about("Delete multiple keys and their values from a namespace")
.arg(
Arg::with_name("path")
.help("the JSON file of key-value pairs to upload, in form [\"<example-key>\", ...]")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
)
.subcommand(
SubCommand::with_name("route")
.about(&*format!(
"{} List or delete worker routes.",
emoji::ROUTE
))
.arg(silent_verbose_arg.clone())
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("list")
.about("List all routes associated with a zone (outputs json)")
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.arg(environment_arg.clone())
.about("Delete a route by id")
.arg(
Arg::with_name("route_id")
.help("the id associated with the route you want to delete (find using `wrangler route list`)")
.required(true)
.index(1)
)
.arg(silent_verbose_arg.clone())
)
)
.subcommand(
SubCommand::with_name("secret")
.about(&*format!(
"{} Generate a secret that can be referenced in the worker script",
emoji::SECRET
))
.arg(silent_verbose_arg.clone())
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("put")
.about("Create or update a secret variable for a script")
.arg(secret_name_arg.clone())
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("delete")
.about("Delete a secret variable from a script")
.arg(secret_name_arg.clone())
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
)
.subcommand(
SubCommand::with_name("list")
.about("List all secrets for a script")
.arg(environment_arg.clone())
.arg(silent_verbose_arg.clone())
)
)
.subcommand(
SubCommand::with_name("generate")
.about(&*format!(
"{} Generate a new worker project",
emoji::DANCERS
))
.arg(
Arg::with_name("name")
.help("the name of your worker! defaults to 'worker'")
.index(1),
)
.arg(
Arg::with_name("template")
.help("a link to a github template! defaults to https://github.com/cloudflare/worker-template")
.index(2),
)
.arg(
Arg::with_name("type")
.short("t")
.long("type")
.takes_value(true)
.help("the type of project you want generated"),
)
.arg(
Arg::with_name("site")
.short("s")
.long("site")
.takes_value(false)
.help("initializes a Workers Sites project. Overrides `type` and `template`"),
)
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("init")
.about(&*format!(
"{} Create a wrangler.toml for an existing project",
emoji::INBOX
))
.arg(
Arg::with_name("name")
.help("the name of your worker! defaults to 'worker'")
.index(1),
)
.arg(
Arg::with_name("type")
.short("t")
.long("type")
.takes_value(true)
.help("the type of project you want generated"),
)
.arg(
Arg::with_name("site")
.short("s")
.long("site")
.takes_value(false)
.help("initializes a Workers Sites project. Overrides `type` and `template`"),
)
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("build")
.about(&*format!(
"{} Build your worker",
emoji::CRAB
))
.arg(
Arg::with_name("env")
.help("environment to build")
.short("e")
.long("env")
.takes_value(true)
)
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("preview")
.about(&*format!(
"{} Preview your code temporarily on cloudflareworkers.com",
emoji::MICROSCOPE
))
.arg(
Arg::with_name("headless")
.help("Don't open the browser on preview")
.long("headless")
.takes_value(false)
)
.arg(
Arg::with_name("method")
.help("Type of request to preview your worker with (get, post)")
.index(1),
)
.arg(
Arg::with_name("body")
.help("Body string to post to your preview worker request")
.index(2),
)
.arg(
Arg::with_name("url")
.help("URL to open in the worker preview")
.short("u")
.long("url")
.takes_value(true)
)
.arg(
Arg::with_name("env")
.help("Environment to preview")
.short("e")
.long("env")
.takes_value(true)
)
.arg(
Arg::with_name("watch")
.help("Watch your project for changes and update the preview automagically")
.long("watch")
.takes_value(false),
)
.arg(verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("dev")
.about(&*format!(
"{} Start a local server for developing your worker",
emoji::EAR
))
.arg(
Arg::with_name("env")
.help("environment to build")
.short("e")
.long("env")
.takes_value(true)
)
.arg(
Arg::with_name("port")
.help("port to listen on. defaults to 8787")
.short("p")
.long("port")
.takes_value(true)
)
.arg(
Arg::with_name("host")
.help("domain to test behind your worker. defaults to example.com")
.short("h")
.long("host")
.takes_value(true)
)
.arg(
Arg::with_name("ip")
.help("ip to listen on. defaults to localhost")
.short("i")
.long("ip")
.takes_value(true)
)
.arg(verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("publish")
.about(&*format!(
"{} Publish your worker to the orange cloud",
emoji::UP
))
.arg(
Arg::with_name("env")
.help("environments to publish to")
.short("e")
.long("env")
.takes_value(true)
)
.arg(verbose_arg.clone())
.arg(
Arg::with_name("release")
.hidden(true)
.long("release")
.takes_value(false)
.help("[deprecated] alias of wrangler publish")
),
)
.subcommand(
SubCommand::with_name("config")
.about(&*format!(
"{} Set up wrangler with your Cloudflare account",
emoji::SLEUTH
))
.arg(
Arg::with_name("api-key")
.help("use an email and global API key for authentication. This is not recommended; use API tokens (the default) if possible")
.long("api-key")
.takes_value(false),
)
.arg(
Arg::with_name("no-verify")
.help("do not verify provided credentials before writing out Wrangler config file")
.long("no-verify")
.takes_value(false),
)
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("subdomain")
.about(&*format!(
"{} Configure your workers.dev subdomain",
emoji::WORKER
))
.arg(
Arg::with_name("name")
.help("the subdomain on workers.dev you'd like to reserve")
.index(1),
)
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("whoami")
.about(&*format!(
"{} Retrieve your user info and test your auth config",
emoji::SLEUTH
))
.arg(silent_verbose_arg.clone()),
)
.subcommand(
SubCommand::with_name("tail")
.about(&*format!("{} Aggregate logs from production worker", emoji::TAIL))
.arg(
Arg::with_name("env")
.help("environment to tail logs from")
.short("e")
.long("env")
.takes_value(true)
)
.arg(
Arg::with_name("tunnel_port")
.help("port to accept tail log requests")
.short("p")
.long("port")
.takes_value(true)
)
.arg(
Arg::with_name("metrics_port")
.help("provides endpoint for cloudflared metrics. used to retrieve tunnel url")
.long("metrics")
.takes_value(true)
)
.arg(verbose_arg.clone())
)
.get_matches();
let config_path = Path::new("./wrangler.toml");
let not_recommended_msg = styles::warning("(Not Recommended)");
let recommended_cmd_msg = styles::highlight("`wrangler config --api-key`");
let api_token_url = styles::url("https://dash.cloudflare.com/profile/api-tokens");
let token_support_url = styles::url(
"https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys",
);
if let Some(matches) = matches.subcommand_matches("config") {
// If api-key flag isn't present, use the default auth option (API token)
let default = !matches.is_present("api-key");
let user: GlobalUser = if default {
// API Tokens are the default
message::billboard(&format!("To find your API Token, go to {}\nand create it using the \"Edit Cloudflare Workers\" template.\n\nIf you are trying to use your Global API Key instead of an API Token\n{}, run {}.", api_token_url, not_recommended_msg, recommended_cmd_msg));
let api_token: String = interactive::get_user_input("Enter API Token: ");
GlobalUser::TokenAuth { api_token }
} else {
message::billboard(&format!("We don't recommend using your Global API Key!\nPlease consider using an API Token instead.\n\n{}", token_support_url));
let email: String = interactive::get_user_input("Enter Email: ");
let api_key: String = interactive::get_user_input("Enter Global API Key: ");
GlobalUser::GlobalKeyAuth { email, api_key }
};
let verify = !matches.is_present("no-verify");
commands::global_config(&user, verify)?;
} else if let Some(matches) = matches.subcommand_matches("generate") {
let name = matches.value_of("name").unwrap_or("worker");
let site = matches.is_present("site");
let template = matches.value_of("template");
let mut target_type = None;
let template = if site {
if template.is_some() {
failure::bail!("You cannot pass a template and the --site flag to wrangler generate. If you'd like to use the default site boilerplate, run wrangler generate --site. If you'd like to use another site boilerplate, omit --site when running wrangler generate.")
}
"https://github.com/cloudflare/worker-sites-template"
} else {
if let Some(type_value) = matches.value_of("type") {
target_type = Some(TargetType::from_str(&type_value.to_lowercase())?);
}
let default_template = "https://github.com/cloudflare/worker-template";
template.unwrap_or(match target_type {
Some(ref pt) => match pt {
TargetType::Rust => "https://github.com/cloudflare/rustwasm-worker-template",
_ => default_template,
},
_ => default_template,
})
};
log::info!(
"Generate command called with template {}, and name {}",
template,
name
);
commands::generate(name, template, target_type, site)?;
} else if let Some(matches) = matches.subcommand_matches("init") {
let name = matches.value_of("name");
let site = matches.is_present("site");
let target_type = if site {
// Workers Sites projects are always webpack for now
Some(TargetType::Webpack)
} else {
match matches.value_of("type") {
Some(s) => Some(settings::toml::TargetType::from_str(&s.to_lowercase())?),
None => None,
}
};
commands::init(name, target_type, site)?;
} else if let Some(matches) = matches.subcommand_matches("build") {
commands::build(matches)?;
} else if let Some(matches) = matches.subcommand_matches("preview") {
log::info!("Getting project settings");
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let target = manifest.get_target(env)?;
// the preview command can be called with or without a Global User having been config'd
// so we convert this Result into an Option
let user = settings::global_user::GlobalUser::new().ok();
let method = matches.value_of("method").unwrap_or("get");
let url = matches.value_of("url").unwrap_or("https://example.com");
let body = match matches.value_of("body") {
Some(s) => Some(s.to_string()),
None => None,
};
let livereload = matches.is_present("watch");
let verbose = matches.is_present("verbose");
let headless = matches.is_present("headless");
let method = HttpMethod::from_str(method)?;
let url = Url::parse(url)?;
// Validate the URL scheme
failure::ensure!(
match url.scheme() {
"http" => true,
"https" => true,
_ => false,
},
"Invalid URL scheme (use either \"https\" or \"http\")"
);
let options = PreviewOpt {
method,
url,
body,
livereload,
headless,
};
commands::preview(target, user, options, verbose)?;
} else if let Some(matches) = matches.subcommand_matches("dev") {
log::info!("Starting dev server");
let port: Option<u16> = matches
.value_of("port")
.map(|p| p.parse().expect("--port expects a number"));
let host = matches.value_of("host");
let ip = matches.value_of("ip");
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let target = manifest.get_target(env)?;
let user = settings::global_user::GlobalUser::new().ok();
let verbose = matches.is_present("verbose");
commands::dev::dev(target, user, host, port, ip, verbose)?;
} else if matches.subcommand_matches("whoami").is_some() {
log::info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;
commands::whoami(&user)?;
} else if let Some(matches) = matches.subcommand_matches("publish") {
log::info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;
let release = matches.is_present("release");
if release {
let publish_release_msg = styles::highlight("`wrangler publish --release`");
let publish_msg = styles::highlight("`wrangler publish`");
let environments_url = styles::url("https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments");
message::warn(&format!(
"{} is deprecated and behaves exactly the same as {}.",
publish_release_msg, publish_msg
));
message::warn(&format!("See {} for more information.", environments_url));
}
log::info!("Getting project settings");
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let mut target = manifest.get_target(env)?;
let deploy_config = manifest.deploy_config(env)?;
let verbose = matches.is_present("verbose");
commands::publish(&user, &mut target, deploy_config, verbose)?;
} else if let Some(matches) = matches.subcommand_matches("subdomain") {
log::info!("Getting project settings");
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let target = manifest.get_target(env)?;
log::info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;
let name = matches.value_of("name");
if let Some(name) = name {
commands::subdomain::set_subdomain(&name, &user, &target)?;
} else {
commands::subdomain::get_subdomain(&user, &target)?;
}
} else if let Some(route_matches) = matches.subcommand_matches("route") {
let user = settings::global_user::GlobalUser::new()?;
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let env_zone_id = if let Some(environment) = manifest.get_environment(env)? {
environment.zone_id.as_ref()
} else {
None
};
let zone_id: Result<String, failure::Error> = if let Some(zone_id) = env_zone_id {
Ok(zone_id.to_string())
} else if let Some(zone_id) = manifest.zone_id {
Ok(zone_id)
} else {
failure::bail!(
"You must specify a zone_id in `wrangler.toml` to use `wrangler route` commands."
)
};
match route_matches.subcommand() {
("list", Some(_)) => {
commands::route::list(zone_id?, &user)?;
}
("delete", Some(delete_matches)) => {
let route_id = delete_matches.value_of("route_id").unwrap();
commands::route::delete(zone_id?, &user, route_id)?;
}
("", None) => message::warn("route expects a subcommand"),
_ => unreachable!(),
}
} else if let Some(secrets_matches) = matches.subcommand_matches("secret") {
log::info!("Getting project settings");
let manifest = settings::toml::Manifest::new(config_path)?;
log::info!("Getting User settings");
let user = settings::global_user::GlobalUser::new()?;
match secrets_matches.subcommand() {
("put", Some(create_matches)) => {
let name = create_matches.value_of("name");
let env = create_matches.value_of("env");
let target = manifest.get_target(env)?;
if let Some(name) = name {
commands::secret::create_secret(&name, &user, &target)?;
}
}
("delete", Some(delete_matches)) => {
let name = delete_matches.value_of("name");
let env = delete_matches.value_of("env");
let target = manifest.get_target(env)?;
if let Some(name) = name {
commands::secret::delete_secret(&name, &user, &target)?;
}
}
("list", Some(list_matches)) => {
let env = list_matches.value_of("env");
let target = manifest.get_target(env)?;
commands::secret::list_secrets(&user, &target)?;
}
("", None) => message::warn("secret expects a subcommand"),
_ => unreachable!(),
}
} else if let Some(kv_matches) = matches.subcommand_matches("kv:namespace") {
let manifest = settings::toml::Manifest::new(config_path)?;
let user = settings::global_user::GlobalUser::new()?;
match kv_matches.subcommand() {
("create", Some(create_matches)) => {
let env = create_matches.value_of("env");
let target = manifest.get_target(env)?;
let binding = create_matches.value_of("binding").unwrap();
commands::kv::namespace::create(&target, env, &user, binding)?;
}
("delete", Some(delete_matches)) => {
let env = delete_matches.value_of("env");
let target = manifest.get_target(env)?;
let namespace_id = match delete_matches.value_of("binding") {
Some(namespace_binding) => {
commands::kv::get_namespace_id(&target, namespace_binding)?
}
None => delete_matches
.value_of("namespace-id")
.unwrap() // clap configs ensure that if "binding" isn't present, "namespace-id" must be.
.to_string(),
};
commands::kv::namespace::delete(&target, &user, &namespace_id)?;
}
("list", Some(list_matches)) => {
let env = list_matches.value_of("env");
let target = manifest.get_target(env)?;
commands::kv::namespace::list(&target, &user)?;
}
("", None) => message::warn("kv:namespace expects a subcommand"),
_ => unreachable!(),
}
} else if let Some(kv_matches) = matches.subcommand_matches("kv:key") {
let manifest = settings::toml::Manifest::new(config_path)?;
let user = settings::global_user::GlobalUser::new()?;
// Get environment and bindings
let (subcommand, subcommand_matches) = kv_matches.subcommand();
let (target, namespace_id) = match subcommand_matches {
Some(subcommand_matches) => {
let env = subcommand_matches.value_of("env");
let target = manifest.get_target(env)?;
let namespace_id = match subcommand_matches.value_of("binding") {
Some(namespace_binding) => {
commands::kv::get_namespace_id(&target, namespace_binding)?
}
None => subcommand_matches
.value_of("namespace-id")
.unwrap() // clap configs ensure that if "binding" isn't present,"namespace-id" must be.
.to_string(),
};
(target, namespace_id)
}
None => unreachable!(), // this is unreachable because all kv:key commands have required arguments.
};
match (subcommand, subcommand_matches) {
("get", Some(get_key_matches)) => {
let key = get_key_matches.value_of("key").unwrap();
commands::kv::key::get(&target, &user, &namespace_id, key)?
}
("put", Some(put_key_matches)) => {
let key = put_key_matches.value_of("key").unwrap().to_string();
// If is_file is true, overwrite value to be the contents of the given
// filename in the 'value' arg.
let value = put_key_matches.value_of("value").unwrap().to_string();
let is_file = put_key_matches.is_present("path");
let expiration = put_key_matches
.value_of("expiration")
.map(|e| e.to_string());
let expiration_ttl = put_key_matches
.value_of("expiration-ttl")
.map(|t| t.to_string());
let kv_metadata = KVMetaData {
namespace_id,
key,
value,
is_file,
expiration,
expiration_ttl,
};
commands::kv::key::put(&target, &user, kv_metadata)?
}
("delete", Some(delete_key_matches)) => {
let key = delete_key_matches.value_of("key").unwrap();
commands::kv::key::delete(&target, &user, &namespace_id, key)?
}
("list", Some(list_key_matches)) => {
let prefix = list_key_matches.value_of("prefix");
commands::kv::key::list(&target, &user, &namespace_id, prefix)?
}
("", None) => message::warn("kv:key expects a subcommand"),
_ => unreachable!(),
}
} else if let Some(kv_matches) = matches.subcommand_matches("kv:bulk") {
let manifest = settings::toml::Manifest::new(config_path)?;
let user = settings::global_user::GlobalUser::new()?;
// Get environment and bindings
let (subcommand, subcommand_matches) = kv_matches.subcommand();
let (target, namespace_id) = match subcommand_matches {
Some(subcommand_matches) => {
let env = subcommand_matches.value_of("env");
let target = manifest.get_target(env)?;
let namespace_id = match subcommand_matches.value_of("binding") {
Some(namespace_binding) => {
commands::kv::get_namespace_id(&target, namespace_binding)?
}
None => subcommand_matches
.value_of("namespace-id")
.unwrap() // clap configs ensure that if "binding" isn't present,"namespace-id" must be.
.to_string(),
};
(target, namespace_id)
}
None => unreachable!(), // this is unreachable because all kv:key commands have required arguments.
};
match (subcommand, subcommand_matches) {
("put", Some(put_bulk_matches)) => {
let path = put_bulk_matches.value_of("path").unwrap();
commands::kv::bulk::put(&target, &user, &namespace_id, Path::new(path))?
}
("delete", Some(delete_bulk_matches)) => {
let path = delete_bulk_matches.value_of("path").unwrap();
commands::kv::bulk::delete(&target, &user, &namespace_id, Path::new(path))?
}
("", None) => message::warn("kv:bulk expects a subcommand"),
_ => unreachable!(),
}
} else if let Some(matches) = matches.subcommand_matches("tail") {
let manifest = settings::toml::Manifest::new(config_path)?;
let env = matches.value_of("env");
let target = manifest.get_target(env)?;
let user = settings::global_user::GlobalUser::new()?;
let tunnel_port: Option<u16> = matches
.value_of("tunnel_port")
.map(|p| p.parse().expect("--port expects a number"));
let metrics_port: Option<u16> = matches
.value_of("metrics_port")
.map(|p| p.parse().expect("--metrics expects a number"));
let verbose = matches.is_present("verbose");
commands::tail::start(&target, &user, tunnel_port, metrics_port, verbose)?;
}
Ok(())
}