/
file.go
116 lines (96 loc) · 2.54 KB
/
file.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
//go:build !wasm
package cmd
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
// from: https://stackoverflow.com/questions/10510691/how-to-check-whether-a-file-or-directory-exists
func fileExists(path string) (bool, error) {
if path == "" {
return false, nil
}
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
var fileReadCmd = &cobra.Command{
Use: "read",
Short: "Read File",
Long: `Read File`,
Run: func(cmd *cobra.Command, args []string) {
file := getStringFlag(cmd, "file", true)
exist := must(fileExists(file))
if !exist {
exitWithError(fmt.Errorf(`file "%s" not exist`, file))
}
//#nosec G304 -- implementation of generic utility
content := must(os.ReadFile(file))
outputBytes(cmd, content)
},
}
var fileWriteCmd = &cobra.Command{
Use: "write",
Short: "File Write",
Long: `File Write`,
Run: func(cmd *cobra.Command, args []string) {
byt := getInputBytes(cmd, args)
files := getStringFlag(cmd, "file", true)
overwrite := getBoolFlag(cmd, "overwrite", false)
multi := getBoolFlag(cmd, "multi", false)
binary := getBoolFlag(cmd, "binary", false)
var contents [][]byte
if multi {
var data []string
exitWithError(json.Unmarshal(byt, &data))
contents = make([][]byte, len(data))
for i, str := range data {
if binary {
contents[i] = must(base64.StdEncoding.DecodeString(str))
} else {
contents[i] = []byte(str)
}
}
} else {
contents = make([][]byte, 1)
if binary {
contents[0] = must(base64.StdEncoding.DecodeString(string(byt)))
} else {
contents[0] = byt
}
}
paths := strings.Split(files, ",")
res := make([]string, len(paths))
for i, path := range paths {
path := must(filepath.Abs(path))
exist := must(fileExists(path))
if exist && !overwrite {
exitWithError(fmt.Errorf(`file "%s" already exist`, path))
}
byt := contents[i]
exitWithError(os.WriteFile(path, byt, 0600))
res[i] = path
}
outputString(cmd, strings.Join(res, "\n"))
},
}
func init() {
pf := fileReadCmd.PersistentFlags()
pf.StringP("file", "p", "", "File path")
pf = fileWriteCmd.PersistentFlags()
pf.StringP("file", "p", "", "File path")
pf.BoolP("multi", "m", false, "Input as JSON array with multiple contents")
pf.BoolP("overwrite", "o", false, "Overwrite if already exist")
pf.BoolP("binary", "b", false, "Input as Base64 encoded content")
rootCmd.AddCommand(fileReadCmd)
rootCmd.AddCommand(fileWriteCmd)
}