func ClientFromCmdCredentials(c Credentials) (*phraseapp.Client, error) { defaultCredentials, e := ConfigDefaultCredentials() if e != nil { return nil, e } return phraseapp.NewClient(PhraseAppCredentials(c), defaultCredentials) }
func selectFormat(data *WizardData) error { auth := phraseapp.Credentials{Token: data.AccessToken} client, err := phraseapp.NewClient(auth, nil) if err != nil { return err } formats, err := client.FormatsList(1, 25) if err != nil { return err } for counter, format := range formats { fmt.Printf("%2d. %s - %s, File-Extension: %s\n", counter+1, format.ApiName, format.Name, format.Extension) } var id string mainFormatDefault := "" if data.MainFormat != "" { mainFormatDefault = fmt.Sprintf(" [Press enter for default: %s]", data.MainFormat) } fmt.Printf("Select the format you want to use for language files you download from PhraseApp%s: ", mainFormatDefault) id = prompt() if id == "" && data.MainFormat != "" { data.Format = data.MainFormat return DisplayWizard(data, next(data), "") } number, err := strconv.Atoi(id) if err != nil { number = 0 for index, format := range formats { if format.ApiName == id { number = index + 1 } } if number < 1 { return DisplayWizard(data, "selectFormat", fmt.Sprintf("Argument Error: Please select a format from the list by specifying its position in the list.")) } } else if number < 1 || number > len(formats)+1 { return DisplayWizard(data, "selectFormat", fmt.Sprintf("Argument Error: Please select a format from the list by specifying its position in the list.")) } data.Format = formats[number-1].ApiName data.FormatExtension = formats[number-1].Extension return DisplayWizard(data, next(data), "") }
func selectProjectStep(data *WizardData) error { auth := phraseapp.Credentials{Token: data.AccessToken, Host: data.Host} fmt.Println("Please select your project:") var err error client, err = phraseapp.NewClient(auth, nil) var wg sync.WaitGroup out := make(chan []phraseapp.Project, 1) wg.Add(1) channelEnd := ChannelEnd{} getProjects := func(channelEnd *ChannelEnd) { var projects []*phraseapp.Project // time.Sleep(500 * time.Millisecond) projects, err = client.ProjectsList(1, 25) var array []phraseapp.Project for _, res := range projects { array = append(array, *res) } out <- array channelEnd.closed = true return } go getProjects(&channelEnd) go func(channelEnd *ChannelEnd, wg *sync.WaitGroup) { spinner("Loading projects... ", 0, channelEnd, wg) }(&channelEnd, &wg) var projects []phraseapp.Project projects = <-out clean() wg.Wait() close(out) if err != nil { unauth_match, match_err := regexp.MatchString("401", err.Error()) if match_err != nil { fmt.Fprintln(os.Stderr, err.Error()) panic(match_err) } if unauth_match { errmsg := fmt.Sprintf("Argument Error: AccessToken '%s' is invalid. It may be revoked. Please create a new Access Token.", data.AccessToken) data.AccessToken = "" ReportError("Invalid AccessToken", errmsg) return fmt.Errorf(errmsg) } else { return err } } if len(projects) == 1 { data.ProjectID = projects[0].ID data.MainFormat = projects[0].MainFormat fmt.Printf("You've got one project, \"%s\". Answer \"y\" to select this or \"n\" to create a new project: ", projects[0].Name) answer := prompt() if answer == "y" { return DisplayWizard(data, next(data), "") } else { data.ProjectID = "" data.MainFormat = "" return DisplayWizard(data, "newProject", "") } } for counter, project := range projects { fmt.Printf("%2d. %s (Id: %s)\n", counter+1, project.Name, project.ID) } fmt.Printf("%2d. Create new project\n", len(projects)+1) fmt.Print("Select project: ") id := prompt() number, err := strconv.Atoi(id) if err != nil || number < 1 || number > len(projects)+1 { return DisplayWizard(data, "selectProject", fmt.Sprintf("Argument Error: Please select a project from the list by specifying its position in the list, e.g. 2 for the second project.")) } if number == len(projects)+1 { return DisplayWizard(data, "newProject", "") } selectedProject := projects[number-1] data.ProjectID = selectedProject.ID data.MainFormat = selectedProject.MainFormat return DisplayWizard(data, next(data), "") }