github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/commands/repo.go (about) 1 package commands 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 8 cmds "github.com/ipfs/go-ipfs/commands" 9 corerepo "github.com/ipfs/go-ipfs/core/corerepo" 10 u "github.com/ipfs/go-ipfs/util" 11 ) 12 13 var RepoCmd = &cmds.Command{ 14 Helptext: cmds.HelpText{ 15 Tagline: "Manipulate the IPFS repo", 16 ShortDescription: ` 17 'ipfs repo' is a plumbing command used to manipulate the repo. 18 `, 19 }, 20 21 Subcommands: map[string]*cmds.Command{ 22 "gc": repoGcCmd, 23 }, 24 } 25 26 var repoGcCmd = &cmds.Command{ 27 Helptext: cmds.HelpText{ 28 Tagline: "Perform a garbage collection sweep on the repo", 29 ShortDescription: ` 30 'ipfs repo gc' is a plumbing command that will sweep the local 31 set of stored objects and remove ones that are not pinned in 32 order to reclaim hard disk space. 33 `, 34 }, 35 36 Options: []cmds.Option{ 37 cmds.BoolOption("quiet", "q", "Write minimal output"), 38 }, 39 Run: func(req cmds.Request, res cmds.Response) { 40 n, err := req.InvocContext().GetNode() 41 if err != nil { 42 res.SetError(err, cmds.ErrNormal) 43 return 44 } 45 46 gcOutChan, err := corerepo.GarbageCollectAsync(n, req.Context()) 47 if err != nil { 48 res.SetError(err, cmds.ErrNormal) 49 return 50 } 51 52 outChan := make(chan interface{}) 53 res.SetOutput((<-chan interface{})(outChan)) 54 55 go func() { 56 defer close(outChan) 57 for k := range gcOutChan { 58 outChan <- k 59 } 60 }() 61 }, 62 Type: corerepo.KeyRemoved{}, 63 Marshalers: cmds.MarshalerMap{ 64 cmds.Text: func(res cmds.Response) (io.Reader, error) { 65 outChan, ok := res.Output().(<-chan interface{}) 66 if !ok { 67 return nil, u.ErrCast() 68 } 69 70 quiet, _, err := res.Request().Option("quiet").Bool() 71 if err != nil { 72 return nil, err 73 } 74 75 marshal := func(v interface{}) (io.Reader, error) { 76 obj, ok := v.(*corerepo.KeyRemoved) 77 if !ok { 78 return nil, u.ErrCast() 79 } 80 81 buf := new(bytes.Buffer) 82 if quiet { 83 buf = bytes.NewBufferString(string(obj.Key) + "\n") 84 } else { 85 buf = bytes.NewBufferString(fmt.Sprintf("removed %s\n", obj.Key)) 86 } 87 return buf, nil 88 } 89 90 return &cmds.ChannelMarshaler{ 91 Channel: outChan, 92 Marshaler: marshal, 93 Res: res, 94 }, nil 95 }, 96 }, 97 }