forked from pachyderm/pachyderm
/
cmds.go
862 lines (785 loc) · 26.1 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
package cmds
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"text/tabwriter"
"golang.org/x/sync/errgroup"
"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/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/spf13/cobra"
)
const (
codestart = "```sh"
codeend = "```"
// DefaultParallelism is the default parallelism used by get-file
// and put-file.
DefaultParallelism = 10
)
// Cmds returns a slice containing pfs commands.
func Cmds(address string, noMetrics *bool) []*cobra.Command {
metrics := !*noMetrics
repo := &cobra.Command{
Use: "repo",
Short: "Docs for repos.",
Long: `Repos, short for repository, are the top level data object in Pachyderm.
Repos are created with create-repo.`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
return nil
}),
}
createRepo := &cobra.Command{
Use: "create-repo repo-name",
Short: "Create a new repo.",
Long: "Create a new repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.CreateRepo(args[0])
}),
}
inspectRepo := &cobra.Command{
Use: "inspect-repo repo-name",
Short: "Return info about a repo.",
Long: "Return info about a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
repoInfo, err := client.InspectRepo(args[0])
if err != nil {
return err
}
if repoInfo == nil {
return fmt.Errorf("repo %s not found", args[0])
}
return pretty.PrintDetailedRepoInfo(repoInfo)
}),
}
var listRepoProvenance cmdutil.RepeatedStringArg
listRepo := &cobra.Command{
Use: "list-repo",
Short: "Return all repos.",
Long: "Reutrn all repos.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
repoInfos, err := c.ListRepo(listRepoProvenance)
if err != nil {
return err
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintRepoHeader(writer)
for _, repoInfo := range repoInfos {
pretty.PrintRepoInfo(writer, repoInfo)
}
return writer.Flush()
}),
}
listRepo.Flags().VarP(&listRepoProvenance, "provenance", "p", "list only repos with the specified repos provenance")
var force bool
deleteRepo := &cobra.Command{
Use: "delete-repo repo-name",
Short: "Delete a repo.",
Long: "Delete a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.DeleteRepo(args[0], force)
}),
}
deleteRepo.Flags().BoolVarP(&force, "force", "f", false, "remove the repo regardless of errors; use with care")
commit := &cobra.Command{
Use: "commit",
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 it through fuse or with 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.
This layers the data in the commit over the data in the parent.
`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
return nil
}),
}
var parent string
startCommit := &cobra.Command{
Use: "start-commit repo-name [branch]",
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.
Examples:
` + codestart + `# Start a new commit in repo "test" that's not on any branch
$ pachctl start-commit test
# Start a commit in repo "test" on branch "master"
$ pachctl start-commit test master
# Start a commit with "master" as the parent in repo "test", on a new branch "patch"; essentially a fork.
$ pachctl start-commit test patch -p master
# Start a commit with XXX as the parent in repo "test", not on any branch
$ pachctl start-commit test -p XXX
` + codeend,
Run: cmdutil.RunBoundedArgs(1, 2, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
var branch string
if len(args) == 2 {
branch = args[1]
}
commit, err := client.StartCommitParent(args[0], branch, parent)
if err != nil {
return err
}
fmt.Println(commit.ID)
return nil
}),
}
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.")
finishCommit := &cobra.Command{
Use: "finish-commit repo-name commit-id",
Short: "Finish a started commit.",
Long: "Finish a started commit. Commit-id must be a writeable commit.",
Run: cmdutil.RunFixedArgs(2, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.FinishCommit(args[0], args[1])
}),
}
inspectCommit := &cobra.Command{
Use: "inspect-commit repo-name commit-id",
Short: "Return info about a commit.",
Long: "Return info about a commit.",
Run: cmdutil.RunFixedArgs(2, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
commitInfo, err := client.InspectCommit(args[0], args[1])
if err != nil {
return err
}
if commitInfo == nil {
return fmt.Errorf("commit %s not found", args[1])
}
return pretty.PrintDetailedCommitInfo(commitInfo)
}),
}
var from string
var number int
listCommit := &cobra.Command{
Use: "list-commit repo-name",
Short: "Return all commits on a set of repos.",
Long: `Return all commits on a set of repos.
Examples:
` + codestart + `# return commits in repo "foo"
$ pachctl list-commit foo
# return commits in repo "foo" on branch "master"
$ pachctl list-commit foo master
# return the last 20 commits in repo "foo" on branch "master"
$ pachctl list-commit foo master -n 20
# return commits that are the ancestors of XXX
$ pachctl list-commit foo XXX
# return commits in repo "foo" since commit XXX
$ pachctl list-commit foo master --from XXX
` + codeend,
Run: cmdutil.RunBoundedArgs(1, 2, func(args []string) (retErr error) {
c, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
var to string
if len(args) == 2 {
to = args[1]
}
commitInfos, err := c.ListCommit(args[0], to, from, uint64(number))
if err != nil {
return err
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintCommitInfoHeader(writer)
for _, commitInfo := range commitInfos {
pretty.PrintCommitInfo(writer, commitInfo)
}
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")
var repos cmdutil.RepeatedStringArg
flushCommit := &cobra.Command{
Use: "flush-commit commit [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.
Examples:
` + codestart + `# return commits caused by foo/XXX and bar/YYY
$ pachctl flush-commit foo/XXX bar/YYY
# return commits caused by foo/XXX leading to repos bar and baz
$ pachctl flush-commit foo/XXX -r bar -r baz
` + codeend,
Run: cmdutil.Run(func(args []string) error {
commits, err := cmdutil.ParseCommits(args)
if err != nil {
return err
}
c, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
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
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintCommitInfoHeader(writer)
for {
commitInfo, err := commitIter.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
pretty.PrintCommitInfo(writer, commitInfo)
}
return writer.Flush()
}),
}
flushCommit.Flags().VarP(&repos, "repos", "r", "Wait only for commits leading to a specific set of repos")
listBranch := &cobra.Command{
Use: "list-branch <repo-name>",
Short: "Return all branches on a repo.",
Long: "Return all branches on a repo.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
branches, err := client.ListBranch(args[0])
if err != nil {
return err
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintBranchHeader(writer)
for _, branch := range branches {
pretty.PrintBranch(writer, branch)
}
return writer.Flush()
}),
}
setBranch := &cobra.Command{
Use: "set-branch <repo-name> <commit-id/branch-name> <new-branch-name>",
Short: "Set a commit and its ancestors to a branch",
Long: `Set a commit and its ancestors to a branch.
Examples:
` + codestart + `# Set commit XXX and its ancestors as branch master in repo foo.
$ pachctl set-branch foo XXX master
# Set the head of branch test as branch master in repo foo.
# After running this command, "test" and "master" both point to the
# same commit.
$ pachctl set-branch foo test master` + codeend,
Run: cmdutil.RunFixedArgs(3, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.SetBranch(args[0], args[1], args[2])
}),
}
deleteBranch := &cobra.Command{
Use: "delete-branch <repo-name> <branch-name>",
Short: "Delete a branch",
Long: "Delete a branch, while leaving the commits intact",
Run: cmdutil.RunFixedArgs(2, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.DeleteBranch(args[0], args[1])
}),
}
file := &cobra.Command{
Use: "file",
Short: "Docs for files.",
Long: `Files are the lowest level data object in Pachyderm.
Files can be written to started (but not finished) commits with put-file.
Files can be read from finished commits with get-file.`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
return nil
}),
}
var filePaths []string
var recursive bool
var inputFile string
var parallelism uint
var split string
var targetFileDatums uint
var targetFileBytes uint
var putFileCommit bool
putFile := &cobra.Command{
Use: "put-file repo-name branch path/to/file/in/pfs",
Short: "Put a file into the filesystem.",
Long: `Put-file supports a number of ways to insert data into pfs:
` + codestart + `# Put data from stdin as repo/branch/path:
echo "data" | pachctl put-file repo branch path
# Put data from stding as repo/branch/path and start / finish a new commit on the branch.
echo "data" | pachctl put-file -c repo branch path
# Put a file from the local filesystem as repo/branch/path:
pachctl put-file repo branch path -f file
# Put a file from the local filesystem as repo/branch/file:
pachctl put-file repo branch -f file
# Put the contents of a directory as repo/branch/path/dir/file:
pachctl put-file -r repo branch path -f dir
# Put the contents of a directory as repo/branch/dir/file:
pachctl put-file -r repo branch -f dir
# Put the data from a URL as repo/branch/path:
pachctl put-file repo branch path -f http://host/path
# Put the data from a URL as repo/branch/path:
pachctl put-file repo branch -f http://host/path
# Put several files or URLs that are listed in file.
# Files and URLs should be newline delimited.
pachctl put-file 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.
pachctl put-file repo branch -i http://host/path
` + codeend,
Run: cmdutil.RunBoundedArgs(2, 3, func(args []string) (retErr error) {
client, err := client.NewMetricsClientFromAddressWithConcurrency(address, metrics, "user", parallelism)
if err != nil {
return err
}
repoName := args[0]
branch := args[1]
var path string
if len(args) == 3 {
path = args[2]
}
if putFileCommit {
if _, err := client.StartCommit(repoName, branch); err != nil {
return err
}
defer func() {
if err := client.FinishCommit(repoName, branch); err != nil && retErr == nil {
retErr = err
}
}()
}
limiter := limit.New(int(parallelism))
var sources []string
if inputFile != "" {
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 {
sources = filePaths
}
var eg errgroup.Group
for _, source := range sources {
source := source
if len(args) == 2 {
// The user has not specified a path so we use source as path.
if source == "-" {
return fmt.Errorf("no filename specified")
}
eg.Go(func() error {
return putFileHelper(client, repoName, branch, joinPaths("", source), source, recursive, limiter, split, targetFileDatums, targetFileBytes)
})
} else if len(sources) == 1 && len(args) == 3 {
// 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(client, repoName, branch, path, source, recursive, limiter, split, targetFileDatums, targetFileBytes)
})
} else if len(sources) > 1 && len(args) == 3 {
// 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(client, repoName, branch, joinPaths(path, source), source, recursive, limiter, split, targetFileDatums, targetFileBytes)
})
}
}
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().UintVarP(¶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")
putFile.Flags().UintVar(&targetFileDatums, "target-file-datums", 0, "the target upper bound of the number of datums that each file contains; 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().BoolVarP(&putFileCommit, "commit", "c", false, "Put file(s) in a new commit.")
var outputPath string
getFile := &cobra.Command{
Use: "get-file repo-name commit-id path/to/file",
Short: "Return the contents of a file.",
Long: "Return the contents of a file.",
Run: cmdutil.RunFixedArgs(3, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
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(client, outputPath, args[0], args[1], args[2], false, int(parallelism))
}
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 client.GetFile(args[0], args[1], args[2], 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().UintVarP(¶llelism, "parallelism", "p", DefaultParallelism, "The maximum number of files that can be downloaded in parallel")
inspectFile := &cobra.Command{
Use: "inspect-file repo-name commit-id path/to/file",
Short: "Return info about a file.",
Long: "Return info about a file.",
Run: cmdutil.RunFixedArgs(3, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
fileInfo, err := client.InspectFile(args[0], args[1], args[2])
if err != nil {
return err
}
if fileInfo == nil {
return fmt.Errorf("file %s not found", args[2])
}
return pretty.PrintDetailedFileInfo(fileInfo)
}),
}
listFile := &cobra.Command{
Use: "list-file repo-name commit-id path/to/dir",
Short: "Return the files in a directory.",
Long: "Return the files in a directory.",
Run: cmdutil.RunBoundedArgs(2, 3, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
var path string
if len(args) == 3 {
path = args[2]
}
fileInfos, err := client.ListFile(args[0], args[1], path)
if err != nil {
return err
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintFileInfoHeader(writer)
for _, fileInfo := range fileInfos {
pretty.PrintFileInfo(writer, fileInfo)
}
return writer.Flush()
}),
}
deleteFile := &cobra.Command{
Use: "delete-file repo-name commit-id path/to/file",
Short: "Delete a file.",
Long: "Delete a file.",
Run: cmdutil.RunFixedArgs(3, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.DeleteFile(args[0], args[1], args[2])
}),
}
getObject := &cobra.Command{
Use: "get-object hash",
Short: "Return the contents of an object",
Long: "Return the contents of an object",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.GetObject(args[0], os.Stdout)
}),
}
getTag := &cobra.Command{
Use: "get-tag tag",
Short: "Return the contents of a tag",
Long: "Return the contents of a tag",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
return client.GetTag(args[0], os.Stdout)
}),
}
var debug bool
var allCommits bool
mount := &cobra.Command{
Use: "mount path/to/mount/point",
Short: "Mount pfs locally. This command blocks.",
Long: "Mount pfs locally. This command blocks.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := client.NewMetricsClientFromAddress(address, metrics, "fuse")
if err != nil {
return err
}
go func() { client.KeepConnected(nil) }()
mounter := fuse.NewMounter(address, client)
mountPoint := args[0]
ready := make(chan bool)
go func() {
<-ready
fmt.Println("Filesystem mounted, CTRL-C to exit.")
}()
err = mounter.Mount(mountPoint, nil, ready, debug, false)
if err != nil {
return err
}
return nil
}),
}
mount.Flags().BoolVarP(&debug, "debug", "d", false, "Turn on debug messages.")
mount.Flags().BoolVarP(&allCommits, "all-commits", "a", false, "Show archived and cancelled commits.")
var all bool
unmount := &cobra.Command{
Use: "unmount path/to/mount/point",
Short: "Unmount pfs.",
Long: "Unmount pfs.",
Run: cmdutil.RunBoundedArgs(0, 1, func(args []string) error {
if len(args) == 1 {
return syscall.Unmount(args[0], 0)
}
if all {
stdin := strings.NewReader(`
mount | grep pfs:// | cut -f 3 -d " "
`)
var stdout bytes.Buffer
if err := cmdutil.RunIO(cmdutil.IO{
Stdin: stdin,
Stdout: &stdout,
Stderr: os.Stderr,
}, "sh"); err != nil {
return err
}
scanner := bufio.NewScanner(&stdout)
var mounts []string
for scanner.Scan() {
mounts = append(mounts, scanner.Text())
}
if len(mounts) == 0 {
fmt.Println("No mounts found.")
return nil
}
fmt.Printf("Unmount the following filesystems? yN\n")
for _, mount := range mounts {
fmt.Printf("%s\n", mount)
}
r := bufio.NewReader(os.Stdin)
bytes, err := r.ReadBytes('\n')
if err != nil {
return err
}
if bytes[0] == 'y' || bytes[0] == 'Y' {
for _, mount := range mounts {
if err := syscall.Unmount(mount, 0); err != nil {
return err
}
}
}
}
return nil
}),
}
unmount.Flags().BoolVarP(&all, "all", "a", false, "unmount all pfs mounts")
var result []*cobra.Command
result = append(result, repo)
result = append(result, createRepo)
result = append(result, inspectRepo)
result = append(result, listRepo)
result = append(result, deleteRepo)
result = append(result, commit)
result = append(result, startCommit)
result = append(result, finishCommit)
result = append(result, inspectCommit)
result = append(result, listCommit)
result = append(result, flushCommit)
result = append(result, listBranch)
result = append(result, setBranch)
result = append(result, deleteBranch)
result = append(result, file)
result = append(result, putFile)
result = append(result, getFile)
result = append(result, inspectFile)
result = append(result, listFile)
result = append(result, deleteFile)
result = append(result, getObject)
result = append(result, getTag)
result = append(result, mount)
result = append(result, unmount)
return result
}
func parseCommitMounts(args []string) []*fuse.CommitMount {
var result []*fuse.CommitMount
for _, arg := range args {
commitMount := &fuse.CommitMount{Commit: client.NewCommit("", "")}
repo, commitAlias := path.Split(arg)
commitMount.Commit.Repo.Name = path.Clean(repo)
split := strings.Split(commitAlias, ":")
if len(split) > 0 {
commitMount.Commit.ID = split[0]
}
if len(split) > 1 {
commitMount.Alias = split[1]
}
result = append(result, commitMount)
}
return result
}
func putFileHelper(client *client.APIClient, repo, commit, path, source string, recursive bool, limiter limit.ConcurrencyLimiter, split string, targetFileDatums uint, targetFileBytes uint) (retErr error) {
putFile := func(reader io.Reader) error {
if split == "" {
_, err := client.PutFile(repo, commit, path, reader)
return err
}
var delimiter pfsclient.Delimiter
switch split {
case "line":
delimiter = pfsclient.Delimiter_LINE
case "json":
delimiter = pfsclient.Delimiter_JSON
default:
return fmt.Errorf("unrecognized delimiter '%s'; only accepts 'json' or 'line'", split)
}
_, err := client.PutFileSplit(repo, commit, path, delimiter, int64(targetFileDatums), int64(targetFileBytes), reader)
return err
}
if source == "-" {
limiter.Acquire()
defer limiter.Release()
return putFile(os.Stdin)
}
// try parsing the filename as a url, if it is one do a PutFileURL
if url, err := url.Parse(source); err == nil && url.Scheme != "" {
limiter.Acquire()
defer limiter.Release()
return client.PutFileURL(repo, commit, path, url.String(), recursive)
}
if recursive {
var eg errgroup.Group
if err := filepath.Walk(source, func(filePath string, info os.FileInfo, err error) error {
// file doesn't exist
if info == nil {
return fmt.Errorf("%s doesn't exist", filePath)
}
if info.IsDir() {
return nil
}
eg.Go(func() error {
return putFileHelper(client, repo, commit, filepath.Join(path, strings.TrimPrefix(filePath, source)), filePath, false, limiter, split, targetFileDatums, targetFileBytes)
})
return nil
}); err != nil {
return err
}
return eg.Wait()
}
limiter.Acquire()
defer limiter.Release()
f, err := os.Open(source)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil && retErr == nil {
retErr = err
}
}()
return putFile(f)
}
func joinPaths(prefix, filePath string) string {
if url, err := url.Parse(filePath); err == nil && url.Scheme != "" {
if url.Scheme == "pfs" {
// pfs paths are of the form pfs://host/repo/branch/path we don't
// want to prefix every file with host/repo so we remove those
splitPath := strings.Split(strings.TrimPrefix(url.Path, "/"), "/")
if len(splitPath) < 3 {
return prefix
}
return filepath.Join(append([]string{prefix}, splitPath[2:]...)...)
}
return filepath.Join(prefix, strings.TrimPrefix(url.Path, "/"))
}
return filepath.Join(prefix, filePath)
}