| 1 |
/*- |
| 2 |
* Copyright 2019 Square Inc. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"io" |
| 21 |
"os" |
| 22 |
) |
| 23 |
|
| 24 |
// Read input from file or stdin |
| 25 |
func readInput(path string) []byte { |
| 26 |
var bytes []byte |
| 27 |
var err error |
| 28 |
|
| 29 |
if path != "" { |
| 30 |
bytes, err = os.ReadFile(path) |
| 31 |
} else { |
| 32 |
bytes, err = io.ReadAll(os.Stdin) |
| 33 |
} |
| 34 |
|
| 35 |
app.FatalIfError(err, "unable to read input") |
| 36 |
return bytes |
| 37 |
} |
| 38 |
|
| 39 |
// Get input stream from file or stdin |
| 40 |
func inputStream(path string) *os.File { |
| 41 |
var file *os.File |
| 42 |
var err error |
| 43 |
|
| 44 |
if path != "" { |
| 45 |
file, err = os.Open(path) |
| 46 |
} else { |
| 47 |
file = os.Stdin |
| 48 |
} |
| 49 |
|
| 50 |
app.FatalIfError(err, "unable to read input") |
| 51 |
return file |
| 52 |
} |
| 53 |
|
| 54 |
// Write output to file or stdin |
| 55 |
func writeOutput(path string, data []byte) { |
| 56 |
var err error |
| 57 |
|
| 58 |
if path != "" { |
| 59 |
err = os.WriteFile(path, data, 0644) |
| 60 |
} else { |
| 61 |
_, err = os.Stdout.Write(data) |
| 62 |
} |
| 63 |
|
| 64 |
app.FatalIfError(err, "unable to write output") |
| 65 |
} |
| 66 |
|
| 67 |
// Get output stream for file or stdout |
| 68 |
func outputStream(path string) *os.File { |
| 69 |
var file *os.File |
| 70 |
var err error |
| 71 |
|
| 72 |
if path != "" { |
| 73 |
file, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 74 |
} else { |
| 75 |
file = os.Stdout |
| 76 |
} |
| 77 |
|
| 78 |
app.FatalIfError(err, "unable to write output") |
| 79 |
return file |
| 80 |
} |
| 81 |
|
| 82 |
// Byte contents of key file |
| 83 |
func keyBytes() []byte { |
| 84 |
keyBytes, err := os.ReadFile(*keyFile) |
| 85 |
app.FatalIfError(err, "unable to read key file") |
| 86 |
return keyBytes |
| 87 |
} |
| 88 |
|
| 89 |
// Write new file to current dir |
| 90 |
func writeNewFile(filename string, data []byte, perm os.FileMode) error { |
| 91 |
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
n, err := f.Write(data) |
| 96 |
if err == nil && n < len(data) { |
| 97 |
err = io.ErrShortWrite |
| 98 |
} |
| 99 |
if err1 := f.Close(); err == nil { |
| 100 |
err = err1 |
| 101 |
} |
| 102 |
return err |
| 103 |
} |