package main import ( "flag" "fmt" "log" "os" "git.fluffy.pw/matu6968/webdesk-appmarket-golang" "github.com/joho/godotenv" ) type Config struct { ServerURL string AuthToken string } func loadConfig() (*Config, error) { if err := godotenv.Load(); err != nil { return nil, fmt.Errorf("error loading .env file: %v", err) } serverURL := os.Getenv("SERVER_URL") if serverURL == "" { return nil, fmt.Errorf("SERVER_URL not set in .env file") } authToken := os.Getenv("AUTH_TOKEN") if authToken == "" { return nil, fmt.Errorf("AUTH_TOKEN not set in .env file") } return &Config{ ServerURL: serverURL, AuthToken: authToken, }, nil } func printUsage() { fmt.Println("Usage:") fmt.Println(" appmarketcli list - List all apps") fmt.Println(" appmarketcli upload -name NAME -ver VER -info INFO -pub PUB [-appid `123456789011`] -file PATH - Upload new app") fmt.Println(" appmarketcli edit -id APPID -name NAME -ver VER -info INFO -pub PUB [-file PATH] - Edit app") fmt.Println(" appmarketcli delete -id APPID - Delete app") } func main() { // Define command-line flags listCmd := flag.NewFlagSet("list", flag.ExitOnError) uploadCmd := flag.NewFlagSet("upload", flag.ExitOnError) uploadName := uploadCmd.String("name", "", "App name") uploadVer := uploadCmd.String("ver", "", "App version") uploadInfo := uploadCmd.String("info", "", "App info") uploadPub := uploadCmd.String("pub", "", "App publisher") uploadFile := uploadCmd.String("file", "", "Path to app file") uploadAppID := uploadCmd.String("appid", "", "Optional custom app ID") editCmd := flag.NewFlagSet("edit", flag.ExitOnError) editID := editCmd.String("id", "", "App ID") editName := editCmd.String("name", "", "App name") editVer := editCmd.String("ver", "", "App version") editInfo := editCmd.String("info", "", "App info") editPub := editCmd.String("pub", "", "App publisher") editFile := editCmd.String("file", "", "Path to app file (optional)") deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError) deleteID := deleteCmd.String("id", "", "App ID") if len(os.Args) < 2 { printUsage() os.Exit(1) } // Load configuration config, err := loadConfig() if err != nil { log.Fatalf("Failed to load configuration: %v", err) } // Create client c := client.NewClient(config.ServerURL, config.AuthToken) // Parse commands switch os.Args[1] { case "list": listCmd.Parse(os.Args[2:]) apps, err := c.GetApps() if err != nil { log.Fatalf("Failed to get apps: %v", err) } fmt.Println("Available apps:") for _, app := range apps { fmt.Printf("ID: %s\nName: %s\nVersion: %s\nInfo: %s\nPublisher: %s\nPath: %s\n\n", app.AppID, app.Name, app.Ver, app.Info, app.Pub, app.Path) } case "upload": uploadCmd.Parse(os.Args[2:]) if *uploadName == "" || *uploadVer == "" || *uploadInfo == "" || *uploadPub == "" || *uploadFile == "" { fmt.Println("All fields are required for upload (except appid)") uploadCmd.PrintDefaults() os.Exit(1) } metadata := client.AppMetadata{ Name: *uploadName, Ver: *uploadVer, Info: *uploadInfo, Pub: *uploadPub, } newApp, err := c.UploadApp(metadata, *uploadFile, *uploadAppID) if err != nil { log.Fatalf("Failed to upload app: %v", err) } fmt.Printf("Successfully uploaded app with ID: %s\n", newApp.AppID) case "edit": editCmd.Parse(os.Args[2:]) if *editID == "" || *editName == "" || *editVer == "" || *editInfo == "" || *editPub == "" { fmt.Println("All fields except file are required for edit") editCmd.PrintDefaults() os.Exit(1) } metadata := client.AppMetadata{ Name: *editName, Ver: *editVer, Info: *editInfo, Pub: *editPub, } err := c.EditApp(*editID, metadata, *editFile) if err != nil { log.Fatalf("Failed to edit app: %v", err) } fmt.Println("Successfully updated app") case "delete": deleteCmd.Parse(os.Args[2:]) if *deleteID == "" { fmt.Println("App ID is required for delete") deleteCmd.PrintDefaults() os.Exit(1) } err := c.DeleteApp(*deleteID) if err != nil { log.Fatalf("Failed to delete app: %v", err) } fmt.Println("Successfully deleted app") default: printUsage() os.Exit(1) } }