1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/util.go

107 lines
1.8 KiB
Go
Raw Permalink Normal View History

2018-05-28 21:50:44 -04:00
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
2018-05-29 04:28:32 -04:00
package deno
2018-05-19 05:38:51 -04:00
2018-05-19 05:53:29 -04:00
import (
2018-05-25 12:25:55 -04:00
"fmt"
2018-05-19 05:53:29 -04:00
"net/url"
2018-05-23 11:27:56 -04:00
"os"
"strings"
2018-05-19 05:53:29 -04:00
)
2018-05-25 12:25:55 -04:00
func logDebug(format string, v ...interface{}) {
// Unless the debug flag is specified, discard logs.
if *flagDebug {
fmt.Printf(format+"\n", v...)
2018-05-25 12:25:55 -04:00
}
}
2018-05-25 17:01:18 -04:00
// exists returns whether the given file or directory exists or not
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
panic(err)
}
2018-05-19 05:56:02 -04:00
func assert(cond bool, msg string) {
2018-05-19 05:38:51 -04:00
if !cond {
panic(msg)
}
}
2018-05-19 05:53:29 -04:00
2018-05-19 05:56:02 -04:00
func isRemote(filename string) bool {
2018-05-19 05:53:29 -04:00
u, err := url.Parse(filename)
check(err)
return u.IsAbs()
}
func check(e error) {
if e != nil {
panic(e)
}
}
2018-05-23 11:27:56 -04:00
func exitOnError(err error) {
if err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
}
2018-05-27 03:46:18 -04:00
func async(cb func()) {
wg.Add(1)
go func() {
cb()
wg.Done()
}()
}
const wildcard = "[WILDCARD]"
// Matches the pattern string against the text string. The pattern can
// contain "[WILDCARD]" substrings which will match one or more characters.
// Returns true if matched.
func patternMatch(pattern string, text string) bool {
// Empty pattern only match empty text.
if len(pattern) == 0 {
return len(text) == 0
}
if pattern == wildcard {
return true
}
parts := strings.Split(pattern, wildcard)
if len(parts) == 1 {
return pattern == text
}
if strings.HasPrefix(text, parts[0]) {
text = text[len(parts[0]):]
} else {
return false
}
for i := 1; i < len(parts); i++ {
// If the last part is empty, we match.
if i == len(parts)-1 {
if parts[i] == "" || parts[i] == "\n" {
return true
}
}
index := strings.Index(text, parts[i])
if index < 0 {
return false
}
text = text[index+len(parts[i]):]
}
return len(text) == 0
}