// splitURL breaks a url into an index name and remote name func splitURL(url string) (string, string, string, error) { nameParts := strings.SplitN(strings.Trim(url, "/"), "/", 3) if len(nameParts) < 2 { return "", "", "", errors.New("Fatal: Invalid repository name (ex: \"get3w.com/myname/myrepo\")") } if len(nameParts) == 2 { return get3w.DefaultRepositoryHost(), nameParts[0], nameParts[1], nil } return nameParts[0], nameParts[1], nameParts[2], nil }
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 (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 }