This commit is contained in:
marcel-dempers 2021-03-21 22:18:54 +11:00
parent e3dfa21ca6
commit e183d4b9b8
2 changed files with 111 additions and 103 deletions

View File

@ -10,11 +10,15 @@ func main() {
//'videos get' subcommand //'videos get' subcommand
getCmd := flag.NewFlagSet("get", flag.ExitOnError) getCmd := flag.NewFlagSet("get", flag.ExitOnError)
// inputs for `videos get` command
getAll := getCmd.Bool("all", false, "Get all videos") getAll := getCmd.Bool("all", false, "Get all videos")
getID := getCmd.String("id", "", "YouTube video ID") getID := getCmd.String("id", "", "YouTube video ID")
//'videos add' subcommand //'videos add' subcommand
addCmd := flag.NewFlagSet("add", flag.ExitOnError) addCmd := flag.NewFlagSet("add", flag.ExitOnError)
// inputs for `videos add` command
addID := addCmd.String("id", "", "YouTube video ID") addID := addCmd.String("id", "", "YouTube video ID")
addTitle := addCmd.String("title", "", "YouTube video Title") addTitle := addCmd.String("title", "", "YouTube video Title")
addUrl := addCmd.String("url", "", "YouTube video URL") addUrl := addCmd.String("url", "", "YouTube video URL")
@ -33,12 +37,13 @@ func main() {
case "add": // if its the 'add' command case "add": // if its the 'add' command
HandleAdd(addCmd, addID,addTitle,addUrl, addImageUrl, addDesc) HandleAdd(addCmd, addID,addTitle,addUrl, addImageUrl, addDesc)
default: // if we don't understand the input default: // if we don't understand the input
fmt.Println("expected 'get' or 'add' subcommands")
os.Exit(1)
} }
} }
func HandleGet(getCmd *flag.FlagSet, all *bool, id *string){ func HandleGet(getCmd *flag.FlagSet, all *bool, id *string){
getCmd.Parse(os.Args[2:]) getCmd.Parse(os.Args[2:])
if *all == false && *id == "" { if *all == false && *id == "" {
@ -66,16 +71,20 @@ func HandleGet(getCmd *flag.FlagSet, all *bool, id *string){
if id == video.Id { if id == video.Id {
fmt.Printf("ID \t Title \t URL \t ImageURL \t Description \n") fmt.Printf("ID \t Title \t URL \t ImageURL \t Description \n")
fmt.Printf("%v \t %v \t %v \t %v \t %v \n",video.Id, video.Title, video.Url, video.Imageurl,video.Description) fmt.Printf("%v \t %v \t %v \t %v \t %v \n",video.Id, video.Title, video.Url, video.Imageurl,video.Description)
}
}
}
}
}
}
} }
func ValidateVideo(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){ func ValidateVideo(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){
addCmd.Parse(os.Args[2:])
if *id == "" || *title == "" || *url == "" || *imageUrl == "" || *description == "" { if *id == "" || *title == "" || *url == "" || *imageUrl == "" || *description == "" {
fmt.Println("all fields are required for adding a video") fmt.Print("all fields are required for adding a video")
addCmd.PrintDefaults() addCmd.PrintDefaults()
os.Exit(1) os.Exit(1)
} }
@ -83,7 +92,6 @@ func ValidateVideo(addCmd *flag.FlagSet,id *string, title *string, url *string,
} }
func HandleAdd(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){ func HandleAdd(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){
addCmd.Parse(os.Args[2:])
ValidateVideo(addCmd, id,title,url, imageUrl, description) ValidateVideo(addCmd, id,title,url, imageUrl, description)
@ -99,4 +107,5 @@ func HandleAdd(addCmd *flag.FlagSet,id *string, title *string, url *string, imag
videos = append(videos,video) videos = append(videos,video)
saveVideos(videos) saveVideos(videos)
} }

View File

@ -13,7 +13,6 @@ type video struct {
Url string Url string
} }
func getVideos()(videos []video){ func getVideos()(videos []video){
fileBytes, err := ioutil.ReadFile("./videos.json") fileBytes, err := ioutil.ReadFile("./videos.json")