<commit_before>package ghastly

import (
	"fmt"
	"time"
)

// A version of the configuration for a particular service. Backends, domains,
// etc. belong to a particular version.
type Version struct {
	Number    int64
	ServiceId string
	Active    bool
	Locked    bool
	Comment   string
	Testing   bool
	Staging   bool
	Deployed  bool
	Network   VersionNetwork
	deletedAt time.Time
	service   *Service
}

type VersionNetwork struct {
	Name                string
	Description         string
	AvailableAll        bool
	AvailableRestricted bool
	AvailablePrivate    bool
	CustomerId          string
}

func (s *Service) populateVersion(versionData map[string]interface{}) (*Version, error) {
	active, _ := versionData["active"].(bool)
	locked, _ := versionData["locked"].(bool)
	testing, _ := versionData["testing"].(bool)
	staging, _ := versionData["staging"].(bool)
	deployed, _ := versionData["deployed"].(bool)
	comment, _ := versionData["comment"].(string)
	return &Version{Number: int64(versionData["number"].(float64)), ServiceId: versionData["service_id"].(string), Active: active, Locked: locked, Comment: comment, Testing: testing, Staging: staging, Deployed: deployed, service: s}, nil
}

// Create a brand new, pristine version of a service, with nothing in it.
func (s *Service) NewVersion() (*Version, error) {
	params := map[string]string{"service": s.Id}
	url := s.TaskURL("/version")
	resp, err := s.ghastly.PostFormParams(url, params)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return s.populateVersion(vData)
}

// Get a particular version of this service identified by the version number.
func (s *Service) GetVersion(number int64) (*Version, error) {
	u := fmt.Sprintf("/version/%d", number)
	url := s.TaskURL(u)
	resp, err := s.ghastly.Get(url)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return s.populateVersion(vData)
}

/*
// List all versions belonging to a service.
func (s *Service) ListVersions() ([]*Version, error) {

}
*/

// Clone this version of the service, returning the new version.
func (v *Version) Clone() (*Version, error) {
	u := fmt.Sprintf("/version/%d/clone", v.Number)
	url := v.service.TaskURL(u)
	resp, err := v.service.ghastly.Put(url, nil)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return v.service.populateVersion(vData)
}

func (v *Version) Activate() error {

}<commit_msg>Comment out version.Activate - can't activate versions right now without any backends or such<commit_after>package ghastly

import (
	"fmt"
	"time"
)

// A version of the configuration for a particular service. Backends, domains,
// etc. belong to a particular version.
type Version struct {
	Number    int64
	ServiceId string
	Active    bool
	Locked    bool
	Comment   string
	Testing   bool
	Staging   bool
	Deployed  bool
	Network   VersionNetwork
	deletedAt time.Time
	service   *Service
}

type VersionNetwork struct {
	Name                string
	Description         string
	AvailableAll        bool
	AvailableRestricted bool
	AvailablePrivate    bool
	CustomerId          string
}

func (s *Service) populateVersion(versionData map[string]interface{}) (*Version, error) {
	active, _ := versionData["active"].(bool)
	locked, _ := versionData["locked"].(bool)
	testing, _ := versionData["testing"].(bool)
	staging, _ := versionData["staging"].(bool)
	deployed, _ := versionData["deployed"].(bool)
	comment, _ := versionData["comment"].(string)
	return &Version{Number: int64(versionData["number"].(float64)), ServiceId: versionData["service_id"].(string), Active: active, Locked: locked, Comment: comment, Testing: testing, Staging: staging, Deployed: deployed, service: s}, nil
}

// Create a brand new, pristine version of a service, with nothing in it.
func (s *Service) NewVersion() (*Version, error) {
	params := map[string]string{"service": s.Id}
	url := s.TaskURL("/version")
	resp, err := s.ghastly.PostFormParams(url, params)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return s.populateVersion(vData)
}

// Get a particular version of this service identified by the version number.
func (s *Service) GetVersion(number int64) (*Version, error) {
	u := fmt.Sprintf("/version/%d", number)
	url := s.TaskURL(u)
	resp, err := s.ghastly.Get(url)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return s.populateVersion(vData)
}

/*
// List all versions belonging to a service.
func (s *Service) ListVersions() ([]*Version, error) {

}
*/

// Clone this version of the service, returning the new version.
func (v *Version) Clone() (*Version, error) {
	u := fmt.Sprintf("/version/%d/clone", v.Number)
	url := v.service.TaskURL(u)
	resp, err := v.service.ghastly.Put(url, nil)
	if err != nil {
		return nil, err
	}
	vData, err := ParseJson(resp.Body)
	if err != nil {
		return nil, err
	}

	return v.service.populateVersion(vData)
}

/*
// Activate this version of the service.
func (v *Version) Activate() error {

}
*
{'fraction_non_alphanumeric': 0.13375673595073134, 'fraction_numerical': 0.003079291762894534, 'mean_word_length': 3.8982092365692744, 'pattern_counts': {'":': 2, '<': 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': '7674001', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1687, 'n_words': 518}