<commit_before>package service

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"

	"github.com/spf13/cobra"

	"stash.ovh.net/sailabove/sailgo/internal"
)

var cmdAddLink []string
var cmdAddNetworkAllow string
var addPublish []string
var cmdAddGateway string
var cmdAddVolume string
var batch bool
var cmdAddRedeploy bool
var cmdAddBody Add
var cmdAddNetwork []string

func addCmd() *cobra.Command {

	cmd := &cobra.Command{
		Use:   "add",
		Short: "Add a new docker service",
		Long: `add [<namespace>/]<repository>[:tag] [namespace/]<service-name>
		--model         Container model
		--number        Number of container to run
		[--link         name:alias]
		[--network      {public|private|<namespace name>}]
		[--network-allow [network:]ip[/mask] Use IPs whitelist]
		[--publish, -p  Publish a container's port to the host]
		[                 format: network:publishedPort:containerPort, network::containerPort, publishedPort:containerPort, containerPort]
		[--gateway      network-input:network-output
		[--restart {no|always[:<max>]|on-failure[:<max>]}]
		[--volume       /path:size] (Size in GB)
		[--batch        do not attach console on start]
		[--redeploy     if the service already exists, redeploy instead]

		override docker options:
			--user
			--entrypoint
			--command
			--workdir
			--environment KEY=val
		other options:
		`,
		Run: cmdAdd,
	}
	cmd.Flags().StringVarP(&cmdAddBody.ContainerModel, "model", "", "x1", "Container model")
	cmd.Flags().IntVarP(&cmdAddBody.ContainerNumber, "number", "", 1, "Number of container to run")
	cmd.Flags().StringSliceVarP(&cmdAddLink, "link", "", nil, "name:alias")
	cmd.Flags().StringSliceVar(&cmdAddNetwork, "network", []string{"public", "private"}, "public|private|<namespace name>")
	cmd.Flags().StringVarP(&cmdAddNetworkAllow, "network-allow", "", "", "[network:]ip[/mask] Use IPs whitelist")
	cmd.Flags().StringSliceVarP(&addPublish, "publish", "p", nil, "Publish a container's port to the host")
	cmd.Flags().StringVarP(&cmdAddGateway, "gateway", "", "", "network-input:network-output")
	cmd.Flags().StringVarP(&cmdAddBody.RestartPolicy, "restart", "", "no", "{no|always[:<max>]|on-failure[:<max>]}")
	cmd.Flags().StringVarP(&cmdAddVolume, "volume", "", "", "/path:size] (Size in GB)")
	cmd.Flags().BoolVarP(&batch, "batch", "", false, "do not attach console on start")
	cmd.Flags().BoolVarP(&cmdAddRedeploy, "redeploy", "", false, "if the service already exists, redeploy instead")
	cmd.Flags().StringSliceVarP(&cmdAddBody.ContainerEnvironment, "env", "e", nil, "override docker environment")
	// TODO [--pool <name>  use private hosts pool <name>]
	return cmd
}

// PortConfig is a parameter of Add to modify exposed container ports
type PortConfig struct {
	PublishedPort string `json:"published_port"`
	Network       string `json:"network,omitempty"`
}

// Add struct holds all parameters sent to /applications/%s/services/%s?stream
type Add struct {
	Service              string                       `json:"-"`
	Volumes              map[string]string            `json:"volumes,omitempty"`
	Repository           string                       `json:"repository"`
	ContainerUser        string                       `json:"container_user"`
	RestartPolicy        string                       `json:"restart_policy"`
	ContainerCommand     []string                     `json:"container_command,omitempty"`
	ContainerNetwork     map[string]map[string]string `json:"container_network"`
	ContainerEntrypoint  string                       `json:"container_user"`
	ContainerNumber      int                          `json:"container_number"`
	RepositoryTag        string                       `json:"repository_tag"`
	Links                map[string]string            `json:"links"`
	Application          string                       `json:"namespace"`
	ContainerWorkdir     string                       `json:"container_workdir"`
	ContainerEnvironment []string                     `json:"container_environment"`
	ContainerModel       string                       `json:"container_model"`
	ContainerPorts       map[string][]PortConfig      `json:"container_ports"`
}

func cmdAdd(cmd *cobra.Command, args []string) {
	cmdAddBody.ContainerNetwork = make(map[string]map[string]string)
	cmdAddBody.Links = make(map[string]string)
	cmdAddBody.Volumes = make(map[string]string)
	cmdAddBody.ContainerPorts = make(map[string][]PortConfig)
	cmdAddBody.ContainerCommand = make([]string, 0)

	if len(args) != 2 {
		fmt.Printf("Invalid usage. sailgo service add <application>/<repository>[:tag] <service>. Please see sailgo service add --help\n")
		return
	}<commit_msg>fix : omitempty on workdir<commit_after>package service

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"

	"github.com/spf13/cobra"

	"stash.ovh.net/sailabove/sailgo/internal"
)

var cmdAddLink []string
var cmdAddNetworkAllow string
var addPublish []string
var cmdAddGateway string
var cmdAddVolume string
var batch bool
var cmdAddRedeploy bool
var cmdAddBody Add
var cmdAddNetwork []string

func addCmd() *cobra.Command {

	cmd := &cobra.Command{
		Use:   "add",
		Short: "Add a new docker service",
		Long: `add [<namespace>/]<repository>[:tag] [namespace/]<service-name>
		--model         Container model
		--number        Number of container to run
		[--link         name:alias]
		[--network      {public|private|<namespace name>}]
		[--network-allow [network:]ip[/mask] Use IPs whitelist]
		[--publish, -p  Publish a container's port to the host]
		[                 format: network:publishedPort:containerPort, network::containerPort, publishedPort:containerPort, containerPort]
		[--gateway      network-input:network-output
		[--restart {no|always[:<max>]|on-failure[:<max>]}]
		[--volume       /path:size] (Size in GB)
		[--batch        do not attach console on start]
		[--redeploy     if the service already exists, redeploy instead]

		override docker options:
			--user
			--entrypoint
			--command
			--workdir
			--environment KEY=val
		other options:
		`,
		Run: cmdAdd,
	}
	cmd.Flags().StringVarP(&cmdAddBody.ContainerModel, "model", "", "x1", "Container model")
	cmd.Flags().IntVarP(&cmdAddBody.ContainerNumber, "number", "", 1, "Number of container to run")
	cmd.Flags().StringSliceVarP(&cmdAddLink, "link", "", nil, "name:alias")
	cmd.Flags().StringSliceVar(&cmdAddNetwork, "network", []string{"public", "private"}, "public|private|<namespace name>")
	cmd.Flags().StringVarP(&cmdAddNetworkAllow, "network-allow", "", "", "[network:]ip[/mask] Use IPs whitelist")
	cmd.Flags().StringSliceVarP(&addPublish, "publish", "p", nil, "Publish a container's port to the host")
	cmd.Flags().StringVarP(&cmdAddGateway, "gateway", "", "", "network-input:network-output")
	cmd.Flags().StringVarP(&cmdAddBody.RestartPolicy, "restart", "", "no", "{no|always[:<max>]|on-failure[:<max>]}")
	cmd.Flags().StringVarP(&cmdAddVolume, "volume", "", "", "/path:size] (Size in GB)")
	cmd.Flags().BoolVarP(&batch, "batch", "", false, "do not attach console on start")
	cmd.Flags().BoolVarP(&cmdAddRedeploy, "redeploy", "", false, "if the service already exists, redeploy instead")
	cmd.Flags().StringSliceVarP(&cmdAddBody.ContainerEnvironment, "env", "e", nil, "override docker environment")
	// TODO [--pool <name>  use private hosts pool <name>]
	return cmd
}

// PortConfig is a parameter of Add to modify exposed container ports
type PortConfig struct {
	PublishedPort string `json:"published_port"`
	Network       string `json:"network,omitempty"`
}

// Add struct holds all parameters sent to /applications/%s/services/%s?stream
type Add struct {
	Service              string                       `json:"-"`
	Volumes              map[string]string            `json:"volumes,omitempty"`
	Repository           string                       `json:"repository"`
	ContainerUser        string                       `json:"container_user"`
	RestartPolicy        string                       `json:"restart_policy"`
	ContainerCommand     []string                     `json:"container_command,omitempty"`
	ContainerNetwork     map[string]map[string]string `json:"container_network"`
	ContainerEntrypoint  string                       `json:"container_user"`
	ContainerNumber      int                          `json:"container_number"`
	RepositoryTag        string                       `json:"repository_tag"`
	Links                map[string]string            `json:"links"`
	Application          string                       `json:"namespace"`
	ContainerWorkdir     string                       `json:"container_workdir,omitempty"`
	ContainerEnvironment []string                     `json:"container_environment"`
	ContainerModel       string                       `json:"container_model"`
	ContainerPorts       map[string][]PortConfig      `json:"container_ports"`
}

func cmdAdd(cmd *cobra.Command, args []string) {
	cmdAddBody.ContainerNetwork = make(map[string]map[string]string)
	cmdAddBody.Links = make(map[string]string)
	cmdAddBody.Volumes = make(map[string]string)
	cmdAddBody.ContainerPorts = make(map[string][]PortConfig)
	cmdAddBody.ContainerCommand = make([]string, 0)

	if len(args) != 2 {
		fmt.Printf("Invalid usage. sailgo service add <application>/<repository>[:tag] <service>. Please see sailgo service add --help\n")
		return
	}
{'fraction_non_alphanumeric': 0.13937506711049072, 'fraction_numerical': 0.0012885214216686353, 'mean_word_length': 3.342191142191142, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1624012', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2697, 'n_words': 657}