github.com/devops-filetransfer/sshego@v7.0.4+incompatible/mailgun.go (about)

     1  package sshego
     2  
     3  import (
     4  	"bufio"
     5  	"flag"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"strings"
    10  )
    11  
    12  // MailgunConfig sets up sending
    13  // backup emails through Mailgun.
    14  // See https://mailgun.com.
    15  //
    16  type MailgunConfig struct {
    17  
    18  	// MAILGUN_DOMAIN
    19  	Domain string
    20  
    21  	// MAILGUN_PUBLIC_API_KEY
    22  	PublicApiKey string
    23  
    24  	//MAILGUN_SECRET_API_KEY
    25  	SecretApiKey string
    26  }
    27  
    28  // LoadConfig reads configuration from a file, expecting
    29  // KEY=value pair on each line;
    30  // values optionally enclosed in double quotes.
    31  func (c *MailgunConfig) LoadConfig(path string) error {
    32  	if !fileExists(path) {
    33  		return fmt.Errorf("path '%s' does not exist", path)
    34  	}
    35  
    36  	file, err := os.OpenFile(path, os.O_RDONLY, 0)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	defer file.Close()
    41  
    42  	bufIn := bufio.NewReader(file)
    43  	lineNum := int64(1)
    44  	for {
    45  		lastLine, err := bufIn.ReadBytes('\n')
    46  		if err != nil && err != io.EOF {
    47  			return err
    48  		}
    49  
    50  		if err == io.EOF && len(lastLine) == 0 {
    51  			break
    52  		}
    53  		line := string(lastLine)
    54  		line = strings.Trim(line, "\n\r\t ")
    55  
    56  		if len(line) > 0 && line[0] == '#' {
    57  			// comment, ignore
    58  		} else {
    59  
    60  			splt := strings.SplitN(line, "=", 2)
    61  			if len(splt) != 2 {
    62  				/*fmt.Fprintf(os.Stderr, "ignoring malformed (path: '%s') "+
    63  				"config line(%v): '%s'\n",
    64  				path, lineNum, line)
    65  				*/
    66  				continue
    67  			}
    68  			key := strings.Trim(splt[0], "\t\n\r ")
    69  			val := strings.Trim(splt[1], "\t\n\r ")
    70  
    71  			val = trim(val)
    72  
    73  			switch key {
    74  			case "MAILGUN_DOMAIN":
    75  				c.Domain = val
    76  
    77  			case "MAILGUN_PUBLIC_API_KEY":
    78  				c.PublicApiKey = val
    79  
    80  			case "MAILGUN_SECRET_API_KEY":
    81  				c.SecretApiKey = val
    82  			}
    83  		}
    84  		lineNum++
    85  
    86  		if err == io.EOF {
    87  			break
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  // SaveConfig writes the config structs to the given io.Writer
    95  func (c *MailgunConfig) SaveConfig(fd io.Writer) error {
    96  
    97  	_, err := fmt.Fprintf(fd, `#
    98  # config for Mailgun:
    99  #
   100  `)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	fmt.Fprintf(fd, "MAILGUN_DOMAIN=\"%s\"\n", c.Domain)
   105  	fmt.Fprintf(fd, "MAILGUN_PUBLIC_API_KEY=\"%s\"\n", c.PublicApiKey)
   106  	fmt.Fprintf(fd, "MAILGUN_SECRET_API_KEY=\"%s\"\n", c.SecretApiKey)
   107  	return nil
   108  }
   109  
   110  // DefineFlags should be called before myflags.Parse().
   111  func (c *MailgunConfig) DefineFlags(fs *flag.FlagSet) {
   112  	fs.StringVar(&c.Domain, "mailgun-domain", "", "(supports -adduser) mailgun domain from which to send. Only required if sending backup emails.")
   113  	fs.StringVar(&c.PublicApiKey, "mailgun-pubkey", "", "(supports -adduser) mailgun public api key. Only required if sending backup emails.")
   114  	fs.StringVar(&c.SecretApiKey, "mailgun-secretkey", "", "(supports -adduser) mailgun secret api key. Only required if sending backup emails.")
   115  }
   116  
   117  // ValidateConfig should be called after myflags.Parse().
   118  func (c *MailgunConfig) ValidateConfig() error {
   119  	return nil
   120  }