ref: 3946ea6ad8f57628f22b006e3acc365cc0d8136c cisco/csc/cmd/instance/show.go -rw-r--r-- 2.4 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package instance

import (
	"cisco/csc/cmd"
	"cisco/sdk"
	"fmt"
	"github.com/spf13/cobra"
	"strconv"
)

type Row struct {
	title string
	value string
}

var TableSize int = 120

func printSep(num int) {
	var i int = 0
	var sep int = 0

	if num > 0 {
		sep = TableSize / num
	}

	for i <= TableSize {
		if i == 0 || i == TableSize || (num != 0 && i%sep == 0) {
			fmt.Printf("+")
		} else {
			fmt.Printf("-")
		}

		i = i + 1
	}

	fmt.Printf("\n")
}

func printWithPad(row Row, max int) {
	var length int = len(row.title) + len(row.value) + 2

	fmt.Printf("\033[1;39m%s:\033[0m %s", row.title, row.value)
	for length < max {
		fmt.Printf(" ")
		length = length + 1
	}
}

func printRows(rows []Row) {
	printSep(len(rows))

	fmt.Printf("|")

	for _, row := range rows {
		fmt.Printf(" ")
		printWithPad(row, (TableSize/len(rows))-2)
		fmt.Printf("|")
	}
	fmt.Printf("\n")
}

var instanceShow = &cobra.Command{
	Use:   "show",
	Short: "Show an instance",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		instance, err := cisco.ShowInstance(args[0])

		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
			return
		}
		printRows([]Row{{
			title: "Name",
			value: instance.Name,
		}})

		printRows([]Row{
			{
				title: "Status",
				value: instance.Status,
			},
			{
				title: "Type",
				value: instance.Type,
			},
			{
				title: "Image",
				value: instance.OS,
			},
			{
				title: "Region",
				value: "fr-par",
			},
		})
		printRows([]Row{
			{
				title: "Instance ID",
				value: instance.Uuid,
			},
			{
				title: "Image ID",
				value: "",
			},
		})

		printRows([]Row{
			{
				title: "Volumes",
				value: "1",
			},
			{
				title: "Storage",
				value: strconv.Itoa(int(instance.Size)) + "GB",
			},
		})

		printRows([]Row{
			{
				title: "Public IP",
				value: "None",
			},
			{
				title: "Private IP",
				value: instance.Ipv4Private,
			},
		})

		printRows([]Row{
			{
				title: "IPv6",
				value: instance.Ipv6,
			},
			{
				title: "Netmask",
				value: instance.Ipv6Mask,
			},
		})

		printRows([]Row{
			{
				title: "SSH Command",
				value: "ssh root@" + instance.Ipv6,
			},
		})

		printRows([]Row{
			{
				title: "Public DNS",
				value: instance.Uuid + ".public.louifox.house",
			},
		})

		printRows([]Row{
			{
				title: "Private DNS",
				value: instance.Uuid + ".internal.louifox.house",
			},
		})

		printSep(0)

	},
}

func init() {
	cmd.InstanceRootCmd.AddCommand(instanceShow)
}