/
cmds.go
1420 lines (1302 loc) · 46.7 KB
/
cmds.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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmds
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
gosync "sync"
"syscall"
"golang.org/x/sync/errgroup"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/types"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/limit"
pfsclient "github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pkg/grpcutil"
"github.com/pachyderm/pachyderm/src/server/pfs/fuse"
"github.com/pachyderm/pachyderm/src/server/pfs/pretty"
"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
"github.com/pachyderm/pachyderm/src/server/pkg/sync"
"github.com/pachyderm/pachyderm/src/server/pkg/tabwriter"
txncmds "github.com/pachyderm/pachyderm/src/server/transaction/cmds"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
// DefaultParallelism is the default parallelism used by 'get file' and 'put file'.
DefaultParallelism = 10
)
// Cmds returns a slice containing pfs commands.
func Cmds(noMetrics *bool, noPortForwarding *bool) []*cobra.Command {
var commands []*cobra.Command
raw := false
rawFlags := pflag.NewFlagSet("", pflag.ContinueOnError)
rawFlags.BoolVar(&raw, "raw", false, "disable pretty printing, print raw json")
fullTimestamps := false
fullTimestampsFlags := pflag.NewFlagSet("", pflag.ContinueOnError)
fullTimestampsFlags.BoolVar(&fullTimestamps, "full-timestamps", false, "Return absolute timestamps (as opposed to the default, relative timestamps).")
marshaller := &jsonpb.Marshaler{Indent: " "}
repoDocs := &cobra.Command{
Short: "Docs for repos.",
Long: `Repos, short for repository, are the top level data objects in Pachyderm.
Repos contain version-controlled directories and files. Files can be of any size
or type (e.g. csv, binary, images, etc).`,
}
cmdutil.SetDocsUsage(repoDocs)
commands = append(commands, cmdutil.CreateAlias(repoDocs, "repo"))
var description string
createRepo := &cobra.Command{
Use: "{{alias}} <repo>",
Short: "Create a new repo.",
Long: "Create a new repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
err = txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
_, err = c.PfsAPIClient.CreateRepo(
c.Ctx(),
&pfsclient.CreateRepoRequest{
Repo: client.NewRepo(args[0]),
Description: description,
},
)
return err
})
return grpcutil.ScrubGRPC(err)
}),
}
createRepo.Flags().StringVarP(&description, "description", "d", "", "A description of the repo.")
commands = append(commands, cmdutil.CreateAlias(createRepo, "create repo"))
updateRepo := &cobra.Command{
Use: "{{alias}} <repo>",
Short: "Update a repo.",
Long: "Update a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
err = txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
_, err = c.PfsAPIClient.CreateRepo(
c.Ctx(),
&pfsclient.CreateRepoRequest{
Repo: client.NewRepo(args[0]),
Description: description,
Update: true,
},
)
return err
})
return grpcutil.ScrubGRPC(err)
}),
}
updateRepo.Flags().StringVarP(&description, "description", "d", "", "A description of the repo.")
commands = append(commands, cmdutil.CreateAlias(updateRepo, "update repo"))
inspectRepo := &cobra.Command{
Use: "{{alias}} <repo>",
Short: "Return info about a repo.",
Long: "Return info about a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
repoInfo, err := c.InspectRepo(args[0])
if err != nil {
return err
}
if repoInfo == nil {
return fmt.Errorf("repo %s not found", args[0])
}
if raw {
return marshaller.Marshal(os.Stdout, repoInfo)
}
ri := &pretty.PrintableRepoInfo{
RepoInfo: repoInfo,
FullTimestamps: fullTimestamps,
}
return pretty.PrintDetailedRepoInfo(ri)
}),
}
inspectRepo.Flags().AddFlagSet(rawFlags)
inspectRepo.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(inspectRepo, "inspect repo"))
listRepo := &cobra.Command{
Short: "Return all repos.",
Long: "Return all repos.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
repoInfos, err := c.ListRepo()
if err != nil {
return err
}
if raw {
for _, repoInfo := range repoInfos {
if err := marshaller.Marshal(os.Stdout, repoInfo); err != nil {
return err
}
}
return nil
}
header := pretty.RepoHeader
if (len(repoInfos) > 0) && (repoInfos[0].AuthInfo != nil) {
header = pretty.RepoAuthHeader
}
writer := tabwriter.NewWriter(os.Stdout, header)
for _, repoInfo := range repoInfos {
pretty.PrintRepoInfo(writer, repoInfo, fullTimestamps)
}
return writer.Flush()
}),
}
listRepo.Flags().AddFlagSet(rawFlags)
listRepo.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(listRepo, "list repo"))
var force bool
var all bool
deleteRepo := &cobra.Command{
Use: "{{alias}} <repo>",
Short: "Delete a repo.",
Long: "Delete a repo.",
Run: cmdutil.RunBoundedArgs(0, 1, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
request := &pfsclient.DeleteRepoRequest{
Force: force,
All: all,
}
if len(args) > 0 {
if all {
return fmt.Errorf("cannot use the --all flag with an argument")
}
request.Repo = client.NewRepo(args[0])
} else if !all {
return fmt.Errorf("either a repo name or the --all flag needs to be provided")
}
err = txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
_, err = c.PfsAPIClient.DeleteRepo(c.Ctx(), request)
return err
})
return grpcutil.ScrubGRPC(err)
}),
}
deleteRepo.Flags().BoolVarP(&force, "force", "f", false, "remove the repo regardless of errors; use with care")
deleteRepo.Flags().BoolVar(&all, "all", false, "remove all repos")
commands = append(commands, cmdutil.CreateAlias(deleteRepo, "delete repo"))
commitDocs := &cobra.Command{
Short: "Docs for commits.",
Long: `Commits are atomic transactions on the content of a repo.
Creating a commit is a multistep process:
- start a new commit with 'start commit'
- write files to the commit via 'put file'
- finish the new commit with 'finish commit'
Commits that have been started but not finished are NOT durable storage.
Commits become reliable (and immutable) when they are finished.
Commits can be created with another commit as a parent.`,
}
cmdutil.SetDocsUsage(commitDocs)
commands = append(commands, cmdutil.CreateAlias(commitDocs, "commit"))
var parent string
startCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Start a new commit.",
Long: "Start a new commit with parent-commit as the parent, or start a commit on the given branch; if the branch does not exist, it will be created.",
Example: `# Start a new commit in repo "test" that's not on any branch
$ {{alias}} test
# Start a commit in repo "test" on branch "master"
$ {{alias}} test@master
# Start a commit with "master" as the parent in repo "test", on a new branch "patch"; essentially a fork.
$ {{alias}} test@patch -p master
# Start a commit with XXX as the parent in repo "test", not on any branch
$ {{alias}} test -p XXX`,
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
branch, err := cmdutil.ParseBranch(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
var commit *pfsclient.Commit
err = txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
var err error
commit, err = c.PfsAPIClient.StartCommit(
c.Ctx(),
&pfsclient.StartCommitRequest{
Branch: branch.Name,
Parent: client.NewCommit(branch.Repo.Name, parent),
Description: description,
},
)
return err
})
if err == nil {
fmt.Println(commit.ID)
}
return grpcutil.ScrubGRPC(err)
}),
}
startCommit.Flags().StringVarP(&parent, "parent", "p", "", "The parent of the new commit, unneeded if branch is specified and you want to use the previous head of the branch as the parent.")
startCommit.MarkFlagCustom("parent", "__pachctl_get_commit $(__parse_repo ${nouns[0]})")
startCommit.Flags().StringVarP(&description, "message", "m", "", "A description of this commit's contents")
startCommit.Flags().StringVar(&description, "description", "", "A description of this commit's contents (synonym for --message)")
commands = append(commands, cmdutil.CreateAlias(startCommit, "start commit"))
finishCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Finish a started commit.",
Long: "Finish a started commit. Commit-id must be a writeable commit.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
commit, err := cmdutil.ParseCommit(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
err = txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
_, err = c.PfsAPIClient.FinishCommit(
c.Ctx(),
&pfsclient.FinishCommitRequest{
Commit: commit,
Description: description,
},
)
return err
})
return grpcutil.ScrubGRPC(err)
}),
}
finishCommit.Flags().StringVarP(&description, "message", "m", "", "A description of this commit's contents (overwrites any existing commit description)")
finishCommit.Flags().StringVar(&description, "description", "", "A description of this commit's contents (synonym for --message)")
commands = append(commands, cmdutil.CreateAlias(finishCommit, "finish commit"))
inspectCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Return info about a commit.",
Long: "Return info about a commit.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
commit, err := cmdutil.ParseCommit(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
commitInfo, err := c.InspectCommit(commit.Repo.Name, commit.ID)
if err != nil {
return err
}
if commitInfo == nil {
return fmt.Errorf("commit %s not found", commit.ID)
}
if raw {
return marshaller.Marshal(os.Stdout, commitInfo)
}
ci := &pretty.PrintableCommitInfo{
CommitInfo: commitInfo,
FullTimestamps: fullTimestamps,
}
return pretty.PrintDetailedCommitInfo(ci)
}),
}
inspectCommit.Flags().AddFlagSet(rawFlags)
inspectCommit.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(inspectCommit, "inspect commit"))
var from string
var number int
listCommit := &cobra.Command{
Use: "{{alias}} <repo>[@<branch>]",
Short: "Return all commits on a repo.",
Long: "Return all commits on a repo.",
Example: `
# return commits in repo "foo"
$ {{alias}} foo
# return commits in repo "foo" on branch "master"
$ {{alias}} foo@master
# return the last 20 commits in repo "foo" on branch "master"
$ {{alias}} foo@master -n 20
# return commits in repo "foo" since commit XXX
$ {{alias}} foo@master --from XXX`,
Run: cmdutil.RunFixedArgs(1, func(args []string) (retErr error) {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
branch, err := cmdutil.ParseBranch(args[0])
if err != nil {
return err
}
if raw {
return c.ListCommitF(branch.Repo.Name, branch.Name, from, uint64(number), func(ci *pfsclient.CommitInfo) error {
return marshaller.Marshal(os.Stdout, ci)
})
}
writer := tabwriter.NewWriter(os.Stdout, pretty.CommitHeader)
if err := c.ListCommitF(branch.Repo.Name, branch.Name, from, uint64(number), func(ci *pfsclient.CommitInfo) error {
pretty.PrintCommitInfo(writer, ci, fullTimestamps)
return nil
}); err != nil {
return err
}
return writer.Flush()
}),
}
listCommit.Flags().StringVarP(&from, "from", "f", "", "list all commits since this commit")
listCommit.Flags().IntVarP(&number, "number", "n", 0, "list only this many commits; if set to zero, list all commits")
listCommit.MarkFlagCustom("from", "__pachctl_get_commit $(__parse_repo ${nouns[0]})")
listCommit.Flags().AddFlagSet(rawFlags)
listCommit.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(listCommit, "list commit"))
printCommitIter := func(commitIter client.CommitInfoIterator) error {
if raw {
for {
commitInfo, err := commitIter.Next()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if err := marshaller.Marshal(os.Stdout, commitInfo); err != nil {
return err
}
}
}
writer := tabwriter.NewWriter(os.Stdout, pretty.CommitHeader)
for {
commitInfo, err := commitIter.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
pretty.PrintCommitInfo(writer, commitInfo, fullTimestamps)
}
return writer.Flush()
}
var repos cmdutil.RepeatedStringArg
flushCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit> ...",
Short: "Wait for all commits caused by the specified commits to finish and return them.",
Long: "Wait for all commits caused by the specified commits to finish and return them.",
Example: `
# return commits caused by foo@XXX and bar@YYY
$ {{alias}} foo@XXX bar@YYY
# return commits caused by foo@XXX leading to repos bar and baz
$ {{alias}} foo@XXX -r bar -r baz`,
Run: cmdutil.Run(func(args []string) error {
commits, err := cmdutil.ParseCommits(args)
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
var toRepos []*pfsclient.Repo
for _, repoName := range repos {
toRepos = append(toRepos, client.NewRepo(repoName))
}
commitIter, err := c.FlushCommit(commits, toRepos)
if err != nil {
return err
}
return printCommitIter(commitIter)
}),
}
flushCommit.Flags().VarP(&repos, "repos", "r", "Wait only for commits leading to a specific set of repos")
flushCommit.MarkFlagCustom("repos", "__pachctl_get_repo")
flushCommit.Flags().AddFlagSet(rawFlags)
flushCommit.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(flushCommit, "flush commit"))
var newCommits bool
subscribeCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch>",
Short: "Print commits as they are created (finished).",
Long: "Print commits as they are created in the specified repo and branch. By default, all existing commits on the specified branch are returned first. A commit is only considered 'created' when it's been finished.",
Example: `
# subscribe to commits in repo "test" on branch "master"
$ {{alias}} test@master
# subscribe to commits in repo "test" on branch "master", but only since commit XXX.
$ {{alias}} test@master --from XXX
# subscribe to commits in repo "test" on branch "master", but only for new commits created from now on.
$ {{alias}} test@master --new`,
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
branch, err := cmdutil.ParseBranch(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
if newCommits && from != "" {
return fmt.Errorf("--new and --from cannot be used together")
}
if newCommits {
from = branch.Name
}
commitIter, err := c.SubscribeCommit(branch.Repo.Name, branch.Name, from, pfsclient.CommitState_STARTED)
if err != nil {
return err
}
return printCommitIter(commitIter)
}),
}
subscribeCommit.Flags().StringVar(&from, "from", "", "subscribe to all commits since this commit")
subscribeCommit.MarkFlagCustom("from", "__pachctl_get_commit $(__parse_repo ${nouns[0]})")
subscribeCommit.Flags().BoolVar(&newCommits, "new", false, "subscribe to only new commits created from now on")
subscribeCommit.Flags().AddFlagSet(rawFlags)
subscribeCommit.Flags().AddFlagSet(fullTimestampsFlags)
commands = append(commands, cmdutil.CreateAlias(subscribeCommit, "subscribe commit"))
deleteCommit := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Delete an input commit.",
Long: "Delete an input commit. An input is a commit which is not the output of a pipeline.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
commit, err := cmdutil.ParseCommit(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
return txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
return c.DeleteCommit(commit.Repo.Name, commit.ID)
})
}),
}
commands = append(commands, cmdutil.CreateAlias(deleteCommit, "delete commit"))
branchDocs := &cobra.Command{
Short: "Docs for branches.",
Long: `A branch in Pachyderm is an alias for a Commit ID.
The branch reference will "float" to always refer to the latest commit on the
branch, known as the HEAD commit. Not all commits must be on a branch and
multiple branches can refer to the same commit.
Any pachctl command that can take a Commit ID, can take a branch name instead.`,
}
cmdutil.SetDocsUsage(branchDocs)
commands = append(commands, cmdutil.CreateAlias(branchDocs, "branch"))
var branchProvenance cmdutil.RepeatedStringArg
var head string
createBranch := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Create a new branch, or update an existing branch, on a repo.",
Long: "Create a new branch, or update an existing branch, on a repo, starting a commit on the branch will also create it, so there's often no need to call this.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
branch, err := cmdutil.ParseBranch(args[0])
if err != nil {
return err
}
provenance, err := cmdutil.ParseBranches(branchProvenance)
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
return txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
return c.CreateBranch(branch.Repo.Name, branch.Name, head, provenance)
})
}),
}
createBranch.Flags().VarP(&branchProvenance, "provenance", "p", "The provenance for the branch. format: <repo>@<branch-or-commit>")
createBranch.MarkFlagCustom("provenance", "__pachctl_get_repo_commit")
createBranch.Flags().StringVarP(&head, "head", "", "", "The head of the newly created branch.")
createBranch.MarkFlagCustom("head", "__pachctl_get_commit $(__parse_repo ${nouns[0]})")
commands = append(commands, cmdutil.CreateAlias(createBranch, "create branch"))
listBranch := &cobra.Command{
Use: "{{alias}} <repo>",
Short: "Return all branches on a repo.",
Long: "Return all branches on a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
branches, err := c.ListBranch(args[0])
if err != nil {
return err
}
if raw {
for _, branch := range branches {
if err := marshaller.Marshal(os.Stdout, branch); err != nil {
return err
}
}
return nil
}
writer := tabwriter.NewWriter(os.Stdout, pretty.BranchHeader)
for _, branch := range branches {
pretty.PrintBranch(writer, branch)
}
return writer.Flush()
}),
}
listBranch.Flags().AddFlagSet(rawFlags)
commands = append(commands, cmdutil.CreateAlias(listBranch, "list branch"))
deleteBranch := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>",
Short: "Delete a branch",
Long: "Delete a branch, while leaving the commits intact",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
branch, err := cmdutil.ParseBranch(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
return txncmds.WithActiveTransaction(c, func(c *client.APIClient) error {
return c.DeleteBranch(branch.Repo.Name, branch.Name, force)
})
}),
}
deleteBranch.Flags().BoolVarP(&force, "force", "f", false, "remove the branch regardless of errors; use with care")
commands = append(commands, cmdutil.CreateAlias(deleteBranch, "delete branch"))
fileDocs := &cobra.Command{
Short: "Docs for files.",
Long: `Files are the lowest level data objects in Pachyderm.
Files can be of any type (e.g. csv, binary, images, etc) or size and can be
written to started (but not finished) commits with 'put file'. Files can be read
from commits with 'get file'.`,
}
cmdutil.SetDocsUsage(fileDocs)
commands = append(commands, cmdutil.CreateAlias(fileDocs, "file"))
var filePaths []string
var recursive bool
var inputFile string
var parallelism int
var split string
var targetFileDatums uint
var targetFileBytes uint
var headerRecords uint
var putFileCommit bool
var overwrite bool
putFile := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>[:<path/in/pfs>]",
Short: "Put a file into the filesystem.",
Long: "Put a file into the filesystem. This supports a number of ways to insert data into pfs.",
Example: `
# Put data from stdin as repo/branch/path:
$ echo "data" | {{alias}} repo@branch:/path
# Put data from stdin as repo/branch/path and start / finish a new commit on the branch.
$ echo "data" | {{alias}} -c repo@branch:/path
# Put a file from the local filesystem as repo/branch/path:
$ {{alias}} repo@branch:/path -f file
# Put a file from the local filesystem as repo/branch/file:
$ {{alias}} repo@branch -f file
# Put the contents of a directory as repo/branch/path/dir/file:
$ {{alias}} -r repo@branch:/path -f dir
# Put the contents of a directory as repo/branch/dir/file:
$ {{alias}} -r repo@branch -f dir
# Put the contents of a directory as repo/branch/file, i.e. put files at the top level:
$ {{alias}} -r repo@branch:/ -f dir
# Put the data from a URL as repo/branch/path:
$ {{alias}} repo@branch:/path -f http://host/path
# Put the data from a URL as repo/branch/path:
$ {{alias}} repo@branch -f http://host/path
# Put the data from an S3 bucket as repo/branch/s3_object:
$ {{alias}} repo@branch -r -f s3://my_bucket
# Put several files or URLs that are listed in file.
# Files and URLs should be newline delimited.
$ {{alias}} repo@branch -i file
# Put several files or URLs that are listed at URL.
# NOTE this URL can reference local files, so it could cause you to put sensitive
# files into your Pachyderm cluster.
$ {{alias}} repo@branch -i http://host/path`,
Run: cmdutil.RunFixedArgs(1, func(args []string) (retErr error) {
file, err := cmdutil.ParseFile(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user", client.WithMaxConcurrentStreams(parallelism))
if err != nil {
return err
}
defer c.Close()
// load data into pachyderm
pfc, err := c.NewPutFileClient()
if err != nil {
return err
}
defer func() {
if err := pfc.Close(); err != nil && retErr == nil {
retErr = err
}
}()
if putFileCommit {
fmt.Fprintf(os.Stderr, "flag --commit / -c is deprecated; as of 1.7.2, you will get the same behavior without it\n")
}
limiter := limit.New(int(parallelism))
var sources []string
if inputFile != "" {
// User has provided a file listing sources, one per line. Read sources
var r io.Reader
if inputFile == "-" {
r = os.Stdin
} else if url, err := url.Parse(inputFile); err == nil && url.Scheme != "" {
resp, err := http.Get(url.String())
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil && retErr == nil {
retErr = err
}
}()
r = resp.Body
} else {
inputFile, err := os.Open(inputFile)
if err != nil {
return err
}
defer func() {
if err := inputFile.Close(); err != nil && retErr == nil {
retErr = err
}
}()
r = inputFile
}
// scan line by line
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if filePath := scanner.Text(); filePath != "" {
sources = append(sources, filePath)
}
}
} else {
// User has provided a single source
sources = filePaths
}
// Arguments parsed; create putFileHelper and begin copying data
var eg errgroup.Group
filesPut := &gosync.Map{}
for _, source := range sources {
source := source
if file.Path == "" {
// The user has not specified a path so we use source as path.
if source == "-" {
return fmt.Errorf("must specify filename when reading data from stdin")
}
eg.Go(func() error {
return putFileHelper(c, pfc, file.Commit.Repo.Name, file.Commit.ID, joinPaths("", source), source, recursive, overwrite, limiter, split, targetFileDatums, targetFileBytes, headerRecords, filesPut)
})
} else if len(sources) == 1 {
// We have a single source and the user has specified a path,
// we use the path and ignore source (in terms of naming the file).
eg.Go(func() error {
return putFileHelper(c, pfc, file.Commit.Repo.Name, file.Commit.ID, file.Path, source, recursive, overwrite, limiter, split, targetFileDatums, targetFileBytes, headerRecords, filesPut)
})
} else {
// We have multiple sources and the user has specified a path,
// we use that path as a prefix for the filepaths.
eg.Go(func() error {
return putFileHelper(c, pfc, file.Commit.Repo.Name, file.Commit.ID, joinPaths(file.Path, source), source, recursive, overwrite, limiter, split, targetFileDatums, targetFileBytes, headerRecords, filesPut)
})
}
}
return eg.Wait()
}),
}
putFile.Flags().StringSliceVarP(&filePaths, "file", "f", []string{"-"}, "The file to be put, it can be a local file or a URL.")
putFile.Flags().StringVarP(&inputFile, "input-file", "i", "", "Read filepaths or URLs from a file. If - is used, paths are read from the standard input.")
putFile.Flags().BoolVarP(&recursive, "recursive", "r", false, "Recursively put the files in a directory.")
putFile.Flags().IntVarP(¶llelism, "parallelism", "p", DefaultParallelism, "The maximum number of files that can be uploaded in parallel.")
putFile.Flags().StringVar(&split, "split", "", "Split the input file into smaller files, subject to the constraints of --target-file-datums and --target-file-bytes. Permissible values are `line`, `json`, `sql` and `csv`.")
putFile.Flags().UintVar(&targetFileDatums, "target-file-datums", 0, "The upper bound of the number of datums that each file contains, the last file will contain fewer if the datums don't divide evenly; needs to be used with --split.")
putFile.Flags().UintVar(&targetFileBytes, "target-file-bytes", 0, "The target upper bound of the number of bytes that each file contains; needs to be used with --split.")
putFile.Flags().UintVar(&headerRecords, "header-records", 0, "the number of records that will be converted to a PFS 'header', and prepended to future retrievals of any subset of data from PFS; needs to be used with --split=(json|line|csv)")
putFile.Flags().BoolVarP(&putFileCommit, "commit", "c", false, "DEPRECATED: Put file(s) in a new commit.")
putFile.Flags().BoolVarP(&overwrite, "overwrite", "o", false, "Overwrite the existing content of the file, either from previous commits or previous calls to 'put file' within this commit.")
commands = append(commands, cmdutil.CreateAlias(putFile, "put file"))
copyFile := &cobra.Command{
Use: "{{alias}} <src-repo>@<src-branch-or-commit>:<src-path> <dst-repo>@<dst-branch-or-commit>:<dst-path>",
Short: "Copy files between pfs paths.",
Long: "Copy files between pfs paths.",
Run: cmdutil.RunFixedArgs(2, func(args []string) (retErr error) {
srcFile, err := cmdutil.ParseFile(args[0])
if err != nil {
return err
}
destFile, err := cmdutil.ParseFile(args[1])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user", client.WithMaxConcurrentStreams(parallelism))
if err != nil {
return err
}
defer c.Close()
return c.CopyFile(
srcFile.Commit.Repo.Name, srcFile.Commit.ID, srcFile.Path,
destFile.Commit.Repo.Name, destFile.Commit.ID, destFile.Path,
overwrite,
)
}),
}
copyFile.Flags().BoolVarP(&overwrite, "overwrite", "o", false, "Overwrite the existing content of the file, either from previous commits or previous calls to 'put file' within this commit.")
commands = append(commands, cmdutil.CreateAlias(copyFile, "copy file"))
var outputPath string
getFile := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>:<path/in/pfs>",
Short: "Return the contents of a file.",
Long: "Return the contents of a file.",
Example: `
# get file "XXX" on branch "master" in repo "foo"
$ {{alias}} foo@master:XXX
# get file "XXX" in the parent of the current head of branch "master"
# in repo "foo"
$ {{alias}} foo@master^:XXX
# get file "XXX" in the grandparent of the current head of branch "master"
# in repo "foo"
$ {{alias}} foo@master^2:XXX`,
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
file, err := cmdutil.ParseFile(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
if recursive {
if outputPath == "" {
return fmt.Errorf("an output path needs to be specified when using the --recursive flag")
}
puller := sync.NewPuller()
return puller.Pull(c, outputPath, file.Commit.Repo.Name, file.Commit.ID, file.Path, false, false, parallelism, nil, "")
}
var w io.Writer
// If an output path is given, print the output to stdout
if outputPath == "" {
w = os.Stdout
} else {
f, err := os.Create(outputPath)
if err != nil {
return err
}
defer f.Close()
w = f
}
return c.GetFile(file.Commit.Repo.Name, file.Commit.ID, file.Path, 0, 0, w)
}),
}
getFile.Flags().BoolVarP(&recursive, "recursive", "r", false, "Recursively download a directory.")
getFile.Flags().StringVarP(&outputPath, "output", "o", "", "The path where data will be downloaded.")
getFile.Flags().IntVarP(¶llelism, "parallelism", "p", DefaultParallelism, "The maximum number of files that can be downloaded in parallel")
commands = append(commands, cmdutil.CreateAlias(getFile, "get file"))
inspectFile := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>:<path/in/pfs>",
Short: "Return info about a file.",
Long: "Return info about a file.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
file, err := cmdutil.ParseFile(args[0])
if err != nil {
return err
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
fileInfo, err := c.InspectFile(file.Commit.Repo.Name, file.Commit.ID, file.Path)
if err != nil {
return err
}
if fileInfo == nil {
return fmt.Errorf("file %s not found", file.Path)
}
if raw {
return marshaller.Marshal(os.Stdout, fileInfo)
}
return pretty.PrintDetailedFileInfo(fileInfo)
}),
}
inspectFile.Flags().AddFlagSet(rawFlags)
commands = append(commands, cmdutil.CreateAlias(inspectFile, "inspect file"))
var history string
listFile := &cobra.Command{
Use: "{{alias}} <repo>@<branch-or-commit>[:<path/in/pfs>]",
Short: "Return the files in a directory.",
Long: "Return the files in a directory.",
Example: `
# list top-level files on branch "master" in repo "foo"
$ {{alias}} foo@master
# list files under directory "dir" on branch "master" in repo "foo"
$ {{alias}} foo@master:dir
# list top-level files in the parent commit of the current head of "master"
# in repo "foo"
$ {{alias}} foo@master^
# list top-level files in the grandparent of the current head of "master"
# in repo "foo"
$ {{alias}} foo@master^2
# list the last n versions of top-level files on branch "master" in repo "foo"
$ {{alias}} foo@master --history n
# list all versions of top-level files on branch "master" in repo "foo"
$ {{alias}} foo@master --history all`,
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
file, err := cmdutil.ParseFile(args[0])
if err != nil {
return err
}
history, err := cmdutil.ParseHistory(history)
if err != nil {
return fmt.Errorf("error parsing history flag: %v", err)
}
c, err := client.NewOnUserMachine(!*noMetrics, !*noPortForwarding, "user")
if err != nil {
return err
}
defer c.Close()
if raw {
return c.ListFileF(file.Commit.Repo.Name, file.Commit.ID, file.Path, history, func(fi *pfsclient.FileInfo) error {
return marshaller.Marshal(os.Stdout, fi)
})
}
header := pretty.FileHeader
if history != 0 {
header = pretty.FileHeaderWithCommit
}
writer := tabwriter.NewWriter(os.Stdout, header)
if err := c.ListFileF(file.Commit.Repo.Name, file.Commit.ID, file.Path, history, func(fi *pfsclient.FileInfo) error {
pretty.PrintFileInfo(writer, fi, fullTimestamps, history != 0)
return nil
}); err != nil {
return err
}
return writer.Flush()
}),
}
listFile.Flags().AddFlagSet(rawFlags)
listFile.Flags().AddFlagSet(fullTimestampsFlags)