func parseTemplate(filename, parentID string, attrs smAttributes) ([]*smPackage, error) { n, err := parser.Parse(filename) if err != nil { return nil, err } if n.Type != parser.AstTemplate { return nil, errors.Errorf("expected template node, got %s", n.Type) } pkgs := []*smPackage{} tmplAttrs := attrs.Copy() for _, child := range n.Children { switch child.Type { case parser.AstAttributes: newAttrs, err := newAttributes(child) if err != nil { return nil, err } tmplAttrs.MergeInplace(newAttrs) default: npkgs, err := handleChild(parentID, filepath.Dir(filename), tmplAttrs, child) if err != nil { return nil, err } pkgs = append(pkgs, npkgs...) } } return pkgs, nil }
func ReadFile(filename string) (*smResource, error) { astN, err := parser.Parse(filename) if err != nil { return nil, err } return convertToTarget(filepath.Dir(filename), astN) }
func run() error { for _, file := range os.Args[1:] { stat, err := os.Lstat(file) switch { case os.IsNotExist(err): return errors.Errorf("file %q does not exist", file) case err != nil: return errors.Wrap(err, "failed to stat smd file") } astNode, err := parser.Parse(file) if err != nil { return err } err = ioutil.WriteFile(file, []byte(astNode.String()), stat.Mode().Perm()) if err != nil { return errors.Wrap(err, "failed to write file") } } return nil }