iso.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 shell
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/util/shellutils"
  22. "yunion.io/x/onecloud/pkg/util/isoutils"
  23. )
  24. func init() {
  25. type DetectOSOptions struct {
  26. ISO string `help:"ISO file"`
  27. }
  28. shellutils.R(&DetectOSOptions{}, "detect", "Detect", func(args *DetectOSOptions) error {
  29. stat, err := os.Stat(args.ISO)
  30. if err != nil {
  31. return err
  32. }
  33. if stat.IsDir() {
  34. // 如果是目录,仅遍历一层目录下的 .iso 文件
  35. entries, err := os.ReadDir(args.ISO)
  36. if err != nil {
  37. return err
  38. }
  39. for _, entry := range entries {
  40. if entry.IsDir() {
  41. continue // 跳过子目录
  42. }
  43. if strings.HasSuffix(strings.ToLower(entry.Name()), ".iso") {
  44. path := filepath.Join(args.ISO, entry.Name())
  45. fmt.Printf("Processing: %s\n", path)
  46. f, err := os.Open(path)
  47. if err != nil {
  48. fmt.Printf("Error opening %s: %v\n", path, err)
  49. continue // 继续处理其他文件
  50. }
  51. isoInfo, err := isoutils.DetectOSFromISO(f)
  52. f.Close()
  53. if err != nil {
  54. fmt.Printf("Error detecting OS from %s: %v\n", path, err)
  55. continue
  56. }
  57. fmt.Printf("%s: %s\n", path, jsonutils.Marshal(isoInfo))
  58. }
  59. }
  60. return nil
  61. }
  62. // 如果是文件,按原逻辑处理
  63. f, err := os.Open(args.ISO)
  64. if err != nil {
  65. return err
  66. }
  67. defer f.Close()
  68. isoInfo, err := isoutils.DetectOSFromISO(f)
  69. if err != nil {
  70. return err
  71. }
  72. fmt.Printf("%s: %s\n", args.ISO, jsonutils.Marshal(isoInfo))
  73. return nil
  74. })
  75. }