@@ -56,6 +56,75 @@ func contentForLogging(content []byte) string {
5656 return res
5757}
5858
59+ func processPath (path string ) error {
60+ if strings .HasPrefix (path , "//depot/google3/" ) {
61+ path = strings .Replace (path , "//depot/google3/" , "" , 1 )
62+ }
63+ displayPath := path
64+ if path == stdinPlaceholderPath {
65+ displayPath = * stdinDisplayPath
66+ log .Info ("path " , path , " displayed as " , displayPath )
67+ } else {
68+ log .Info ("path " , path )
69+ }
70+
71+ content , err := read (path )
72+ if os .IsNotExist (err ) {
73+ log .Error ("Ignoring path: " , err )
74+ return fmt .Errorf ("path not found" )
75+ } else if err != nil {
76+ return err
77+ }
78+
79+ // Only pass the verbose logger if its level is enabled.
80+ var logger parser.Logger
81+ if l := log .V (2 ); l {
82+ logger = l
83+ }
84+ newContent , err := parser .FormatWithConfig (content , parser.Config {
85+ ExpandAllChildren : * expandAllChildren ,
86+ SkipAllColons : * skipAllColons ,
87+ SortFieldsByFieldName : * sortFieldsByFieldName ,
88+ SortRepeatedFieldsByContent : * sortRepeatedFieldsByContent ,
89+ SortRepeatedFieldsBySubfield : strings .Split (* sortRepeatedFieldsBySubfield , "," ),
90+ RemoveDuplicateValuesForRepeatedFields : * removeDuplicateValuesForRepeatedFields ,
91+ AllowTripleQuotedStrings : * allowTripleQuotedStrings ,
92+ WrapStringsAtColumn : * wrapStringsAtColumn ,
93+ WrapHTMLStrings : * wrapHTMLStrings ,
94+ WrapStringsAfterNewlines : * wrapStringsAfterNewlines ,
95+ WrapStringsWithoutWordwrap : * wrapStringsWithoutWordwrap ,
96+ PreserveAngleBrackets : * preserveAngleBrackets ,
97+ SmartQuotes : * smartQuotes ,
98+ Logger : logger ,
99+ })
100+ if err != nil {
101+ errorf ("parser.Format for path %v with content %q returned err %v" , displayPath , contentForLogging (content ), err )
102+ return fmt .Errorf ("parser.Format failed" )
103+ }
104+ log .V (2 ).Infof ("New content for path %s: %q" , displayPath , newContent )
105+
106+ return write (path , content , newContent )
107+ }
108+
109+ func write (path string , content , newContent []byte ) error {
110+ if path == stdinPlaceholderPath {
111+ fmt .Print (string (newContent ))
112+ return nil
113+ }
114+ if bytes .Equal (content , newContent ) {
115+ log .Info ("No change for path " , path )
116+ return nil
117+ }
118+ if * dryRun {
119+ fmt .Println (string (newContent ))
120+ return nil
121+ }
122+ if err := ioutil .WriteFile (path , newContent , 0664 ); err != nil {
123+ return err
124+ }
125+ return nil
126+ }
127+
59128func main () {
60129 flag .Parse ()
61130 paths := flag .Args ()
@@ -65,67 +134,11 @@ func main() {
65134 log .Info ("paths: " , paths )
66135 errs := 0
67136 for _ , path := range paths {
68- if strings .HasPrefix (path , "//depot/google3/" ) {
69- path = strings .Replace (path , "//depot/google3/" , "" , 1 )
70- }
71- displayPath := path
72- if path == stdinPlaceholderPath {
73- displayPath = * stdinDisplayPath
74- log .Info ("path " , path , " displayed as " , displayPath )
75- } else {
76- log .Info ("path " , path )
77- }
78-
79- content , err := read (path )
80- if os .IsNotExist (err ) {
81- log .Error ("Ignoring path: " , err )
82- errs ++
83- continue
84- } else if err != nil {
85- log .Exit (err )
86- }
87-
88- // Only pass the verbose logger if its level is enabled.
89- var logger parser.Logger
90- if l := log .V (2 ); l {
91- logger = l
92- }
93- newContent , err := parser .FormatWithConfig (content , parser.Config {
94- ExpandAllChildren : * expandAllChildren ,
95- SkipAllColons : * skipAllColons ,
96- SortFieldsByFieldName : * sortFieldsByFieldName ,
97- SortRepeatedFieldsByContent : * sortRepeatedFieldsByContent ,
98- SortRepeatedFieldsBySubfield : strings .Split (* sortRepeatedFieldsBySubfield , "," ),
99- RemoveDuplicateValuesForRepeatedFields : * removeDuplicateValuesForRepeatedFields ,
100- AllowTripleQuotedStrings : * allowTripleQuotedStrings ,
101- WrapStringsAtColumn : * wrapStringsAtColumn ,
102- WrapHTMLStrings : * wrapHTMLStrings ,
103- WrapStringsAfterNewlines : * wrapStringsAfterNewlines ,
104- WrapStringsWithoutWordwrap : * wrapStringsWithoutWordwrap ,
105- PreserveAngleBrackets : * preserveAngleBrackets ,
106- SmartQuotes : * smartQuotes ,
107- Logger : logger ,
108- })
109- if err != nil {
110- errorf ("parser.Format for path %v with content %q returned err %v" , displayPath , contentForLogging (content ), err )
111- errs ++
112- continue
113- }
114- log .V (2 ).Infof ("New content for path %s: %q" , displayPath , newContent )
115-
116- if path == stdinPlaceholderPath {
117- fmt .Print (string (newContent ))
118- continue
119- }
120- if bytes .Equal (content , newContent ) {
121- log .Info ("No change for path " , displayPath )
122- continue
123- }
124- if * dryRun {
125- fmt .Println (string (newContent ))
126- continue
127- }
128- if err := ioutil .WriteFile (path , newContent , 0664 ); err != nil {
137+ if err := processPath (path ); err != nil {
138+ if err .Error () == "path not found" || err .Error () == "parser.Format failed" {
139+ errs ++
140+ continue
141+ }
129142 log .Exit (err )
130143 }
131144 }
0 commit comments