github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/file_hcl.go (about)

     1  package appfile
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/hcl/ast"
     7  	"github.com/hashicorp/hcl/hcl/token"
     8  )
     9  
    10  var emptyAssign = token.Pos{Line: 1}
    11  
    12  // HCL converts the Appfile to an HCL AST, allowing printing of the Appfile
    13  // back to HCL.
    14  //
    15  // Note that if you parsed the File from HCL, this will not convert it
    16  // back to the same HCL. Comments, in particular, won't be preserved.
    17  func (f *File) HCL() *ast.File {
    18  	// Convert all the various components into members of the root object
    19  	items := make([]*ast.ObjectItem, 0, 10+len(f.Imports))
    20  	for _, imp := range f.Imports {
    21  		items = append(items, imp.HCL())
    22  	}
    23  	items = append(items, f.Application.HCL())
    24  	items = append(items, f.Project.HCL())
    25  	for _, infra := range f.Infrastructure {
    26  		items = append(items, infra.HCL())
    27  	}
    28  	items = append(items, f.Customization.HCL()...)
    29  
    30  	// Finalize
    31  	return &ast.File{
    32  		Node: &ast.ObjectList{
    33  			Items: items,
    34  		},
    35  	}
    36  }
    37  
    38  func (f *CustomizationSet) HCL() []*ast.ObjectItem {
    39  	if f == nil {
    40  		return nil
    41  	}
    42  
    43  	items := make([]*ast.ObjectItem, 0, len(f.Raw))
    44  	for _, c := range f.Raw {
    45  		items = append(items, c.HCL())
    46  	}
    47  
    48  	return items
    49  }
    50  
    51  func (f *Customization) HCL() *ast.ObjectItem {
    52  	items := make([]*ast.ObjectItem, 0, len(f.Config))
    53  	for k, v := range f.Config {
    54  		var val ast.Node
    55  		switch t := v.(type) {
    56  		case string:
    57  			val = &ast.LiteralType{
    58  				Token: token.Token{
    59  					Type: token.STRING,
    60  					Text: fmt.Sprintf(`"%s"`, t),
    61  				},
    62  			}
    63  		default:
    64  			panic(fmt.Sprintf("can't convert to HCL: %T", t))
    65  		}
    66  
    67  		items = append(items, &ast.ObjectItem{
    68  			Keys: []*ast.ObjectKey{
    69  				&ast.ObjectKey{
    70  					Token: token.Token{Type: token.IDENT, Text: k},
    71  				},
    72  			},
    73  			Val:    val,
    74  			Assign: emptyAssign,
    75  		})
    76  	}
    77  
    78  	return &ast.ObjectItem{
    79  		Keys: []*ast.ObjectKey{
    80  			&ast.ObjectKey{
    81  				Token: token.Token{Type: token.IDENT, Text: "customization"},
    82  			},
    83  			&ast.ObjectKey{
    84  				Token: token.Token{
    85  					Type: token.STRING,
    86  					Text: fmt.Sprintf(`"%s"`, f.Type),
    87  				},
    88  			},
    89  		},
    90  		Val: &ast.ObjectType{
    91  			List: &ast.ObjectList{
    92  				Items: items,
    93  			},
    94  		},
    95  	}
    96  }
    97  
    98  func (f *Dependency) HCL() *ast.ObjectItem {
    99  	items := make([]*ast.ObjectItem, 0, 1)
   100  	items = append(items, &ast.ObjectItem{
   101  		Keys: []*ast.ObjectKey{
   102  			&ast.ObjectKey{
   103  				Token: token.Token{Type: token.IDENT, Text: "source"},
   104  			},
   105  		},
   106  		Val: &ast.LiteralType{
   107  			Token: token.Token{
   108  				Type: token.STRING,
   109  				Text: fmt.Sprintf(`"%s"`, f.Source),
   110  			},
   111  		},
   112  		Assign: emptyAssign,
   113  	})
   114  
   115  	return &ast.ObjectItem{
   116  		Keys: []*ast.ObjectKey{
   117  			&ast.ObjectKey{
   118  				Token: token.Token{Type: token.IDENT, Text: "dependency"},
   119  			},
   120  		},
   121  		Val: &ast.ObjectType{
   122  			List: &ast.ObjectList{
   123  				Items: items,
   124  			},
   125  		},
   126  	}
   127  }
   128  
   129  func (f *Import) HCL() *ast.ObjectItem {
   130  	return &ast.ObjectItem{
   131  		Keys: []*ast.ObjectKey{
   132  			&ast.ObjectKey{
   133  				Token: token.Token{Type: token.IDENT, Text: "import"},
   134  			},
   135  			&ast.ObjectKey{
   136  				Token: token.Token{
   137  					Type: token.STRING,
   138  					Text: fmt.Sprintf(`"%s"`, f.Source),
   139  				},
   140  			},
   141  		},
   142  		Val: &ast.ObjectType{},
   143  	}
   144  }
   145  
   146  func (f *Application) HCL() *ast.ObjectItem {
   147  	items := make([]*ast.ObjectItem, 0, 2+len(f.Dependencies))
   148  	items = append(items, &ast.ObjectItem{
   149  		Keys: []*ast.ObjectKey{
   150  			&ast.ObjectKey{
   151  				Token: token.Token{
   152  					Type: token.IDENT,
   153  					Text: "name",
   154  					Pos:  token.Pos{Line: 1},
   155  				},
   156  			},
   157  		},
   158  		Val: &ast.LiteralType{
   159  			Token: token.Token{
   160  				Type: token.STRING,
   161  				Text: fmt.Sprintf(`"%s"`, f.Name),
   162  			},
   163  		},
   164  		Assign: emptyAssign,
   165  	})
   166  	if f.Type != "" {
   167  		items = append(items, &ast.ObjectItem{
   168  			Keys: []*ast.ObjectKey{
   169  				&ast.ObjectKey{
   170  					Token: token.Token{
   171  						Type: token.IDENT,
   172  						Text: "type",
   173  						Pos:  token.Pos{Line: 2},
   174  					},
   175  				},
   176  			},
   177  			Val: &ast.LiteralType{
   178  				Token: token.Token{
   179  					Type: token.STRING,
   180  					Text: fmt.Sprintf(`"%s"`, f.Type),
   181  				},
   182  			},
   183  			Assign: emptyAssign,
   184  		})
   185  	}
   186  	for _, dep := range f.Dependencies {
   187  		item := dep.HCL()
   188  		items = append(items, item)
   189  	}
   190  
   191  	return &ast.ObjectItem{
   192  		Keys: []*ast.ObjectKey{
   193  			&ast.ObjectKey{
   194  				Token: token.Token{Type: token.IDENT, Text: "application"},
   195  			},
   196  		},
   197  		Val: &ast.ObjectType{
   198  			List: &ast.ObjectList{
   199  				Items: items,
   200  			},
   201  		},
   202  	}
   203  }
   204  
   205  func (f *Project) HCL() *ast.ObjectItem {
   206  	items := make([]*ast.ObjectItem, 0, 2)
   207  	items = append(items, &ast.ObjectItem{
   208  		Keys: []*ast.ObjectKey{
   209  			&ast.ObjectKey{
   210  				Token: token.Token{
   211  					Type: token.IDENT,
   212  					Text: "name",
   213  					Pos:  token.Pos{Line: 1},
   214  				},
   215  			},
   216  		},
   217  		Val: &ast.LiteralType{
   218  			Token: token.Token{
   219  				Type: token.STRING,
   220  				Text: fmt.Sprintf(`"%s"`, f.Name),
   221  			},
   222  		},
   223  		Assign: emptyAssign,
   224  	})
   225  	items = append(items, &ast.ObjectItem{
   226  		Keys: []*ast.ObjectKey{
   227  			&ast.ObjectKey{
   228  				Token: token.Token{
   229  					Type: token.IDENT,
   230  					Text: "infrastructure",
   231  					Pos:  token.Pos{Line: 2},
   232  				},
   233  			},
   234  		},
   235  		Val: &ast.LiteralType{
   236  			Token: token.Token{
   237  				Type: token.STRING,
   238  				Text: fmt.Sprintf(`"%s"`, f.Infrastructure),
   239  			},
   240  		},
   241  		Assign: emptyAssign,
   242  	})
   243  
   244  	return &ast.ObjectItem{
   245  		Keys: []*ast.ObjectKey{
   246  			&ast.ObjectKey{
   247  				Token: token.Token{Type: token.IDENT, Text: "project"},
   248  			},
   249  		},
   250  		Val: &ast.ObjectType{
   251  			List: &ast.ObjectList{
   252  				Items: items,
   253  			},
   254  		},
   255  	}
   256  }
   257  
   258  func (f *Infrastructure) HCL() *ast.ObjectItem {
   259  	items := make([]*ast.ObjectItem, 0, 3+len(f.Foundations))
   260  	items = append(items, &ast.ObjectItem{
   261  		Keys: []*ast.ObjectKey{
   262  			&ast.ObjectKey{
   263  				Token: token.Token{
   264  					Type: token.IDENT,
   265  					Text: "name",
   266  					Pos:  token.Pos{Line: 1},
   267  				},
   268  			},
   269  		},
   270  		Val: &ast.LiteralType{
   271  			Token: token.Token{
   272  				Type: token.STRING,
   273  				Text: fmt.Sprintf(`"%s"`, f.Name),
   274  			},
   275  		},
   276  		Assign: emptyAssign,
   277  	})
   278  	items = append(items, &ast.ObjectItem{
   279  		Keys: []*ast.ObjectKey{
   280  			&ast.ObjectKey{
   281  				Token: token.Token{
   282  					Type: token.IDENT,
   283  					Text: "type",
   284  					Pos:  token.Pos{Line: 2},
   285  				},
   286  			},
   287  		},
   288  		Val: &ast.LiteralType{
   289  			Token: token.Token{
   290  				Type: token.STRING,
   291  				Text: fmt.Sprintf(`"%s"`, f.Type),
   292  			},
   293  		},
   294  		Assign: emptyAssign,
   295  	})
   296  	items = append(items, &ast.ObjectItem{
   297  		Keys: []*ast.ObjectKey{
   298  			&ast.ObjectKey{
   299  				Token: token.Token{
   300  					Type: token.IDENT,
   301  					Text: "flavor",
   302  					Pos:  token.Pos{Line: 3},
   303  				},
   304  			},
   305  		},
   306  		Val: &ast.LiteralType{
   307  			Token: token.Token{
   308  				Type: token.STRING,
   309  				Text: fmt.Sprintf(`"%s"`, f.Flavor),
   310  			},
   311  		},
   312  		Assign: emptyAssign,
   313  	})
   314  
   315  	return &ast.ObjectItem{
   316  		Keys: []*ast.ObjectKey{
   317  			&ast.ObjectKey{
   318  				Token: token.Token{Type: token.IDENT, Text: "infrastructure"},
   319  			},
   320  		},
   321  		Val: &ast.ObjectType{
   322  			List: &ast.ObjectList{
   323  				Items: items,
   324  			},
   325  		},
   326  	}
   327  }