github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/command/inspect/command.go (about) 1 package inspect 2 3 import ( 4 "flag" 5 "fmt" 6 "github.com/mitchellh/packer/packer" 7 "log" 8 "sort" 9 "strings" 10 ) 11 12 type Command struct{} 13 14 func (Command) Help() string { 15 return strings.TrimSpace(helpText) 16 } 17 18 func (c Command) Synopsis() string { 19 return "see components of a template" 20 } 21 22 func (c Command) Run(env packer.Environment, args []string) int { 23 flags := flag.NewFlagSet("inspect", flag.ContinueOnError) 24 flags.Usage = func() { env.Ui().Say(c.Help()) } 25 if err := flags.Parse(args); err != nil { 26 return 1 27 } 28 29 args = flags.Args() 30 if len(args) != 1 { 31 flags.Usage() 32 return 1 33 } 34 35 // Read the file into a byte array so that we can parse the template 36 log.Printf("Reading template: %#v", args[0]) 37 tpl, err := packer.ParseTemplateFile(args[0]) 38 if err != nil { 39 env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err)) 40 return 1 41 } 42 43 // Convenience... 44 ui := env.Ui() 45 46 // Variables 47 if len(tpl.Variables) == 0 { 48 ui.Say("Variables:\n") 49 ui.Say(" <No variables>") 50 } else { 51 requiredHeader := false 52 for k, v := range tpl.Variables { 53 if v.Required { 54 if !requiredHeader { 55 requiredHeader = true 56 ui.Say("Required variables:\n") 57 } 58 59 ui.Machine("template-variable", k, v.Default, "1") 60 ui.Say(" " + k) 61 } 62 } 63 64 if requiredHeader { 65 ui.Say("") 66 } 67 68 ui.Say("Optional variables and their defaults:\n") 69 keys := make([]string, 0, len(tpl.Variables)) 70 max := 0 71 for k, _ := range tpl.Variables { 72 keys = append(keys, k) 73 if len(k) > max { 74 max = len(k) 75 } 76 } 77 78 sort.Strings(keys) 79 80 for _, k := range keys { 81 v := tpl.Variables[k] 82 if v.Required { 83 continue 84 } 85 86 padding := strings.Repeat(" ", max-len(k)) 87 output := fmt.Sprintf(" %s%s = %s", k, padding, v.Default) 88 89 ui.Machine("template-variable", k, v.Default, "0") 90 ui.Say(output) 91 } 92 } 93 94 ui.Say("") 95 96 // Builders 97 ui.Say("Builders:\n") 98 if len(tpl.Builders) == 0 { 99 ui.Say(" <No builders>") 100 } else { 101 keys := make([]string, 0, len(tpl.Builders)) 102 max := 0 103 for k, _ := range tpl.Builders { 104 keys = append(keys, k) 105 if len(k) > max { 106 max = len(k) 107 } 108 } 109 110 sort.Strings(keys) 111 112 for _, k := range keys { 113 v := tpl.Builders[k] 114 padding := strings.Repeat(" ", max-len(k)) 115 output := fmt.Sprintf(" %s%s", k, padding) 116 if v.Name != v.Type { 117 output = fmt.Sprintf("%s (%s)", output, v.Type) 118 } 119 120 ui.Machine("template-builder", k, v.Type) 121 ui.Say(output) 122 123 } 124 } 125 126 ui.Say("") 127 128 // Provisioners 129 ui.Say("Provisioners:\n") 130 if len(tpl.Provisioners) == 0 { 131 ui.Say(" <No provisioners>") 132 } else { 133 for _, v := range tpl.Provisioners { 134 ui.Machine("template-provisioner", v.Type) 135 ui.Say(fmt.Sprintf(" %s", v.Type)) 136 } 137 } 138 139 return 0 140 }