|
| 1 | +// Copyright (C) 2015 Scaleway. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE.md file. |
| 4 | + |
| 5 | +package cli |
| 6 | + |
| 7 | +var cmdIPS = &Command{ |
| 8 | + Exec: runIPS, |
| 9 | + UsageLine: "_ips [--new|--attach|--delete] [IP_ID [SERVER_ID]]", |
| 10 | + |
| 11 | + Description: "Interacts with your IPs", |
| 12 | + Hidden: true, |
| 13 | + Help: "Interacts with your IPs", |
| 14 | + Examples: ` |
| 15 | + $ scw _ips |
| 16 | + $ scw _ips IP_ID |
| 17 | + $ scw _ips --new |
| 18 | + $ scw _ips --attach IP_ID SERVER_ID |
| 19 | + $ scw _ips --delete IP_ID |
| 20 | +`, |
| 21 | +} |
| 22 | + |
| 23 | +func init() { |
| 24 | + cmdIPS.Flag.BoolVar(&ipHelp, []string{"h", "-help"}, false, "Print usage") |
| 25 | + cmdIPS.Flag.BoolVar(&ipNew, []string{"n", "-new"}, false, "Add a new IP") |
| 26 | + cmdIPS.Flag.BoolVar(&ipAttach, []string{"a", "-attach"}, false, "Attach an IP to a server") |
| 27 | + cmdIPS.Flag.StringVar(&ipDelete, []string{"d", "-delete"}, "", "Detele an IP") |
| 28 | +} |
| 29 | + |
| 30 | +var ipHelp bool // -h, --help flag |
| 31 | +var ipNew bool // -n, --new flag |
| 32 | +var ipAttach bool // -a, --attach flag |
| 33 | +var ipDelete string // -d, --delete flag |
| 34 | + |
| 35 | +func runIPS(cmd *Command, args []string) error { |
| 36 | + if ipHelp { |
| 37 | + return cmd.PrintUsage() |
| 38 | + } |
| 39 | + if ipNew { |
| 40 | + ip, err := cmd.API.NewIP() |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + printRawMode(cmd.Streams().Stdout, ip) |
| 45 | + return nil |
| 46 | + } |
| 47 | + if ipDelete != "" { |
| 48 | + return cmd.API.DeleteIP(ipDelete) |
| 49 | + } |
| 50 | + if ipAttach { |
| 51 | + if len(args) != 2 { |
| 52 | + return cmd.PrintShortUsage() |
| 53 | + } |
| 54 | + return cmd.API.AttachIP(args[0], args[1]) |
| 55 | + } |
| 56 | + if len(args) == 1 { |
| 57 | + ip, err := cmd.API.GetIP(args[0]) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + printRawMode(cmd.Streams().Stdout, *ip) |
| 62 | + return nil |
| 63 | + } |
| 64 | + ips, err := cmd.API.GetIPS() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + printRawMode(cmd.Streams().Stdout, *ips) |
| 69 | + return nil |
| 70 | +} |
0 commit comments