github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/gover/shutil.go (about)

     1  // Copyright 2016 The Go 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  package main
     6  
     7  import "strings"
     8  
     9  func shellEscape(x string) string {
    10  	if len(x) == 0 {
    11  		return "''"
    12  	}
    13  	for _, r := range x {
    14  		if 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || '0' <= r && r <= '9' || strings.ContainsRune("@%_-+:,./", r) {
    15  			continue
    16  		}
    17  		// Unsafe character.
    18  		return "'" + strings.Replace(x, "'", "'\"'\"'", -1) + "'"
    19  	}
    20  	return x
    21  
    22  }