gitlab.com/CoiaPrant/sqlite3@v1.19.1/addport.go (about)

     1  // Copyright 2022 The Sqlite Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  )
    16  
    17  func fail(rc int, msg string, args ...interface{}) {
    18  	fmt.Fprintf(os.Stderr, msg, args...)
    19  	os.Exit(rc)
    20  }
    21  
    22  func main() {
    23  	if len(os.Args) != 3 {
    24  		fail(1, "expected 2 args: pattern and replacement\n")
    25  	}
    26  
    27  	pattern := os.Args[1]
    28  	replacement := os.Args[2]
    29  	if err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
    30  		if err != nil {
    31  			return err
    32  		}
    33  
    34  		if info.IsDir() {
    35  			return nil
    36  		}
    37  
    38  		dir, file := filepath.Split(path)
    39  		if x := strings.Index(file, pattern); x >= 0 {
    40  			// pattern      freebsd
    41  			// replacement  netbsd
    42  			// file         libc_freebsd_amd64.go
    43  			// replaced     libc_netbsd_amd64.go
    44  			//              01234567890123456789
    45  			//                        1
    46  			// x            5
    47  			file = file[:x] + replacement + file[x+len(pattern):]
    48  			dst := filepath.Join(dir, file)
    49  			b, err := os.ReadFile(path)
    50  			if err != nil {
    51  				return fmt.Errorf("reading %s: %v", path, err)
    52  			}
    53  
    54  			if err := os.WriteFile(dst, b, 0640); err != nil {
    55  				return fmt.Errorf("writing %s: %v", dst, err)
    56  			}
    57  			fmt.Printf("%s -> %s\n", path, dst)
    58  		}
    59  
    60  		return nil
    61  	}); err != nil {
    62  		fail(1, "%s", err)
    63  	}
    64  }