<commit_before>// Copyright © 2017 <NAME> <EMAIL>
//
// 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"
	"github.com/edio/n4dgrpc/client"
	"github.com/edio/n4dgrpc/convertions"
	mesh "github.com/linkerd/linkerd/mesh/core/src/main/protobuf"
	"github.com/spf13/cobra"
)

type ResolveConfig struct {
	FailOnNegBinding   bool
	FailOnEmptyReplica bool
	NoStream           bool
	StreamOnly         bool
	Root               *mesh.Path
	Name               *mesh.Path
	ResolutionStrategy client.ResolutionStrategy
}

var (
	resolveConfig = ResolveConfig{}
)

var resolveCmd = &cobra.Command{
	Use:   "resolve PATH [NAMESPACE]",
	Short: "Resolve PATH to replica set in NAMESPACE",
	Long: `Resolve PATH to replica set in NAMESPACE

This command invokes 2 operations on namerd: binding and then resolving the
bound name.

See https://linkerd.io/advanced/routing/ for details.

By default command exits with zero even if binding is negative or resolved
replica set is empty. See options to change this behavior.
`,
	Args: func(cmd *cobra.Command, args []string) error {
		if err := cobra.RangeArgs(1, 2)(cmd, args); err != nil {
			return err
		}

		{
			var name *mesh.Path
			var err error
			if name, err = convertions.StrToPath(args[0]); err != nil {
				return fmt.Errorf("NAME: %v", err)
			}
			resolveConfig.Name = name
		}

		{
			rootStr := DefaultRoot
			if len(args) == 2 {
				rootStr = args[1]
			}

			var root *mesh.Path
			var err error
			if root, err = convertions.StrToPath(rootStr); err != nil {
				return fmt.Errorf("ROOT: %v", err)
			}
			resolveConfig.Root = root
		}

		if resolveConfig.NoStream && resolveConfig.StreamOnly {
			return fmt.Errorf("--no-stream and --stream-only are mutually exclusive")<commit_msg>Imporve resolve help<commit_after>// Copyright © 2017 <NAME> <EMAIL>
//
// 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"
	"github.com/edio/n4dgrpc/client"
	"github.com/edio/n4dgrpc/convertions"
	mesh "github.com/linkerd/linkerd/mesh/core/src/main/protobuf"
	"github.com/spf13/cobra"
)

type ResolveConfig struct {
	FailOnNegBinding   bool
	FailOnEmptyReplica bool
	NoStream           bool
	StreamOnly         bool
	Root               *mesh.Path
	Name               *mesh.Path
	ResolutionStrategy client.ResolutionStrategy
}

var (
	resolveConfig = ResolveConfig{}
)

var resolveCmd = &cobra.Command{
	Use:   "resolve PATH [NAMESPACE]",
	Short: "Resolve PATH to replica set in NAMESPACE",
	Long: `Resolve PATH to replica set in NAMESPACE

This command invokes 2 operations on namerd: binding and then resolving the
bound name.

See https://linkerd.io/advanced/routing/ for details.

For resolution n4dgrpc tries to use GetReplicas gRPC method first, however if
underlying resolver fails to resolve the Path quickly, method will return
Pending result.

If this is the case, then n4dgrpc falls back to StreamReplicas gRPC method,
requests a stream and waits for the first available update.

This behavior may be overridden by supplying --stream-only or --no-stream flag.

By default command exits with zero even if binding is negative or resolved
replica set is empty or pending. Flags -f and -F change this behavior.
`,
	Args: func(cmd *cobra.Command, args []string) error {
		if err := cobra.RangeArgs(1, 2)(cmd, args); err != nil {
			return err
		}

		{
			var name *mesh.Path
			var err error
			if name, err = convertions.StrToPath(args[0]); err != nil {
				return fmt.Errorf("NAME: %v", err)
			}
			resolveConfig.Name = name
		}

		{
			rootStr := DefaultRoot
			if len(args) == 2 {
				rootStr = args[1]
			}

			var root *mesh.Path
			var err error
			if root, err = convertions.StrToPath(rootStr); err != nil {
				return fmt.Errorf("ROOT: %v", err)
			}
			resolveConfig.Root = root
		}

		if resolveConfig.NoStream && resolveConfig.StreamOnly {
			return fmt.Errorf("--no-stream and --stream-only are mutually exclusive")
{'fraction_non_alphanumeric': 0.09675456389452333, 'fraction_numerical': 0.007707910750507099, 'mean_word_length': 3.291557876414273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6560944', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1503, 'n_words': 553}