func init() { token := os.Getenv("GET3W_AUTH_TOKEN") if token == "" { print("!!! No access token. Some tests won't run. !!!\n\n") Client = get3w.NewClient("") } else { Client = get3w.NewClient(token) Auth = true } }
// Login user login func Login() echo.HandlerFunc { return func(c echo.Context) error { input := &get3w.UserLoginInput{} err := api.LoadRequestInput(c, input) if err != nil { return api.ErrorBadRequest(c, err) } if input.Account == "" || input.Password == "" { return api.ErrorUnauthorized(c, nil) } client := get3w.NewClient("") output, _, err := client.Users.Login(input) if err != nil { return api.ErrorUnauthorized(c, err) } config, _ := home.LoadConfig() config.AuthConfig = home.AuthConfig{ Username: output.User.Username, Password: input.Password, AccessToken: output.AccessToken, } if err := config.Save(); err != nil { return api.ErrorInternal(c, fmt.Errorf("ERROR: failed to save config file: %v", err)) } return c.JSON(http.StatusOK, output) } }
func (cli *Get3WCli) get(url, dir string) error { authConfig := &cli.config.AuthConfig var host, owner, name string var err error if url != "" { host, owner, name, err = splitURL(url) if err != nil { return err } if dir == "" { dir = name } } parser, err := storage.NewLocalParser(authConfig.Username, dir) if err != nil { return err } if url == "" { host = get3w.DefaultRepositoryHost() owner = authConfig.Username name = parser.Name } client := get3w.NewClient(cli.config.AuthConfig.AccessToken) if host != get3w.DefaultRepositoryHost() { return fmt.Errorf("ERROR: Only %s supported\n", get3w.DefaultRepositoryHost()) } fmt.Printf("Getting repository '%s/%s/%s'...\n", host, owner, name) fmt.Print("Counting objects: ") output, _, err := client.Apps.FilesChecksum(owner, name) if err != nil { return err } fmt.Printf("%d, done.\n", len(output.Files)) for path, remoteChecksum := range output.Files { download := false if !parser.Storage.IsExist(parser.Storage.GetSourceKey(path)) { download = true } else { checksum, _ := parser.Storage.Checksum(parser.Storage.GetSourceKey(path)) if checksum != remoteChecksum { download = true } } if download { fmt.Printf("Receiving object: %s", path) fileOutput, _, err := client.Apps.GetFile(owner, name, path) if err != nil { return err } data, err := base64.StdEncoding.DecodeString(fileOutput.Content) if err != nil { return err } parser.Storage.Write(parser.Storage.GetSourceKey(path), data) fmt.Println(", done.") } } // parser.Config.Repository = repo // err = parser.WriteConfig() // if err != nil { // return err // } return cli.build(dir) }
func addFromCloud(dirPath, origin string, authConfig *home.AuthConfig) (string, error) { var err error nameParts := strings.SplitN(strings.Trim(origin, "/"), "/", 2) owner, name := nameParts[0], nameParts[1] appPath := filepath.Join(dirPath, name) parser, err := storage.NewLocalParser(authConfig.Username, appPath) if err != nil { return "", err } client := get3w.NewClient(authConfig.AccessToken) fmt.Printf("Getting repository '%s/%s'...\n", owner, name) fmt.Print("Counting objects: ") output, _, err := client.Apps.FilesChecksum(owner, name) if err != nil { return "", err } fmt.Printf("%d, done.\n", len(output.Files)) for path, remoteChecksum := range output.Files { download := false if !parser.Storage.IsExist(parser.Storage.GetSourceKey(path)) { download = true } else { checksum, _ := parser.Storage.Checksum(parser.Storage.GetSourceKey(path)) if checksum != remoteChecksum { download = true } } if download { fmt.Printf("Receiving object: %s", path) fileOutput, _, err := client.Apps.GetFile(owner, name, path) if err != nil { return "", err } data, err := base64.StdEncoding.DecodeString(fileOutput.Content) if err != nil { return "", err } parser.Storage.Write(parser.Storage.GetSourceKey(path), data) fmt.Println(", done.") } } // parser.Config.Repository = repo // err = parser.WriteConfig() // if err != nil { // return err // } builder, err := storage.NewLocalParser(authConfig.Username, appPath) if err != nil { return "", err } return appPath, builder.Build(true) }
func TestUsers(t *testing.T) { signupInput := &get3w.UserSignupInput{ Username: username, Email: email, Password: password, } signupOutput, _, err := tests.Client.Users.Signup(signupInput) if err != nil { t.Fatalf("Users.Signup returned error: %v", err) } if signupOutput.AccessToken == "" { t.Errorf("Users.Signup returned no access token") } if signupOutput.User == nil { t.Errorf("Users.Signup returned no user") } loginInput := &get3w.UserLoginInput{ Account: username, Password: password, } loginOutput, _, err := tests.Client.Users.Login(loginInput) if err != nil { t.Fatalf("Users.Login returned error: %v", err) } if loginOutput.AccessToken == "" { t.Errorf("Users.Login returned no access token") } if loginOutput.User == nil { t.Errorf("Users.Login returned no user") } passwordForgotInput := &get3w.UserPasswordForgotInput{ Email: email, } _, err = tests.Client.Users.PasswordForgot(passwordForgotInput) if err != nil { t.Fatalf("Users.PasswordForgot returned error: %v", err) } clientAuth := get3w.NewClient(loginOutput.AccessToken) editInput := map[string]interface{}{ "company": "company", } user, _, err := clientAuth.Users.Edit(editInput) if err != nil { t.Fatalf("Users.Edit returned error: %v", err) } if user == nil { t.Errorf("Users.Edit returned no user") } passwordResetInput := &get3w.UserPasswordResetInput{ CurrentPassword: password, NewPassword: newPassword, } _, err = clientAuth.Users.PasswordReset(passwordResetInput) if err != nil { t.Fatalf("Users.PasswordReset returned error: %v", err) } deleteInput := &get3w.UserDeleteInput{ Password: newPassword, } _, err = clientAuth.Users.Delete(deleteInput) if err != nil { t.Fatalf("Users.Delete returned error: %v", err) } }
// 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 }
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 }
func (cli *Get3WCli) status(dir string) error { authConfig := &cli.config.AuthConfig parser, err := storage.NewLocalParser(authConfig.Username, dir) if err != nil { return err } if authConfig.Username == "" || authConfig.AccessToken == "" { fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", "status") authConfig, err = cli.login("", "") if err != nil { return err } } else { fmt.Fprintf(cli.out, "\nYour Username:%s\n", authConfig.Username) } host := get3w.DefaultRepositoryHost() owner := authConfig.Username name := parser.Name client := get3w.NewClient(authConfig.AccessToken) output, _, err := client.Apps.FilesChecksum(owner, name) if err != nil { return err } files := output.Files localFiles, err := parser.Storage.GetAllFiles(parser.Storage.GetSourcePrefix("")) if err != nil { return 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(localFile.Path) if checksum != localChecksum { pathMap[localFile.Path] = 0 } } } for path := range files { if !parser.Storage.IsExist(path) { pathMap[path] = -1 } } fmt.Fprintf(cli.out, "Local repository: %s\n", parser.Path) fmt.Fprintf(cli.out, "Remote repository: %s/%s/%s\n", host, owner, name) //Your branch is up-to-date with 'origin/master'. if len(pathMap) == 0 { fmt.Fprintln(cli.out, "Everything up-to-date") return nil } fmt.Fprintln(cli.out, "Diff:") for path, val := range pathMap { if val > 0 { fmt.Fprintf(cli.out, "\t+added:%s\n", path) } } for path, val := range pathMap { if val < 0 { fmt.Fprintf(cli.out, "\t-removed:%s\n", path) } } for path, val := range pathMap { if val == 0 { fmt.Fprintf(cli.out, "\tmodified:%s\n", path) } } return nil }