-
Notifications
You must be signed in to change notification settings - Fork 2
/
replaceExt.go
49 lines (40 loc) · 1.09 KB
/
replaceExt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Package scandir contains utilities to find a filetype in a specific location
package scandir
import (
"log"
"os"
"path/filepath"
"strings"
)
var input string = ""
var fromExt string = ""
var toExt string = ""
// Replace the file extension in a string with another
func ReplaceExt(params ...string) string {
// Break when too few params are provided
if len(params) <= 2 {
log.Println("Missing required parameters")
log.Println("scandir.ReplaceExt(\"<input string>\", \"<from extension>\", \"<to extension>\")")
log.Println("scandir.ReplaceExt(\"/home/user/directory/file.css\", \".css\", \".min.css\")")
os.Exit(1)
}
// Param 1, The string
if len(params) > 0 {
input = params[0]
}
// Param 2, From file extension
if len(params) > 1 {
fromExt = params[1]
}
// Param 3, To extension
if len(params) > 2 {
toExt = params[2]
}
// If the input string did have a file extension. We can get it
// And replace it with the extension we want to get out
currentExt := filepath.Ext(input)
if len(currentExt) > 0 {
input = strings.TrimSuffix(input, currentExt) + toExt
}
return input
}