github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/domain/internal/commands/commands.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"sort"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/hashicorp/errwrap"
    12  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
    13  	"github.com/henvic/wedeploycli/color"
    14  	"github.com/henvic/wedeploycli/command/internal/we"
    15  	"github.com/henvic/wedeploycli/fancy"
    16  	"github.com/henvic/wedeploycli/list"
    17  	"github.com/henvic/wedeploycli/services"
    18  )
    19  
    20  // Command for domains
    21  type Command struct {
    22  	SetupHost      cmdflagsfromhost.SetupHost
    23  	ServicesClient *services.Client
    24  
    25  	Domains []string
    26  }
    27  
    28  // Show domains
    29  func (c *Command) Show(ctx context.Context) error {
    30  	c.Domains = []string{}
    31  
    32  	var l = list.New(list.Filter{
    33  		Project:  c.SetupHost.Project(),
    34  		Services: []string{c.SetupHost.Service()},
    35  	})
    36  
    37  	if err := l.Once(ctx, we.Context()); err != nil {
    38  		return err
    39  	}
    40  
    41  	var service, err = c.ServicesClient.Get(ctx,
    42  		c.SetupHost.Project(),
    43  		c.SetupHost.Service())
    44  
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	c.Domains = service.CustomDomains
    50  
    51  	if len(c.Domains) == 0 {
    52  		_, _ = fmt.Fprintf(os.Stderr, "No custom domains found.\n")
    53  		return nil
    54  	}
    55  
    56  	sort.Slice(c.Domains, func(i, j int) bool {
    57  		return c.Domains[i] < c.Domains[j]
    58  	})
    59  
    60  	fmt.Printf("%s\t%s\n",
    61  		color.Format(color.FgHiBlack, "#"),
    62  		color.Format(color.FgHiBlack, "Domain"))
    63  
    64  	for pos, v := range c.Domains {
    65  		fmt.Printf("%d\t%s\n", pos+1, v)
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  // Add domains
    72  func (c *Command) Add(ctx context.Context, args []string) error {
    73  	var domains, err = c.getAddDomains(args)
    74  
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	for _, domain := range domains {
    80  		err = c.ServicesClient.AddDomain(ctx, c.SetupHost.Project(), c.SetupHost.Service(), domain)
    81  
    82  		if err != nil {
    83  			return errwrap.Wrapf("can't add \""+domain+"\": {{err}}", err)
    84  		}
    85  
    86  		fmt.Printf("Custom domain \"%v\" added.\n", domain)
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  // Delete domains
    93  func (c *Command) Delete(ctx context.Context, args []string) error {
    94  	var domains, err = c.getDeleteDomains(args)
    95  
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	for _, domain := range domains {
   101  		err := c.ServicesClient.RemoveDomain(ctx, c.SetupHost.Project(), c.SetupHost.Service(), domain)
   102  
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		fmt.Printf("Custom domain \"%s\" deleted.\n", domain)
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func (c *Command) getAddDomains(args []string) ([]string, error) {
   114  	if len(args) != 0 {
   115  		return args, nil
   116  	}
   117  
   118  	fmt.Println(fancy.Question("Type custom domains for \"" + c.SetupHost.Host() + "\" (e.g., example.com example.net)"))
   119  	var domainss, err = fancy.Prompt()
   120  
   121  	if err != nil {
   122  		return []string{}, err
   123  	}
   124  
   125  	var domains = strings.Split(domainss, " ")
   126  	return filterEmptyDomains(domains), nil
   127  }
   128  
   129  func (c *Command) getDeleteDomains(args []string) ([]string, error) {
   130  	if len(args) != 0 {
   131  		return args, nil
   132  	}
   133  
   134  	fmt.Println(fancy.Question("Select a custom domain # or address to delete from \"" + c.SetupHost.Host() + "\""))
   135  	var domainss, err = fancy.Prompt()
   136  
   137  	if err != nil {
   138  		return []string{}, err
   139  	}
   140  
   141  	var domains = strings.Split(domainss, " ")
   142  
   143  	for index, domain := range domains {
   144  		d := c.getDomainOrOption(domain)
   145  
   146  		if d != domain {
   147  			domains[index] = d
   148  		}
   149  	}
   150  
   151  	return filterEmptyDomains(domains), nil
   152  }
   153  
   154  func (c *Command) getDomainOrOption(answer string) string {
   155  	for _, d := range c.Domains {
   156  		if answer == d {
   157  			return d
   158  		}
   159  	}
   160  
   161  	switch num, err := strconv.Atoi(answer); {
   162  	case err != nil || num < 1 || num > len(c.Domains):
   163  		return answer
   164  	default:
   165  		return c.Domains[num-1]
   166  	}
   167  }
   168  
   169  func filterEmptyDomains(domains []string) []string {
   170  	var filtered []string
   171  
   172  	for _, d := range domains {
   173  		if d != "" {
   174  			filtered = append(filtered, d)
   175  		}
   176  	}
   177  
   178  	return filtered
   179  }