<commit_before>// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"os"
	"sort"
	"text/tabwriter"

	"github.com/cilium/cilium/api/v1/models"
	"github.com/cilium/cilium/pkg/endpoint"

	"github.com/go-openapi/swag"
	"github.com/spf13/cobra"
)

var noHeaders bool

// endpointListCmd represents the endpoint_list command
var endpointListCmd = &cobra.Command{
	Use:   "list",
	Short: "List all endpoints",
	Run: func(cmd *cobra.Command, args []string) {
		listEndpoints()
	},
}

func init() {
	endpointCmd.AddCommand(endpointListCmd)
	endpointListCmd.Flags().BoolVar(&noHeaders, "no-headers", false, "Do not print headers")
	AddMultipleOutput(endpointListCmd)
}

func listEndpoint(w *tabwriter.Writer, ep *models.Endpoint, id string, label string) {
	var isPolicyEnabled string
	if swag.BoolValue(ep.PolicyEnabled) {
		isPolicyEnabled = "Enabled"
	} else {
		isPolicyEnabled = "Disabled"
	}
	fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
		ep.ID, isPolicyEnabled, id, label, ep.Addressing.IPV6, ep.Addressing.IPV4, ep.State)
}

func listEndpoints() {
	eps, err := client.EndpointList()
	if err != nil {
		Fatalf("cannot get endpoint list: %s\n", err)
	}

	endpoint.OrderEndpointAsc(eps)

	w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)

	const (
		labelsIDTitle    = "IDENTITY"
		labelsDesTitle   = "LABELS (source:key[=value])"
		ipv6Title        = "IPv6"
		ipv4Title        = "IPv4"
		endpointTitle    = "ENDPOINT"
		statusTitle      = "STATUS"
		policyTitle      = "POLICY"
		enforcementTitle = "ENFORCEMENT"
	)

	if !noHeaders {
		fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
			endpointTitle, policyTitle, labelsIDTitle, labelsDesTitle, ipv6Title, ipv4Title, statusTitle)
		fmt.Fprintf(w, "\t%s\t\t\t\t\t\t\n", enforcementTitle)
	}

	if len(dumpOutput) > 0 {
		if err := OutputPrinter(eps); err != nil {
			os.Exit(1)
		}
		return
	}

	for _, ep := range eps {
		if ep.Identity == nil {
			listEndpoint(w, ep, "<no label id>", "")
		} else {
			id := fmt.Sprintf("%d", ep.Identity.ID)

			if len(ep.Identity.Labels) == 0 {
				listEndpoint(w, ep, id, "no labels")
			} else {
				first := true
				lbls := []string(ep.Identity.Labels)
				sort.Strings(lbls)
				for _, lbl := range lbls {
					if first {
						listEndpoint(w, ep, id, lbl)
						first = false
					} else {
						fmt.Fprintf(w, "\t\t\t%s\t\t\t\t\n", lbl)
					}
				}
			}
		}

	}
	w.Flush()
}
<commit_msg>cli: Report assigned labels instead of identity labels<commit_after>// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"os"
	"sort"
	"text/tabwriter"

	"github.com/cilium/cilium/api/v1/models"
	"github.com/cilium/cilium/pkg/endpoint"

	"github.com/go-openapi/swag"
	"github.com/spf13/cobra"
)

var noHeaders bool

// endpointListCmd represents the endpoint_list command
var endpointListCmd = &cobra.Command{
	Use:   "list",
	Short: "List all endpoints",
	Run: func(cmd *cobra.Command, args []string) {
		listEndpoints()
	},
}

func init() {
	endpointCmd.AddCommand(endpointListCmd)
	endpointListCmd.Flags().BoolVar(&noHeaders, "no-headers", false, "Do not print headers")
	AddMultipleOutput(endpointListCmd)
}

func listEndpoint(w *tabwriter.Writer, ep *models.Endpoint, id string, label string) {
	var isPolicyEnabled string
	if swag.BoolValue(ep.PolicyEnabled) {
		isPolicyEnabled = "Enabled"
	} else {
		isPolicyEnabled = "Disabled"
	}
	fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
		ep.ID, isPolicyEnabled, id, label, ep.Addressing.IPV6, ep.Addressing.IPV4, ep.State)
}

func listEndpoints() {
	eps, err := client.EndpointList()
	if err != nil {
		Fatalf("cannot get endpoint list: %s\n", err)
	}

	endpoint.OrderEndpointAsc(eps)

	w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)

	const (
		labelsIDTitle    = "IDENTITY"
		labelsDesTitle   = "LABELS (source:key[=value])"
		ipv6Title        = "IPv6"
		ipv4Title        = "IPv4"
		endpointTitle    = "ENDPOINT"
		statusTitle      = "STATUS"
		policyTitle      = "POLICY"
		enforcementTitle = "ENFORCEMENT"
	)

	if !noHeaders {
		fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
			endpointTitle, policyTitle, labelsIDTitle, labelsDesTitle, ipv6Title, ipv4Title, statusTitle)
		fmt.Fprintf(w, "\t%s\t\t\t\t\t\t\n", enforcementTitle)
	}

	if len(dumpOutput) > 0 {
		if err := OutputPrinter(eps); err != nil {
			os.Exit(1)
		}
		return
	}

	for _, ep := range eps {
		id := "<no label id>"
		if ep.Identity != nil {
			id = fmt.Sprintf("%d", ep.Identity.ID)
		}

		if len(ep.Labels.OrchestrationIdentity) == 0 {
			listEndpoint(w, ep, id, "no labels")
		} else {

			first := true
			lbls := ep.Labels.OrchestrationIdentity
			sort.Strings(lbls)
			for _, lbl := range lbls {
				if first {
					listEndpoint(w, ep, id, lbl)
					first = false
				} else {
					fmt.Fprintf(w, "\t\t\t%s\t\t\t\t\n", lbl)
				}
			}
		}

	}
	w.Flush()
}

{'fraction_non_alphanumeric': 0.1441578148710167, 'fraction_numerical': 0.008767492834260664, 'mean_word_length': 3.358559882439383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5495568', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2177, 'n_words': 539}