-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
59 lines (46 loc) · 1.1 KB
/
pipe.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
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
)
func pipe(cmd string, input io.Reader) ([]byte, error) {
extpipe := exec.Command(cmd)
extpipe.Stdin = input
var pipeout bytes.Buffer
extpipe.Stdout = &pipeout
if err := extpipe.Start(); err != nil {
return nil, fmt.Errorf("pipe start: %w", err)
}
if err := extpipe.Wait(); err != nil {
return nil, fmt.Errorf("pipe wait: %w", err)
}
return pipeout.Bytes(), nil
}
func targetPath(dest, newext string, uglyURLs bool) (dir string, filename string) {
filename = "index." + newext
dir = dest
if uglyURLs && dest != "index" {
// make the last element in destination the file
filename = filepath.Base(dest) + "." + newext
// set the parent directory of that file to be the dir to create
dir = filepath.Dir(dest)
}
if dest == "index" {
dir = "."
}
return
}
func mkdir(dir string) (bool, error) {
// skip if this exists
if _, err := os.Stat(dir); err == nil {
return false, nil
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return false, fmt.Errorf("mkdir: %w", err)
}
return true, nil
}