|
| 1 | +package speaker |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/devopsdays/devopsdays-cli/helpers" |
| 9 | + "github.com/devopsdays/devopsdays-cli/names" |
| 10 | + "github.com/devopsdays/devopsdays-cli/talk" |
| 11 | + "github.com/fatih/color" |
| 12 | + survey "gopkg.in/AlecAivazis/survey.v1" |
| 13 | +) |
| 14 | + |
| 15 | +// Prompts for a new speaker |
| 16 | +var qsCreateSpeaker = []*survey.Question{ |
| 17 | + { |
| 18 | + Name: "name", |
| 19 | + Prompt: &survey.Input{ |
| 20 | + Message: "What is the speaker's name?", |
| 21 | + Help: "This is the speaker's full name", |
| 22 | + }, |
| 23 | + Validate: survey.Required, |
| 24 | + }, |
| 25 | + { |
| 26 | + Name: "bio", |
| 27 | + Prompt: &survey.Editor{Message: "Enter the speaker's bio [Enter to launch editor]"}, |
| 28 | + }, |
| 29 | + { |
| 30 | + Name: "website", |
| 31 | + Prompt: &survey.Input{ |
| 32 | + Message: "What is the speaker's website? [optional]", |
| 33 | + Help: "This should be the full URL to the speaker's website. Example: https://www.mattstratton.com", |
| 34 | + }, |
| 35 | + Validate: func(val interface{}) error { |
| 36 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "facebook") == false) { |
| 37 | + return errors.New("Please enter a valid URL.") |
| 38 | + } |
| 39 | + return nil |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "twitter", |
| 44 | + Prompt: &survey.Input{ |
| 45 | + Message: "What is the speaker's Twitter? [optional]", |
| 46 | + Help: "Twitter username can include the @ symbol or not. Examples: '@mattstratton' or 'mattstratton", |
| 47 | + }, |
| 48 | + Validate: func(val interface{}) error { |
| 49 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "twitter") == false) { |
| 50 | + return errors.New("Please enter a valid Twitter handle. It should not have any spaces.") |
| 51 | + } |
| 52 | + return nil |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "facebook", |
| 57 | + Prompt: &survey.Input{ |
| 58 | + Message: "What is the speaker's Facebook URL? [optional]", |
| 59 | + Help: "This should be the full URL to the speaker's Facebook page. Example: https://www.facebook.com/matt.stratton", |
| 60 | + }, |
| 61 | + Validate: func(val interface{}) error { |
| 62 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "facebook") == false) { |
| 63 | + return errors.New("Please enter a valid Facebook URL. It should be a URL.") |
| 64 | + } |
| 65 | + return nil |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + Name: "linkedin", |
| 70 | + Prompt: &survey.Input{ |
| 71 | + Message: "What is the speaker's LinkedIn URL? [optional]", |
| 72 | + Help: "This should be the full URL to the speaker's LinkedIn profile. Example: https://www.linkedin.com/in/mattstratton/", |
| 73 | + }, |
| 74 | + Validate: func(val interface{}) error { |
| 75 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "linkedin")) == false { |
| 76 | + return errors.New("Please enter a valid LinkedIn URL. It should be a URL.") |
| 77 | + } |
| 78 | + return nil |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: "github", |
| 83 | + Prompt: &survey.Input{ |
| 84 | + Message: "What is the speaker's GitHub username? [optional]", |
| 85 | + Help: "This should be the username, not the URL. Example: mattstratton", |
| 86 | + }, |
| 87 | + Validate: func(val interface{}) error { |
| 88 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "github")) == false { |
| 89 | + return errors.New("Please enter a valid GitHub username. It should be the username, not URL.") |
| 90 | + } |
| 91 | + return nil |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + Name: "gitlab", |
| 96 | + Prompt: &survey.Input{ |
| 97 | + Message: "What is the speaker's GitLab username? [optional]", |
| 98 | + Help: "This should be the username, not the URL. Example: mattstratton", |
| 99 | + }, |
| 100 | + Validate: func(val interface{}) error { |
| 101 | + if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "gitlab")) == false { |
| 102 | + return errors.New("Please enter a valid GitLab username. It should be the username, not URL.") |
| 103 | + } |
| 104 | + return nil |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + Name: "imagepath", |
| 109 | + Prompt: &survey.Input{ |
| 110 | + Message: "Enter the path to the speaker's image. [optional]", |
| 111 | + Help: "Path to speaker image. Must be a PNG or JPG file. Example: /Users/mattstratton/Pictures/matt-stratton.png", |
| 112 | + }, |
| 113 | + Validate: func(val interface{}) error { |
| 114 | + str, _ := val.(string) |
| 115 | + if str != "" { |
| 116 | + if _, err := os.Stat(str); err != nil { |
| 117 | + return errors.New("File not found.") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | + }, |
| 123 | + }, |
| 124 | +} |
| 125 | + |
| 126 | +// CreateSpeaker takes input from the user to create a new speaker |
| 127 | +func CreateSpeaker(speakerName, city, year string) (err error) { |
| 128 | + |
| 129 | + answers := struct { |
| 130 | + Name string |
| 131 | + Bio string |
| 132 | + Website string |
| 133 | + Twitter string |
| 134 | + Facebook string |
| 135 | + Linkedin string |
| 136 | + Github string |
| 137 | + Gitlab string |
| 138 | + ImagePath string |
| 139 | + Talk string |
| 140 | + }{} |
| 141 | + |
| 142 | + surveyErr := survey.Ask(qsCreateSpeaker, &answers) |
| 143 | + if surveyErr != nil { |
| 144 | + fmt.Println(surveyErr.Error()) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + name := false |
| 149 | + prompt := &survey.Confirm{ |
| 150 | + Message: "Do you want to add this speaker to an existing talk?", |
| 151 | + } |
| 152 | + survey.AskOne(prompt, &name, nil) |
| 153 | + |
| 154 | + t := "" |
| 155 | + if name == true { |
| 156 | + myList, _ := talk.GetTalks(city, year) |
| 157 | + prompt := &survey.Select{ |
| 158 | + Message: "Choose a talk:", |
| 159 | + Options: myList, |
| 160 | + } |
| 161 | + survey.AskOne(prompt, &t, nil) |
| 162 | + color.Yellow("NOT IMPLEMENTED") |
| 163 | + } |
| 164 | + |
| 165 | + if answers.ImagePath != "" { |
| 166 | + answers.ImagePath = SpeakerImage(answers.ImagePath, names.NameClean(answers.Name), city, year) |
| 167 | + } |
| 168 | + |
| 169 | + mySpeaker := Speaker{ |
| 170 | + Name: names.NameClean(answers.Name), |
| 171 | + Title: answers.Name, |
| 172 | + Website: answers.Website, |
| 173 | + Twitter: helpers.TwitterClean(answers.Twitter), |
| 174 | + Facebook: answers.Facebook, |
| 175 | + Linkedin: answers.Linkedin, |
| 176 | + Github: answers.Github, |
| 177 | + Gitlab: answers.Gitlab, |
| 178 | + ImagePath: answers.ImagePath, |
| 179 | + Bio: answers.Bio, |
| 180 | + } |
| 181 | + |
| 182 | + NewSpeaker(mySpeaker, city, year) |
| 183 | + |
| 184 | + return |
| 185 | +} |
0 commit comments