-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
125 lines (109 loc) · 2.61 KB
/
util.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
// SPDX-License-Identifier: GPL-3.0-only
//
// Copyright (C) 2019 cmr@informatik.wtf
// This file is part of rmb.
//
// rmb is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// rmb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rmb. If not, see <https://www.gnu.org/licenses/>.
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
const usage = `usage: rmb <conf>
rmb <conf> page
rmb -h
'rmb <conf>' Compile the website described in the YAML conf file
'rmb <conf> page' Create an empty index or empty post via cli prompts
Copyright (C) 2019 cmr@informatik.wtf
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions; see
the provided COPYING file.`
func console(w io.Writer, s string, vargs ...interface{}) {
if !strings.HasPrefix(s, "rmb: ") {
s = "rmb: " + s
}
switch {
case len(vargs) == 0:
fmt.Fprintf(w, s)
default:
fmt.Fprintf(w, s, vargs...)
}
}
func stdout(s string, vargs ...interface{}) {
console(os.Stdout, s, vargs...)
}
func stderr(s string, vargs ...interface{}) {
console(os.Stderr, s, vargs...)
}
func kill(err error, s string, vargs ...interface{}) {
if perr(err, s, vargs...) {
os.Exit(1)
}
return
}
func perr(err error, s string, vargs ...interface{}) (ok bool) {
if err != nil {
stderr("%+v\n", err)
if s != "" {
stderr(s, vargs...)
}
ok = true
}
return
}
func unixfy(f string) (s string) {
var x []rune
for _, c := range f {
if c >= 'A' && c <= 'Z' {
// Lowercase
c += 0x20
} else if c == ' ' {
// Replace space with underscore
c = '_'
} else if !(c >= 'a' && c <= 'z' || c == '_' ||
c >= '0' && c <= '9') {
// Drop any non-alphanumeric or underscore character
continue
}
x = append(x, c)
}
s = string(x)
return
}
func dircpy(from string, dest string) (err error) {
files, err := filepath.Glob(filepath.Join(from, "*"))
if perr(err, "") {
return
}
var cmderr bytes.Buffer
for _, f := range files {
cmd := exec.Command("cp", f, dest)
cmd.Stderr = &cmderr
cmderr.Reset()
err := cmd.Run()
if err != nil {
stderr("%+s\n", cmderr)
break
}
}
return
}
func tostring(b []byte) (s string) {
s = string(b)
return
}