github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/status/status.go (about)

     1  package status
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/tabwriter"
     7  	"time"
     8  
     9  	"github.com/daticahealth/cli/commands/environments"
    10  	"github.com/daticahealth/cli/commands/services"
    11  	"github.com/daticahealth/cli/models"
    12  	"github.com/pmylund/sortutil"
    13  )
    14  
    15  const dateForm = "2006-01-02T15:04:05"
    16  
    17  var historicalStatus = map[string]bool{
    18  	"finished":    true,
    19  	"failed":      true,
    20  	"disappeared": true,
    21  	"killed":      true,
    22  	"unknown":     true,
    23  }
    24  
    25  func CmdStatus(envID string, is IStatus, ie environments.IEnvironments, iservices services.IServices, historical bool) error {
    26  	env, err := ie.Retrieve(envID)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	svcs, err := iservices.ListByEnvID(env.ID, env.Pod)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	return is.Status(env, svcs, historical)
    35  }
    36  
    37  // Status prints out all of the non-utility services and their running jobs
    38  func (s *SStatus) Status(env *models.Environment, services *[]models.Service, historical bool) error {
    39  	w := &tabwriter.Writer{}
    40  	w.Init(os.Stdout, 0, 8, 4, '\t', 0)
    41  
    42  	fmt.Fprintln(w, env.Name+" (environment ID = "+env.ID+"):")
    43  	fmt.Fprintln(w, "Label\tStatus\tCreated At")
    44  
    45  	sortutil.AscByField(*services, "Label")
    46  
    47  	for _, service := range *services {
    48  		if service.Type != "" {
    49  			jobs, err := s.Jobs.List(service.ID, 1, 100)
    50  			if err != nil {
    51  				return err
    52  			}
    53  			for _, job := range *jobs {
    54  				if !historical && historicalStatus[job.Status] {
    55  					continue
    56  				}
    57  				displayType := service.Label
    58  				if job.Type != "deploy" {
    59  					displayType = fmt.Sprintf("%s (%s)", service.Label, job.Type)
    60  					if job.Type == "worker" {
    61  						// fetch the worker separately to get the procfile target run
    62  						workerJob, err := s.Jobs.Retrieve(job.ID, service.ID, true)
    63  						if err != nil {
    64  							return err
    65  						}
    66  						if workerJob.Spec != nil && workerJob.Spec.Payload != nil && workerJob.Spec.Payload.Environment != nil {
    67  							if target, contains := workerJob.Spec.Payload.Environment["PROCFILE_TARGET"]; contains {
    68  								displayType = fmt.Sprintf("%s (%s: target=%s)", service.Label, job.Type, target)
    69  							}
    70  						}
    71  					}
    72  				} else if service.Type == "code" && len(service.ReleaseVersion) > 0 {
    73  					displayType = fmt.Sprintf("%s (git:%s)", service.Label, service.ReleaseVersion)
    74  				}
    75  
    76  				t, _ := time.Parse(dateForm, job.CreatedAt)
    77  				fmt.Fprintln(w, displayType+"\t"+job.Status+"\t"+t.Local().Format(time.ANSIC))
    78  			}
    79  			if service.Type == "code" {
    80  				latestBuildJobs, err := s.Jobs.RetrieveByType(service.ID, "build", 1, 1)
    81  				if err != nil {
    82  					return err
    83  				}
    84  				for _, latestBuildJob := range *latestBuildJobs {
    85  					if !historical && historicalStatus[latestBuildJob.Status] {
    86  						continue
    87  					}
    88  					if latestBuildJob.ID == "" {
    89  						fmt.Fprintln(w, "--------"+"\t"+service.Label+"\t"+"-------"+"\t"+"---------------")
    90  					} else if latestBuildJob.ID != "" {
    91  						t, _ := time.Parse(dateForm, latestBuildJob.CreatedAt)
    92  						displayType := fmt.Sprintf("%s (%s)", service.Label, latestBuildJob.Type)
    93  						fmt.Fprintln(w, displayType+"\t"+latestBuildJob.Status+"\t"+t.Local().Format(time.ANSIC))
    94  					}
    95  				}
    96  			}
    97  		}
    98  	}
    99  	w.Flush()
   100  	return nil
   101  }