github.com/maruel/nin@v0.0.0-20220112143044-f35891e3ce7e/cmd/nin/main.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"strings"
    21  
    22  	"github.com/maruel/nin"
    23  )
    24  
    25  func main() {
    26  	os.Exit(mainImpl())
    27  }
    28  
    29  type missingDependencyPrinter struct {
    30  }
    31  
    32  func (m *missingDependencyPrinter) OnMissingDep(node *nin.Node, path string, generator *nin.Rule) {
    33  	fmt.Printf("Missing dep: %s uses %s (generated by %s)\n", node.Path, path, generator.Name)
    34  }
    35  
    36  // Log a fatalf message and exit.
    37  func fatalf(msg string, s ...interface{}) {
    38  	fmt.Fprintf(os.Stderr, "nin: fatal: ")
    39  	fmt.Fprintf(os.Stderr, msg, s...)
    40  	fmt.Fprintf(os.Stderr, "\n")
    41  	// On Windows, some tools may inject extra threads.
    42  	// exit() may block on locks held by those threads, so forcibly exit.
    43  	_ = os.Stderr.Sync()
    44  	_ = os.Stdout.Sync()
    45  	os.Exit(1)
    46  }
    47  
    48  // Log a warning message.
    49  func warningf(msg string, s ...interface{}) {
    50  	fmt.Fprintf(os.Stderr, "nin: warning: ")
    51  	fmt.Fprintf(os.Stderr, msg, s...)
    52  	fmt.Fprintf(os.Stderr, "\n")
    53  }
    54  
    55  // Log an error message.
    56  func errorf(msg string, s ...interface{}) {
    57  	fmt.Fprintf(os.Stderr, "nin: error: ")
    58  	fmt.Fprintf(os.Stderr, msg, s...)
    59  	fmt.Fprintf(os.Stderr, "\n")
    60  }
    61  
    62  // Log an informational message.
    63  func infof(msg string, s ...interface{}) {
    64  	fmt.Fprintf(os.Stdout, "nin: ")
    65  	fmt.Fprintf(os.Stdout, msg, s...)
    66  	fmt.Fprintf(os.Stdout, "\n")
    67  }
    68  
    69  func islatinalpha(c byte) bool {
    70  	// isalpha() is locale-dependent.
    71  	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
    72  }
    73  
    74  // Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
    75  func stripAnsiEscapeCodes(in string) string {
    76  	if strings.IndexByte(in, '\x1B') == -1 {
    77  		return in
    78  	}
    79  	stripped := ""
    80  	//stripped.reserve(in.size())
    81  
    82  	for i := 0; i < len(in); i++ {
    83  		if in[i] != '\x1B' {
    84  			// Not an escape code.
    85  			stripped += string(in[i])
    86  			continue
    87  		}
    88  
    89  		// Only strip CSIs for now.
    90  		if i+1 >= len(in) {
    91  			break
    92  		}
    93  		if in[i+1] != '[' { // Not a CSI.
    94  			continue
    95  		}
    96  		i += 2
    97  
    98  		// Skip everything up to and including the next [a-zA-Z].
    99  		for i < len(in) && !islatinalpha(in[i]) {
   100  			i++
   101  		}
   102  	}
   103  	return stripped
   104  }