Browse Source

Improve error messages

main
Fábio Kaiser Rauber 8 months ago
parent
commit
b02786d117
  1. 20
      main.go

20
main.go

@ -1,6 +1,9 @@
package main package main
import ( import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -11,9 +14,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"bytes"
"compress/gzip"
"encoding/base64"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -202,6 +202,9 @@ func processUserData(configDriveDir string) error {
defer f.Close() defer f.Close()
decodedContent, err := DecodeContent(file.Content, file.Encoding) decodedContent, err := DecodeContent(file.Content, file.Encoding)
if err != nil {
log.Printf("Error decoding permissions for file %s: %s", file.Path, err)
}
n, err := f.Write(decodedContent) n, err := f.Write(decodedContent)
if err != nil { if err != nil {
log.Printf("Error writing file %s: %s", file.Path, err) log.Printf("Error writing file %s: %s", file.Path, err)
@ -210,7 +213,7 @@ func processUserData(configDriveDir string) error {
} }
} }
// Run commands // Run commands
for _, cmd := range cc.RunCmd { for _, cmd := range cc.RunCmd {
cmdArgs := strings.Fields(cmd) cmdArgs := strings.Fields(cmd)
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).CombinedOutput() output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).CombinedOutput()
@ -357,17 +360,18 @@ func (f *CloudConfigFile) OSPermissions() (os.FileMode, error) {
// Parse string representation of file mode as integer // Parse string representation of file mode as integer
perm, err := strconv.ParseInt(f.Permissions, 8, 32) perm, err := strconv.ParseInt(f.Permissions, 8, 32)
if err != nil { if err != nil {
return 0, fmt.Errorf("Unable to parse file permissions %q as integer", f.Permissions) return 0, fmt.Errorf("unable to parse file permissions %q as integer", f.Permissions)
} }
return os.FileMode(perm), nil return os.FileMode(perm), nil
} }
// Decoding functions // Decoding functions
// copied from github.com/flatcar-linux/coreos-cloudinit
func DecodeBase64Content(content string) ([]byte, error) { func DecodeBase64Content(content string) ([]byte, error) {
output, err := base64.StdEncoding.DecodeString(content) output, err := base64.StdEncoding.DecodeString(content)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to decode base64: %q", err) return nil, fmt.Errorf("unable to decode base64: %q", err)
} }
return output, nil return output, nil
@ -377,7 +381,7 @@ func DecodeGzipContent(content string) ([]byte, error) {
gzr, err := gzip.NewReader(bytes.NewReader([]byte(content))) gzr, err := gzip.NewReader(bytes.NewReader([]byte(content)))
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to decode gzip: %q", err) return nil, fmt.Errorf("unable to decode gzip: %q", err)
} }
defer gzr.Close() defer gzr.Close()
@ -408,5 +412,5 @@ func DecodeContent(content string, encoding string) ([]byte, error) {
return DecodeGzipContent(string(gz)) return DecodeGzipContent(string(gz))
} }
return nil, fmt.Errorf("Unsupported encoding %q", encoding) return nil, fmt.Errorf("unsupported encoding %q", encoding)
} }
Loading…
Cancel
Save