<commit_before>package status

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/fatih/color"
	"github.com/taskcluster/taskcluster-cli/cmds/root"

	"github.com/shibukawa/configdir"
	"github.com/spf13/cobra"
)

const (
	manifestURL = "https://references.taskcluster.net/manifest.json"
)

var (
	pingURLs          PingURLs
	validArgs         []string
	cache             = Cache()
	pingURLsCachePath = filepath.Join("cmds", "status", "pingURLs.json")
)

type (
	PingURLs map[string]string

	CachedURLs struct {
		LastUpdated time.Time `json:"lastUpdated"`
		PingURLs    PingURLs  `json:"pingURLs"`
	}

	PingResponse struct {
		Alive  bool    `json:"alive"`
		Uptime float64 `json:"uptime"`
	}

	API struct {
		BaseURL string     `json:"baseUrl"`
		Entries []APIEntry `json:"entries"`
	}

	APIEntry struct {
		Name  string `json:"name"`
		Route string `json:"route"`
	}
)

// CacheFilePath returns the file system path to the cache file storing the ping URLs
func Cache() (cache *configdir.Config) {
	configDirs := configdir.New("taskcluster", "taskcluster-cli")
	cache = configDirs.QueryCacheFolder()
	return
}

func init() {
	var err error
	pingURLs, err = NewPingURLs()
	if err != nil {
		panic(err)
	}

	validArgs = make([]string, len(pingURLs))
	i := 0
	for k := range pingURLs {
		validArgs[i] = k
		i++
	}
	use := "status"
	for _, validArg := range validArgs {
		use = use + " [" + validArg + "]"
	}
	statusCmd := &cobra.Command{
		Short: "taskcluster-cli status will query the current running status of taskcluster services",
		Long: `When called without arguments, taskcluster-cli status will return the current running
status of all production taskcluster services.

By specifying one or more optional services as arguments, you can limit the
services included in the status report.`,
		PreRunE:            preRun,
		Use:                use,
		ValidArgs:          validArgs,
		RunE:               status,
		DisableFlagParsing: true,
	}

	// Add the task subtree to the root.
	root.Command.AddCommand(statusCmd)
}

// NewPingURLs returns the ping URLs to use. The caller does not need to be
// concerned about whether these URLs are retrieved from a local cache, or from
// querying web services.
func NewPingURLs() (pingURLs PingURLs, err error) {
	if !cache.Exists(pingURLsCachePath) {
		return RefreshCache(manifestURL, cache, pingURLsCachePath)<commit_msg>correction: taskcluster-cli -> taskcluster (#138)<commit_after>package status

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/fatih/color"
	"github.com/taskcluster/taskcluster-cli/cmds/root"

	"github.com/shibukawa/configdir"
	"github.com/spf13/cobra"
)

const (
	manifestURL = "https://references.taskcluster.net/manifest.json"
)

var (
	pingURLs          PingURLs
	validArgs         []string
	cache             = Cache()
	pingURLsCachePath = filepath.Join("cmds", "status", "pingURLs.json")
)

type (
	PingURLs map[string]string

	CachedURLs struct {
		LastUpdated time.Time `json:"lastUpdated"`
		PingURLs    PingURLs  `json:"pingURLs"`
	}

	PingResponse struct {
		Alive  bool    `json:"alive"`
		Uptime float64 `json:"uptime"`
	}

	API struct {
		BaseURL string     `json:"baseUrl"`
		Entries []APIEntry `json:"entries"`
	}

	APIEntry struct {
		Name  string `json:"name"`
		Route string `json:"route"`
	}
)

// CacheFilePath returns the file system path to the cache file storing the ping URLs
func Cache() (cache *configdir.Config) {
	configDirs := configdir.New("taskcluster", "taskcluster-cli")
	cache = configDirs.QueryCacheFolder()
	return
}

func init() {
	var err error
	pingURLs, err = NewPingURLs()
	if err != nil {
		panic(err)
	}

	validArgs = make([]string, len(pingURLs))
	i := 0
	for k := range pingURLs {
		validArgs[i] = k
		i++
	}
	use := "status"
	for _, validArg := range validArgs {
		use = use + " [" + validArg + "]"
	}
	statusCmd := &cobra.Command{
		Short: "status queries the current running status of taskcluster services",
		Long: `When called without arguments, taskcluster status will return the current running
status of all production taskcluster services.

By specifying one or more optional services as arguments, you can limit the
services included in the status report.`,
		PreRunE:            preRun,
		Use:                use,
		ValidArgs:          validArgs,
		RunE:               status,
		DisableFlagParsing: true,
	}

	// Add the task subtree to the root.
	root.Command.AddCommand(statusCmd)
}

// NewPingURLs returns the ping URLs to use. The caller does not need to be
// concerned about whether these URLs are retrieved from a local cache, or from
// querying web services.
func NewPingURLs() (pingURLs PingURLs, err error) {
	if !cache.Exists(pingURLsCachePath) {
		return RefreshCache(manifestURL, cache, pingURLsCachePath)
{'fraction_non_alphanumeric': 0.11321142387507704, 'fraction_numerical': 0.002671049928087117, 'mean_word_length': 3.56232427366448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2809497', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1627, 'n_words': 464}