Пример #1
0
func (c *attrCmd) RunCommand(up *Uploader, args []string) os.Error {
	if len(args) != 3 {
		return os.NewError("Attr takes 3 args: <permanode> <attr> <value>")
	}
	permanode, attr, value := args[0], args[1], args[2]

	var err os.Error

	pn := blobref.Parse(permanode)
	if pn == nil {
		return fmt.Errorf("Error parsing blobref %q", permanode)
	}
	m := schema.NewSetAttributeClaim(pn, attr, value)
	if c.add {
		if c.del {
			return os.NewError("Add and del options are exclusive")
		}
		m = schema.NewAddAttributeClaim(pn, attr, value)
	} else {
		// TODO: del, which can make <value> be optional
		if c.del {
			return os.NewError("del not yet implemented")
		}
	}
	put, err := up.UploadAndSignMap(m)
	handleResult(m["claimType"].(string), put, err)
	return nil
}
Пример #2
0
func (c *permanodeCmd) RunCommand(up *Uploader, args []string) os.Error {
	if len(args) > 0 {
		return os.NewError("Permanode command doesn't take any additional arguments")
	}

	var (
		permaNode *client.PutResult
		err       os.Error
	)
	permaNode, err = up.UploadNewPermanode()
	handleResult("permanode", permaNode, err)

	if c.name != "" {
		put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(permaNode.BlobRef, "title", c.name))
		handleResult("claim-permanode-title", put, err)
	}
	if c.tag != "" {
		tags := strings.Split(c.tag, ",")
		m := schema.NewSetAttributeClaim(permaNode.BlobRef, "tag", tags[0])
		for _, tag := range tags {
			m = schema.NewAddAttributeClaim(permaNode.BlobRef, "tag", tag)
			put, err := up.UploadAndSignMap(m)
			handleResult("claim-permanode-tag", put, err)
		}
	}
	return nil
}
Пример #3
0
func (c *fileCmd) RunCommand(up *Uploader, args []string) os.Error {
	if len(args) == 0 {
		return UsageError("No files or directories given.")
	}
	if c.name != "" && !c.makePermanode {
		return UsageError("Can't set name without using --permanode")
	}
	if c.tag != "" && !c.makePermanode {
		return UsageError("Can't set tag without using --permanode")
	}
	if c.memstats {
		sr := new(statsStatReceiver)
		up.altStatReceiver = sr
		AddSaveHook(func() { sr.DumpStats() })
	}
	if c.statcache {
		cache := NewFlatStatCache()
		AddSaveHook(func() { cache.Save() })
		up.statCache = cache
	}
	if c.havecache {
		cache := NewFlatHaveCache()
		AddSaveHook(func() { cache.Save() })
		up.haveCache = cache
	}

	var (
		permaNode *client.PutResult
		lastPut   *client.PutResult
		err       os.Error
	)
	if c.makePermanode {
		if len(args) != 1 {
			return fmt.Errorf("The --permanode flag can only be used with exactly one file or directory argument")
		}
		permaNode, err = up.UploadNewPermanode()
		if err != nil {
			return fmt.Errorf("Uploading permanode: %v", err)
		}
	}

	for _, filename := range args {
		lastPut, err = up.UploadFile(filename, c.rollSplits)
		handleResult("file", lastPut, err)

		if permaNode != nil {
			put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(permaNode.BlobRef, "camliContent", lastPut.BlobRef.String()))
			handleResult("claim-permanode-content", put, err)
			if c.name != "" {
				put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(permaNode.BlobRef, "name", c.name))
				handleResult("claim-permanode-name", put, err)
			}
			if c.tag != "" {
				tags := strings.Split(c.tag, ",")
				m := schema.NewSetAttributeClaim(permaNode.BlobRef, "tag", tags[0])
				for _, tag := range tags {
					m = schema.NewAddAttributeClaim(permaNode.BlobRef, "tag", tag)
					put, err := up.UploadAndSignMap(m)
					handleResult("claim-permanode-tag", put, err)
				}
			}
			handleResult("permanode", permaNode, nil)
		}
	}
	return nil
}
Пример #4
0
func main() {
	jsonsign.AddFlags()
	flag.Parse()

	if *flagSplits {
		showSplits()
		return
	}

	nOpts := sumSet(flagFile, flagBlob, flagPermanode, flagInit, flagShare, flagRemove,
		flagSetAttr, flagAddAttr)
	if !(nOpts == 1 ||
		(nOpts == 2 && *flagFile && *flagPermanode)) {
		usage("Conflicting mode options.")
	}

	cc := client.NewOrFail()
	if !*flagVerbose {
		cc.SetLogger(nil)
	}
	up := &Uploader{
		Client: cc,
		entityFetcher: &jsonsign.CachingEntityFetcher{
			Fetcher: &jsonsign.FileEntityFetcher{File: cc.SecretRingFile()},
		},
	}
	switch {
	case *flagInit:
		doInit()
		return
	case *flagFile || *flagBlob:
		var (
			permaNode *client.PutResult
			lastPut   *client.PutResult
			err       os.Error
		)
		if n := flag.NArg(); *flagPermanode {
			if n != 1 {
				log.Fatalf("Options --permanode and --file can only be used together when there's exactly one argument")
			}
			permaNode, err = up.UploadNewPermanode()
			if err != nil {
				log.Fatalf("Error uploading permanode: %v", err)
			}
		}
		for n := 0; n < flag.NArg(); n++ {
			if *flagBlob {
				lastPut, err = up.UploadFileBlob(flag.Arg(n))
				handleResult("blob", lastPut, err)
			} else {
				lastPut, err = up.UploadFile(flag.Arg(n))
				handleResult("file", lastPut, err)
			}
		}
		if permaNode != nil {
			put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(permaNode.BlobRef, "camliContent", lastPut.BlobRef.String()))
			handleResult("claim-permanode-content", put, err)
			if *flagName != "" {
				put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(permaNode.BlobRef, "name", *flagName))
				handleResult("claim-permanode-name", put, err)
			}
			handleResult("permanode", permaNode, nil)
		}
	case *flagPermanode:
		if flag.NArg() > 0 {
			log.Fatalf("--permanode doesn't take any additional arguments")
		}
		pr, err := up.UploadNewPermanode()
		handleResult("permanode", pr, err)
		if *flagName != "" {
			put, err := up.UploadAndSignMap(schema.NewSetAttributeClaim(pr.BlobRef, "name", *flagName))
			handleResult("permanode-name", put, err)
		}
	case *flagShare:
		if flag.NArg() != 1 {
			log.Fatalf("--share only supports one blobref")
		}
		br := blobref.Parse(flag.Arg(0))
		if br == nil {
			log.Fatalf("BlobRef is invalid: %q", flag.Arg(0))
		}
		pr, err := up.UploadShare(br, *flagTransitive)
		handleResult("share", pr, err)
	case *flagRemove:
		if flag.NArg() == 0 {
			log.Fatalf("--remove takes one or more blobrefs")
		}
		err := up.RemoveBlobs(blobref.ParseMulti(flag.Args()))
		if err != nil {
			log.Printf("Error removing blobs %s: %s", strings.Join(flag.Args(), ","), err)
			wereErrors = true
		}
	case *flagAddAttr || *flagSetAttr:
		if flag.NArg() != 3 {
			log.Fatalf("--set-attr and --add-attr take 3 args: <permanode> <attr> <value>")
		}
		pn := blobref.Parse(flag.Arg(0))
		if pn == nil {
			log.Fatalf("Error parsing blobref %q", flag.Arg(0))
		}
		m := schema.NewSetAttributeClaim(pn, flag.Arg(1), flag.Arg(2))
		if *flagAddAttr {
			m = schema.NewAddAttributeClaim(pn, flag.Arg(1), flag.Arg(2))
		}
		put, err := up.UploadAndSignMap(m)
		handleResult(m["claimType"].(string), put, err)
	}

	if *flagVerbose {
		stats := up.Stats()
		log.Printf("Client stats: %s", stats.String())
	}
	if wereErrors {
		os.Exit(2)
	}
}