<commit_before>package clusterregistrationtokens

import (
	"fmt"
	"net/url"

	"github.com/rancher/norman/types"
	"github.com/rancher/rancher/pkg/image"
	"github.com/rancher/rancher/pkg/settings"
	"github.com/rancher/rancher/pkg/systemtemplate"
)

const (
	commandFormat         = "kubectl apply -f %s"
	insecureCommandFormat = "curl --insecure -sfL %s | kubectl apply -f -"
	nodeCommandFormat     = "sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run %s --server %s --token %s%s"

	windowsNodeCommandFormat = `PowerShell -Sta -NoLogo -NonInteractive -Command "& {docker run --rm -v C:/:C:/host --isolation hyperv %s -server %s -token %s%s; if($?){& c:/etc/rancher/run.ps1;}}"`
)

func Formatter(request *types.APIContext, resource *types.RawResource) {
	var (
		caNonWindows = ""
		caWindows    = ""
	)
	ca := systemtemplate.CAChecksum()
	if ca != "" {
		caNonWindows = " --ca-checksum " + ca
		caWindows = " -caChecksum " + ca
	}

	token, _ := resource.Values["token"].(string)
	if token != "" {
		url := getURL(request, token)
		resource.Values["insecureCommand"] = fmt.Sprintf(insecureCommandFormat, url)
		resource.Values["command"] = fmt.Sprintf(commandFormat, url)
		resource.Values["nodeCommand"] = fmt.Sprintf(nodeCommandFormat,
			image.Resolve(settings.AgentImage.Get()),
			getRootURL(request),
			token,
			caNonWindows)
		resource.Values["token"] = token
		resource.Values["manifestUrl"] = url
		resource.Values["windowsNodeCommand"] = fmt.Sprintf(windowsNodeCommandFormat,
			image.Resolve(settings.AgentImage.Get()),
			getRootURL(request),
			token,
			caWindows)
	}
}

func NodeCommand(token string) string {
	ca := systemtemplate.CAChecksum()
	if ca != "" {
		ca = " --ca-checksum " + ca
	}

	return fmt.Sprintf(nodeCommandFormat,
		image.Resolve(settings.AgentImage.Get()),
		getRootURL(nil),
		token,
		ca)
}

func getRootURL(request *types.APIContext) string {
	serverURL := settings.ServerURL.Get()
	if serverURL == "" {
		if request != nil {
			serverURL = request.URLBuilder.RelativeToRoot("")
		}
	} else {
		u, err := url.Parse(serverURL)
		if err != nil {
			if request != nil {
				serverURL = request.URLBuilder.RelativeToRoot("")
			}
		} else {
			u.Path = ""
			serverURL = u.String()
		}
	}

	return serverURL
}

func getURL(request *types.APIContext, token string) string {
	path := "/v3/import/" + token + ".yaml"
	serverURL := settings.ServerURL.Get()
	if serverURL == "" {
		serverURL = request.URLBuilder.RelativeToRoot(path)
	} else {
		u, err := url.Parse(serverURL)
		if err != nil {
			serverURL = request.URLBuilder.RelativeToRoot(path)
		} else {
			u.Path = path
			serverURL = u.String()
		}
	}

	return serverURL
}
<commit_msg>Support to run Windows worker adding command on PowerShell<commit_after>package clusterregistrationtokens

import (
	"fmt"
	"net/url"

	"github.com/rancher/norman/types"
	"github.com/rancher/rancher/pkg/image"
	"github.com/rancher/rancher/pkg/settings"
	"github.com/rancher/rancher/pkg/systemtemplate"
)

const (
	commandFormat         = "kubectl apply -f %s"
	insecureCommandFormat = "curl --insecure -sfL %s | kubectl apply -f -"
	nodeCommandFormat     = "sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run %s --server %s --token %s%s"

	windowsNodeCommandFormat = `PowerShell -Sta -NoLogo -NonInteractive -Command "& {docker run --rm -v C:/:C:/host --isolation hyperv %s -server %s -token %s%s; Out-String -InputObject $? | Select-String -AllMatches -Pattern 'True' | ForEach-Object {& c:/etc/rancher/run.ps1;}}"`
)

func Formatter(request *types.APIContext, resource *types.RawResource) {
	var (
		caNonWindows = ""
		caWindows    = ""
	)
	ca := systemtemplate.CAChecksum()
	if ca != "" {
		caNonWindows = " --ca-checksum " + ca
		caWindows = " -caChecksum " + ca
	}

	token, _ := resource.Values["token"].(string)
	if token != "" {
		url := getURL(request, token)
		resource.Values["insecureCommand"] = fmt.Sprintf(insecureCommandFormat, url)
		resource.Values["command"] = fmt.Sprintf(commandFormat, url)
		resource.Values["nodeCommand"] = fmt.Sprintf(nodeCommandFormat,
			image.Resolve(settings.AgentImage.Get()),
			getRootURL(request),
			token,
			caNonWindows)
		resource.Values["token"] = token
		resource.Values["manifestUrl"] = url
		resource.Values["windowsNodeCommand"] = fmt.Sprintf(windowsNodeCommandFormat,
			image.Resolve(settings.AgentImage.Get()),
			getRootURL(request),
			token,
			caWindows)
	}
}

func NodeCommand(token string) string {
	ca := systemtemplate.CAChecksum()
	if ca != "" {
		ca = " --ca-checksum " + ca
	}

	return fmt.Sprintf(nodeCommandFormat,
		image.Resolve(settings.AgentImage.Get()),
		getRootURL(nil),
		token,
		ca)
}

func getRootURL(request *types.APIContext) string {
	serverURL := settings.ServerURL.Get()
	if serverURL == "" {
		if request != nil {
			serverURL = request.URLBuilder.RelativeToRoot("")
		}
	} else {
		u, err := url.Parse(serverURL)
		if err != nil {
			if request != nil {
				serverURL = request.URLBuilder.RelativeToRoot("")
			}
		} else {
			u.Path = ""
			serverURL = u.String()
		}
	}

	return serverURL
}

func getURL(request *types.APIContext, token string) string {
	path := "/v3/import/" + token + ".yaml"
	serverURL := settings.ServerURL.Get()
	if serverURL == "" {
		serverURL = request.URLBuilder.RelativeToRoot(path)
	} else {
		u, err := url.Parse(serverURL)
		if err != nil {
			serverURL = request.URLBuilder.RelativeToRoot(path)
		} else {
			u.Path = path
			serverURL = u.String()
		}
	}

	return serverURL
}

{'fraction_non_alphanumeric': 0.15365463275831406, 'fraction_numerical': 0.0007113640405477503, 'mean_word_length': 3.754015215553677, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7658362', 'n_tokens_mistral': 2117, 'n_tokens_neox': 1960, 'n_words': 388}