<commit_before>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"text/template"
	"time"

	skvs "github.com/experimental-platform/platform-skvs/client"
	"github.com/experimental-platform/platform-utils/dockerutil"
	"github.com/michaelklishin/rabbit-hole"
)

// where RabbitMQ lives in SKVS
var RabbitSKVS string = "app/rabbitmq"

type rabbitConnector interface {
	PutVhost(string, rabbithole.VhostSettings) (*http.Response, error)
	DeleteVhost(string) (*http.Response, error)
	PutUser(string, rabbithole.UserSettings) (*http.Response, error)
	DeleteUser(string) (*http.Response, error)
	UpdatePermissionsIn(string, string, rabbithole.Permissions) (*http.Response, error)
	ListPermissions() ([]rabbithole.PermissionInfo, error)
}

type realRabbit struct {
	con *rabbithole.Client
}

func getOrCreateCredentials() (string, string, bool, error) {
	var user, passwd string
	var err error
	created := false
	// make sure that SKVS holds some credentials
	user, err = s.Get(fmt.Sprintf("%s/user", RabbitSKVS))
	if err != nil {
		created = true
		user = "rabbitadmin"
		err = s.Set(fmt.Sprintf("%s/user", RabbitSKVS), user)
		if err != nil {
			panic(err)
		}
	}
	passwd, err = s.Get(fmt.Sprintf("%s/passwd", RabbitSKVS))
	if err != nil {
		created = true
		passwd = <PASSWORD>(32)
		err = s.Set(fmt.Sprintf("%s/passwd", RabbitSKVS), passwd)
		if err != nil {
			panic(err)
		}
	}
	return user, passwd, created, err
}

func (rec *realRabbit) connect() {
	if rec.con == nil {
		// get rabbitmq IP address
		host, err := dockerutil.GetContainerIP("rabbitmq")
		if err != nil {
			panic(err)
		}
		host = fmt.Sprintf("http://%s:15672", host)
		// get or create login credentials
		user, passwd, created, err := getOrCreateCredentials()
		if err != nil {
			panic(err)
		}
		// if the credentials were newly created, use default credentials to create a new admin
		if created {
			con, err := rabbithole.NewClient(host, "guest", "guest")
			if err != nil {
				panic(err)
			}
			userInfo := rabbithole.UserSettings{
				Name:     user,
				Password: <PASSWORD>,
				Tags:     "administrator",
			}
			_, err = con.PutUser(user, userInfo)
			if err != nil {
				panic(err)
			}
		}
		// create a new connection using credentials
		rec.con, err = rabbithole.NewClient(host, user, passwd)
		if err != nil {
			panic(err)
		}
		if created {
			// remove the old guest account
			_, err = rec.con.DeleteUser("guest")
			if err != nil {
				panic(err)
			}
		}
	}
	return
}

func (rec *realRabbit) PutVhost(a string, b rabbithole.VhostSettings) (*http.Response, error) {
	rec.connect()
	return rec.con.PutVhost(a, b)
}

func (rec *realRabbit) DeleteVhost(a string) (*http.Response, error) {
	rec.connect()
	return rec.con.DeleteVhost(a)<commit_msg>Get account data from SKVS.<commit_after>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"text/template"
	"time"

	skvs "github.com/experimental-platform/platform-skvs/client"
	"github.com/experimental-platform/platform-utils/dockerutil"
	"github.com/michaelklishin/rabbit-hole"
)

// where RabbitMQ lives in SKVS
var RabbitSKVS string = "app/rabbitmq"

type rabbitConnector interface {
	PutVhost(string, rabbithole.VhostSettings) (*http.Response, error)
	DeleteVhost(string) (*http.Response, error)
	PutUser(string, rabbithole.UserSettings) (*http.Response, error)
	DeleteUser(string) (*http.Response, error)
	UpdatePermissionsIn(string, string, rabbithole.Permissions) (*http.Response, error)
	ListPermissions() ([]rabbithole.PermissionInfo, error)
}

type realRabbit struct {
	con *rabbithole.Client
}

func getOrCreateCredentials() (string, string) {
	user, err := s.Get(fmt.Sprintf("%s/user", RabbitSKVS))
	if err != nil {
		user = "guest"
	}
	passwd, err := s.Get(fmt.Sprintf("%s/passwd", RabbitSKVS))
	if err != nil {
		passwd = "<PASSWORD>"
	}
	return user, passwd
}

func (rec *realRabbit) connect() {
	if rec.con == nil {
		// get rabbitmq IP address
		host, err := dockerutil.GetContainerIP("rabbitmq")
		if err != nil {
			panic(err)
		}
		host = fmt.Sprintf("http://%s:15672", host)
		user, passwd := getOrCreateCredentials()
		if err != nil {
			panic(err)
		}
		rec.con, err = rabbithole.NewClient(host, user, passwd)
		if err != nil {
			panic(err)
		}
	}
	return
}

func (rec *realRabbit) PutVhost(a string, b rabbithole.VhostSettings) (*http.Response, error) {
	rec.connect()
	return rec.con.PutVhost(a, b)
}

func (rec *realRabbit) DeleteVhost(a string) (*http.Response, error) {
	rec.connect()
	return rec.con.DeleteVhost(a)
{'fraction_non_alphanumeric': 0.14442013129102846, 'fraction_numerical': 0.00262582056892779, 'mean_word_length': 4.017563117453348, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '511947', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1652, 'n_words': 393}