Пример #1
0
// Push local to cloud.
func (parser *Parser) Push(authConfig *home.AuthConfig, out io.Writer) (shouldLogin bool, err error) {
	if authConfig.Username == "" || authConfig.AccessToken == "" {
		return true, fmt.Errorf("ERROR: Authentication failed\n")
	}

	client := get3w.NewClient(authConfig.AccessToken)
	output, _, err := client.Apps.FilesChecksum(parser.Owner, parser.Name)
	if err != nil {
		return false, err
	}
	files := output.Files

	localFiles, err := parser.Storage.GetAllFiles(parser.Storage.GetSourcePrefix(""))
	if err != nil {
		return false, err
	}

	// 1 specified add, 0 specified edit, -1 specified delete
	pathMap := make(map[string]int)

	for _, localFile := range localFiles {
		if localFile.IsDir || parser.IsLocalFile(localFile) {
			continue
		}
		checksum := files[localFile.Path]
		if checksum == "" {
			pathMap[localFile.Path] = 1
		} else {
			localChecksum, _ := parser.Storage.Checksum(parser.Storage.GetSourceKey(localFile.Path))
			if checksum != localChecksum {
				pathMap[localFile.Path] = 0
			}
		}
	}
	for path := range files {
		if !parser.Storage.IsExist(parser.Storage.GetSourceKey(path)) {
			pathMap[path] = -1
		}
	}

	fmt.Fprintf(out, "Local repository: %s\n", parser.Path)
	fmt.Fprintf(out, "Remote repository: %s/%s\n", parser.Owner, parser.Name)

	if len(pathMap) == 0 {
		fmt.Fprintln(out, "Everything up-to-date")
		return false, nil
	}

	gzPath := home.Path(stringutils.UUID() + ".tar.gz")

	err = ioutils.Pack(gzPath, parser.Path, pathMap)
	if err != nil {
		return false, err
	}

	data, err := ioutil.ReadFile(gzPath)
	if err != nil {
		return false, err
	}
	os.Remove(gzPath)

	blob := base64.StdEncoding.EncodeToString(data)

	input := &get3w.FilesPushInput{
		Blob: blob,
	}

	for path, val := range pathMap {
		if val > 0 {
			fmt.Fprintf(out, "\t+added:%s\n", path)
			input.Added = append(input.Added, path)
		}
	}
	for path, val := range pathMap {
		if val < 0 {
			fmt.Fprintf(out, "\t-removed:%s\n", path)
			input.Removed = append(input.Removed, path)
		}
	}
	for path, val := range pathMap {
		if val == 0 {
			fmt.Fprintf(out, "\tmodified:%s\n", path)
			input.Modified = append(input.Modified, path)
		}
	}

	_, _, err = client.Apps.FilesPush(parser.Owner, parser.Name, input)
	if err != nil {
		return false, err
	}

	fmt.Fprintln(out, "done.")
	return false, nil
}
Пример #2
0
func (cli *Get3WCli) login(username, password string) (*home.AuthConfig, error) {
	// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
	if runtime.GOOS == "windows" {
		cli.in = os.Stdin
	}

	promptDefault := func(prompt string, configDefault string) {
		if configDefault == "" {
			fmt.Fprintf(cli.out, "%s: ", prompt)
		} else {
			fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
		}
	}

	readInput := func(in io.Reader, out io.Writer) string {
		reader := bufio.NewReader(in)
		line, _, err := reader.ReadLine()
		if err != nil {
			fmt.Fprintln(out, err.Error())
			os.Exit(1)
		}
		return string(line)
	}

	authConfig := cli.config.AuthConfig

	if username == "" {
		promptDefault("Username", authConfig.Username)
		username = readInput(cli.in, cli.out)
		username = strings.TrimSpace(username)
		if username == "" {
			username = authConfig.Username
		}
	}

	// Assume that a different username means they may not want to use
	// the password or email from the config file, so prompt them
	if username != authConfig.Username {
		if password == "" {
			promptDefault("Password", authConfig.Password)
			password = readInput(cli.in, cli.out)
			if password == "" {
				password = authConfig.Password
			}
		}
	} else {
		// However, if they don't override the username use the
		// password or email from the cmd line if specified. IOW, allow
		// then to change/override them.  And if not specified, just
		// use what's in the config file
		if password == "" {
			password = authConfig.Password
		}
	}
	authConfig.Username = username
	authConfig.Password = password

	client := get3w.NewClient("")
	input := &get3w.UserLoginInput{
		Account:  username,
		Password: password,
	}
	output, resp, err := client.Users.Login(input)

	if resp.StatusCode == 401 {
		cli.config.AuthConfig = home.AuthConfig{}
		if err2 := cli.config.Save(); err2 != nil {
			fmt.Fprintf(cli.out, "WARNING: could not save config file: %v\n", err2)
		}
		return nil, err
	}
	if err != nil {
		return nil, err
	}

	authConfig.AccessToken = output.AccessToken
	cli.config.AuthConfig = authConfig
	if err := cli.config.Save(); err != nil {
		return nil, fmt.Errorf("ERROR: failed to save config file: %v", err)
	}
	fmt.Fprintf(cli.out, "INFO: login credentials saved in %s\n", home.Path(home.RootConfigName))

	if resp.Status != "" {
		fmt.Fprintf(cli.out, "%s\n", resp.Status)
	}
	return &authConfig, nil
}