func testConfig(t *testing.T, n string) *config.Config { c, err := config.LoadDir(filepath.Join(fixtureDir, n)) if err != nil { t.Fatalf("err: %s", err) } return c }
// NewTreeModule is like NewTree except it parses the configuration in // the directory and gives it a specific name. Use a blank name "" to specify // the root module. func NewTreeModule(name, dir string) (*Tree, error) { c, err := config.LoadDir(dir) if err != nil { return nil, err } return NewTree(name, c), nil }
func TestTreeLoad_copyable(t *testing.T) { dir := tempDir(t) storage := &getter.FolderStorage{StorageDir: dir} cfg := testConfig(t, "basic") tree := NewTree("", cfg) // This should get things if err := tree.Load(storage, GetModeGet); err != nil { t.Fatalf("err: %s", err) } if !tree.Loaded() { t.Fatal("should be loaded") } // This should no longer error if err := tree.Load(storage, GetModeNone); err != nil { t.Fatalf("err: %s", err) } // Now we copy the directory, this COPIES symlink values, and // doesn't create symlinks themselves. That is important. dir2 := tempDir(t) os.RemoveAll(dir2) defer os.RemoveAll(dir2) if err := copy.CopyDir(dir, dir2); err != nil { t.Fatalf("err: %s", err) } // Now copy the configuration cfgDir := tempDir(t) os.RemoveAll(cfgDir) defer os.RemoveAll(cfgDir) if err := copy.CopyDir(cfg.Dir, cfgDir); err != nil { t.Fatalf("err: %s", err) } { cfg, err := config.LoadDir(cfgDir) if err != nil { t.Fatalf("err: %s", err) } tree := NewTree("", cfg) storage := &getter.FolderStorage{StorageDir: dir2} // This should not error since we already got it! if err := tree.Load(storage, GetModeNone); err != nil { t.Fatalf("err: %s", err) } if !tree.Loaded() { t.Fatal("should be loaded") } } }
// Context returns a Terraform Context taking into account the context // options used to initialize this meta configuration. func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error) { opts := m.contextOpts() // First try to just read the plan directly from the path given. f, err := os.Open(path) if err == nil { plan, err := terraform.ReadPlan(f) f.Close() if err == nil { if len(m.variables) > 0 { return nil, false, fmt.Errorf( "You can't set variables with the '-var' or '-var-file' flag\n" + "when you're applying a plan file. The variables used when\n" + "the plan was created will be used. If you wish to use different\n" + "variable values, create a new plan file.") } return plan.Context(opts), true, nil } } // Load up the state var state *terraform.State if statePath != "" { f, err := os.Open(statePath) if err != nil && os.IsNotExist(err) { // If the state file doesn't exist, it is okay, since it // is probably a new infrastructure. err = nil } else if err == nil { state, err = terraform.ReadState(f) f.Close() } if err != nil { return nil, false, fmt.Errorf("Error loading state: %s", err) } } // Store the loaded state m.state = state config, err := config.LoadDir(path) if err != nil { return nil, false, fmt.Errorf("Error loading config: %s", err) } if err := config.Validate(); err != nil { return nil, false, fmt.Errorf("Error validating config: %s", err) } opts.Config = config opts.State = state ctx := terraform.NewContext(opts) return ctx, false, nil }
func (c *ValidateCommand) validate(dir string) int { cfg, err := config.LoadDir(dir) if err != nil { c.Ui.Error(fmt.Sprintf( "Error loading files %v\n", err.Error())) return 1 } err = cfg.Validate() if err != nil { c.Ui.Error(fmt.Sprintf( "Error validating: %v\n", err.Error())) return 1 } return 0 }
func main() { if *dir == "" { log.Fatal("Directory must be specified") } fmt.Println("Valdating directory ", *dir) cfg, err := config.LoadDir(*dir) if err != nil { log.Fatalf("Loading: %s\n\nError: %s", *dir, err) } err = cfg.Validate() if err != nil { log.Fatalf("Validating: %s\n\nError: %s", *dir, err) } }
func jsonify(path string) int { if len(path) < 1 { panic("Need a path, dummy") } tfconfig, err := config.LoadDir(path) if err != nil { panic(err) } cfgjson, err := json.MarshalIndent(tfconfig, "", " ") fmt.Println(string(cfgjson)) return 0 }