github.com/elastic/gosigar@v0.14.3/sys/windows/fix_generated.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  //+build ignore
    19  
    20  package main
    21  
    22  import (
    23  	"flag"
    24  	"io/ioutil"
    25  	"log"
    26  	"regexp"
    27  )
    28  
    29  func main() {
    30  	var filename string
    31  
    32  	log.SetFlags(0)
    33  	flag.StringVar(&filename, "input", "", "name of generated source file to fix")
    34  	flag.Parse()
    35  
    36  	if filename == "" {
    37  		log.Fatal("Name of generated file must be specified with -input flag")
    38  	}
    39  
    40  	if err := fixGeneratedCode(filename); err != nil {
    41  		log.Fatal(err)
    42  	}
    43  }
    44  
    45  var lazySystemRegex = regexp.MustCompile(`(?m)\sNewLazySystemDLL`)
    46  var unsafeImportRegex = regexp.MustCompile(`(?m)"unsafe"`)
    47  
    48  // fixGeneratedCode adds "windows." to locations in the generated source code
    49  // that reference "NewLazySystemDLL" without the package name.
    50  func fixGeneratedCode(filename string) error {
    51  	data, err := ioutil.ReadFile(filename)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	data = lazySystemRegex.ReplaceAll(data, []byte(" windows.NewLazySystemDLL"))
    57  	data = unsafeImportRegex.ReplaceAll(data, []byte(`"unsafe"`+"\n\n\t"+`"golang.org/x/sys/windows"`))
    58  
    59  	return ioutil.WriteFile(filename, data, 0644)
    60  }