示例#1
0
文件: main.go 项目: TheJumpCloud/rsc
func main() {
	log.SetFlags(0)

	if len(os.Args) == 2 && os.Args[1] == "MOUNTSLAVE" {
		mountslave()
		return
	}

	flag.Usage = usage
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		usage()
	}
	z.Mtpt = args[0]

	if cl.Password == "" {
		var err error
		cl.User, cl.Password, err = keychain.UserPasswd(cl.Host, "")
		if err != nil {
			fmt.Fprintf(os.Stderr, "unable to obtain user and password: %s\n", err)
			os.Exit(2)
		}
	}

	if _, err := cl.Stat("/"); err != nil {
		log.Fatal(err)
	}

	// Run in child so that we can exit once child is running.
	r, w, err := os.Pipe()
	if err != nil {
		log.Fatal(err)
	}

	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	enc.Encode(&z)

	cmd := exec.Command(os.Args[0], "MOUNTSLAVE")
	cmd.Stdin = &buf
	cmd.Stdout = w
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		log.Fatalf("mount process: %v", err)
	}
	w.Close()

	ok := make([]byte, 10)
	n, _ := r.Read(ok)
	if n != 2 || string(ok[0:2]) != "OK" {
		os.Exit(1)
	}

	fmt.Fprintf(os.Stderr, "mounted on %s\n", z.Mtpt)
}
示例#2
0
文件: main.go 项目: TheJumpCloud/rsc
func main() {
	flag.Usage = usage
	flag.Parse()
	var buck, obj string
	args := flag.Args()
	switch len(args) {
	case 1:
		buck = args[0]
		if !*list {
			fmt.Fprintf(os.Stderr, "must specify path when not using -l")
			os.Exit(2)
		}
	case 2:
		buck = args[0]
		obj = args[1]
	default:
		usage()
	}

	access, secret, err := keychain.UserPasswd("s3.amazonaws.com", "")
	if err != nil {
		log.Fatal(err)
	}

	auth := aws.Auth{AccessKey: access, SecretKey: secret}

	b := s3.New(auth, aws.USEast).Bucket(buck)
	if *list {
		objs, prefixes, err := b.List("", *delim, obj, 0)
		if err != nil {
			log.Fatal(err)
		}
		for _, p := range prefixes {
			fmt.Printf("%s\n", p)
		}
		for _, obj := range objs {
			fmt.Printf("%s\n", obj.Key)
		}
		return
	}

	data, err := b.Get(obj)
	if err != nil {
		log.Fatal(err)
	}
	os.Stdout.Write(data)
}
示例#3
0
文件: main.go 项目: TheJumpCloud/rsc
func main() {
	flag.Usage = usage
	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		usage()
	}

	if c.Password == "" {
		var err error
		c.User, c.Password, err = keychain.UserPasswd(c.Host, "")
		if err != nil {
			fmt.Fprintf(os.Stderr, "unable to obtain user and password: %s\n", err)
			os.Exit(2)
		}
	}

	name, args := args[0], args[1:]
	for _, c := range cmd {
		if name == c.name {
			switch c.arg {
			case 0, 1:
				if len(args) != c.arg {
					if c.arg == 0 {
						fmt.Fprintf(os.Stderr, "%s takes no arguments\n", name)
						os.Exit(2)
					}
					fmt.Fprintf(os.Stderr, "%s requires 1 argument\n", name)
					os.Exit(2)
				}
			case 2:
				if len(args) == 0 {
					fmt.Fprintf(os.Stderr, "%s requires at least 1 argument\n", name)
					os.Exit(2)
				}
			}
			c.fn(args)
			return
		}
	}
	fmt.Fprintf(os.Stderr, "unknown command %s\n", name)
	os.Exit(2)
}
示例#4
0
文件: main.go 项目: TheJumpCloud/rsc
func mountslave() {
	stdout, _ := syscall.Dup(1)
	syscall.Dup2(2, 1)

	access, secret, err := keychain.UserPasswd("s3.amazonaws.com", "")
	if err != nil {
		log.Fatal(err)
	}
	auth := aws.Auth{AccessKey: access, SecretKey: secret}

	conn, err := arq.Dial(auth)
	if err != nil {
		log.Fatal(err)
	}

	comps, err := conn.Computers()
	if err != nil {
		log.Fatal(err)
	}

	fs := &fuse.Tree{}
	for _, c := range comps {
		fmt.Fprintf(os.Stderr, "scanning %s...\n", c.Name)

		// TODO: Better password protocol.
		_, pw, err := keychain.UserPasswd("arq.swtch.com", c.UUID)
		if err != nil {
			log.Fatal(err)
		}
		c.Unlock(pw)

		folders, err := c.Folders()
		if err != nil {
			log.Fatal(err)
		}

		lastDate := ""
		n := 0
		for _, f := range folders {
			if err := f.Load(); err != nil {
				log.Fatal(err)
			}
			trees, err := f.Trees()
			if err != nil {
				log.Fatal(err)
			}
			for _, t := range trees {
				y, m, d := t.Time.Date()
				date := fmt.Sprintf("%04d/%02d%02d", y, m, d)
				suffix := ""
				if date == lastDate {
					n++
					suffix = fmt.Sprintf(".%d", n)
				} else {
					n = 0
				}
				lastDate = date
				f, err := t.Root()
				if err != nil {
					log.Print(err)
				}
				// TODO: Pass times to fs.Add.
				// fmt.Fprintf(os.Stderr, "%v %s %x\n", t.Time, c.Name+"/"+date+suffix+"/"+t.Path, t.Score)
				fs.Add(c.Name+"/"+date+suffix+"/"+t.Path, &fuseNode{f})
			}
		}
	}

	fmt.Fprintf(os.Stderr, "mounting...\n")

	c, err := fuse.Mount(*mtpt)
	if err != nil {
		log.Fatal(err)
	}
	defer exec.Command("umount", *mtpt).Run()

	syscall.Write(stdout, []byte("OK"))
	syscall.Close(stdout)
	c.Serve(fs)
}
示例#5
0
文件: main.go 项目: TheJumpCloud/rsc
func main() {
	flag.Usage = usage
	flag.Parse()
	log.SetFlags(0)

	args := flag.Args()
	if len(args) < 1 {
		usage()
	}
	title, files := args[0], args[1:]

	user, passwd, err := keychain.UserPasswd("smugmug.com", *smugUser)
	if err != nil {
		log.Fatal(err)
	}

	smug, err := smugmug.Login(user, passwd, apiKey)
	if err != nil {
		log.Fatal(err)
	}

	albums, err := smug.Albums(smug.NickName)
	if err != nil {
		log.Fatal(err)
	}

	var a *smugmug.Album
	for _, a = range albums {
		if a.Title == title {
			goto HaveAlbum
		}
	}

	a, err = smug.CreateAlbum(title)
	if err != nil {
		log.Fatal(err)
	}

HaveAlbum:
	imageFiles := map[string]*smugmug.ImageInfo{}
	if len(files) > 0 {
		images, err := smug.Images(a)
		if err != nil {
			log.Fatal(err)
		}

		n := 0
		c := make(chan *smugmug.ImageInfo)
		rate := make(chan bool, 4)
		for _, image := range images {
			go func(image *smugmug.Image) {
				rate <- true
				info, err := smug.ImageInfo(image)
				<-rate
				if err != nil {
					log.Print(err)
					c <- nil
					return
				}
				c <- info
			}(image)
			n++
		}

		for i := 0; i < n; i++ {
			info := <-c
			if info == nil {
				continue
			}
			imageFiles[info.FileName] = info
		}
	}

	for _, file := range files {
		data, err := ioutil.ReadFile(file)
		if err != nil {
			log.Print(err)
			continue
		}
		_, elem := filepath.Split(file)
		info := imageFiles[elem]
		if info != nil {
			h := md5.New()
			h.Write(data)
			digest := fmt.Sprintf("%x", h.Sum(nil))
			if digest == info.MD5Sum {
				// Already have that image.
				continue
			}
			_, err = smug.ReplaceImage(file, data, &smugmug.Image{ID: info.ID})
		} else {
			_, err = smug.AddImage(file, data, a)
		}
		if err != nil {
			log.Print(err)
		}
	}

	info, err := smug.AlbumInfo(a)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", info.URL)
}