github.com/section/sectionctl@v1.12.3/commands/ps.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/section/sectionctl/api"
    10  )
    11  
    12  // PsCmd checks an application's status on Section's delivery platform
    13  type PsCmd struct {
    14  	AccountID int           `short:"a" help:"ID of account to query"`
    15  	AppID     int           `short:"i" help:"ID of app to query"`
    16  	AppPath   string        `default:"nodejs" help:"Path of NodeJS application in environment repository."`
    17  	Watch     bool          `short:"w" help:"Run repeatedly, output status"`
    18  	Interval  time.Duration `short:"t" default:"10s" help:"Interval to poll if watching"`
    19  }
    20  
    21  func getStatus(as api.AppStatus) string {
    22  	if as.InService && as.State == "Running" {
    23  		return "Running"
    24  	} else if as.State == "Deploying" {
    25  		return "Deploying"
    26  	} else {
    27  		return "Not Running"
    28  	}
    29  }
    30  
    31  // Run executes the command
    32  func (c *PsCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
    33  	var aids []int
    34  	if c.AccountID == 0 {
    35  		s := NewSpinner("Looking up accounts", logWriters)
    36  		s.Start()
    37  
    38  		as, err := api.Accounts()
    39  		if err != nil {
    40  			return fmt.Errorf("unable to look up accounts: %w", err)
    41  		}
    42  		for _, a := range as {
    43  			aids = append(aids, a.ID)
    44  		}
    45  
    46  		s.Stop()
    47  	} else {
    48  		aids = append(aids, c.AccountID)
    49  	}
    50  
    51  	var targets [][]int
    52  	for _, id := range aids {
    53  		if c.AppID == 0 {
    54  			s := NewSpinner("Looking up applications", logWriters)
    55  			s.Start()
    56  
    57  			as, err := api.Applications(id)
    58  			if err != nil {
    59  				return fmt.Errorf("unable to look up applications: %w", err)
    60  			}
    61  			for _, a := range as {
    62  				targets = append(targets, []int{id, a.ID})
    63  			}
    64  
    65  			s.Stop()
    66  		} else {
    67  			targets = append(targets, []int{id, c.AppID})
    68  		}
    69  	}
    70  
    71  	if c.Watch {
    72  		ticker := time.NewTicker(c.Interval)
    73  		for ; true; <-ticker.C {
    74  			err = pollAndOutput(cli, targets, c.AppPath,logWriters)
    75  			if err != nil {
    76  				return err
    77  			}
    78  		}
    79  	} else {
    80  		err = pollAndOutput(cli, targets, c.AppPath,logWriters)
    81  		return err
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func pollAndOutput(cli *CLI, targets [][]int, appPath string, logWriters *LogWriters) error {
    88  	s := NewSpinner("Getting status of apps",logWriters)
    89  	s.Start()
    90  
    91  	table := NewTable(cli, os.Stdout)
    92  	table.SetHeader([]string{"Account ID", "App ID", "App instance name", "App Status", "App Payload ID"})
    93  
    94  	for _, t := range targets {
    95  		appStatus, err := api.ApplicationStatus(t[0], t[1], appPath)
    96  		s.Stop()
    97  		if err != nil {
    98  			return err
    99  		}
   100  
   101  		for _, a := range appStatus {
   102  			r := []string{
   103  				strconv.Itoa(t[0]),
   104  				strconv.Itoa(t[1]),
   105  				a.InstanceName,
   106  				getStatus(a),
   107  				a.PayloadID,
   108  			}
   109  			table.Append(r)
   110  		}
   111  	}
   112  
   113  	table.Render()
   114  	return nil
   115  }