grabrr/cmd/info.go

66 lines
1.4 KiB
Go
Raw Normal View History

2025-07-22 15:09:48 +03:00
package cmd
import (
2025-07-23 16:41:01 +03:00
"encoding/json"
2025-07-22 15:09:48 +03:00
"fmt"
2025-07-23 15:08:33 +03:00
"log"
2025-07-22 15:09:48 +03:00
"github.com/spf13/cobra"
2025-07-23 17:55:12 +03:00
bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
2025-07-22 15:09:48 +03:00
)
// 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) {
2025-07-23 15:08:33 +03:00
if err := runInfoCommand(); err != nil {
log.Fatalf("error: %v", err)
}
2025-07-22 15:09:48 +03:00
},
}
var (
2025-07-23 16:41:01 +03:00
albumURL string
prettyPrint bool
2025-07-23 17:55:12 +03:00
prettyJson bool
2025-07-22 15:09:48 +03:00
)
func init() {
rootCmd.AddCommand(infoCmd)
infoCmd.Flags().StringVarP(&albumURL, "url", "u", "", "Bunkrr album url (required)")
2025-07-23 16:41:01 +03:00
infoCmd.Flags().BoolVarP(&prettyPrint, "pretty", "p", false, "Whether or not to Pretty Print, otherwise export json")
2025-07-23 17:55:12 +03:00
infoCmd.Flags().BoolVarP(&prettyJson, "json", "j", true, "Whether or not to pretty print the JSON output")
2025-07-22 15:09:48 +03:00
infoCmd.MarkFlagRequired("url")
2025-07-23 15:08:33 +03:00
}
func runInfoCommand() error {
album, err := bunkr.FetchAlbumInfo(albumURL)
2025-07-23 17:55:12 +03:00
if err != nil {
return err
}
2025-07-23 15:08:33 +03:00
2025-07-23 16:41:01 +03:00
if prettyPrint {
fmt.Println(album.ToString())
} else {
2025-07-23 17:55:12 +03:00
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)
}
2025-07-23 16:41:01 +03:00
}
}
2025-07-23 17:55:12 +03:00
return nil
2025-07-22 15:09:48 +03:00
}