diff --git a/cmd/instance/instance.go b/cmd/instance/instance.go index 3ac9251..656aa1f 100644 --- a/cmd/instance/instance.go +++ b/cmd/instance/instance.go @@ -59,6 +59,7 @@ func init() { instanceCreateCmd.Flags().StringVar(&script, "script", "", "path to a script that will be uploaded to /usr/local/bin/civo-user-init-script on your instance, read/write/executable only by root and then will be executed at the end of the cloud initialization") instanceCreateCmd.Flags().BoolVar(&skipShebangCheck, "skip-shebang-check", false, "skip the shebang line check when passing a user init script") instanceCreateCmd.Flags().StringSliceVarP(&volumes, "volumes", "v", []string{}, "List of volumes to attach at boot") + instanceCreateCmd.Flags().StringVarP(&volumetype, "volume-type", "", "", "Specify the volume type for the instance") instanceStopCmd.Flags().BoolVarP(&waitStop, "wait", "w", false, "wait until the instance's is stoped") } diff --git a/cmd/instance/instance_create.go b/cmd/instance/instance_create.go index c6871e4..d1b1630 100644 --- a/cmd/instance/instance_create.go +++ b/cmd/instance/instance_create.go @@ -18,7 +18,7 @@ import ( ) var wait bool -var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall string +var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall, volumetype string var script string var skipShebangCheck bool var volumes []string @@ -150,6 +150,13 @@ If you wish to use a custom format, the available fields are: config.ReservedIPv4 = reservedIPv4 } + if volumetype != "" { + if !validateAndSetVolumeType(client, volumetype, config) { + utility.Error("The provided volume type is not valid") + os.Exit(1) + } + } + // Set private_ipv4 if provided if privateIPv4 != "" { config.PrivateIPv4 = privateIPv4 @@ -347,3 +354,23 @@ If you wish to use a custom format, the available fields are: } }, } + +// Helper function to validate volume type and set it in config +func validateAndSetVolumeType(client *civogo.Client, volumetype string, config *civogo.InstanceConfig) bool { + // Fetch volume types from Civo API + volumeTypes, err := client.ListVolumeTypes() + if err != nil { + utility.Error("Unable to list volume types %s", err) + os.Exit(1) + } + + // Check if the provided volume type is valid + for _, v := range volumeTypes { + if v.Name == volumetype { + config.VolumeType = v.Name + return true // Volume type is valid + } + } + + return false // Volume type is not valid +} diff --git a/cmd/instance/instance_show.go b/cmd/instance/instance_show.go index 183f5e9..c8ba467 100644 --- a/cmd/instance/instance_show.go +++ b/cmd/instance/instance_show.go @@ -69,6 +69,7 @@ If you wish to use a custom format, the available fields are: ow.AppendDataWithLabel("hostname", instance.Hostname, "Hostname") ow.AppendDataWithLabel("status", utility.ColorStatus(instance.Status), "Status") ow.AppendDataWithLabel("size", instance.Size, "Size") + ow.AppendDataWithLabel("volume-type", instance.VolumeType, "Volume Type") ow.AppendDataWithLabel("cpu_cores", strconv.Itoa(instance.CPUCores), "Cpu Cores") ow.AppendDataWithLabel("ram_mb", strconv.Itoa(instance.RAMMegabytes), "Ram") ow.AppendDataWithLabel("disk_gb", strconv.Itoa(instance.DiskGigabytes), "SSD disk") diff --git a/go.mod b/go.mod index 6de310e..b0dc216 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect github.com/briandowns/spinner v1.11.1 github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect - github.com/civo/civogo v0.3.82 + github.com/civo/civogo v0.3.84 github.com/dsnet/compress v0.0.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/google/go-github v17.0.0+incompatible // indirect diff --git a/go.sum b/go.sum index b8fda7a..f493363 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,8 @@ github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ= github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/civo/civogo v0.3.82 h1:5UD1BrYzJ851BCnaViAW9GvrCxzU0Dgz9gFEMgI/2zY= -github.com/civo/civogo v0.3.82/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= +github.com/civo/civogo v0.3.84 h1:jf5IT7VJFPaReO6g8B0zqKhsYCIizaGo4PjDLY7Sl6Y= +github.com/civo/civogo v0.3.84/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=