package config import ( "bufio" "errors" "fmt" "io" "strconv" "strings" ) type category struct { ID string Name string } var categories = []category{ {ID: "1", Name: "Film & Animation"}, {ID: "2", Name: "Autos & Vehicles"}, {ID: "10", Name: "Music"}, {ID: "15", Name: "Pets & Animals"}, {ID: "17", Name: "Sports"}, {ID: "19", Name: "Travel & Events"}, {ID: "20", Name: "Gaming"}, {ID: "22", Name: "People & Blogs"}, {ID: "23", Name: "Comedy"}, {ID: "24", Name: "Entertainment"}, {ID: "25", Name: "News & Politics"}, {ID: "26", Name: "Howto & Style"}, {ID: "27", Name: "Education"}, {ID: "28", Name: "Science & Technology"}, {ID: "29", Name: "Nonprofits & Activism"}, } // Interrogate prompts for video metadata, using the current config values as // defaults, and stores the answers in cfg. func Interrogate(cfg *Config, in io.Reader, out io.Writer) error { p := &prompter{in: bufio.NewReader(in), out: out} title, err := p.ask("Title", cfg.Title) if err != nil { return err } cfg.Title = title description, err := p.ask("Description", cfg.Description) if err != nil { return err } cfg.Description = description privacy, err := p.askVisibility(cfg.Privacy) if err != nil { return err } cfg.Privacy = privacy categoryID, err := p.askCategory(cfg.CategoryID) if err != nil { return err } cfg.CategoryID = categoryID return nil } type prompter struct { in *bufio.Reader out io.Writer } func (p *prompter) ask(label, def string) (string, error) { if def != "" { _, _ = fmt.Fprintf(p.out, "%s [%s]: ", label, def) } else { _, _ = fmt.Fprintf(p.out, "%s: ", label) } line, err := p.in.ReadString('\n') if err != nil && !errors.Is(err, io.EOF) { return "", err } if errors.Is(err, io.EOF) && line == "" { return "", io.ErrUnexpectedEOF } line = strings.TrimSpace(line) if line == "" { return def, nil } return line, nil } func (p *prompter) askVisibility(def string) (string, error) { for { value, err := p.ask("Visibility (private, unlisted, public)", def) if err != nil { return "", err } switch value { case "private", "unlisted", "public": return value, nil default: _, _ = fmt.Fprintf(p.out, "Invalid visibility %q; use private, unlisted or public.\n", value) } } } func (p *prompter) askCategory(currentID string) (string, error) { _, _ = fmt.Fprintln(p.out, "Categories:") def := "" for i, c := range categories { _, _ = fmt.Fprintf(p.out, " %2d) %s\n", i+1, c.Name) if c.ID == currentID { def = strconv.Itoa(i + 1) } } label := fmt.Sprintf("Category (1-%d)", len(categories)) for { answer, err := p.ask(label, def) if err != nil { return "", err } n, err := strconv.Atoi(answer) if err == nil && n >= 1 && n <= len(categories) { return categories[n-1].ID, nil } _, _ = fmt.Fprintf(p.out, "Please enter a number between 1 and %d.\n", len(categories)) } }