fmt.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package qemuimgfmt
  15. import (
  16. "strings"
  17. )
  18. type TImageFormat string
  19. const (
  20. QCOW2 = TImageFormat("qcow2")
  21. VMDK = TImageFormat("vmdk")
  22. VHD = TImageFormat("vhd")
  23. ISO = TImageFormat("iso")
  24. RAW = TImageFormat("raw")
  25. TGZ = TImageFormat("tgz")
  26. )
  27. var supportedImageFormats = []TImageFormat{
  28. QCOW2, VMDK, VHD, ISO, RAW, TGZ,
  29. }
  30. func IsSupportedImageFormat(fmtStr string) bool {
  31. for i := 0; i < len(supportedImageFormats); i += 1 {
  32. if fmtStr == string(supportedImageFormats[i]) {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. func (fmt TImageFormat) String() string {
  39. switch string(fmt) {
  40. case "vhd":
  41. return "vpc"
  42. default:
  43. return string(fmt)
  44. }
  45. }
  46. func String2ImageFormat(fmt string) TImageFormat {
  47. switch strings.ToLower(fmt) {
  48. case "vhd", "vpc":
  49. return VHD
  50. case "qcow2":
  51. return QCOW2
  52. case "vmdk":
  53. return VMDK
  54. case "iso":
  55. return ISO
  56. case "raw":
  57. return RAW
  58. case "tgz", "tar":
  59. return TGZ
  60. }
  61. // log.Fatalf("unknown image format!!! %s", fmt)
  62. return TImageFormat(fmt)
  63. }