grabrr/cmd/info.go

66 lines
1.4 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"log"
"github.com/spf13/cobra"
bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
)
// infoCmd represents the info command
var infoCmd = &cobra.Command{
Use: "info",
Short: "A simple info fetch of a bunkr album",
Long: `The command is used to fetch information about a specific bunkrr album, it will reply with all the information it has about it.`,
Run: func(cmd *cobra.Command, args []string) {
if err := runInfoCommand(); err != nil {
log.Fatalf("error: %v", err)
}
},
}
var (
albumURL string
prettyPrint bool
prettyJson bool
)
func init() {
rootCmd.AddCommand(infoCmd)
infoCmd.Flags().StringVarP(&albumURL, "url", "u", "", "Bunkrr album url (required)")
infoCmd.Flags().BoolVarP(&prettyPrint, "pretty", "p", false, "Whether or not to Pretty Print, otherwise export json")
infoCmd.Flags().BoolVarP(&prettyJson, "json", "j", true, "Whether or not to pretty print the JSON output")
infoCmd.MarkFlagRequired("url")
}
func runInfoCommand() error {
album, err := bunkr.FetchAlbumInfo(albumURL)
if err != nil {
return err
}
if prettyPrint {
fmt.Println(album.ToString())
} else {
if prettyJson {
prettyJSON, err := json.MarshalIndent(album, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", prettyJSON)
} else {
jsonObject, err := json.Marshal(album)
if err != nil {
return err
} else {
fmt.Printf("%s", jsonObject)
}
}
}
return nil
}