func (c *PingCmd) Run(args ...string) (err error) { var mod *model.Model if mod, err = model.New(c.WD, false, c.ChunkSize, c.PoolSize); err != nil { return } trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) res, err := trans.ServerInfo() logx.Info(res) return }
func Test_Transport_ServerInfo(t *testing.T) { root := "testdata-Ping" srv, err := fixtures.NewFixtureServer(root) assert.NoError(t, err) defer srv.Stop() mod, err := model.New("", false, proto.CHUNK_SIZE, 16) assert.NoError(t, err) tr := transport.NewTransport(mod, srv.HTTPEndpoint, srv.RPCEndpoints[0], 16) defer tr.Close() res, err := tr.ServerInfo() assert.NoError(t, err) assert.Equal(t, int64(1024*1024*2), res.ChunkSize) }
func (c *UpCmd) Run(args ...string) (err error) { var mod *model.Model if mod, err = model.New(c.WD, c.UseGit, c.ChunkSize, c.PoolSize); err != nil { return } feed := lists.NewFileList(args...).ListDir(c.WD) isDirty, dirty, err := mod.Check(feed...) if err != nil { return } if isDirty { err = fmt.Errorf("dirty files in working tree %s", dirty) return } if c.UseGit { // filter by attrs feed, err = mod.Git.FilterByAttr("bar", feed...) } blobs, err := mod.FeedManifests(true, false, true, feed...) if err != nil { return } logx.Debugf("collected blobs %s", blobs.IDMap()) trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) err = trans.Upload(blobs) if err != nil { return } if c.Squash { if err = mod.SquashBlobs(blobs); err != nil { return } if c.UseGit { err = mod.Git.UpdateIndex(blobs.Names()...) } } return }
func (c *GitPreCommitCmd) Run(args ...string) (err error) { var filenames []string var mod *model.Model if mod, err = model.New(c.WD, true, c.ChunkSize, c.PoolSize); err != nil { return } // In divert we need restrict check by target filenames divert := git.NewDivert(mod.Git) isInDivert, err := divert.IsInProgress() if err != nil { return } if isInDivert { var spec git.DivertSpec if spec, err = divert.ReadSpec(); err != nil { return } filenames = spec.TargetFiles } isDirty, dirty, err := mod.Check(filenames...) if err != nil { return } if isDirty { err = fmt.Errorf("dirty files in working tree %s", dirty) return } feedR, err := mod.Git.Diff(filenames...) if err != nil { return } blobs, err := mod.Git.ManifestsFromDiff(feedR) if err != nil { return } trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) err = trans.Upload(blobs) return }
func seed(t *testing.T, root string) (halt func(), tree *fixtures.Tree, ml *model.Model, srv *fixtures.FixtureServer, trans *transport.Transport) { tree = fixtures.NewTree(root, "") assert.NoError(t, tree.Populate()) ml, err := model.New(tree.CWD, false, proto.CHUNK_SIZE, 32) assert.NoError(t, err) srv, err = fixtures.NewFixtureServer(root) assert.NoError(t, err) trans = transport.NewTransport(ml, srv.HTTPEndpoint, srv.RPCEndpoints[0], 16) halt = func() { trans.Close() srv.Stop() tree.Squash() } return }
func (c *GitInstallCmd) Run(args ...string) (err error) { var mod *model.Model if mod, err = model.New(c.WD, true, c.ChunkSize, c.PoolSize); err != nil { return } defer mod.Close() trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) defer trans.Close() info, err := trans.ServerInfo() if err != nil { return } config := git.NewConfig(info, mod.Git) err = config.Install(c.Log) return }
func (c *SpecExportCmd) Run(args ...string) (err error) { var mod *model.Model if mod, err = model.New(c.WD, c.UseGit, c.ChunkSize, c.PoolSize); err != nil { return } feed := lists.NewFileList(args...).ListDir(c.WD) isDirty, dirty, err := mod.Check(feed...) if err != nil { return } if isDirty { err = fmt.Errorf("dirty files in working tree %s", dirty) return } if c.UseGit { // filter by attrs feed, err = mod.Git.FilterByAttr("bar", feed...) } blobs, err := mod.FeedManifests(true, true, true, feed...) if err != nil { return } // make specmap nameMap := map[string]proto.ID{} for name, m := range blobs { nameMap[name] = m.ID } spec, err := proto.NewSpec(time.Now().UnixNano(), nameMap, []string{}) if err != nil { return } if c.DoCC { ccName := fmt.Sprintf("bar-spec-%d-%s.json", time.Now().UnixNano(), spec.ID) logx.Infof("storing carbon copy to %s", ccName) ccf, err := os.Create(lists.OSFromSlash(lists.OSJoin(c.WD, ccName))) if err != nil { return err } defer ccf.Close() if err = json.NewEncoder(ccf).Encode(&spec); err != nil { return err } } if !c.Upload { err = json.NewEncoder(c.Stdout).Encode(&spec) return } trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) if err = trans.UploadSpec(spec); err != nil { return } fmt.Fprint(c.Stdout, spec.ID) return }
func (c *LsCmd) Run(args ...string) (err error) { if c.NoBlobs && c.NoManifests { err = fmt.Errorf("both -no-blobs and -no-manifests are on") return } mod, err := model.New(c.WD, c.UseGit, proto.CHUNK_SIZE, c.PoolSize) if err != nil { return } feed := lists.NewFileList(args...).ListDir(c.WD) var dirty map[string]struct{} if c.UseGit { if feed, err = mod.Git.FilterByAttr("bar", feed...); err != nil { return } var dirtyst []string if dirtyst, err = mod.Git.DiffFiles(feed...); err != nil { return } dirty = map[string]struct{}{} for _, n := range dirtyst { dirty[n] = struct{}{} } } blobs, err := mod.FeedManifests(!c.NoBlobs, !c.NoManifests, true, feed...) if err != nil { return } missingOnRemote := map[proto.ID]struct{}{} if !c.NoRemote { var exists []proto.ID trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) if exists, err = trans.Check(blobs.IDMap().IDs()); err != nil { return } for _, id := range exists { missingOnRemote[id] = struct{}{} } } // print this stuff w := new(tabwriter.Writer) w.Init(c.Stdout, 0, 8, 2, '\t', 0) var line []string toLine := func(term string) { line = append(line, term) } flushLine := func() { fmt.Fprintln(w, strings.Join(line, "\t")) line = []string{} } if !c.NoHeader { if !c.NoName { toLine("NAME") } if !c.NoBlobs && !c.NoManifests { toLine("BLOB") } if !c.NoRemote { toLine("SYNC") } if c.UseGit { toLine("GIT") } if !c.NoID { toLine("ID") } if !c.NoSize { toLine("SIZE") } flushLine() } var names sort.StringSlice for n, _ := range blobs { names = append(names, n) } names.Sort() var blobMap map[string]bool if !c.NoBlobs && !c.NoManifests { if blobMap, err = mod.IsBlobs(names...); err != nil { return } } for _, name := range names { if !c.NoName { toLine(name) } if !c.NoBlobs && !c.NoManifests { if blobMap[name] { toLine("yes") } else { toLine("no") } } if !c.NoRemote { if _, missing := missingOnRemote[blobs[name].ID]; missing { toLine("no") } else { toLine("yes") } } if c.UseGit { if _, bad := dirty[name]; !bad { toLine("ok") } else { toLine("dirty") } } if !c.NoID { if !c.FullID { toLine(blobs[name].ID.String()[:12]) } else { toLine(blobs[name].ID.String()) } } if !c.NoSize { toLine(fmt.Sprintf("%d", blobs[name].Size)) } flushLine() } w.Flush() return }
func (c *SpecImportCmd) Run(args ...string) (err error) { var spec proto.Spec mod, err := model.New(c.WD, c.UseGit, c.ChunkSize, c.PoolSize) if err != nil { return } trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize) if c.Raw { if err = json.NewDecoder(c.Stdin).Decode(&spec); err != nil { return } } else { // tree spec types id := proto.ID(args[0]) if spec, err = trans.GetSpec(id); err != nil { logx.Debug(spec, err) return } } idm := lists.IDMap{} for n, id := range spec.BLOBs { idm[id] = append(idm[id], n) } // request manifests and mans, err := trans.GetManifests(idm.IDs()) if err != nil { return } feed := idm.ToBlobMap(mans) names := feed.Names() if len(names) == 0 { logx.Fatalf("no manifests on server %s", names) } logx.Debugf("importing %s", names) if c.UseGit { // If git is used - check names for attrs byAttr, err := mod.Git.FilterByAttr("bar", names...) if err != nil { return err } diff := []string{} attrs := map[string]struct{}{} for _, x := range byAttr { attrs[x] = struct{}{} } for _, x := range names { if _, ok := attrs[x]; !ok { diff = append(diff, x) } } if len(diff) > 0 { return fmt.Errorf("some spec blobs is not under bar control %s", diff) } } // get stored links, ignore errors stored, _ := mod.FeedManifests(true, true, false, names...) logx.Debugf("already stored %s", stored.Names()) // squash present toSquash := lists.BlobMap{} for n, m := range feed { m1, ok := stored[filepath.FromSlash(n)] if !ok || m.ID != m1.ID { toSquash[n] = feed[n] } } if c.Squash { if err = mod.SquashBlobs(toSquash); err != nil { return } } for k, _ := range feed { fmt.Fprintf(c.Stdout, "%s ", filepath.FromSlash(k)) } return }