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

@ -1,102 +1,111 @@
package main package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {
//'videos get' subcommand //'videos get' subcommand
getCmd := flag.NewFlagSet("get", flag.ExitOnError) getCmd := flag.NewFlagSet("get", flag.ExitOnError)
getAll := getCmd.Bool("all", false, "Get all videos")
getID := getCmd.String("id", "", "YouTube video ID") // inputs for `videos get` command
getAll := getCmd.Bool("all", false, "Get all videos")
//'videos add' subcommand getID := getCmd.String("id", "", "YouTube video ID")
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
addID := addCmd.String("id", "", "YouTube video ID") //'videos add' subcommand
addTitle := addCmd.String("title", "", "YouTube video Title") addCmd := flag.NewFlagSet("add", flag.ExitOnError)
addUrl := addCmd.String("url", "", "YouTube video URL")
addImageUrl := addCmd.String("imageurl", "", "YouTube video Image URL") // inputs for `videos add` command
addDesc := addCmd.String("desc", "", "YouTube video description") addID := addCmd.String("id", "", "YouTube video ID")
addTitle := addCmd.String("title", "", "YouTube video Title")
if len(os.Args) < 2 { addUrl := addCmd.String("url", "", "YouTube video URL")
fmt.Println("expected 'get' or 'add' subcommands") addImageUrl := addCmd.String("imageurl", "", "YouTube video Image URL")
os.Exit(1) addDesc := addCmd.String("desc", "", "YouTube video description")
}
if len(os.Args) < 2 {
//look at the 2nd argument's value fmt.Println("expected 'get' or 'add' subcommands")
switch os.Args[1] { os.Exit(1)
case "get": // if its the 'get' command }
HandleGet(getCmd, getAll, getID)
case "add": // if its the 'add' command //look at the 2nd argument's value
HandleAdd(addCmd, addID,addTitle,addUrl, addImageUrl, addDesc) switch os.Args[1] {
default: // if we don't understand the input case "get": // if its the 'get' command
fmt.Println("expected 'get' or 'add' subcommands") HandleGet(getCmd, getAll, getID)
os.Exit(1) case "add": // if its the 'add' command
} HandleAdd(addCmd, addID,addTitle,addUrl, addImageUrl, addDesc)
} default: // if we don't understand the input
}
func HandleGet(getCmd *flag.FlagSet, all *bool, id *string){
getCmd.Parse(os.Args[2:])
}
if *all == false && *id == "" {
fmt.Print("id is required or specify --all for all videos") func HandleGet(getCmd *flag.FlagSet, all *bool, id *string){
getCmd.PrintDefaults()
os.Exit(1) getCmd.Parse(os.Args[2:])
}
if *all == false && *id == "" {
if *all { fmt.Print("id is required or specify --all for all videos")
//return all videos getCmd.PrintDefaults()
videos := getVideos() os.Exit(1)
}
fmt.Printf("ID \t Title \t URL \t ImageURL \t Description \n")
for _, video := range videos { if *all {
fmt.Printf("%v \t %v \t %v \t %v \t %v \n",video.Id, video.Title, video.Url, video.Imageurl,video.Description) //return all videos
} videos := getVideos()
return fmt.Printf("ID \t Title \t URL \t ImageURL \t Description \n")
} for _, video := range videos {
fmt.Printf("%v \t %v \t %v \t %v \t %v \n",video.Id, video.Title, video.Url, video.Imageurl,video.Description)
if *id != "" { }
videos := getVideos()
id := *id return
for _, video := range videos { }
if id == video.Id {
fmt.Printf("ID \t Title \t URL \t ImageURL \t Description \n") if *id != "" {
fmt.Printf("%v \t %v \t %v \t %v \t %v \n",video.Id, video.Title, video.Url, video.Imageurl,video.Description) videos := getVideos()
id := *id
} for _, video := range videos {
} if id == video.Id {
} 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)
}
func ValidateVideo(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){ }
}
if *id == "" || *title == "" || *url == "" || *imageUrl == "" || *description == "" {
fmt.Println("all fields are required for adding a video")
addCmd.PrintDefaults()
os.Exit(1) }
}
func ValidateVideo(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){
}
addCmd.Parse(os.Args[2:])
func HandleAdd(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){
addCmd.Parse(os.Args[2:]) if *id == "" || *title == "" || *url == "" || *imageUrl == "" || *description == "" {
fmt.Print("all fields are required for adding a video")
ValidateVideo(addCmd, id,title,url, imageUrl, description) addCmd.PrintDefaults()
os.Exit(1)
video := video{ }
Id: *id,
Title: *title, }
Description: *description,
Imageurl: *imageUrl, func HandleAdd(addCmd *flag.FlagSet,id *string, title *string, url *string, imageUrl *string, description *string ){
Url: *url,
} ValidateVideo(addCmd, id,title,url, imageUrl, description)
videos := getVideos() video := video{
videos = append(videos,video) Id: *id,
Title: *title,
saveVideos(videos) Description: *description,
} Imageurl: *imageUrl,
Url: *url,
}
videos := getVideos()
videos = append(videos,video)
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")