1
0

write_file.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package cert
  2. import (
  3. "os"
  4. "path/filepath"
  5. )
  6. type Content struct {
  7. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  8. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  9. SSLCertificate string `json:"ssl_certificate"`
  10. SSLCertificateKey string `json:"ssl_certificate_key"`
  11. }
  12. func (c *Content) WriteFile() (err error) {
  13. // MkdirAll creates a directory named path, along with any necessary parents,
  14. // and returns nil, or else returns an error.
  15. // The permission bits perm (before umask) are used for all directories that MkdirAll creates.
  16. // If path is already a directory, MkdirAll does nothing and returns nil.
  17. err = os.MkdirAll(filepath.Dir(c.SSLCertificatePath), 0644)
  18. if err != nil {
  19. return
  20. }
  21. err = os.MkdirAll(filepath.Dir(c.SSLCertificateKeyPath), 0644)
  22. if err != nil {
  23. return
  24. }
  25. if c.SSLCertificate != "" {
  26. err = os.WriteFile(c.SSLCertificatePath, []byte(c.SSLCertificate), 0644)
  27. if err != nil {
  28. return
  29. }
  30. }
  31. if c.SSLCertificateKey != "" {
  32. err = os.WriteFile(c.SSLCertificateKeyPath, []byte(c.SSLCertificateKey), 0644)
  33. if err != nil {
  34. return
  35. }
  36. }
  37. return
  38. }