<commit_before>package main

import (
	"sort"
	"strings"
	"time"

	"github.com/docker/go-units"
	"github.com/flynn/flynn/controller/client"
	ct "github.com/flynn/flynn/controller/types"
	"github.com/flynn/go-docopt"
)

func init() {
	register("ps", runPs, `
usage: flynn ps [-a] [-c]

List flynn jobs.

Options:
  -a, --all      Show all jobs (default is running and pending)
  -c, --command  Show command

Example:

       $ flynn ps
       ID                                          TYPE  STATE  CREATED             RELEASE
       host0-52aedfbf-e613-40f2-941a-d832d10fc400  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe8142
       host0-205595d8-206a-46a2-be30-2e98f53df272  web   up     25 seconds ago      cf39a906-38d1-4393-a6b1-8ad2befe8142
       host0-0f34548b-72fa-41fe-a425-abc4ac6a3857  web   up     25 seconds ago      cf39a906-38d1-4393-a6b1-8ad2befe8142

       $ flynn ps --all --command
       ID                                          TYPE  STATE  CREATED             RELEASE				  COMMAND
       host0-52aedfbf-e613-40f2-941a-d832d10fc400  web   up     2 minutes ago       cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-205595d8-206a-46a2-be30-2e98f53df272  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-0f34548b-72fa-41fe-a425-abc4ac6a3857  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-129b821f-3195-4b3b-b04b-669196cfbb03  run   down   5 seconds ago       cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init /bin/bash
`)
}

func runPs(args *docopt.Args, client controller.Client) error {
	jobs, err := client.JobList(mustApp())
	if err != nil {
		return err
	}
	sort.Sort(sortJobs(jobs))

	w := tabWriter()
	defer w.Flush()

	headers := []interface{}{"ID", "TYPE", "STATE", "CREATED", "RELEASE"}
	if args.Bool["--command"] {
		headers = append(headers, "COMMAND")
	}
	listRec(w, headers...)
	for _, j := range jobs {
		if j.Type == "" {
			j.Type = "run"
		}
		if !args.Bool["--all"] && j.State != ct.JobStateUp && j.State != ct.JobStatePending {
			continue
		}
		id := j.ID
		if id == "" {
			id = j.UUID
		}
		var created string
		if j.CreatedAt != nil {
			created = units.HumanDuration(time.Now().UTC().Sub(*j.CreatedAt)) + " ago"
		}
		fields := []interface{}{id, j.Type, j.State, created, j.ReleaseID}
		if args.Bool["--command"] {
			fields = append(fields, strings.Join(j.Args, " "))
		}
		listRec(w, fields...)
	}

	return nil
}

// sortJobs sorts Jobs in chronological order based on their CreatedAt time
type sortJobs []*ct.Job

func (s sortJobs) Len() int { return len(s) }
func (s sortJobs) Less(i, j int) bool {
	return s[i].CreatedAt == nil || s[j].CreatedAt != nil && (*s[j].CreatedAt).Sub(*s[i].CreatedAt) > 0
}
func (s sortJobs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }<commit_msg>cli: Add --quiet flag to `flynn ps`<commit_after>package main

import (
	"fmt"
	"sort"
	"strings"
	"time"

	"github.com/docker/go-units"
	"github.com/flynn/flynn/controller/client"
	ct "github.com/flynn/flynn/controller/types"
	"github.com/flynn/go-docopt"
)

func init() {
	register("ps", runPs, `
usage: flynn ps [-a] [-c] [-q]

List flynn jobs.

Options:
  -a, --all      Show all jobs (default is running and pending)
  -c, --command  Show command
	-q, --quiet    Only display IDs

Example:

       $ flynn ps
       ID                                          TYPE  STATE  CREATED             RELEASE
       host0-52aedfbf-e613-40f2-941a-d832d10fc400  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe8142
       host0-205595d8-206a-46a2-be30-2e98f53df272  web   up     25 seconds ago      cf39a906-38d1-4393-a6b1-8ad2befe8142
       host0-0f34548b-72fa-41fe-a425-abc4ac6a3857  web   up     25 seconds ago      cf39a906-38d1-4393-a6b1-8ad2befe8142

       $ flynn ps --all --command
       ID                                          TYPE  STATE  CREATED             RELEASE				  COMMAND
       host0-52aedfbf-e613-40f2-941a-d832d10fc400  web   up     2 minutes ago       cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-205595d8-206a-46a2-be30-2e98f53df272  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-0f34548b-72fa-41fe-a425-abc4ac6a3857  web   up     About a minute ago  cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init start web
       host0-129b821f-3195-4b3b-b04b-669196cfbb03  run   down   5 seconds ago       cf39a906-38d1-4393-a6b1-8ad2befe842	  /runner/init /bin/bash

			 $ flynn ps --all --quiet
       host0-52aedfbf-e613-40f2-941a-d832d10fc400
       host0-205595d8-206a-46a2-be30-2e98f53df272
       host0-0f34548b-72fa-41fe-a425-abc4ac6a3857
       host0-129b821f-3195-4b3b-b04b-669196cfbb03
`)
}

func runPs(args *docopt.Args, client controller.Client) error {
	jobs, err := client.JobList(mustApp())
	if err != nil {
		return err
	}
	all := args.Bool["--all"] || args.Bool["-a"]
	if args.Bool["--quiet"] || args.Bool["-q"] {
		printJobsQuiet(jobs, all)
	} else {
		printJobs(jobs, all, args.Bool["--command"] || args.Bool["-c"])
	}
	return nil
}

func printJobsQuiet(jobs []*ct.Job, all bool) {
	sort.Sort(sortJobs(jobs))
	for _, j := range jobs {
		if !all && j.State != ct.JobStateUp && j.State != ct.JobStatePending {
			continue
		}
		fmt.Println(j.ID)
	}
}

func printJobs(jobs []*ct.Job, all bool, commandFlagOn bool) {
	sort.Sort(sortJobs(jobs))
	w := tabWriter()
	defer w.Flush()

	headers := []interface{}{"ID", "TYPE", "STATE", "CREATED", "RELEASE"}
	if commandFlagOn {
		headers = append(headers, "COMMAND")
	}
	listRec(w, headers...)
	for _, j := range jobs {
		if !all && j.State != ct.JobStateUp && j.State != ct.JobStatePending {
			continue
		}
		if j.Type == "" {
			j.Type = "run"
		}
		id := j.ID
		if id == "" {
			id = j.UUID
		}
		var created string
		if j.CreatedAt != nil {
			created = units.HumanDuration(time.Now().UTC().Sub(*j.CreatedAt)) + " ago"
		}
		fields := []interface{}{id, j.Type, j.State, created, j.ReleaseID}
		if commandFlagOn {
			fields = append(fields, strings.Join(j.Args, " "))
		}
		listRec(w, fields...)
	}
}

// sortJobs sorts Jobs in chronological order based on their CreatedAt time
type sortJobs []*ct.Job

func (s sortJobs) Len() int { return len(s) }
func (s sortJobs) Less(i, j int) bool {
	return s[i].CreatedAt == nil || s[j].CreatedAt != nil && (*s[j].CreatedAt).Sub(*s[i].CreatedAt) > 0
}
func (s sortJobs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
{'fraction_non_alphanumeric': 0.1423977951309141, 'fraction_numerical': 0.10335323840146991, 'mean_word_length': 2.573304157549234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4381868', 'n_tokens_mistral': 3064, 'n_tokens_neox': 2649, 'n_words': 541}