github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/list/services/services.go (about)

     1  package listservices
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
     8  	"github.com/henvic/wedeploycli/color"
     9  	"github.com/henvic/wedeploycli/command/internal/we"
    10  	"github.com/henvic/wedeploycli/list"
    11  	"github.com/henvic/wedeploycli/projects"
    12  	"github.com/henvic/wedeploycli/services"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // ListServicesCmd is used for getting a list of projects and services
    17  var ListServicesCmd = &cobra.Command{
    18  	Use: "services",
    19  	Example: `  lcp list services --project chat --service data
    20     lcp list services --url data-chat.lfr.cloud`,
    21  	Short:   "Show list of services",
    22  	Args:    cobra.NoArgs,
    23  	PreRunE: preRun,
    24  	RunE:    listRun,
    25  }
    26  
    27  var (
    28  	detailed bool
    29  	watch    bool
    30  )
    31  
    32  var setupHost = cmdflagsfromhost.SetupHost{
    33  	Pattern: cmdflagsfromhost.FullHostPattern,
    34  
    35  	Requires: cmdflagsfromhost.Requires{
    36  		Auth:    true,
    37  		Project: true,
    38  	},
    39  
    40  	HideServicesPrompt:   true,
    41  	PromptMissingProject: true,
    42  }
    43  
    44  func preRun(cmd *cobra.Command, args []string) error {
    45  	if err := setupHost.Process(context.Background(), we.Context()); err != nil {
    46  		return err
    47  	}
    48  
    49  	if !watch {
    50  		return checkProjectOrServiceExists()
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func checkProjectOrServiceExists() (err error) {
    57  	if setupHost.Service() != "" {
    58  		servicesClient := services.New(we.Context())
    59  		// if service exists, project also exists
    60  		_, err = servicesClient.Get(context.Background(), setupHost.Project(), setupHost.Service())
    61  		return err
    62  	}
    63  
    64  	if setupHost.Project() != "" {
    65  		projectsClient := projects.New(we.Context())
    66  		_, err = projectsClient.Get(context.Background(), setupHost.Project())
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func listRun(cmd *cobra.Command, args []string) error {
    74  	var filter = list.Filter{
    75  		Project: setupHost.Project(),
    76  	}
    77  
    78  	if setupHost.Service() != "" {
    79  		filter.Services = []string{
    80  			setupHost.Service(),
    81  		}
    82  	}
    83  
    84  	var l = list.New(filter)
    85  
    86  	if detailed {
    87  		l.Details = list.Detailed
    88  	}
    89  
    90  	if !watch {
    91  		return l.Once(context.Background(), we.Context())
    92  	}
    93  
    94  	fmt.Println(color.Format(color.FgHiBlack,
    95  		"List of services will be updated when a change occurs.\n"))
    96  
    97  	l.Watch(context.Background(), we.Context())
    98  	return nil
    99  }
   100  
   101  func init() {
   102  	setupHost.Init(ListServicesCmd)
   103  
   104  	ListServicesCmd.Flags().BoolVarP(
   105  		&detailed,
   106  		"detailed", "d", false, "Show more services details")
   107  
   108  	ListServicesCmd.Flags().BoolVarP(&watch, "watch", "w", false, "Show and watch for changes")
   109  }