Beispiel #1
0
// GetContainerURL gets URL for the container by name. If no container is assigned
// it uses the first container in the list
func GetContainerURL(name string) {
	// get .cloudcore
	var core storage.Core
	var err error
	if core, err = GetCore(); err != nil {
		log.Fatal(err)
	}
	// get .cloud
	var cloud storage.Cloud
	if cloud, err = GetCloud(); err != nil {
		log.Fatal(err)
	}
	// get container from .cloud
	var container storage.Container
	if container, err = GetContainer(name, &cloud); err != nil {
		log.Fatal(err)
	}
	var s storage.Storage
	var provider storage.Provider
	// define storage backend
	switch {
	case container.Provider == storage.CloudFiles:
		if provider, err = GetProvider(&container, &core); err != nil {
			log.Fatal(err)
		}
		fmt.Println("Container found:\t", container.Name)
		s = &cloudfiles.Storage{
			Provider:  provider,
			Container: container,
			Info:      &cloudfiles.Info{},
			Conn:      rs.RsConnection{},
		}
	}
	// Authenticate (after authentication it is possible to return URL
	if err = s.Authenticate(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Container url is:", s.GetURL().String())
}
Beispiel #2
0
// Sync folder with defined container name string (default is the first container in the list in local .cloud)
func Sync(name string) {
	fmt.Printf("Beginning sync with container: \"%s\"\n", name)
	// get .cloudcore
	var core storage.Core
	var err error
	if core, err = GetCore(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Found\t.cloudcore")
	// get .cloud
	var cloud storage.Cloud
	if cloud, err = GetCloud(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Found\t.cloud")
	var c storage.Container
	if c, err = GetContainer(name, &cloud); err != nil {
		log.Fatal(err)
	}
	// get container provider
	var s storage.Storage
	var p storage.Provider
	switch {
	case c.Provider == storage.CloudFiles:
		if p, err = GetProvider(&c, &core); err != nil {
			log.Fatal(err)
		}
		fmt.Println("Container found:\t", c.Name)
		s = &cloudfiles.Storage{
			Provider:  p,
			Container: c,
			Info:      &cloudfiles.Info{},
			Conn:      rs.RsConnection{},
		}
	default:
		// TODO what would be better to write here
		fmt.Println("Something went wrong!")
		return
	}
	// Authenticate
	if err = s.Authenticate(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Authenticated")
	// walk files upload file to the cloud
	var dir string
	if dir, err = os.Getwd(); err != nil {
		log.Fatal(err)
	}
	if err = filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error {
		// upload only files, not directories
		if !info.IsDir() {
			fp, err := filepath.Rel(dir, filename)
			if err != nil {
				log.Fatal(err)
			}
			if !IsIgnored(fp) {
				data, err := ioutil.ReadFile(fp)
				if err != nil {
					log.Fatal(err)
				}
				fmt.Println("Sync\t", fp)
				if err = s.Create(fp, data); err != nil {
					log.Fatal(err)
				}
			}
		}
		return nil
	}); err != nil {
		log.Fatal(err)
	}
}