github.com/dim13/unifi@v0.0.0-20230308161331-9b04946f5e93/cmd/new-voucher/main.go (about) 1 // Copyright (c) 2014 Dimitri Sokolyuk. All rights reserved. 2 // Use of this source code is governed by ISC-style license 3 // that can be found in the LICENSE file. 4 5 //Example of creating a new voucher, the return value - create time of the voucher 6 7 package main 8 9 import ( 10 "flag" 11 "fmt" 12 "log" 13 "os" 14 "text/tabwriter" 15 "time" 16 17 "github.com/dim13/unifi" 18 ) 19 20 var ( 21 num = flag.String("num", "1", "The number of the new vouchers") 22 multi = flag.String("multi", "0", "If 0 is the multi-voucher") 23 minute = flag.String("minute", "1440", "Duration of the voucher in minutes") 24 note = flag.String("note", "", "Note of the voucher") 25 26 host = flag.String("host", "unifi", "Controller hostname") 27 user = flag.String("user", "admin", "Controller username") 28 pass = flag.String("pass", "unifi", "Controller password") 29 version = flag.Int("version", 5, "Controller base version") 30 port = flag.String("port", "8443", "Controller port") 31 siteid = flag.String("siteid", "default", "Sitename or description") 32 ) 33 34 func main() { 35 w := new(tabwriter.Writer) 36 w.Init(os.Stdout, 0, 8, 3, ' ', 0) 37 defer w.Flush() 38 39 flag.Parse() 40 41 u, err := unifi.Login(*user, *pass, *host, *port, *siteid, *version) 42 if err != nil { 43 log.Fatal("Login returned error: ", err) 44 } 45 defer u.Logout() 46 47 site, err := u.Site(*siteid) 48 if err != nil { 49 log.Fatal(err) 50 } 51 52 jsonData := unifi.NewVoucher{ 53 Cmd: "create-voucher", 54 Expire: "custom", 55 ExpireNumber: *minute, 56 ExpireUnit: "1", 57 N: *num, 58 Note: *note, 59 Quota: *multi, 60 } 61 62 res, err := u.NewVoucher(site, jsonData) 63 if err != nil { 64 log.Fatalln(err) 65 } 66 67 ct := time.Unix(int64(res[0].CreateTime), 0).Format("2006-01-02 15:04:05") 68 fmt.Println(ct) 69 70 }